-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathEventHandler.cpp
More file actions
96 lines (88 loc) · 3.28 KB
/
Copy pathEventHandler.cpp
File metadata and controls
96 lines (88 loc) · 3.28 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
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
#include "tgbot/EventHandler.h"
#include <algorithm>
#include <cstddef>
#include <string>
namespace TgBot {
EventHandler::EventHandler(const EventBroadcaster& broadcaster)
: _broadcaster(broadcaster) {
}
void EventHandler::handleUpdate(const std::shared_ptr<Update>& update) const {
if (update->message != nullptr) {
handleMessage(update->message);
}
if (update->editedMessage != nullptr) {
_broadcaster.broadcastEditedMessage(update->editedMessage);
}
if (update->channelPost != nullptr) {
handleMessage(update->channelPost);
}
if (update->editedChannelPost != nullptr) {
_broadcaster.broadcastEditedMessage(update->editedChannelPost);
}
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->shippingQuery != nullptr) {
_broadcaster.broadcastShippingQuery(update->shippingQuery);
}
if (update->preCheckoutQuery != nullptr) {
_broadcaster.broadcastPreCheckoutQuery(update->preCheckoutQuery);
}
if (update->poll != nullptr) {
_broadcaster.broadcastPoll(update->poll);
}
if (update->pollAnswer != nullptr) {
_broadcaster.broadcastPollAnswer(update->pollAnswer);
}
if (update->myChatMember != nullptr) {
_broadcaster.broadcastMyChatMember(update->myChatMember);
}
if (update->chatMember != nullptr) {
_broadcaster.broadcastChatMember(update->chatMember);
}
if (update->chatJoinRequest != nullptr) {
_broadcaster.broadcastChatJoinRequest(update->chatJoinRequest);
}
if (update->messageReaction != nullptr) {
_broadcaster.broadcastMessageReactionUpdated(update->messageReaction);
}
if (update->messageReactionCount != nullptr) {
_broadcaster.broadcastMessageReactionCountUpdated(update->messageReactionCount);
}
}
void EventHandler::handleMessage(const std::shared_ptr<Message>& message) const {
_broadcaster.broadcastAnyMessage(message);
const std::string text = message->text.value_or("");
if (text.starts_with('/')) {
std::size_t splitPosition;
std::size_t spacePosition = text.find(' ');
std::size_t atSymbolPosition = text.find('@');
if (spacePosition == std::string::npos) {
if (atSymbolPosition == std::string::npos) {
splitPosition = text.size();
} else {
splitPosition = atSymbolPosition;
}
} else if (atSymbolPosition == std::string::npos) {
splitPosition = spacePosition;
} else {
splitPosition = std::min(spacePosition, atSymbolPosition);
}
std::string command = text.substr(1, splitPosition - 1);
if (!_broadcaster.broadcastCommand(command, message)) {
_broadcaster.broadcastUnknownCommand(message);
}
} else {
_broadcaster.broadcastNonCommandMessage(message);
}
if (message->successfulPayment != nullptr) {
_broadcaster.broadcastSuccessfulPayment(message);
}
}
} // namespace TgBot