tgbotxx 1.1.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 private:
32 Ptr<Api> m_api{};
33 std::vector<Ptr<Update>> m_updates{};
34 std::int32_t m_lastUpdateId{};
35 std::atomic<bool> m_running{};
36
37 public:
40 explicit Bot(const std::string& token);
41 virtual ~Bot();
42
45 void start();
46
48 void stop();
49
50 public:
51#pragma region Lifecycle
54 virtual void onStart() {}
55
58 virtual void onStop() {}
59#pragma endregion Lifecycle
60
61#pragma region Messages
64 virtual void onAnyMessage(const Ptr<Message>& message) {}
65
68 virtual void onNonCommandMessage(const Ptr<Message>& message) {}
69
72 virtual void onCommand(const Ptr<Message>& command) {}
73
77 virtual void onUnknownCommand(const Ptr<Message>& message) {}
78
81 virtual void onMessageEdited(const Ptr<Message>& editedMessage) {}
82#pragma endregion Messages
83
84#pragma region Channel
87 virtual void onChannelPost(const Ptr<Message>& channelPost) {}
88
91 virtual void onChannelPostEdited(const Ptr<Message>& editedChannelPost) {}
92#pragma endregion Channel
93
94#pragma region Business
98 virtual void onBusinessConnection(const Ptr<BusinessConnection>& businessConnection) {}
99
102 virtual void onBusinessMessage(const Ptr<Message>& businessMessage) {}
103
106 virtual void onBusinessMessageEdited(const Ptr<Message>& editedBusinessMessage) {}
107
110 virtual void onBusinessMessagesDeleted(const Ptr<BusinessMessagesDeleted>& deletedBusinessMessages) {}
111#pragma endregion Business
112
113#pragma region MessageReaction
118 virtual void onMessageReactionUpdated(const Ptr<MessageReactionUpdated>& messageReaction) {}
119
124 virtual void onMessageReactionCountUpdated(const Ptr<MessageReactionCountUpdated>& messageReactionCount) {}
125#pragma endregion MessageReaction
126
127#pragma region InlineMode
130 virtual void onInlineQuery(const Ptr<InlineQuery>& inlineQuery) {}
131
135 virtual void onChosenInlineResult(const Ptr<ChosenInlineResult>& chosenInlineResult) {}
136
139 virtual void onCallbackQuery(const Ptr<CallbackQuery>& callbackQuery) {}
140
144 virtual void onShippingQuery(const Ptr<ShippingQuery>& shippingQuery) {}
145
148 virtual void onPreCheckoutQuery(const Ptr<PreCheckoutQuery>& preCheckoutQuery) {}
149#pragma endregion InlineMode
150
151#pragma region Payments
154 virtual void onPaidMediaPurchased(const Ptr<PaidMediaPurchased>& purchasedPaidMedia) {}
155#pragma endregion Payments
156
157#pragma region Poll
161 virtual void onPoll(const Ptr<Poll>& poll) {}
162
166 virtual void onPollAnswer(const Ptr<PollAnswer>& pollAnswer) {}
167#pragma endregion Poll
168
169#pragma region Chat
173 virtual void onMyChatMemberUpdated(const Ptr<ChatMemberUpdated>& myChatMember) {}
174
178 virtual void onChatMemberUpdated(const Ptr<ChatMemberUpdated>& chatMember) {}
179
183 virtual void onChatJoinRequest(const Ptr<ChatJoinRequest>& chatJoinRequest) {}
184
187 virtual void onChatBoostUpdated(const Ptr<ChatBoostUpdated>& chatBoost) {}
188
191 virtual void onChatBoostRemoved(const Ptr<ChatBoostRemoved>& removedChatBoost) {}
192#pragma endregion Chat
193
194#pragma region Errors
198 virtual void onLongPollError(const std::string& errorMessage, ErrorCode errorCode) {}
199#pragma endregion Errors
200
201 protected:
203 [[nodiscard]] const Ptr<Api>& getApi() const noexcept;
205 [[nodiscard]] const Ptr<Api>& api() const noexcept;
206
208 [[nodiscard]] bool isRunning() const noexcept;
209
210 private: // Dispatchers
213 void dispatch(const Ptr<Update>& update);
214
218 void dispatchMessages(const Ptr<Update>& update);
219
222 void dispatchChannel(const Ptr<Update>& update);
223
226 void dispatchBusiness(const Ptr<Update>& update);
227
230 void dispatchMessageReaction(const Ptr<Update>& update);
231
234 void dispatchInlineMode(const Ptr<Update>& update);
235
238 void dispatchPayments(const Ptr<Update>& update);
239
242 void dispatchPoll(const Ptr<Update>& update);
243
246 void dispatchChat(const Ptr<Update>& update);
247 };
248}
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:187
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:98
virtual void onCallbackQuery(const Ptr< CallbackQuery > &callbackQuery)
Called when a new incoming callback query is received.
Definition Bot.hpp:139
virtual void onChatJoinRequest(const Ptr< ChatJoinRequest > &chatJoinRequest)
Called when a request to join the chat has been sent.
Definition Bot.hpp:183
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:72
virtual void onUnknownCommand(const Ptr< Message > &message)
Called when an unknown command is received (messages with leading '/' char).
Definition Bot.hpp:77
virtual void onMessageReactionCountUpdated(const Ptr< MessageReactionCountUpdated > &messageReactionCount)
Called when reactions to a message with anonymous reactions were changed.
Definition Bot.hpp:124
virtual void onPoll(const Ptr< Poll > &poll)
Called when a new poll state is received.
Definition Bot.hpp:161
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:91
virtual void onLongPollError(const std::string &errorMessage, ErrorCode errorCode)
Called when the long polling getUpdates fails.
Definition Bot.hpp:198
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:87
virtual void onMessageReactionUpdated(const Ptr< MessageReactionUpdated > &messageReaction)
Called when a reaction to a message was changed by a user.
Definition Bot.hpp:118
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:102
virtual void onInlineQuery(const Ptr< InlineQuery > &inlineQuery)
Called when a new incoming inline query is received.
Definition Bot.hpp:130
virtual void onAnyMessage(const Ptr< Message > &message)
Called when a new message is received of any kind - text, photo, sticker, etc.
Definition Bot.hpp:64
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:148
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:135
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:81
virtual void onStop()
Called when Bot is about to be stopped (triggered by Bot::stop()) Cleanup your code in this callback ...
Definition Bot.hpp:58
virtual void onMyChatMemberUpdated(const Ptr< ChatMemberUpdated > &myChatMember)
Called when the bot's chat member status was updated in a chat.
Definition Bot.hpp:173
virtual void onChatMemberUpdated(const Ptr< ChatMemberUpdated > &chatMember)
Called when a chat member's status was updated in a chat.
Definition Bot.hpp:178
virtual void onStart()
Bot Callbacks.
Definition Bot.hpp:54
virtual void onShippingQuery(const Ptr< ShippingQuery > &shippingQuery)
Called when a new incoming shipping query is received.
Definition Bot.hpp:144
virtual void onChatBoostRemoved(const Ptr< ChatBoostRemoved > &removedChatBoost)
Called when a boost was removed from a chat.
Definition Bot.hpp:191
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:68
virtual void onPollAnswer(const Ptr< PollAnswer > &pollAnswer)
Called when a user changed their answer in a non-anonymous poll.
Definition Bot.hpp:166
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:154
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:106
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:110
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