tgbot-cpp
EventBroadcaster.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Oleg Morozenkov
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
23 #ifndef TGBOT_EVENTBROADCASTER_H
24 #define TGBOT_EVENTBROADCASTER_H
25 
26 #include <string>
27 #include <functional>
28 #include <vector>
29 #include <unordered_map>
30 
31 #include "tgbot/types/Message.h"
35 
36 namespace TgBot {
37 
38 class EventHandler;
39 
46 
47 friend EventHandler;
48 
49 public:
50  typedef std::function<void (const Message::Ptr)> MessageListener;
51  typedef std::function<void (const InlineQuery::Ptr)> InlineQueryListener;
52  typedef std::function<void (const ChosenInlineResult::Ptr)> ChosenInlineResultListener;
53  typedef std::function<void (const CallbackQuery::Ptr)> CallbackQueryListener;
54 
59  inline void onAnyMessage(const MessageListener& listener) {
60  _onAnyMessageListeners.push_back(listener);
61  }
62 
68  inline void onCommand(const std::string& commandName, const MessageListener& listener) {
69  _onCommandListeners[commandName] = listener;
70  }
71 
77  inline void onCommand(const std::initializer_list<std::string>& commandsList, const MessageListener& listener) {
78  for (const auto& command : commandsList)
79  {
80  _onCommandListeners[command] = listener;
81  }
82  }
83 
88  inline void onUnknownCommand(const MessageListener& listener) {
89  _onUnknownCommandListeners.push_back(listener);
90  }
91 
96  inline void onNonCommandMessage(const MessageListener& listener) {
97  _onNonCommandMessageListeners.push_back(listener);
98  }
99 
104  inline void onInlineQuery(const InlineQueryListener& listener) {
105  _onInlineQueryListeners.push_back(listener);
106  }
107 
112  inline void onChosenInlineResult(const ChosenInlineResultListener& listener){
113  _onChosenInlineResultListeners.push_back(listener);
114  }
115 
116  inline void onCallbackQuery(const CallbackQueryListener& listener){
117  _onCallbackQueryListeners.push_back(listener);
118  }
119 
120 private:
121  template<typename ListenerType, typename ObjectType>
122  inline void broadcast(const std::vector<ListenerType>& listeners, const ObjectType object) const {
123  if (!object)
124  return;
125 
126  for (const ListenerType& item : listeners) {
127  item(object);
128  }
129  }
130 
131  inline void broadcastAnyMessage(const Message::Ptr message) const {
132  broadcast<MessageListener, Message::Ptr>(_onAnyMessageListeners, message);
133  }
134 
135  inline bool broadcastCommand(const std::string command, const Message::Ptr message) const {
136  std::unordered_map<std::string, MessageListener>::const_iterator iter = _onCommandListeners.find(command);
137  if (iter == _onCommandListeners.end()) {
138  return false;
139  }
140  iter->second(message);
141  return true;
142  }
143 
144  inline void broadcastUnknownCommand(const Message::Ptr message) const {
145  broadcast<MessageListener, Message::Ptr>(_onUnknownCommandListeners, message);
146  }
147 
148  inline void broadcastNonCommandMessage(const Message::Ptr message) const {
149  broadcast<MessageListener, Message::Ptr>(_onNonCommandMessageListeners, message);
150  }
151 
152  inline void broadcastInlineQuery(const InlineQuery::Ptr query) const {
153  broadcast<InlineQueryListener, InlineQuery::Ptr>(_onInlineQueryListeners, query);
154  }
155 
156  inline void broadcastChosenInlineResult(const ChosenInlineResult::Ptr result) const {
157  broadcast<ChosenInlineResultListener, ChosenInlineResult::Ptr>(_onChosenInlineResultListeners, result);
158  }
159 
160  inline void broadcastCallbackQuery(const CallbackQuery::Ptr result) const {
161  broadcast<CallbackQueryListener, CallbackQuery::Ptr>(_onCallbackQueryListeners, result);
162  }
163 
164  std::vector<MessageListener> _onAnyMessageListeners;
165  std::unordered_map<std::string, MessageListener> _onCommandListeners;
166  std::vector<MessageListener> _onUnknownCommandListeners;
167  std::vector<MessageListener> _onNonCommandMessageListeners;
168  std::vector<InlineQueryListener> _onInlineQueryListeners;
169  std::vector<ChosenInlineResultListener> _onChosenInlineResultListeners;
170  std::vector<CallbackQueryListener> _onCallbackQueryListeners;
171 };
172 
173 }
174 
175 #endif //TGBOT_EVENTBROADCASTER_H
void onAnyMessage(const MessageListener &listener)
Registers listener which receives all messages which the bot can ever receive.
This class holds all event listeners.
std::shared_ptr< CallbackQuery > Ptr
Definition: CallbackQuery.h:22
Definition: Api.h:49
std::function< void(const CallbackQuery::Ptr)> CallbackQueryListener
std::shared_ptr< Message > Ptr
Definition: Message.h:56
std::shared_ptr< ChosenInlineResult > Ptr
void onInlineQuery(const InlineQueryListener &listener)
Registers listener which receives all the inline query.
void onCallbackQuery(const CallbackQueryListener &listener)
std::function< void(const ChosenInlineResult::Ptr)> ChosenInlineResultListener
void onUnknownCommand(const MessageListener &listener)
Registers listener which receives all messages with commands (messages with leading &#39;/&#39; char) which h...
void onCommand(const std::string &commandName, const MessageListener &listener)
Registers listener which receives all messages with commands (messages with leading &#39;/&#39; char)...
std::function< void(const InlineQuery::Ptr)> InlineQueryListener
void onCommand(const std::initializer_list< std::string > &commandsList, const MessageListener &listener)
Registers listener which receives all messages with commands (messages with leading &#39;/&#39; char)...
void onNonCommandMessage(const MessageListener &listener)
Registers listener which receives all messages without commands (messages with no leading &#39;/&#39; char) ...
void onChosenInlineResult(const ChosenInlineResultListener &listener)
Registers listener which receives all the chosen inline result.
std::shared_ptr< InlineQuery > Ptr
Definition: InlineQuery.h:23
std::function< void(const Message::Ptr)> MessageListener