tgbot-cpp
Loading...
Searching...
No Matches
EventBroadcaster.h
Go to the documentation of this file.
1#ifndef TGBOT_EVENTBROADCASTER_H
2#define TGBOT_EVENTBROADCASTER_H
3
4#include "tgbot/export.h"
11#include "tgbot/types/Poll.h"
15
16#include <functional>
17#include <initializer_list>
18#include <string>
19#include <unordered_map>
20#include <vector>
21
22namespace TgBot {
23
24class EventHandler;
25
31class TGBOT_API EventBroadcaster {
32
33friend EventHandler;
34
35public:
36 typedef std::function<void (const Message::Ptr)> MessageListener;
37 typedef std::function<void (const InlineQuery::Ptr)> InlineQueryListener;
38 typedef std::function<void (const ChosenInlineResult::Ptr)> ChosenInlineResultListener;
39 typedef std::function<void (const CallbackQuery::Ptr)> CallbackQueryListener;
40 typedef std::function<void (const ShippingQuery::Ptr)> ShippingQueryListener;
41 typedef std::function<void (const PreCheckoutQuery::Ptr)> PreCheckoutQueryListener;
42 typedef std::function<void (const Poll::Ptr)> PollListener;
43 typedef std::function<void (const PollAnswer::Ptr)> PollAnswerListener;
44 typedef std::function<void (const ChatMemberUpdated::Ptr)> ChatMemberUpdatedListener;
45 typedef std::function<void (const ChatJoinRequest::Ptr)> ChatJoinRequestListener;
46
51 inline void onAnyMessage(const MessageListener& listener) {
52 _onAnyMessageListeners.push_back(listener);
53 }
54
60 inline void onCommand(const std::string& commandName, const MessageListener& listener) {
61 if (listener) {
62 _onCommandListeners[commandName] = listener;
63 } else {
64 _onCommandListeners.erase(commandName);
65 }
66 }
67
73 inline void onCommand(const std::initializer_list<std::string>& commandsList, const MessageListener& listener) {
74 if (listener) {
75 for (const auto& command : commandsList) {
76 _onCommandListeners[command] = listener;
77 }
78 } else {
79 for (const auto& command : commandsList) {
80 _onCommandListeners.erase(command);
81 }
82 }
83 }
84
89 inline void onUnknownCommand(const MessageListener& listener) {
90 _onUnknownCommandListeners.push_back(listener);
91 }
92
97 inline void onNonCommandMessage(const MessageListener& listener) {
98 _onNonCommandMessageListeners.push_back(listener);
99 }
100
105 inline void onEditedMessage(const MessageListener& listener) {
106 _onEditedMessageListeners.push_back(listener);
107 }
108
113 inline void onInlineQuery(const InlineQueryListener& listener) {
114 _onInlineQueryListeners.push_back(listener);
115 }
116
124 _onChosenInlineResultListeners.push_back(listener);
125 }
126
131 inline void onCallbackQuery(const CallbackQueryListener& listener){
132 _onCallbackQueryListeners.push_back(listener);
133 }
134
141 inline void onShippingQuery(const ShippingQueryListener& listener){
142 _onShippingQueryListeners.push_back(listener);
143 }
144
151 inline void onPreCheckoutQuery(const PreCheckoutQueryListener& listener){
152 _onPreCheckoutQueryListeners.push_back(listener);
153 }
154
161 inline void onPoll(const PollListener& listener){
162 _onPollListeners.push_back(listener);
163 }
164
171 inline void onPollAnswer(const PollAnswerListener& listener){
172 _onPollAnswerListeners.push_back(listener);
173 }
174
181 inline void onMyChatMember(const ChatMemberUpdatedListener& listener){
182 _onMyChatMemberListeners.push_back(listener);
183 }
184
191 inline void onChatMember(const ChatMemberUpdatedListener& listener){
192 _onChatMemberListeners.push_back(listener);
193 }
194
201 inline void onChatJoinRequest(const ChatJoinRequestListener& listener){
202 _onChatJoinRequestListeners.push_back(listener);
203 }
204
205private:
206 template<typename ListenerType, typename ObjectType>
207 inline void broadcast(const std::vector<ListenerType>& listeners, const ObjectType object) const {
208 if (!object)
209 return;
210
211 for (const ListenerType& item : listeners) {
212 item(object);
213 }
214 }
215
216 inline void broadcastAnyMessage(const Message::Ptr& message) const {
217 broadcast<MessageListener, Message::Ptr>(_onAnyMessageListeners, message);
218 }
219
220 inline bool broadcastCommand(const std::string& command, const Message::Ptr& message) const {
221 auto iter = _onCommandListeners.find(command);
222 if (iter == _onCommandListeners.end()) {
223 return false;
224 }
225 iter->second(message);
226 return true;
227 }
228
229 inline void broadcastUnknownCommand(const Message::Ptr& message) const {
230 broadcast<MessageListener, Message::Ptr>(_onUnknownCommandListeners, message);
231 }
232
233 inline void broadcastNonCommandMessage(const Message::Ptr& message) const {
234 broadcast<MessageListener, Message::Ptr>(_onNonCommandMessageListeners, message);
235 }
236
237 inline void broadcastEditedMessage(const Message::Ptr& message) const {
238 broadcast<MessageListener, Message::Ptr>(_onEditedMessageListeners, message);
239 }
240
241 inline void broadcastInlineQuery(const InlineQuery::Ptr& query) const {
242 broadcast<InlineQueryListener, InlineQuery::Ptr>(_onInlineQueryListeners, query);
243 }
244
245 inline void broadcastChosenInlineResult(const ChosenInlineResult::Ptr& result) const {
246 broadcast<ChosenInlineResultListener, ChosenInlineResult::Ptr>(_onChosenInlineResultListeners, result);
247 }
248
249 inline void broadcastCallbackQuery(const CallbackQuery::Ptr& result) const {
250 broadcast<CallbackQueryListener, CallbackQuery::Ptr>(_onCallbackQueryListeners, result);
251 }
252
253 inline void broadcastShippingQuery(const ShippingQuery::Ptr& result) const {
254 broadcast<ShippingQueryListener, ShippingQuery::Ptr>(_onShippingQueryListeners, result);
255 }
256
257 inline void broadcastPreCheckoutQuery(const PreCheckoutQuery::Ptr& result) const {
258 broadcast<PreCheckoutQueryListener, PreCheckoutQuery::Ptr>(_onPreCheckoutQueryListeners, result);
259 }
260
261 inline void broadcastPoll(const Poll::Ptr& result) const {
262 broadcast<PollListener, Poll::Ptr>(_onPollListeners, result);
263 }
264
265 inline void broadcastPollAnswer(const PollAnswer::Ptr& result) const {
266 broadcast<PollAnswerListener, PollAnswer::Ptr>(_onPollAnswerListeners, result);
267 }
268
269 inline void broadcastMyChatMember(const ChatMemberUpdated::Ptr& result) const {
270 broadcast<ChatMemberUpdatedListener, ChatMemberUpdated::Ptr>(_onMyChatMemberListeners, result);
271 }
272
273 inline void broadcastChatMember(const ChatMemberUpdated::Ptr& result) const {
274 broadcast<ChatMemberUpdatedListener, ChatMemberUpdated::Ptr>(_onChatMemberListeners, result);
275 }
276
277 inline void broadcastChatJoinRequest(const ChatJoinRequest::Ptr& result) const {
278 broadcast<ChatJoinRequestListener, ChatJoinRequest::Ptr>(_onChatJoinRequestListeners, result);
279 }
280
281 std::vector<MessageListener> _onAnyMessageListeners;
282 std::unordered_map<std::string, MessageListener> _onCommandListeners;
283 std::vector<MessageListener> _onUnknownCommandListeners;
284 std::vector<MessageListener> _onNonCommandMessageListeners;
285 std::vector<MessageListener> _onEditedMessageListeners;
286 std::vector<InlineQueryListener> _onInlineQueryListeners;
287 std::vector<ChosenInlineResultListener> _onChosenInlineResultListeners;
288 std::vector<CallbackQueryListener> _onCallbackQueryListeners;
289 std::vector<ShippingQueryListener> _onShippingQueryListeners;
290 std::vector<PreCheckoutQueryListener> _onPreCheckoutQueryListeners;
291 std::vector<PollListener> _onPollListeners;
292 std::vector<PollAnswerListener> _onPollAnswerListeners;
293 std::vector<ChatMemberUpdatedListener> _onMyChatMemberListeners;
294 std::vector<ChatMemberUpdatedListener> _onChatMemberListeners;
295 std::vector<ChatJoinRequestListener> _onChatJoinRequestListeners;
296};
297
298}
299
300#endif //TGBOT_EVENTBROADCASTER_H
std::shared_ptr< CallbackQuery > Ptr
std::shared_ptr< ChatJoinRequest > Ptr
std::shared_ptr< ChatMemberUpdated > Ptr
std::shared_ptr< ChosenInlineResult > Ptr
This class holds all event listeners.
void onChatMember(const ChatMemberUpdatedListener &listener)
Registers listener which receives a status if a chat member's status was updated in a chat....
void onCommand(const std::string &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,...
std::function< void(const PreCheckoutQuery::Ptr)> PreCheckoutQueryListener
void onNonCommandMessage(const MessageListener &listener)
Registers listener which receives all messages without commands (messages with no leading '/' char)
std::function< void(const ChatMemberUpdated::Ptr)> ChatMemberUpdatedListener
std::function< void(const InlineQuery::Ptr)> InlineQueryListener
void onPoll(const PollListener &listener)
Registers listener which receives new poll states. Bots receive only updates about stopped polls and ...
std::function< void(const ChatJoinRequest::Ptr)> ChatJoinRequestListener
std::function< void(const Poll::Ptr)> PollListener
void onCallbackQuery(const CallbackQueryListener &listener)
Registers listener which receives new incoming callback queries.
std::function< void(const ShippingQuery::Ptr)> ShippingQueryListener
std::function< void(const CallbackQuery::Ptr)> CallbackQueryListener
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(const ChosenInlineResult::Ptr)> ChosenInlineResultListener
void onUnknownCommand(const MessageListener &listener)
Registers listener which receives all messages with commands (messages with leading '/' char) which h...
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.
std::function< void(const Message::Ptr)> MessageListener
void onChatJoinRequest(const ChatJoinRequestListener &listener)
Registers listener which receives requests to join the chat. The bot must have the canInviteUsers adm...
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(const PollAnswer::Ptr)> PollAnswerListener
void onPreCheckoutQuery(const PreCheckoutQueryListener &listener)
Registers listener which receives new incoming pre-checkout queries. Contains full information about ...
void onCommand(const std::initializer_list< std::string > &commandsList, const MessageListener &listener)
Registers listener which receives all messages with commands (messages with leading '/' char).
std::shared_ptr< InlineQuery > Ptr
Definition InlineQuery.h:20
std::shared_ptr< Message > Ptr
Definition Message.h:68
std::shared_ptr< PollAnswer > Ptr
Definition PollAnswer.h:22
std::shared_ptr< Poll > Ptr
Definition Poll.h:22
std::shared_ptr< PreCheckoutQuery > Ptr
std::shared_ptr< ShippingQuery > Ptr
Definition Api.h:40