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 +++++++++++++++++++++++++++++++++++++-- include/tgbot/EventHandler.h | 4 ++ src/EventHandler.cpp | 47 +++++++++---- 3 files changed, 176 insertions(+), 19 deletions(-) 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; }; } diff --git a/include/tgbot/EventHandler.h b/include/tgbot/EventHandler.h index 3f92ca2..88a9bce 100644 --- a/include/tgbot/EventHandler.h +++ b/include/tgbot/EventHandler.h @@ -5,6 +5,10 @@ #include "tgbot/types/Update.h" #include "tgbot/tools/StringTools.h" +#include +#include +#include + namespace TgBot { class TGBOT_API EventHandler { diff --git a/src/EventHandler.cpp b/src/EventHandler.cpp index edf4c13..1a6ed40 100644 --- a/src/EventHandler.cpp +++ b/src/EventHandler.cpp @@ -1,14 +1,20 @@ #include "tgbot/EventHandler.h" -#include -#include -#include - -using namespace std; - namespace TgBot { void EventHandler::handleUpdate(const Update::Ptr& 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); } @@ -18,11 +24,26 @@ void EventHandler::handleUpdate(const Update::Ptr& update) const { if (update->callbackQuery != nullptr) { _broadcaster.broadcastCallbackQuery(update->callbackQuery); } - if (update->message != nullptr) { - handleMessage(update->message); + if (update->shippingQuery != nullptr) { + _broadcaster.broadcastShippingQuery(update->shippingQuery); } - if (update->channelPost != nullptr) { - handleMessage(update->channelPost); + 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); } } @@ -33,13 +54,13 @@ void EventHandler::handleMessage(const Message::Ptr& message) const { std::size_t splitPosition; std::size_t spacePosition = message->text.find(' '); std::size_t atSymbolPosition = message->text.find('@'); - if (spacePosition == string::npos) { - if (atSymbolPosition == string::npos) { + if (spacePosition == std::string::npos) { + if (atSymbolPosition == std::string::npos) { splitPosition = message->text.size(); } else { splitPosition = atSymbolPosition; } - } else if (atSymbolPosition == string::npos) { + } else if (atSymbolPosition == std::string::npos) { splitPosition = spacePosition; } else { splitPosition = std::min(spacePosition, atSymbolPosition); -- cgit v1.2.3