tgbot-cpp
Loading...
Searching...
No Matches
EventBroadcaster.h
Go to the documentation of this file.
1#pragma once
2
3#include "tgbot/Types.h"
4#include "tgbot/export.h"
5
6#include <functional>
7#include <initializer_list>
8#include <string>
9#include <string_view>
10#include <unordered_map>
11#include <vector>
12
13namespace TgBot {
14
15class EventHandler;
16
23 friend EventHandler;
24
25public:
26 using MessageListener = std::function<void(std::shared_ptr<Message>)>;
27 using InlineQueryListener = std::function<void(std::shared_ptr<InlineQuery>)>;
28 using ChosenInlineResultListener = std::function<void(std::shared_ptr<ChosenInlineResult>)>;
29 using CallbackQueryListener = std::function<void(std::shared_ptr<CallbackQuery>)>;
30 using ShippingQueryListener = std::function<void(std::shared_ptr<ShippingQuery>)>;
31 using PreCheckoutQueryListener = std::function<void(std::shared_ptr<PreCheckoutQuery>)>;
32 using PollListener = std::function<void(std::shared_ptr<Poll>)>;
33 using PollAnswerListener = std::function<void(std::shared_ptr<PollAnswer>)>;
34 using ChatMemberUpdatedListener = std::function<void(std::shared_ptr<ChatMemberUpdated>)>;
35 using ChatJoinRequestListener = std::function<void(std::shared_ptr<ChatJoinRequest>)>;
36 using MessageReactionUpdatedListener = std::function<void(std::shared_ptr<MessageReactionUpdated>)>;
37 using MessageReactionCountUpdatedListener = std::function<void(std::shared_ptr<MessageReactionCountUpdated>)>;
38 using MessageGenerationStoppedListener = std::function<void(std::shared_ptr<MessageGenerationStopped>)>;
39 using SuccessfulPaymentListener = std::function<void(std::shared_ptr<Message>, std::shared_ptr<SuccessfulPayment>)>;
40
45 void onAnyMessage(const MessageListener& listener);
46
52 void onCommand(std::string_view commandName, const MessageListener& listener);
53
59 void onCommand(std::initializer_list<std::string_view> commandsList, const MessageListener& listener);
60
65 void onUnknownCommand(const MessageListener& listener);
66
71 void onNonCommandMessage(const MessageListener& listener);
72
77 void onEditedMessage(const MessageListener& listener);
78
83 void onInlineQuery(const InlineQueryListener& listener);
84
92
98
106
114
121 void onPoll(const PollListener& listener);
122
129 void onPollAnswer(const PollAnswerListener& listener);
130
138
146
154
160
166
172
180
181private:
182 struct TransparentStringHash {
183 using is_transparent = void;
184
185 std::size_t operator()(std::string_view value) const noexcept {
186 return std::hash<std::string_view> { }(value);
187 }
188 };
189
190 template<typename ListenerType, typename ObjectType>
191 void broadcast(const std::vector<ListenerType>& listeners, ObjectType object) const;
192
193 void broadcastAnyMessage(const std::shared_ptr<Message>& message) const;
194 bool broadcastCommand(std::string_view command, const std::shared_ptr<Message>& message) const;
195 void broadcastUnknownCommand(const std::shared_ptr<Message>& message) const;
196 void broadcastNonCommandMessage(const std::shared_ptr<Message>& message) const;
197 void broadcastEditedMessage(const std::shared_ptr<Message>& message) const;
198 void broadcastInlineQuery(const std::shared_ptr<InlineQuery>& query) const;
199 void broadcastChosenInlineResult(const std::shared_ptr<ChosenInlineResult>& result) const;
200 void broadcastCallbackQuery(const std::shared_ptr<CallbackQuery>& result) const;
201 void broadcastShippingQuery(const std::shared_ptr<ShippingQuery>& result) const;
202 void broadcastPreCheckoutQuery(const std::shared_ptr<PreCheckoutQuery>& result) const;
203 void broadcastPoll(const std::shared_ptr<Poll>& result) const;
204 void broadcastPollAnswer(const std::shared_ptr<PollAnswer>& result) const;
205 void broadcastMyChatMember(const std::shared_ptr<ChatMemberUpdated>& result) const;
206 void broadcastChatMember(const std::shared_ptr<ChatMemberUpdated>& result) const;
207 void broadcastChatJoinRequest(const std::shared_ptr<ChatJoinRequest>& result) const;
208 void broadcastMessageReactionUpdated(const std::shared_ptr<MessageReactionUpdated>& messageReaction) const;
209 void broadcastMessageReactionCountUpdated(
210 const std::shared_ptr<MessageReactionCountUpdated>& messageReactionCount) const;
211 void
212 broadcastMessageGenerationStopped(const std::shared_ptr<MessageGenerationStopped>& messageGenerationStopped) const;
213 void broadcastSuccessfulPayment(const std::shared_ptr<Message>& message) const;
214
215 std::vector<MessageListener> _onAnyMessageListeners;
216 std::unordered_map<std::string, MessageListener, TransparentStringHash, std::equal_to<>> _onCommandListeners;
217 std::vector<MessageListener> _onUnknownCommandListeners;
218 std::vector<MessageListener> _onNonCommandMessageListeners;
219 std::vector<MessageListener> _onEditedMessageListeners;
220 std::vector<InlineQueryListener> _onInlineQueryListeners;
221 std::vector<ChosenInlineResultListener> _onChosenInlineResultListeners;
222 std::vector<CallbackQueryListener> _onCallbackQueryListeners;
223 std::vector<ShippingQueryListener> _onShippingQueryListeners;
224 std::vector<PreCheckoutQueryListener> _onPreCheckoutQueryListeners;
225 std::vector<PollListener> _onPollListeners;
226 std::vector<PollAnswerListener> _onPollAnswerListeners;
227 std::vector<ChatMemberUpdatedListener> _onMyChatMemberListeners;
228 std::vector<ChatMemberUpdatedListener> _onChatMemberListeners;
229 std::vector<ChatJoinRequestListener> _onChatJoinRequestListeners;
230 std::vector<MessageReactionUpdatedListener> _onMessageReactionUpdatedListener;
231 std::vector<MessageReactionCountUpdatedListener> _onMessageReactionCountUpdatedListener;
232 std::vector<MessageGenerationStoppedListener> _onMessageGenerationStoppedListeners;
233 std::vector<SuccessfulPaymentListener> _onSuccessfulPaymentListeners;
234};
235
236} // namespace TgBot
This class holds all event listeners.
std::function< void(std::shared_ptr< ChatMemberUpdated >)> ChatMemberUpdatedListener
void onChatMember(const ChatMemberUpdatedListener &listener)
Registers listener which receives a status if a chat member's status was updated in a chat....
void onCommand(std::string_view commandName, const MessageListener &listener)
Registers listener which receives all messages with commands (messages with leading '/' char).
void onShippingQuery(const ShippingQueryListener &listener)
Registers listener which receives new incoming shipping queries. Only for invoices with flexible pric...
void onAnyMessage(const MessageListener &listener)
Registers listener which receives new incoming message of any kind - text, photo, sticker,...
void onNonCommandMessage(const MessageListener &listener)
Registers listener which receives all messages without commands (messages with no leading '/' char).
void onPoll(const PollListener &listener)
Registers listener which receives new poll states. Bots receive only updates about stopped polls and ...
std::function< void(std::shared_ptr< ChosenInlineResult >)> ChosenInlineResultListener
void onCallbackQuery(const CallbackQueryListener &listener)
Registers listener which receives new incoming callback queries.
std::function< void(std::shared_ptr< MessageGenerationStopped >)> MessageGenerationStoppedListener
void onPollAnswer(const PollAnswerListener &listener)
Registers listener which receives an answer if a user changed their answer in a non-anonymous poll....
std::function< void(std::shared_ptr< InlineQuery >)> InlineQueryListener
void onUnknownCommand(const MessageListener &listener)
Registers listener which receives all messages with commands (messages with leading '/' char) which h...
void onMessageReaction(const MessageReactionUpdatedListener &listener)
Registers listener which receives new incoming message reaction update event.
std::function< void(std::shared_ptr< PreCheckoutQuery >)> PreCheckoutQueryListener
void onMessageGenerationStopped(const MessageGenerationStoppedListener &listener)
Registers listener which receives updates when a user stops message generation.
void onChosenInlineResult(const ChosenInlineResultListener &listener)
Registers listener which receives the results of an inline query that was chosen by a user and sent t...
void onEditedMessage(const MessageListener &listener)
Registers listener which receives new versions of a message that is known to the bot and was edited.
void onMessageReactionCount(const MessageReactionCountUpdatedListener &listener)
Registers listener which receives new incoming message reaction count update event.
void onChatJoinRequest(const ChatJoinRequestListener &listener)
Registers listener which receives requests to join the chat. The bot must have the canInviteUsers adm...
std::function< void(std::shared_ptr< PollAnswer >)> PollAnswerListener
void onInlineQuery(const InlineQueryListener &listener)
Registers listener which receives new incoming inline queries.
void onMyChatMember(const ChatMemberUpdatedListener &listener)
Registers listener which receives the bot's chat member status if it was updated in a chat....
std::function< void(std::shared_ptr< Poll >)> PollListener
std::function< void(std::shared_ptr< Message >)> MessageListener
void onCommand(std::initializer_list< std::string_view > commandsList, const MessageListener &listener)
Registers listener which receives all messages with commands (messages with leading '/' char).
void onPreCheckoutQuery(const PreCheckoutQueryListener &listener)
Registers listener which receives new incoming pre-checkout queries. Contains full information about ...
std::function< void(std::shared_ptr< CallbackQuery >)> CallbackQueryListener
std::function< void(std::shared_ptr< Message >, std::shared_ptr< SuccessfulPayment >)> SuccessfulPaymentListener
std::function< void(std::shared_ptr< ChatJoinRequest >)> ChatJoinRequestListener
std::function< void(std::shared_ptr< MessageReactionCountUpdated >)> MessageReactionCountUpdatedListener
void onSuccessfulPayment(const SuccessfulPaymentListener &listener)
Registers listener which receives information about successful payments. This listener is triggered w...
std::function< void(std::shared_ptr< ShippingQuery >)> ShippingQueryListener
std::function< void(std::shared_ptr< MessageReactionUpdated >)> MessageReactionUpdatedListener
#define TGBOT_API
Definition export.h:5
Definition Api.h:18