summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorllnulldisk <48621230+llnulldisk@users.noreply.github.com>2022-10-18 18:25:18 +0200
committerllnulldisk <48621230+llnulldisk@users.noreply.github.com>2022-10-18 18:25:18 +0200
commita547fca53b0cad280a4ef3a42b65490efc8e631d (patch)
tree341323c337373c8b04d3ebae7fc2188fd264f82e
parent1d238b70e2655e64b1cb1a51357a9eb0c753181d (diff)
Update EventBroadcaster (#235)
-rw-r--r--include/tgbot/EventBroadcaster.h144
-rw-r--r--include/tgbot/EventHandler.h4
-rw-r--r--src/EventHandler.cpp47
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 <functional>
#include <initializer_list>
#include <string>
-#include <functional>
-#include <vector>
#include <unordered_map>
+#include <vector>
namespace TgBot {
@@ -31,9 +37,15 @@ public:
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 ShippingQuery::Ptr)> ShippingQueryListener;
+ typedef std::function<void (const PreCheckoutQuery::Ptr)> PreCheckoutQueryListener;
+ typedef std::function<void (const Poll::Ptr)> PollListener;
+ typedef std::function<void (const PollAnswer::Ptr)> PollAnswerListener;
+ typedef std::function<void (const ChatMemberUpdated::Ptr)> ChatMemberUpdatedListener;
+ typedef std::function<void (const ChatJoinRequest::Ptr)> 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<typename ListenerType, typename ObjectType>
inline void broadcast(const std::vector<ListenerType>& listeners, const ObjectType object) const {
@@ -142,6 +234,10 @@ private:
broadcast<MessageListener, Message::Ptr>(_onNonCommandMessageListeners, message);
}
+ inline void broadcastEditedMessage(const Message::Ptr& message) const {
+ broadcast<MessageListener, Message::Ptr>(_onEditedMessageListeners, message);
+ }
+
inline void broadcastInlineQuery(const InlineQuery::Ptr& query) const {
broadcast<InlineQueryListener, InlineQuery::Ptr>(_onInlineQueryListeners, query);
}
@@ -154,13 +250,49 @@ private:
broadcast<CallbackQueryListener, CallbackQuery::Ptr>(_onCallbackQueryListeners, result);
}
+ inline void broadcastShippingQuery(const ShippingQuery::Ptr& result) const {
+ broadcast<ShippingQueryListener, ShippingQuery::Ptr>(_onShippingQueryListeners, result);
+ }
+
+ inline void broadcastPreCheckoutQuery(const PreCheckoutQuery::Ptr& result) const {
+ broadcast<PreCheckoutQueryListener, PreCheckoutQuery::Ptr>(_onPreCheckoutQueryListeners, result);
+ }
+
+ inline void broadcastPoll(const Poll::Ptr& result) const {
+ broadcast<PollListener, Poll::Ptr>(_onPollListeners, result);
+ }
+
+ inline void broadcastPollAnswer(const PollAnswer::Ptr& result) const {
+ broadcast<PollAnswerListener, PollAnswer::Ptr>(_onPollAnswerListeners, result);
+ }
+
+ inline void broadcastMyChatMember(const ChatMemberUpdated::Ptr& result) const {
+ broadcast<ChatMemberUpdatedListener, ChatMemberUpdated::Ptr>(_onMyChatMemberListeners, result);
+ }
+
+ inline void broadcastChatMember(const ChatMemberUpdated::Ptr& result) const {
+ broadcast<ChatMemberUpdatedListener, ChatMemberUpdated::Ptr>(_onChatMemberListeners, result);
+ }
+
+ inline void broadcastChatJoinRequest(const ChatJoinRequest::Ptr& result) const {
+ broadcast<ChatJoinRequestListener, ChatJoinRequest::Ptr>(_onChatJoinRequestListeners, result);
+ }
+
std::vector<MessageListener> _onAnyMessageListeners;
std::unordered_map<std::string, MessageListener> _onCommandListeners;
std::vector<MessageListener> _onUnknownCommandListeners;
std::vector<MessageListener> _onNonCommandMessageListeners;
+ std::vector<MessageListener> _onEditedMessageListeners;
std::vector<InlineQueryListener> _onInlineQueryListeners;
std::vector<ChosenInlineResultListener> _onChosenInlineResultListeners;
std::vector<CallbackQueryListener> _onCallbackQueryListeners;
+ std::vector<ShippingQueryListener> _onShippingQueryListeners;
+ std::vector<PreCheckoutQueryListener> _onPreCheckoutQueryListeners;
+ std::vector<PollListener> _onPollListeners;
+ std::vector<PollAnswerListener> _onPollAnswerListeners;
+ std::vector<ChatMemberUpdatedListener> _onMyChatMemberListeners;
+ std::vector<ChatMemberUpdatedListener> _onChatMemberListeners;
+ std::vector<ChatJoinRequestListener> _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 <algorithm>
+#include <cstddef>
+#include <string>
+
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 <algorithm>
-#include <cstddef>
-#include <string>
-
-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);