summaryrefslogtreecommitdiff
path: root/samples/received-text-processing/src/main.cpp
blob: b6069e945fcbc2a17c973d991b8e8015cfd73c0e (plain)
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
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <string>

#include <tgbot/tgbot.h>

using namespace std;
using namespace TgBot;

vector<string> bot_commands = {"start", "test"};

int main() {
    string token(getenv("TOKEN"));
    printf("Token: %s\n", token.c_str());

    bool test_text_state = false;

    Bot bot(token);
    TgLongPoll long_poll(bot);

    bot.getEvents().onCommand("start", [&bot](Message::Ptr message) {
        bot.getApi().sendMessage(message->chat->id, "Hi!");
    });

    bot.getEvents().onCommand("test", [&](Message::Ptr message) {
        bot.getApi().sendMessage(message->chat->id, "Enter text");
        test_text_state = true;
    });

    bot.getEvents().onAnyMessage([&](TgBot::Message::Ptr message) {
        if (test_text_state) {
            bot.getApi().sendMessage(message->chat->id, message->text);
            test_text_state = false;
            return;
        }

        for (const auto& command : bot_commands) {
            if ("/" + command == message->text) {
                return;
            }
        }

        bot.getApi().sendMessage(message->chat->id, "unknown command");
    });

    try {
        printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
        bot.getApi().deleteWebhook();

        while (true) {
            printf("Long poll started\n");
            long_poll.start();
        }
    } catch (exception& e) {
        printf("error: %s\n", e.what());
    }

    return 0;
}