summaryrefslogtreecommitdiff
path: root/src/EventHandler.cpp
blob: d85e0c3c0090d8be410a3a6f2ed9b7a6290442c5 (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
//
// Created by Oleg Morozenkov on 23.01.17.
//

#include "tgbot/EventHandler.h"
#include <algorithm>

using namespace std;

namespace TgBot {

void EventHandler::handleUpdate(Update::Ptr update) const {
    if (update->inlineQuery != nullptr) {
        _broadcaster.broadcastInlineQuery(update->inlineQuery);
    }
    if (update->chosenInlineResult != nullptr) {
        _broadcaster.broadcastChosenInlineResult(update->chosenInlineResult);
    }
    if (update->callbackQuery != nullptr) {
        _broadcaster.broadcastCallbackQuery(update->callbackQuery);
    }
    if (update->message != nullptr) {
        handleMessage(update->message);
    }
}

void EventHandler::handleMessage(Message::Ptr message) const {
    _broadcaster.broadcastAnyMessage(message);

    if (StringTools::startsWith(message->text, "/")) {
        size_t splitPosition;
        size_t spacePosition = message->text.find(' ');
        size_t atSymbolPosition = message->text.find('@');
        if (spacePosition == string::npos) {
            if (atSymbolPosition == string::npos) {
                splitPosition = message->text.size();
            } else {
                splitPosition = atSymbolPosition;
            }
        } else if (atSymbolPosition == string::npos) {
            splitPosition = spacePosition;
        } else {
            splitPosition = std::min(spacePosition, atSymbolPosition);
        }
        std::string command = message->text.substr(1, splitPosition - 1);
        if (!_broadcaster.broadcastCommand(command, message)) {
            _broadcaster.broadcastUnknownCommand(message);
        }
    } else {
        _broadcaster.broadcastNonCommandMessage(message);
    }
}

}