diff options
author | Oleg Morozenkov <reo7sp@users.noreply.github.com> | 2017-01-23 13:10:09 +0300 |
---|---|---|
committer | Oleg Morozenkov <reo7sp@users.noreply.github.com> | 2017-01-23 13:10:09 +0300 |
commit | 989c691916d17c4819c75132dbbe36de1131c467 (patch) | |
tree | 7ad62625b53738f42b5b3be54cc7fcbebab078ed /src/EventHandler.cpp | |
parent | 310792316c48bc8f3218abb30d411f7e1c823747 (diff) |
I think EventBroadcast and EventHandler should not use shared_ptr references too
Diffstat (limited to 'src/EventHandler.cpp')
-rw-r--r-- | src/EventHandler.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/EventHandler.cpp b/src/EventHandler.cpp new file mode 100644 index 0000000..8529400 --- /dev/null +++ b/src/EventHandler.cpp @@ -0,0 +1,51 @@ +// +// Created by Oleg Morozenkov on 23.01.17. +// + +#include "tgbot/EventHandler.h" + +namespace TgBot { + +void EventHandler::handleUpdate(const 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(const Message::Ptr message) const { + _broadcaster->broadcastAnyMessage(message); + + if (StringTools::startsWith(message->text, "/")) { + unsigned long splitPosition; + unsigned long spacePosition = message->text.find(' '); + unsigned long atSymbolPosition = message->text.find('@'); + if (spacePosition == message->text.npos) { + if (atSymbolPosition == message->text.npos) { + splitPosition = message->text.size(); + } else { + splitPosition = atSymbolPosition; + } + } else if (atSymbolPosition == message->text.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); + } +} + +}
\ No newline at end of file |