Skip to content

Commit

Permalink
Added onMessageReactionUpdated, onMessageReactionCountUpdated, onChat…
Browse files Browse the repository at this point in the history
…BoostUpdated and onChatBoostRemoved Bot callbacks
  • Loading branch information
baderouaich committed Mar 11, 2024
1 parent 82313bc commit 431376e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
24 changes: 24 additions & 0 deletions include/tgbotxx/Bot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ namespace tgbotxx {
struct PollAnswer;
struct ChatMemberUpdated;
struct ChatJoinRequest;
struct MessageReactionUpdated;
struct MessageReactionCountUpdated;
struct ChatBoostUpdated;
struct ChatBoostRemoved;

class Bot {
private:
Expand Down Expand Up @@ -69,6 +73,18 @@ namespace tgbotxx {
/// @param editedMessage Edited Message object
virtual void onEditedMessage(const Ptr<Message>& editedMessage) {}

/// @brief Called when a reaction to a message was changed by a user.
/// @note The bot must be an administrator in the chat and must explicitly specify "message_reaction" in the list of allowed_updates to receive these updates using Api::setAllowedUpdates().
/// The update isn't received for reactions set by bots.
/// @param messageReaction MessageReactionUpdated object
virtual void onMessageReactionUpdated(const Ptr<MessageReactionUpdated>& messageReaction) {}

/// @brief Called when reactions to a message with anonymous reactions were changed.
/// @note The bot must be an administrator in the chat and must explicitly specify "message_reaction_count" in the list of allowed_updates to receive these updates using Api::setAllowedUpdates().
/// The updates are grouped and can be sent with delay up to a few minutes.
/// @param messageReactionCount MessageReactionCountUpdated object
virtual void onMessageReactionCountUpdated(const Ptr<MessageReactionCountUpdated>& messageReactionCount) {}

/// @brief Called when a new incoming inline query is received
/// @param inlineQuery InlineQuery object
virtual void onInlineQuery(const Ptr<InlineQuery>& inlineQuery) {}
Expand Down Expand Up @@ -116,6 +132,14 @@ namespace tgbotxx {
/// @param chatJoinRequest ChatJoinRequest object
virtual void onChatJoinRequest(const Ptr<ChatJoinRequest>& chatJoinRequest) {}

/// @brief Called when a chat boost was added or changed.
/// @param chatBoostUpdated ChatBoostUpdated object
virtual void onChatBoostUpdated(const Ptr<ChatBoostUpdated>& chatBoostUpdated) {}

/// @brief Called when a boost was removed from a chat.
/// @param chatBoostRemoved ChatBoostRemoved object
virtual void onChatBoostRemoved(const Ptr<ChatBoostRemoved>& chatBoostRemoved) {}

/// @brief Called when the long polling getUpdates fails.
/// @param reason the reason of failure
virtual void onLongPollError(const std::string& reason) {}
Expand Down
34 changes: 33 additions & 1 deletion src/Bot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
#include <tgbotxx/objects/Message.hpp>
#include <tgbotxx/objects/Update.hpp>
#include <tgbotxx/utils/StringUtils.hpp>
#include <tgbotxx/objects/ChosenInlineResult.hpp>
#include <tgbotxx/objects/InlineQuery.hpp>
#include <tgbotxx/objects/CallbackQuery.hpp>
#include <tgbotxx/objects/ShippingQuery.hpp>
#include <tgbotxx/objects/PreCheckoutQuery.hpp>
#include <tgbotxx/objects/Poll.hpp>
#include <tgbotxx/objects/PollAnswer.hpp>
#include <tgbotxx/objects/ChatMemberUpdated.hpp>
#include <tgbotxx/objects/ChatJoinRequest.hpp>
#include <tgbotxx/objects/MessageReactionUpdated.hpp>
#include <tgbotxx/objects/MessageReactionCountUpdated.hpp>
#include <tgbotxx/objects/ChatBoostUpdated.hpp>
#include <tgbotxx/objects/ChatBoostRemoved.hpp>
using namespace tgbotxx;

Bot::Bot(const std::string& token)
Expand Down Expand Up @@ -82,6 +95,14 @@ void Bot::dispatch(const Ptr<Update>& update) {
/// Callback -> onInlineQuery
this->onInlineQuery(update->inlineQuery);
}
if (update->messageReaction) {
/// Callback -> onMessageReactionUpdated
this->onMessageReactionUpdated(update->messageReaction);
}
if (update->messageReactionCount) {
/// Callback -> onMessageReactionCountUpdated
this->onMessageReactionCountUpdated(update->messageReactionCount);
}
if (update->chosenInlineResult) {
/// Callback -> onChosenInlineResult
this->onChosenInlineResult(update->chosenInlineResult);
Expand Down Expand Up @@ -118,6 +139,14 @@ void Bot::dispatch(const Ptr<Update>& update) {
/// Callback -> onChatJoinRequest
this->onChatJoinRequest(update->chatJoinRequest);
}
if (update->chatBoost) {
/// Callback -> onChatBoostUpdated
this->onChatBoostUpdated(update->chatBoost);
}
if (update->removedChatBoost) {
/// Callback -> onChatBoostRemoved
this->onChatBoostRemoved(update->removedChatBoost);
}
}

const Ptr<Api>& Bot::getApi() const noexcept { return m_api; }
Expand All @@ -127,11 +156,14 @@ void Bot::dispatchMessage(const Ptr<Message>& message) {
/// Callback -> onAnyMessage
this->onAnyMessage(message);

/// Is this message a Command ? (starts with '/' character)
// Is this message a Command? (starts with '/' character)
if (not message->text.empty() and message->text[0] == '/') {
std::size_t firstSpacePos = message->text.find_first_of(" \t\n\r");
if (firstSpacePos == std::string::npos)
firstSpacePos = message->text.size();
/// TODO:
/// - When a user sends a /command inside a Group chat, to distinguish the sender Bot which can be many with same command names the command is sent in format /command@botname
/// - Cache myCommands to avoid multiple api calls
std::string command = message->text.substr(1, firstSpacePos - 1);
std::vector<Ptr<BotCommand>> myCommands = m_api->getMyCommands();
bool isKnownCommand = std::any_of(myCommands.begin(), myCommands.end(), [&command](const Ptr<BotCommand>& cmd) noexcept {
Expand Down

0 comments on commit 431376e

Please sign in to comment.