Skip to content

Commit 1b10d2f

Browse files
authored
Merge pull request #335 from Gorillza/hotfix
Added functionality to handle onSuccessfulPayment through Update with…
2 parents 3bef4dc + 342773b commit 1b10d2f

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

include/tgbot/EventBroadcaster.h

+12-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ friend EventHandler;
4444
typedef std::function<void (const PollAnswer::Ptr)> PollAnswerListener;
4545
typedef std::function<void (const ChatMemberUpdated::Ptr)> ChatMemberUpdatedListener;
4646
typedef std::function<void (const ChatJoinRequest::Ptr)> ChatJoinRequestListener;
47-
typedef std::function<void (const SuccessfulPayment::Ptr)> SuccessfulPaymentListener;
47+
typedef std::function<void (const Message::Ptr, const SuccessfulPayment::Ptr)> SuccessfulPaymentListener;
4848

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

217+
218+
219+
217220
private:
218221
template<typename ListenerType, typename ObjectType>
219222
inline void broadcast(const std::vector<ListenerType>& listeners, const ObjectType object) const {
@@ -290,10 +293,16 @@ friend EventHandler;
290293
broadcast<ChatJoinRequestListener, ChatJoinRequest::Ptr>(_onChatJoinRequestListeners, result);
291294
}
292295

293-
inline void broadcastSuccessfulPayment(const SuccessfulPayment::Ptr& payment) const {
294-
broadcast<SuccessfulPaymentListener, SuccessfulPayment::Ptr>(_onSuccessfulPaymentListeners, payment);
296+
inline void broadcastSuccessfulPayment(const Message::Ptr& message) const {
297+
if (!message || !message->successfulPayment) {
298+
return;
299+
}
300+
for (const auto& listener : _onSuccessfulPaymentListeners) {
301+
listener(message, message->successfulPayment);
302+
}
295303
}
296304

305+
297306
std::vector<MessageListener> _onAnyMessageListeners;
298307
std::unordered_map<std::string, MessageListener> _onCommandListeners;
299308
std::vector<MessageListener> _onUnknownCommandListeners;

include/tgbot/types/Update.h

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "tgbot/types/ChatJoinRequest.h"
1818
#include "tgbot/types/ChatBoostUpdated.h"
1919
#include "tgbot/types/ChatBoostRemoved.h"
20+
#include "tgbot/types/SuccessfulPayment.h"
2021

2122
#include <cstdint>
2223
#include <memory>
@@ -183,6 +184,13 @@ class Update {
183184
* The bot must be an administrator in the chat to receive these updates.
184185
*/
185186
ChatBoostRemoved::Ptr removedChatBoost;
187+
188+
/**
189+
* @brief Optional. A boost was removed from a chat.
190+
*
191+
* The bot must be an administrator in the chat to receive these updates.
192+
*/
193+
SuccessfulPayment::Ptr successfulPayment;
186194
};
187195
}
188196

src/EventHandler.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ void EventHandler::handleMessage(const Message::Ptr& message) const {
7272
} else {
7373
_broadcaster.broadcastNonCommandMessage(message);
7474
}
75-
75+
7676
if (message->successfulPayment != nullptr) {
77-
_broadcaster.broadcastSuccessfulPayment(message->successfulPayment);
77+
_broadcaster.broadcastSuccessfulPayment(message);
7878
}
79+
7980
}
8081

8182
}

src/TgTypeParser.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Update::Ptr TgTypeParser::parseJsonAndGetUpdate(const boost::property_tree::ptre
2727
result->chatJoinRequest = tryParseJson<ChatJoinRequest>(&TgTypeParser::parseJsonAndGetChatJoinRequest, data, "chat_join_request");
2828
result->chatBoost = tryParseJson<ChatBoostUpdated>(&TgTypeParser::parseJsonAndGetChatBoostUpdated, data, "chat_boost");
2929
result->removedChatBoost = tryParseJson<ChatBoostRemoved>(&TgTypeParser::parseJsonAndGetChatBoostRemoved, data, "removed_chat_boost");
30+
result->successfulPayment = tryParseJson<SuccessfulPayment>(&TgTypeParser::parseJsonAndGetSuccessfulPayment, data, "successful_payment");
3031
return result;
3132
}
3233

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

0 commit comments

Comments
 (0)