aboutsummaryrefslogtreecommitdiffstats
path: root/main.cpp
blob: 9e0d4a43c37bea0cd85f2d331d772176d6e4d61b (plain) (blame)
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
90
91
92
93
94
95
96
97
98
99
100
#include <string>
#include "include/toml.hpp"
#include "include/json.hpp"
#include <tgbot/tgbot.h>
#include <cpr/cpr.h>

using json = nlohmann::json;

int main() {
    auto config = toml::parse_file("../config.toml");


    TgBot::Bot bot(config["telegram"]["apikey"].value_or(""));


    bot.getEvents().onAnyMessage([&bot, &config](TgBot::Message::Ptr const &message){

        std::istringstream msg(message->text);
        std::string command, commandtmp, arguments;

        std::getline(msg, commandtmp, ' ');
        std::getline(msg, arguments);

        if(commandtmp.at(0) != '/'){
            return 0;
        } else{
            command = commandtmp.substr(1, commandtmp.length()-1);
        }

        json request;
        request["command"] = command;
        request["arguments"] = arguments;
        request["session"] = std::to_string(message->from->id);


        cpr::Response r = cpr::Post(cpr::Url{config["n_core"]["address"].value_or("localhost:18080")},
                                    cpr::Body{request.dump()});
        if(r.status_code != 200) {
            std::cerr << "Server gave back error " << r.status_code << ".\n";
            bot.getApi().sendMessage(message->chat->id, "The Server encountered an Error.\nWe are working on a Fix");
            return 1;
        }



        auto jreply = json::parse(r.text);

        std::string outMessage;

        if(jreply["success"] == true) {

            for (auto const &x : jreply["reply"].items()) {


                if (x.value().at("annotations").at(0).at("type") == "none") {
                    outMessage += x.value().at("text");
                } else if (x.value().at("annotations").at(0).at("type") == "bold") {
                    outMessage.append("<b>").append(x.value().at("text")).append("</b>");
                } else if (x.value().at("annotations").at(0).at("type") == "underline") {
                    outMessage.append("<u>").append(x.value().at("text")).append("</u>");
                } else if (x.value().at("annotations").at(0).at("type") == "italic") {
                    outMessage.append("<i>").append(x.value().at("text")).append("</i>");
                } else if (x.value().at("annotations").at(0).at("type") == "strikethrough") {
                    outMessage.append("<strike>").append(x.value().at("text")).append("</strike>");
                } else if (x.value().at("annotations").at(0).at("type") == "link") {
                    outMessage.append("<a href=\"").append(x.value().at("text")).append("\" >").append(x.value().at("annotations").at(0).at("extra").at("annotations").at("text").dump()).append("</a>");
                } else if (x.value().at("annotations").at(0).at("type") == "command") {
                    outMessage.append("/").append(x.value().at("text"));
                } else if (x.value().at("annotations").at(0).at("type") == "attachment") {
                    outMessage =+ "Attachments are unfortunately not supported";
                }

            }
            if(!outMessage.empty()) {
                bot.getApi().sendMessage(message->chat->id, outMessage, false, 0, nullptr, "HTML");
            }
        } else {
            outMessage.append("Command was not successfully executed.\n").append(jreply["reply"].at(0).at("text"));
            bot.getApi().sendMessage(message->chat->id, outMessage);
        }
        return 0;
    });

    try {
        printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
        TgBot::TgLongPoll longPoll(bot);
        while (true) {
            //printf("Long poll started\n");
            longPoll.start();
        }
    } catch (TgBot::TgException& e) {
        printf("error: %s\n", e.what());
    }


    return 0;

}