1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <string>
#include <vector>
#include <tgbot/tgbot.h>
using namespace std;
using namespace TgBot;
void createOneColumnKeyboard(const vector<string>& buttonStrings, ReplyKeyboardMarkup::Ptr& kb)
{
for (size_t i = 0; i < buttonStrings.size(); ++i) {
vector<KeyboardButton::Ptr> row;
KeyboardButton::Ptr button(new KeyboardButton);
button->text = buttonStrings[i];
row.push_back(button);
kb->keyboard.push_back(row);
}
}
void createKeyboard(const vector<vector<string>>& buttonLayout, ReplyKeyboardMarkup::Ptr& kb)
{
for (size_t i = 0; i < buttonLayout.size(); ++i) {
vector<KeyboardButton::Ptr> row;
for (size_t j = 0; j < buttonLayout[i].size(); ++j) {
KeyboardButton::Ptr button(new KeyboardButton);
button->text = buttonLayout[i][j];
row.push_back(button);
}
kb->keyboard.push_back(row);
}
}
int main() {
string token(getenv("TOKEN"));
printf("Token: %s\n", token.c_str());
Bot bot(token);
ReplyKeyboardMarkup::Ptr keyboardOneCol(new ReplyKeyboardMarkup);
createOneColumnKeyboard({"Option 1", "Option 2", "Option 3"}, keyboardOneCol);
ReplyKeyboardMarkup::Ptr keyboardWithLayout(new ReplyKeyboardMarkup);
createKeyboard({
{"Dog", "Cat", "Mouse"},
{"Green", "White", "Red"},
{"On", "Off"},
{"Back"},
{"Info", "About", "Map", "Etc"}
}, keyboardWithLayout);
bot.getEvents().onCommand("start", [&bot, &keyboardOneCol](Message::Ptr message) {
bot.getApi().sendMessage(message->chat->id, "/start for one column keyboard\n/layout for a more complex keyboard", false, 0, keyboardOneCol);
});
bot.getEvents().onCommand("layout", [&bot, &keyboardWithLayout](Message::Ptr message) {
bot.getApi().sendMessage(message->chat->id, "/start for one column keyboard\n/layout for a more complex keyboard", false, 0, keyboardWithLayout);
});
bot.getEvents().onAnyMessage([&bot](Message::Ptr message) {
printf("User wrote %s\n", message->text.c_str());
if (StringTools::startsWith(message->text, "/start") || StringTools::startsWith(message->text, "/layout")) {
return;
}
bot.getApi().sendMessage(message->chat->id, "Your message is: " + message->text);
});
signal(SIGINT, [](int s) {
printf("SIGINT got\n");
exit(0);
});
try {
printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
bot.getApi().deleteWebhook();
TgLongPoll longPoll(bot);
while (true) {
printf("Long poll started\n");
longPoll.start();
}
} catch (exception& e) {
printf("error: %s\n", e.what());
}
return 0;
}
|