-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathmain.cpp
More file actions
60 lines (46 loc) · 1.66 KB
/
Copy pathmain.cpp
File metadata and controls
60 lines (46 loc) · 1.66 KB
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
#include <cstdlib>
#include <exception>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <tgbot/tgbot.h>
const std::vector<std::string> botCommands { "start", "test" };
int main() {
const auto token = std::string(std::getenv("TOKEN"));
bool isWaitingForText = false;
TgBot::Bot bot(token);
TgBot::TgLongPoll longPoll(bot);
bot.getEvents().onCommand("start", [&bot](std::shared_ptr<TgBot::Message> message) {
bot.getApi().sendMessage(message->chat->id, "Hi!");
});
bot.getEvents().onCommand("test", [&](std::shared_ptr<TgBot::Message> message) {
bot.getApi().sendMessage(message->chat->id, "Enter text");
isWaitingForText = true;
});
bot.getEvents().onAnyMessage([&](std::shared_ptr<TgBot::Message> message) {
if (isWaitingForText) {
bot.getApi().sendMessage(message->chat->id, message->text.value_or(""));
isWaitingForText = false;
return;
}
for (const auto& command : botCommands) {
if ("/" + command == message->text.value_or("")) {
return;
}
}
bot.getApi().sendMessage(message->chat->id, "unknown command");
});
const auto handleError = [](const std::exception& error) {
std::cout << "error: " << error.what() << std::endl;
};
try {
std::cout << "Bot username: " << bot.getApi().getMe()->username.value_or("") << std::endl;
bot.getApi().deleteWebhook();
longPoll.startLoop(handleError);
} catch (const std::exception& error) {
handleError(error);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}