tgbotxx 1.2.9.2
Telegram Bot C++ Library
Loading...
Searching...
No Matches
Bot.hpp
Go to the documentation of this file.
1#pragma once
2#include "Exception.hpp"
3#include <cstdint>
4#include <string>
6#include <vector>
7
8namespace tgbotxx {
9 class Api;
10 struct Message;
11 struct Update;
12 struct BotCommand;
13 struct InlineQuery;
14 struct ChosenInlineResult;
15 struct CallbackQuery;
16 struct ShippingQuery;
17 struct PreCheckoutQuery;
18 struct Poll;
19 struct PollAnswer;
20 struct ChatMemberUpdated;
21 struct ChatJoinRequest;
22 struct MessageReactionUpdated;
23 struct MessageReactionCountUpdated;
24 struct ChatBoostUpdated;
25 struct ChatBoostRemoved;
26 struct BusinessConnection;
27 struct BusinessMessagesDeleted;
28 struct PaidMediaPurchased;
29
30 class Bot {
31 Ptr<Api> m_api{};
32 std::vector<Ptr<Update>> m_updates{};
33 std::int32_t m_lastUpdateId{};
34 std::shared_ptr<std::atomic<bool>> m_stopped{};
35
36 public:
39 explicit Bot(const std::string& token);
40 virtual ~Bot();
41
44 void start();
45
47 void stop();
48
49 public:
50#pragma region Lifecycle
53 virtual void onStart() {}
54
57 virtual void onStop() {}
58#pragma endregion Lifecycle
59
60#pragma region Messages
63 virtual void onAnyMessage(const Ptr<Message>& message) {}
64
67 virtual void onNonCommandMessage(const Ptr<Message>& message) {}
68
71 virtual void onCommand(const Ptr<Message>& command) {}
72
76 virtual void onUnknownCommand(const Ptr<Message>& message) {}
77
80 virtual void onMessageEdited(const Ptr<Message>& editedMessage) {}
81#pragma endregion Messages
82
83#pragma region Channel
86 virtual void onChannelPost(const Ptr<Message>& channelPost) {}
87
90 virtual void onChannelPostEdited(const Ptr<Message>& editedChannelPost) {}
91#pragma endregion Channel
92
93#pragma region Business
97 virtual void onBusinessConnection(const Ptr<BusinessConnection>& businessConnection) {}
98
101 virtual void onBusinessMessage(const Ptr<Message>& businessMessage) {}
102
105 virtual void onBusinessMessageEdited(const Ptr<Message>& editedBusinessMessage) {}
106
109 virtual void onBusinessMessagesDeleted(const Ptr<BusinessMessagesDeleted>& deletedBusinessMessages) {}
110#pragma endregion Business
111
112#pragma region MessageReaction
117 virtual void onMessageReactionUpdated(const Ptr<MessageReactionUpdated>& messageReaction) {}
118
123 virtual void onMessageReactionCountUpdated(const Ptr<MessageReactionCountUpdated>& messageReactionCount) {}
124#pragma endregion MessageReaction
125
126#pragma region InlineMode
129 virtual void onInlineQuery(const Ptr<InlineQuery>& inlineQuery) {}
130
134 virtual void onChosenInlineResult(const Ptr<ChosenInlineResult>& chosenInlineResult) {}
135
138 virtual void onCallbackQuery(const Ptr<CallbackQuery>& callbackQuery) {}
139
143 virtual void onShippingQuery(const Ptr<ShippingQuery>& shippingQuery) {}
144
147 virtual void onPreCheckoutQuery(const Ptr<PreCheckoutQuery>& preCheckoutQuery) {}
148#pragma endregion InlineMode
149
150#pragma region Payments
153 virtual void onPaidMediaPurchased(const Ptr<PaidMediaPurchased>& purchasedPaidMedia) {}
154#pragma endregion Payments
155
156#pragma region Poll
160 virtual void onPoll(const Ptr<Poll>& poll) {}
161
165 virtual void onPollAnswer(const Ptr<PollAnswer>& pollAnswer) {}
166#pragma endregion Poll
167
168#pragma region Chat
172 virtual void onMyChatMemberUpdated(const Ptr<ChatMemberUpdated>& myChatMember) {}
173
177 virtual void onChatMemberUpdated(const Ptr<ChatMemberUpdated>& chatMember) {}
178
182 virtual void onChatJoinRequest(const Ptr<ChatJoinRequest>& chatJoinRequest) {}
183
186 virtual void onChatBoostUpdated(const Ptr<ChatBoostUpdated>& chatBoost) {}
187
190 virtual void onChatBoostRemoved(const Ptr<ChatBoostRemoved>& removedChatBoost) {}
191#pragma endregion Chat
192
193#pragma region Errors
197 virtual void onLongPollError(const std::string& errorMessage, ErrorCode errorCode) {}
198#pragma endregion Errors
199
200 protected:
205
208
209 private: // Dispatchers
212 void dispatch(const Ptr<Update>& update);
213
217 void dispatchMessages(const Ptr<Update>& update);
218
221 void dispatchChannel(const Ptr<Update>& update);
222
225 void dispatchBusiness(const Ptr<Update>& update);
226
229 void dispatchMessageReaction(const Ptr<Update>& update);
230
233 void dispatchInlineMode(const Ptr<Update>& update);
234
237 void dispatchPayments(const Ptr<Update>& update);
238
241 void dispatchPoll(const Ptr<Update>& update);
242
245 void dispatchChat(const Ptr<Update>& update);
246 };
247}
Api Methods https://core.telegram.org/bots/api#available-methods.
Definition Api.hpp:75
virtual void onChatBoostUpdated(const Ptr< ChatBoostUpdated > &chatBoost)
Called when a chat boost was added or changed.
Definition Bot.hpp:186
virtual void onBusinessConnection(const Ptr< BusinessConnection > &businessConnection)
Called when the bot was connected to or disconnected from a business account, or a user edited an exi...
Definition Bot.hpp:97
virtual void onCallbackQuery(const Ptr< CallbackQuery > &callbackQuery)
Called when a new incoming callback query is received.
Definition Bot.hpp:138
virtual void onChatJoinRequest(const Ptr< ChatJoinRequest > &chatJoinRequest)
Called when a request to join the chat has been sent.
Definition Bot.hpp:182
bool isRunning() const noexcept
Returns true if the Bot long polling is currently running.
Bot(const std::string &token)
Constructs a new Bot object.
virtual void onCommand(const Ptr< Message > &command)
Called when a new command is received (messages with leading '/' char).
Definition Bot.hpp:71
virtual void onUnknownCommand(const Ptr< Message > &message)
Called when an unknown command is received (messages with leading '/' char).
Definition Bot.hpp:76
virtual void onMessageReactionCountUpdated(const Ptr< MessageReactionCountUpdated > &messageReactionCount)
Called when reactions to a message with anonymous reactions were changed.
Definition Bot.hpp:123
virtual void onPoll(const Ptr< Poll > &poll)
Called when a new poll state is received.
Definition Bot.hpp:160
virtual void onChannelPostEdited(const Ptr< Message > &editedChannelPost)
Called when a new version of a channel post that is known to the bot and was edited.
Definition Bot.hpp:90
virtual void onLongPollError(const std::string &errorMessage, ErrorCode errorCode)
Called when the long polling getUpdates fails.
Definition Bot.hpp:197
void stop()
Stop the long polling.
virtual void onChannelPost(const Ptr< Message > &channelPost)
Called when a new incoming channel post of any kind (text, photo, sticker, etc.) is received.
Definition Bot.hpp:86
virtual void onMessageReactionUpdated(const Ptr< MessageReactionUpdated > &messageReaction)
Called when a reaction to a message was changed by a user.
Definition Bot.hpp:117
const Ptr< Api > & api() const noexcept
Returns Api object.
virtual void onBusinessMessage(const Ptr< Message > &businessMessage)
Called when a new message from a connected business account is received.
Definition Bot.hpp:101
virtual void onInlineQuery(const Ptr< InlineQuery > &inlineQuery)
Called when a new incoming inline query is received.
Definition Bot.hpp:129
virtual void onAnyMessage(const Ptr< Message > &message)
Called when a new message is received of any kind - text, photo, sticker, etc.
Definition Bot.hpp:63
virtual void onPreCheckoutQuery(const Ptr< PreCheckoutQuery > &preCheckoutQuery)
Called when a new incoming pre-checkout query is received. Contains full information about checkout.
Definition Bot.hpp:147
virtual void onChosenInlineResult(const Ptr< ChosenInlineResult > &chosenInlineResult)
Called when the result of an inline query that was chosen by a user and sent to their chat partner.
Definition Bot.hpp:134
virtual void onMessageEdited(const Ptr< Message > &editedMessage)
Called when a new version of a message that is known to the bot and was edited.
Definition Bot.hpp:80
virtual void onStop()
Called when Bot is about to be stopped (triggered by Bot::stop()) Cleanup your code in this callback ...
Definition Bot.hpp:57
virtual void onMyChatMemberUpdated(const Ptr< ChatMemberUpdated > &myChatMember)
Called when the bot's chat member status was updated in a chat.
Definition Bot.hpp:172
virtual void onChatMemberUpdated(const Ptr< ChatMemberUpdated > &chatMember)
Called when a chat member's status was updated in a chat.
Definition Bot.hpp:177
virtual void onStart()
Bot Callbacks.
Definition Bot.hpp:53
virtual void onShippingQuery(const Ptr< ShippingQuery > &shippingQuery)
Called when a new incoming shipping query is received.
Definition Bot.hpp:143
virtual void onChatBoostRemoved(const Ptr< ChatBoostRemoved > &removedChatBoost)
Called when a boost was removed from a chat.
Definition Bot.hpp:190
const Ptr< Api > & getApi() const noexcept
Returns Api object.
virtual void onNonCommandMessage(const Ptr< Message > &message)
Called when a non-command message is received of any kind - text, photo, sticker, etc.
Definition Bot.hpp:67
virtual void onPollAnswer(const Ptr< PollAnswer > &pollAnswer)
Called when a user changed their answer in a non-anonymous poll.
Definition Bot.hpp:165
virtual void onPaidMediaPurchased(const Ptr< PaidMediaPurchased > &purchasedPaidMedia)
Called when a user purchased paid media with a non-empty payload sent by the bot in a non-channel cha...
Definition Bot.hpp:153
virtual void onBusinessMessageEdited(const Ptr< Message > &editedBusinessMessage)
Called when a new version of a message from a connected business account is received.
Definition Bot.hpp:105
void start()
Start the long polling.
virtual ~Bot()
virtual void onBusinessMessagesDeleted(const Ptr< BusinessMessagesDeleted > &deletedBusinessMessages)
Called when Messages were deleted from a connected business account.
Definition Bot.hpp:109
std::shared_ptr< T > Ptr
Definition Ptr.hpp:6
ErrorCode
https://core.telegram.org/api/errors
Definition Exception.hpp:8
This object represents an incoming update. At most one of the optional parameters can be present in a...
Definition Update.hpp:27