SklavaBot 0.1.0
Telegram Bot in C++
Loading...
Searching...
No Matches
commands.cpp
Go to the documentation of this file.
1#include <boost/algorithm/string.hpp>
2#include <boost/date_time/posix_time/conversion.hpp>
3#include <boost/date_time/posix_time/posix_time.hpp>
4#include <cstdint>
5#include <iostream>
6#include <leptonica/allheaders.h>
7#include <string>
8#include <tgbot/tgbot.h>
9#include <vector>
10
11#include "config.hpp"
12#include "metar.hpp"
13#include "ocr.hpp"
14#include "tgbot/types/Message.h"
15#include "utils.hpp"
16
20const std::string PARSE_MODE("Markdown");
21
22const std::string HELP_MESSAGE =
23 "Hello, I'm " BOT_NAME "!\n"
24 "I'm a bot created by " BOT_AUTHOR "!\n"
25 "My commands:\n"
26 "`/start` - _start the bot_\n"
27 "`/help` - _show this message_\n"
28 "`/info` - _show compilation machine information_\n"
29 "`/ocr` - _optical Character Recognition with tesseract_\n"
30 "`/metar <ICAO Code>` - _get METAR information_\n"
31 "\n"
32 "My source code is available at " BOT_GITHUB_REPO "\n"
33 "Version: " BOT_VERSION "\n";
34
35const std::string INFO_MESSAGE = "System Informations\n\n"
36 "*OS*: _"
37#if defined(__linux__)
38 "Linux"
39#elif defined(__APPLE__)
40 "MacOS"
41#elif defined(_WIN32)
42#if defined(_WIN64)
43 "Windows x86-64"
44#else
45 "Windows x86"
46#endif
47#endif
48 " with "
49#if defined(__amd64__)
50 "CPU x86-64"
51#elif defined(__i386__)
52 "CPU x86"
53#elif defined(__arm__)
54 "SoC ARMv7"
55#elif defined(__arm64__)
56 "SoC ARMv8"
57#endif
58 "_\n"
59 "*Compiler*: _"
60#if defined(__clang__)
61 "CLANG " __clang_version__
62#elif defined(__GNUC__)
63 "GCC (GNU C Compiler) " __GNUC__
64 "." __GNUC_MINOR__
65#else
66 "Unknown"
67#endif
68 "_\n";
69
70namespace Bot {
71void register_handlers(TgBot::Bot *bot) {
72
79 bot->getEvents().onCommand("start", [bot](TgBot::Message::Ptr message) {
80 tools::log_cmd("start", message->from->username, message->date, LOG::Info);
81 bot->getApi().sendMessage(message->chat->id, "Hi!");
82 });
83
90 bot->getEvents().onCommand("help", [bot](TgBot::Message::Ptr message) {
91 tools::log_cmd("help", message->from->username, message->date, LOG::Info);
92 bot->getApi().sendMessage(message->chat->id, HELP_MESSAGE, false, 0,
93 nullptr, PARSE_MODE);
94 });
95
102 bot->getEvents().onCommand("info", [bot](TgBot::Message::Ptr message) {
103 tools::log_cmd("info", message->from->username, message->date, LOG::Info);
104 bot->getApi().sendMessage(message->chat->id, INFO_MESSAGE, false, 0,
105 nullptr, PARSE_MODE);
106 });
107
114 bot->getEvents().onCommand("ocr", [bot](TgBot::Message::Ptr message) {
115 tools::log_cmd("ocr", message->from->username, message->date, LOG::Info);
116
117 TgBot::Message::Ptr reply = message->replyToMessage;
118
119 if (reply == nullptr) {
120 tools::log_cmd("No Message Reply", message->from->username,
121 boost::posix_time::to_time_t(
122 boost::posix_time::second_clock::universal_time()),
123 LOG::Warn);
124 bot->getApi().sendMessage(message->chat->id,
125 "You should reply to a message");
126 return;
127 }
128
129 TgBot::File::Ptr file =
130 bot->getApi().getFile(reply->photo[reply->photo.size() - 1]->fileId);
131#if defined(DEBUG)
132 tools::log_cmd("DOWNLOADED FILE", reply->from->username, reply->date, LOG::Debug);
133#endif
134 std::string fileContent = bot->getApi().downloadFile(file->filePath);
135
136 Pix *pixImage =
137 pixReadMem(reinterpret_cast<const l_uint8 *>(fileContent.c_str()),
138 fileContent.size());
139
140 if (pixImage == nullptr) {
141 tools::log_cmd("Failed to read the image from memory", "PROGRAM",
142 boost::posix_time::to_time_t(
143 boost::posix_time::second_clock::universal_time()),
144 LOG::Error);
145 return;
146 }
147
148 bot->getApi().sendMessage(message->chat->id, tools::getTextFromImage(pixImage));
149 });
150
157 bot->getEvents().onCommand("metar", [bot](TgBot::Message::Ptr message) {
158 tools::log_cmd("metar", message->from->username, message->date, LOG::Info);
159
160 std::vector<std::string> text;
161 boost::split(text, message->text, [](char c) { return c == ' '; });
162
163 bot->getApi().sendMessage(message->chat->id,
164 "āœˆļø METAR for " + text[1] + "\nšŸ•’ " +
165 tools::getMetar(text[1]),
166 false, message->messageId);
167 });
168}
169
170} // namespace Bot
const std::string INFO_MESSAGE
Definition commands.cpp:35
const std::string HELP_MESSAGE
Definition commands.cpp:22
const std::string PARSE_MODE("Markdown")
Telegram parse mode.
#define BOT_NAME
Definition config.hpp:4
#define BOT_VERSION
Definition config.hpp:6
#define BOT_AUTHOR
Definition config.hpp:8
#define BOT_GITHUB_REPO
Definition config.hpp:9
void register_handlers(TgBot::Bot *bot)
Register all the handlers for bot commands.
Definition commands.cpp:71
const std::string Warn
Definition utils.hpp:45
const std::string Info
Definition utils.hpp:44
const std::string Error
Definition utils.hpp:46
const std::string Debug
Definition utils.hpp:47
void log_cmd(std::string command, std::string user, std::int32_t timestamp, const std::string log_type)
Function to print a better log.
Definition utils.cpp:17
std::string getMetar(std::string ICAO)
Get METAR information from stations.
Definition metar.cpp:28
std::string getTextFromImage(Pix *image)
Definition ocr.cpp:13