23 #ifndef TGBOT_EVENTBROADCASTER_H 24 #define TGBOT_EVENTBROADCASTER_H 29 #include <unordered_map> 60 _onAnyMessageListeners.push_back(listener);
68 inline void onCommand(
const std::string& commandName,
const MessageListener& listener) {
69 _onCommandListeners[commandName] = listener;
77 inline void onCommand(
const std::initializer_list<std::string>& commandsList,
const MessageListener& listener) {
78 for (
const auto& command : commandsList)
80 _onCommandListeners[command] = listener;
89 _onUnknownCommandListeners.push_back(listener);
97 _onNonCommandMessageListeners.push_back(listener);
105 _onInlineQueryListeners.push_back(listener);
113 _onChosenInlineResultListeners.push_back(listener);
117 _onCallbackQueryListeners.push_back(listener);
121 template<
typename ListenerType,
typename ObjectType>
122 inline void broadcast(
const std::vector<ListenerType>& listeners,
const ObjectType
object)
const {
126 for (
const ListenerType& item : listeners) {
131 inline void broadcastAnyMessage(
const Message::Ptr message)
const {
132 broadcast<MessageListener, Message::Ptr>(_onAnyMessageListeners, message);
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()) {
140 iter->second(message);
144 inline void broadcastUnknownCommand(
const Message::Ptr message)
const {
145 broadcast<MessageListener, Message::Ptr>(_onUnknownCommandListeners, message);
148 inline void broadcastNonCommandMessage(
const Message::Ptr message)
const {
149 broadcast<MessageListener, Message::Ptr>(_onNonCommandMessageListeners, message);
153 broadcast<InlineQueryListener, InlineQuery::Ptr>(_onInlineQueryListeners, query);
157 broadcast<ChosenInlineResultListener, ChosenInlineResult::Ptr>(_onChosenInlineResultListeners, result);
161 broadcast<CallbackQueryListener, CallbackQuery::Ptr>(_onCallbackQueryListeners, result);
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;
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
std::function< void(const CallbackQuery::Ptr)> CallbackQueryListener
std::shared_ptr< Message > Ptr
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 '/' char) which h...
void onCommand(const std::string &commandName, const MessageListener &listener)
Registers listener which receives all messages with commands (messages with leading '/' 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 '/' char)...
void onNonCommandMessage(const MessageListener &listener)
Registers listener which receives all messages without commands (messages with no leading '/' char) ...
void onChosenInlineResult(const ChosenInlineResultListener &listener)
Registers listener which receives all the chosen inline result.
std::shared_ptr< InlineQuery > Ptr
std::function< void(const Message::Ptr)> MessageListener