tgbotxx  1.1.6.9
Telegram Bot C++ Library
Bot.hpp
Go to the documentation of this file.
1 #pragma once
2 #include "Exception.hpp"
3 #include <cstdint>
4 #include <string>
5 #include <tgbotxx/utils/Ptr.hpp>
6 #include <vector>
7 
8 namespace 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 
27  class Bot {
28  private:
29  Ptr<Api> m_api{};
30  std::vector<Ptr<Update>> m_updates{};
31  std::int32_t m_lastUpdateId{};
32  std::atomic<bool> m_running{};
33 
34  public:
37  explicit Bot(const std::string& token);
38  ~Bot();
39 
42  void start();
43 
45  void stop();
46 
47  public:
50  virtual void onStart() {}
51 
54  virtual void onStop() {}
55 
58  virtual void onAnyMessage(const Ptr<Message>& message) {}
59 
62  virtual void onNonCommandMessage(const Ptr<Message>& message) {}
63 
66  virtual void onCommand(const Ptr<Message>& command) {}
67 
71  virtual void onUnknownCommand(const Ptr<Message>& message) {}
72 
75  virtual void onEditedMessage(const Ptr<Message>& editedMessage) {}
76 
81  virtual void onMessageReactionUpdated(const Ptr<MessageReactionUpdated>& messageReaction) {}
82 
87  virtual void onMessageReactionCountUpdated(const Ptr<MessageReactionCountUpdated>& messageReactionCount) {}
88 
91  virtual void onInlineQuery(const Ptr<InlineQuery>& inlineQuery) {}
92 
96  virtual void onChosenInlineResult(const Ptr<ChosenInlineResult>& chosenInlineResult) {}
97 
100  virtual void onCallbackQuery(const Ptr<CallbackQuery>& callbackQuery) {}
101 
105  virtual void onShippingQuery(const Ptr<ShippingQuery>& shippingQuery) {}
106 
109  virtual void onPreCheckoutQuery(const Ptr<PreCheckoutQuery>& preCheckoutQuery) {}
110 
114  virtual void onPoll(const Ptr<Poll>& poll) {}
115 
119  virtual void onPollAnswer(const Ptr<PollAnswer>& pollAnswer) {}
120 
124  virtual void onMyChatMember(const Ptr<ChatMemberUpdated>& myChatMemberUpdated) {}
125 
129  virtual void onChatMember(const Ptr<ChatMemberUpdated>& chatMemberUpdated) {}
130 
134  virtual void onChatJoinRequest(const Ptr<ChatJoinRequest>& chatJoinRequest) {}
135 
138  virtual void onChatBoostUpdated(const Ptr<ChatBoostUpdated>& chatBoostUpdated) {}
139 
142  virtual void onChatBoostRemoved(const Ptr<ChatBoostRemoved>& chatBoostRemoved) {}
143 
147  virtual void onLongPollError(const std::string& errorMessage, ErrorCode errorCode) {}
148 
149  protected:
151  [[nodiscard]] const Ptr<Api>& getApi() const noexcept;
153  [[nodiscard]] const Ptr<Api>& api() const noexcept;
154 
156  [[nodiscard]] bool isRunning() const noexcept;
157 
158  private:
161  void dispatch(const Ptr<Update>& update);
162 
166  void dispatchMessage(const Ptr<Message>& message);
167  };
168 }
Api Methods https://core.telegram.org/bots/api#available-methods.
Definition: Api.hpp:56
virtual void onCallbackQuery(const Ptr< CallbackQuery > &callbackQuery)
Called when a new incoming callback query is received.
Definition: Bot.hpp:100
virtual void onChatJoinRequest(const Ptr< ChatJoinRequest > &chatJoinRequest)
Called when a request to join the chat has been sent.
Definition: Bot.hpp:134
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 onChatBoostRemoved(const Ptr< ChatBoostRemoved > &chatBoostRemoved)
Called when a boost was removed from a chat.
Definition: Bot.hpp:142
virtual void onCommand(const Ptr< Message > &command)
Called when a new command is received (messages with leading '/' char).
Definition: Bot.hpp:66
const Ptr< Api > & api() const noexcept
Returns Api object.
virtual void onUnknownCommand(const Ptr< Message > &message)
Called when an unknown command is received (messages with leading '/' char).
Definition: Bot.hpp:71
virtual void onMessageReactionCountUpdated(const Ptr< MessageReactionCountUpdated > &messageReactionCount)
Called when reactions to a message with anonymous reactions were changed.
Definition: Bot.hpp:87
virtual void onPoll(const Ptr< Poll > &poll)
Called when a new poll state is received.
Definition: Bot.hpp:114
virtual void onLongPollError(const std::string &errorMessage, ErrorCode errorCode)
Called when the long polling getUpdates fails.
Definition: Bot.hpp:147
void stop()
Stop the long polling.
virtual void onChatMember(const Ptr< ChatMemberUpdated > &chatMemberUpdated)
Called when a chat member's status was updated in a chat.
Definition: Bot.hpp:129
virtual void onMessageReactionUpdated(const Ptr< MessageReactionUpdated > &messageReaction)
Called when a reaction to a message was changed by a user.
Definition: Bot.hpp:81
virtual void onMyChatMember(const Ptr< ChatMemberUpdated > &myChatMemberUpdated)
Called when the bot's chat member status was updated in a chat.
Definition: Bot.hpp:124
virtual void onInlineQuery(const Ptr< InlineQuery > &inlineQuery)
Called when a new incoming inline query is received.
Definition: Bot.hpp:91
virtual void onAnyMessage(const Ptr< Message > &message)
Called when a new message is received of any kind - text, photo, sticker, etc.
Definition: Bot.hpp:58
const Ptr< Api > & getApi() const noexcept
Returns Api object.
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:109
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:96
virtual void onStop()
Called when Bot is about to be stopped (triggered by Bot::stop()) Cleanup your code in this callback ...
Definition: Bot.hpp:54
virtual void onChatBoostUpdated(const Ptr< ChatBoostUpdated > &chatBoostUpdated)
Called when a chat boost was added or changed.
Definition: Bot.hpp:138
virtual void onStart()
Called before Bot starts receiving updates (triggered by Bot::start()) Use this callback to initializ...
Definition: Bot.hpp:50
virtual void onShippingQuery(const Ptr< ShippingQuery > &shippingQuery)
Called when a new incoming shipping query is received.
Definition: Bot.hpp:105
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:62
virtual void onPollAnswer(const Ptr< PollAnswer > &pollAnswer)
Called when a user changed their answer in a non-anonymous poll.
Definition: Bot.hpp:119
void start()
Start the long polling.
virtual void onEditedMessage(const Ptr< Message > &editedMessage)
Called when a new version of a message that is known to the bot and was edited.
Definition: Bot.hpp:75
Definition: Api.hpp:14
std::shared_ptr< T > Ptr
Definition: Ptr.hpp:6
ErrorCode
https://core.telegram.org/api/errors
Definition: Exception.hpp:8
This object represents a message. https://core.telegram.org/bots/api#message.
Definition: Message.hpp:48
This object represents an incoming update. At most one of the optional parameters can be present in a...
Definition: Update.hpp:22