From a547fca53b0cad280a4ef3a42b65490efc8e631d Mon Sep 17 00:00:00 2001 From: llnulldisk <48621230+llnulldisk@users.noreply.github.com> Date: Tue, 18 Oct 2022 18:25:18 +0200 Subject: Update EventBroadcaster (#235) --- include/tgbot/EventBroadcaster.h | 144 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 138 insertions(+), 6 deletions(-) (limited to 'include/tgbot/EventBroadcaster.h') diff --git a/include/tgbot/EventBroadcaster.h b/include/tgbot/EventBroadcaster.h index ebecf3b..8773f35 100644 --- a/include/tgbot/EventBroadcaster.h +++ b/include/tgbot/EventBroadcaster.h @@ -6,12 +6,18 @@ #include "tgbot/types/InlineQuery.h" #include "tgbot/types/ChosenInlineResult.h" #include "tgbot/types/CallbackQuery.h" +#include "tgbot/types/ShippingQuery.h" +#include "tgbot/types/PreCheckoutQuery.h" +#include "tgbot/types/Poll.h" +#include "tgbot/types/PollAnswer.h" +#include "tgbot/types/ChatMemberUpdated.h" +#include "tgbot/types/ChatJoinRequest.h" +#include #include #include -#include -#include #include +#include namespace TgBot { @@ -31,9 +37,15 @@ public: typedef std::function InlineQueryListener; typedef std::function ChosenInlineResultListener; typedef std::function CallbackQueryListener; + typedef std::function ShippingQueryListener; + typedef std::function PreCheckoutQueryListener; + typedef std::function PollListener; + typedef std::function PollAnswerListener; + typedef std::function ChatMemberUpdatedListener; + typedef std::function ChatJoinRequestListener; /** - * @brief Registers listener which receives all messages which the bot can ever receive. + * @brief Registers listener which receives new incoming message of any kind - text, photo, sticker, etc. * @param listener Listener. */ inline void onAnyMessage(const MessageListener& listener) { @@ -87,7 +99,15 @@ public: } /** - * @brief Registers listener which receives all the inline query. + * @brief Registers listener which receives new versions of a message that is known to the bot and was edited + * @param listener Listener. + */ + inline void onEditedMessage(const MessageListener& listener) { + _onEditedMessageListeners.push_back(listener); + } + + /** + * @brief Registers listener which receives new incoming inline queries * @param listener Listener. */ inline void onInlineQuery(const InlineQueryListener& listener) { @@ -95,7 +115,9 @@ public: } /** - * @brief Registers listener which receives all the chosen inline result. + * @brief Registers listener which receives the results of an inline query that was chosen by a user and sent to their chat partner. + * Please see https://core.telegram.org/bots/inline#collecting-feedback for details on how to enable these updates for your bot. + * * @param listener Listener. */ inline void onChosenInlineResult(const ChosenInlineResultListener& listener){ @@ -103,13 +125,83 @@ public: } /** - * @brief Registers listener which receives all the callback query. + * @brief Registers listener which receives new incoming callback queries * @param listener Listener. */ inline void onCallbackQuery(const CallbackQueryListener& listener){ _onCallbackQueryListeners.push_back(listener); } + /** + * @brief Registers listener which receives new incoming shipping queries. + * Only for invoices with flexible price + * + * @param listener Listener. + */ + inline void onShippingQuery(const ShippingQueryListener& listener){ + _onShippingQueryListeners.push_back(listener); + } + + /** + * @brief Registers listener which receives new incoming pre-checkout queries. + * Contains full information about checkout + * + * @param listener Listener. + */ + inline void onPreCheckoutQuery(const PreCheckoutQueryListener& listener){ + _onPreCheckoutQueryListeners.push_back(listener); + } + + /** + * @brief Registers listener which receives new poll states. + * Bots receive only updates about stopped polls and polls, which are sent by the bot + * + * @param listener Listener. + */ + inline void onPoll(const PollListener& listener){ + _onPollListeners.push_back(listener); + } + + /** + * @brief Registers listener which receives an answer if a user changed their answer in a non-anonymous poll. + * Bots receive new votes only in polls that were sent by the bot itself. + * + * @param listener Listener. + */ + inline void onPollAnswer(const PollAnswerListener& listener){ + _onPollAnswerListeners.push_back(listener); + } + + /** + * @brief Registers listener which receives the bot's chat member status if it was updated in a chat. + * For private chats, this update is received only when the bot is blocked or unblocked by the user. + * + * @param listener Listener. + */ + inline void onMyChatMember(const ChatMemberUpdatedListener& listener){ + _onMyChatMemberListeners.push_back(listener); + } + + /** + * @brief Registers listener which receives a status if a chat member's status was updated in a chat. + * The bot must be an administrator in the chat and must explicitly specify “chat_member” in the list of allowedUpdates to receive these updates. + * + * @param listener Listener. + */ + inline void onChatMember(const ChatMemberUpdatedListener& listener){ + _onChatMemberListeners.push_back(listener); + } + + /** + * @brief Registers listener which receives requests to join the chat. + * The bot must have the canInviteUsers administrator right in the chat to receive these updates. + * + * @param listener Listener. + */ + inline void onChatJoinRequest(const ChatJoinRequestListener& listener){ + _onChatJoinRequestListeners.push_back(listener); + } + private: template inline void broadcast(const std::vector& listeners, const ObjectType object) const { @@ -142,6 +234,10 @@ private: broadcast(_onNonCommandMessageListeners, message); } + inline void broadcastEditedMessage(const Message::Ptr& message) const { + broadcast(_onEditedMessageListeners, message); + } + inline void broadcastInlineQuery(const InlineQuery::Ptr& query) const { broadcast(_onInlineQueryListeners, query); } @@ -154,13 +250,49 @@ private: broadcast(_onCallbackQueryListeners, result); } + inline void broadcastShippingQuery(const ShippingQuery::Ptr& result) const { + broadcast(_onShippingQueryListeners, result); + } + + inline void broadcastPreCheckoutQuery(const PreCheckoutQuery::Ptr& result) const { + broadcast(_onPreCheckoutQueryListeners, result); + } + + inline void broadcastPoll(const Poll::Ptr& result) const { + broadcast(_onPollListeners, result); + } + + inline void broadcastPollAnswer(const PollAnswer::Ptr& result) const { + broadcast(_onPollAnswerListeners, result); + } + + inline void broadcastMyChatMember(const ChatMemberUpdated::Ptr& result) const { + broadcast(_onMyChatMemberListeners, result); + } + + inline void broadcastChatMember(const ChatMemberUpdated::Ptr& result) const { + broadcast(_onChatMemberListeners, result); + } + + inline void broadcastChatJoinRequest(const ChatJoinRequest::Ptr& result) const { + broadcast(_onChatJoinRequestListeners, result); + } + std::vector _onAnyMessageListeners; std::unordered_map _onCommandListeners; std::vector _onUnknownCommandListeners; std::vector _onNonCommandMessageListeners; + std::vector _onEditedMessageListeners; std::vector _onInlineQueryListeners; std::vector _onChosenInlineResultListeners; std::vector _onCallbackQueryListeners; + std::vector _onShippingQueryListeners; + std::vector _onPreCheckoutQueryListeners; + std::vector _onPollListeners; + std::vector _onPollAnswerListeners; + std::vector _onMyChatMemberListeners; + std::vector _onChatMemberListeners; + std::vector _onChatJoinRequestListeners; }; } -- cgit v1.2.3