Skip to content

Commit

Permalink
Merge pull request #335 from Gorillza/hotfix
Browse files Browse the repository at this point in the history
Added functionality to handle onSuccessfulPayment through Update with…
  • Loading branch information
reo7sp authored Feb 2, 2025
2 parents 3bef4dc + 342773b commit 1b10d2f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
15 changes: 12 additions & 3 deletions include/tgbot/EventBroadcaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ friend EventHandler;
typedef std::function<void (const PollAnswer::Ptr)> PollAnswerListener;
typedef std::function<void (const ChatMemberUpdated::Ptr)> ChatMemberUpdatedListener;
typedef std::function<void (const ChatJoinRequest::Ptr)> ChatJoinRequestListener;
typedef std::function<void (const SuccessfulPayment::Ptr)> SuccessfulPaymentListener;
typedef std::function<void (const Message::Ptr, const SuccessfulPayment::Ptr)> SuccessfulPaymentListener;

/**
* @brief Registers listener which receives new incoming message of any kind - text, photo, sticker, etc.
Expand Down Expand Up @@ -214,6 +214,9 @@ friend EventHandler;
_onSuccessfulPaymentListeners.push_back(listener);
}




private:
template<typename ListenerType, typename ObjectType>
inline void broadcast(const std::vector<ListenerType>& listeners, const ObjectType object) const {
Expand Down Expand Up @@ -290,10 +293,16 @@ friend EventHandler;
broadcast<ChatJoinRequestListener, ChatJoinRequest::Ptr>(_onChatJoinRequestListeners, result);
}

inline void broadcastSuccessfulPayment(const SuccessfulPayment::Ptr& payment) const {
broadcast<SuccessfulPaymentListener, SuccessfulPayment::Ptr>(_onSuccessfulPaymentListeners, payment);
inline void broadcastSuccessfulPayment(const Message::Ptr& message) const {
if (!message || !message->successfulPayment) {
return;
}
for (const auto& listener : _onSuccessfulPaymentListeners) {
listener(message, message->successfulPayment);
}
}


std::vector<MessageListener> _onAnyMessageListeners;
std::unordered_map<std::string, MessageListener> _onCommandListeners;
std::vector<MessageListener> _onUnknownCommandListeners;
Expand Down
8 changes: 8 additions & 0 deletions include/tgbot/types/Update.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "tgbot/types/ChatJoinRequest.h"
#include "tgbot/types/ChatBoostUpdated.h"
#include "tgbot/types/ChatBoostRemoved.h"
#include "tgbot/types/SuccessfulPayment.h"

#include <cstdint>
#include <memory>
Expand Down Expand Up @@ -183,6 +184,13 @@ class Update {
* The bot must be an administrator in the chat to receive these updates.
*/
ChatBoostRemoved::Ptr removedChatBoost;

/**
* @brief Optional. A boost was removed from a chat.
*
* The bot must be an administrator in the chat to receive these updates.
*/
SuccessfulPayment::Ptr successfulPayment;
};
}

Expand Down
5 changes: 3 additions & 2 deletions src/EventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ void EventHandler::handleMessage(const Message::Ptr& message) const {
} else {
_broadcaster.broadcastNonCommandMessage(message);
}

if (message->successfulPayment != nullptr) {
_broadcaster.broadcastSuccessfulPayment(message->successfulPayment);
_broadcaster.broadcastSuccessfulPayment(message);
}

}

}
2 changes: 2 additions & 0 deletions src/TgTypeParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Update::Ptr TgTypeParser::parseJsonAndGetUpdate(const boost::property_tree::ptre
result->chatJoinRequest = tryParseJson<ChatJoinRequest>(&TgTypeParser::parseJsonAndGetChatJoinRequest, data, "chat_join_request");
result->chatBoost = tryParseJson<ChatBoostUpdated>(&TgTypeParser::parseJsonAndGetChatBoostUpdated, data, "chat_boost");
result->removedChatBoost = tryParseJson<ChatBoostRemoved>(&TgTypeParser::parseJsonAndGetChatBoostRemoved, data, "removed_chat_boost");
result->successfulPayment = tryParseJson<SuccessfulPayment>(&TgTypeParser::parseJsonAndGetSuccessfulPayment, data, "successful_payment");
return result;
}

Expand Down Expand Up @@ -59,6 +60,7 @@ std::string TgTypeParser::parseUpdate(const Update::Ptr& object) const {
appendToJson(result, "chat_join_request", parseChatJoinRequest(object->chatJoinRequest));
appendToJson(result, "chat_boost", parseChatBoostUpdated(object->chatBoost));
appendToJson(result, "removed_chat_boost", parseChatBoostRemoved(object->removedChatBoost));
appendToJson(result, "successful_payment", parseSuccessfulPayment(object->successfulPayment));
removeLastComma(result);
result += '}';
return result;
Expand Down

0 comments on commit 1b10d2f

Please sign in to comment.