From f889903cd7132ce39e81da15d2f335ac511b2e1f Mon Sep 17 00:00:00 2001 From: Andrea Giove Date: Sun, 27 Mar 2016 11:16:28 +0200 Subject: Add new types and new event broadcaster for support the inline mode. --- include/tgbot/EventBroadcaster.h | 44 ++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 9 deletions(-) (limited to 'include/tgbot/EventBroadcaster.h') diff --git a/include/tgbot/EventBroadcaster.h b/include/tgbot/EventBroadcaster.h index 3ec8865..d97dbbb 100644 --- a/include/tgbot/EventBroadcaster.h +++ b/include/tgbot/EventBroadcaster.h @@ -29,6 +29,8 @@ #include #include "tgbot/types/Message.h" +#include "tgbot/types/InlineQuery.h" +#include "tgbot/types/ChosenInlineResult.h" namespace TgBot { @@ -44,6 +46,8 @@ friend EventHandler; public: typedef std::function MessageListener; + typedef std::function InlineQueryListener; + typedef std::function ChosenInlineResultListener; /** * Registers listener which receives all messages which the bot can ever receive. @@ -78,13 +82,29 @@ public: _onNonCommandMessageListeners.push_back(listener); } + inline void onInlineQuery(const InlineQueryListener& listener) { + _onInlineQueryListeners.push_back(listener); + } + + inline void onChosenInlineResult(const ChosenInlineResultListener& listener){ + _onChosenInlineResultListeners.push_back(listener); + } + private: - inline void broadcastAnyMessage(const Message::Ptr& message) const { - for (const MessageListener& item : _onAnyMessageListeners) { - item(message); + template + inline void broadcast(const std::vector& listeners, const ObjectType& object) const { + if (!object) + return; + + for (const ListenerType& item : listeners) { + item(object); } } + inline void broadcastAnyMessage(const Message::Ptr& message) const { + broadcast(_onAnyMessageListeners, message); + } + inline bool broadcastCommand(const std::string command, const Message::Ptr& message) const { std::map::const_iterator iter = _onCommandListeners.find(command); if (iter == _onCommandListeners.end()) { @@ -95,21 +115,27 @@ private: } inline void broadcastUnknownCommand(const Message::Ptr& message) const { - for (const MessageListener& item : _onUnknownCommandListeners) { - item(message); - } + broadcast(_onUnknownCommandListeners, message); } inline void broadcastNonCommandMessage(const Message::Ptr& message) const { - for (const MessageListener& item : _onNonCommandMessageListeners) { - item(message); - } + broadcast(_onNonCommandMessageListeners, message); + } + + inline void broadcastInlineQuery(const InlineQuery::Ptr& query) const { + broadcast(_onInlineQueryListeners, query); + } + + inline void broadcastChosenInlineResult(const ChosenInlineResult::Ptr& result) const { + broadcast(_onChosenInlineResultListeners, result); } std::vector _onAnyMessageListeners; std::map _onCommandListeners; std::vector _onUnknownCommandListeners; std::vector _onNonCommandMessageListeners; + std::vector _onInlineQueryListeners; + std::vector _onChosenInlineResultListeners; }; } -- cgit v1.2.3