diff options
-rw-r--r-- | CMakeLists.txt | 4 | ||||
-rw-r--r-- | include/tgbot/EventBroadcaster.h | 24 | ||||
-rw-r--r-- | include/tgbot/EventHandler.h | 40 | ||||
-rw-r--r-- | src/EventHandler.cpp | 51 |
4 files changed, 69 insertions, 50 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index e25e560..cdaf588 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,9 @@ set(SRC_LIST src/net/HttpParser.cpp src/net/TgLongPoll.cpp src/tools/StringTools.cpp - src/types/InlineQueryResult.cpp) + src/types/InlineQueryResult.cpp + src/EventHandler.cpp +) ### libs # threads diff --git a/include/tgbot/EventBroadcaster.h b/include/tgbot/EventBroadcaster.h index 12daef3..4378c10 100644 --- a/include/tgbot/EventBroadcaster.h +++ b/include/tgbot/EventBroadcaster.h @@ -46,10 +46,10 @@ class EventBroadcaster { friend EventHandler; public: - typedef std::function<void (const Message::Ptr&)> MessageListener; - typedef std::function<void (const InlineQuery::Ptr&)> InlineQueryListener; - typedef std::function<void (const ChosenInlineResult::Ptr&)> ChosenInlineResultListener; - typedef std::function<void (const CallbackQuery::Ptr&)> CallbackQueryListener; + typedef std::function<void (const Message::Ptr)> MessageListener; + typedef std::function<void (const InlineQuery::Ptr)> InlineQueryListener; + typedef std::function<void (const ChosenInlineResult::Ptr)> ChosenInlineResultListener; + typedef std::function<void (const CallbackQuery::Ptr)> CallbackQueryListener; /** * Registers listener which receives all messages which the bot can ever receive. @@ -106,7 +106,7 @@ public: private: template<typename ListenerType, typename ObjectType> - inline void broadcast(const std::vector<ListenerType>& listeners, const ObjectType& object) const { + inline void broadcast(const std::vector<ListenerType>& listeners, const ObjectType object) const { if (!object) return; @@ -115,11 +115,11 @@ private: } } - inline void broadcastAnyMessage(const Message::Ptr& message) const { + inline void broadcastAnyMessage(const Message::Ptr message) const { broadcast<MessageListener, Message::Ptr>(_onAnyMessageListeners, message); } - inline bool broadcastCommand(const std::string command, const Message::Ptr& message) const { + inline bool broadcastCommand(const std::string command, const Message::Ptr message) const { std::map<std::string, MessageListener>::const_iterator iter = _onCommandListeners.find(command); if (iter == _onCommandListeners.end()) { return false; @@ -128,23 +128,23 @@ private: return true; } - inline void broadcastUnknownCommand(const Message::Ptr& message) const { + inline void broadcastUnknownCommand(const Message::Ptr message) const { broadcast<MessageListener, Message::Ptr>(_onUnknownCommandListeners, message); } - inline void broadcastNonCommandMessage(const Message::Ptr& message) const { + inline void broadcastNonCommandMessage(const Message::Ptr message) const { broadcast<MessageListener, Message::Ptr>(_onNonCommandMessageListeners, message); } - inline void broadcastInlineQuery(const InlineQuery::Ptr& query) const { + inline void broadcastInlineQuery(const InlineQuery::Ptr query) const { broadcast<InlineQueryListener, InlineQuery::Ptr>(_onInlineQueryListeners, query); } - inline void broadcastChosenInlineResult(const ChosenInlineResult::Ptr& result) const { + inline void broadcastChosenInlineResult(const ChosenInlineResult::Ptr result) const { broadcast<ChosenInlineResultListener, ChosenInlineResult::Ptr>(_onChosenInlineResultListeners, result); } - inline void broadcastCallbackQuery(const CallbackQuery::Ptr& result) const { + inline void broadcastCallbackQuery(const CallbackQuery::Ptr result) const { broadcast<CallbackQueryListener, CallbackQuery::Ptr>(_onCallbackQueryListeners, result); } diff --git a/include/tgbot/EventHandler.h b/include/tgbot/EventHandler.h index 63ecde3..7fa68ee 100644 --- a/include/tgbot/EventHandler.h +++ b/include/tgbot/EventHandler.h @@ -31,50 +31,16 @@ namespace TgBot { class EventHandler { - void 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); - } - }; - public: explicit EventHandler(const EventBroadcaster* broadcaster) : _broadcaster(broadcaster) { } - inline void handleUpdate(const Update::Ptr& update) const { - if (update->inlineQuery != NULL) - _broadcaster->broadcastInlineQuery(update->inlineQuery); - if (update->chosenInlineResult != NULL) - _broadcaster->broadcastChosenInlineResult(update->chosenInlineResult); - if (update->callbackQuery != NULL) - _broadcaster->broadcastCallbackQuery(update->callbackQuery); - if (update->message != NULL) - handleMessage(update->message); - } + void handleUpdate(const Update::Ptr update) const; private: const EventBroadcaster* _broadcaster; + + void handleMessage(const Message::Ptr message) const; }; } 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 |