Skip to content

Commit 62c0d51

Browse files
authored
Merge pull request #10 from Royna2544/copilot/update-telegram-bot-api-again
Add event handlers for message reactions, business accounts, and chat boosts
2 parents 75376be + 5629047 commit 62c0d51

File tree

3 files changed

+191
-4
lines changed

3 files changed

+191
-4
lines changed

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,28 @@ Documentation is located [here](http://reo7sp.github.io/tgbot-cpp).
99

1010
## State
1111

12-
- [x] Telegram Bot API 7.2
13-
- [ ] [MaybeInaccessibleMessage](https://core.telegram.org/bots/api#maybeinaccessiblemessage)
14-
- [ ] [Message->pinnedMessage](https://core.telegram.org/bots/api#message)
15-
- [ ] [CallbackQuery->message](https://core.telegram.org/bots/api#callbackquery)
12+
This library implements most features from **Telegram Bot API 7.2** with additional features from later versions.
13+
14+
### Recently Added Features:
15+
- [x] Message Reactions (Bot API 7.x+)
16+
- MessageReactionUpdated event handling
17+
- MessageReactionCountUpdated event handling
18+
- [x] Business Account Integration (Bot API 7.x+)
19+
- BusinessConnection updates
20+
- Business message handling
21+
- Deleted business messages tracking
22+
- [x] Chat Boosts (Bot API 7.x+)
23+
- ChatBoostUpdated events
24+
- ChatBoostRemoved events
25+
26+
### Known Limitations:
27+
- [ ] [MaybeInaccessibleMessage](https://core.telegram.org/bots/api#maybeinaccessiblemessage) - Planned for future implementation
28+
- [ ] [Message->pinnedMessage](https://core.telegram.org/bots/api#message) - Requires MaybeInaccessibleMessage
29+
- [ ] [CallbackQuery->message](https://core.telegram.org/bots/api#callbackquery) - Requires MaybeInaccessibleMessage
1630
- [ ] [Deep Linking](https://core.telegram.org/bots/features#deep-linking)
1731

32+
**Note:** Full Bot API 8.x and 9.x support is planned. Contributions are welcome!
33+
1834

1935
## Sample
2036

include/tgbot/EventBroadcaster.h

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
#include "tgbot/types/PollAnswer.h"
1313
#include "tgbot/types/ChatMemberUpdated.h"
1414
#include "tgbot/types/ChatJoinRequest.h"
15+
#include "tgbot/types/MessageReactionUpdated.h"
16+
#include "tgbot/types/MessageReactionCountUpdated.h"
17+
#include "tgbot/types/BusinessConnection.h"
18+
#include "tgbot/types/BusinessMessagesDeleted.h"
19+
#include "tgbot/types/ChatBoostUpdated.h"
20+
#include "tgbot/types/ChatBoostRemoved.h"
1521

1622
#include <functional>
1723
#include <initializer_list>
@@ -43,6 +49,12 @@ friend EventHandler;
4349
typedef std::function<void (const PollAnswer::Ptr)> PollAnswerListener;
4450
typedef std::function<void (const ChatMemberUpdated::Ptr)> ChatMemberUpdatedListener;
4551
typedef std::function<void (const ChatJoinRequest::Ptr)> ChatJoinRequestListener;
52+
typedef std::function<void (const MessageReactionUpdated::Ptr)> MessageReactionUpdatedListener;
53+
typedef std::function<void (const MessageReactionCountUpdated::Ptr)> MessageReactionCountUpdatedListener;
54+
typedef std::function<void (const BusinessConnection::Ptr)> BusinessConnectionListener;
55+
typedef std::function<void (const BusinessMessagesDeleted::Ptr)> BusinessMessagesDeletedListener;
56+
typedef std::function<void (const ChatBoostUpdated::Ptr)> ChatBoostUpdatedListener;
57+
typedef std::function<void (const ChatBoostRemoved::Ptr)> ChatBoostRemovedListener;
4658

4759
/**
4860
* @brief Registers listener which receives new incoming message of any kind - text, photo, sticker, etc.
@@ -202,6 +214,101 @@ friend EventHandler;
202214
_onChatJoinRequestListeners.push_back(listener);
203215
}
204216

217+
/**
218+
* @brief Registers listener which receives message reaction updates.
219+
*
220+
* The bot must be an administrator in the chat and must explicitly specify
221+
* "message_reaction" in the list of allowedUpdates to receive these updates.
222+
* The update isn't received for reactions set by bots.
223+
*
224+
* @param listener Listener.
225+
*/
226+
inline void onMessageReaction(const MessageReactionUpdatedListener& listener){
227+
_onMessageReactionUpdatedListeners.push_back(listener);
228+
}
229+
230+
/**
231+
* @brief Registers listener which receives message reaction count updates.
232+
*
233+
* The bot must be an administrator in the chat and must explicitly specify
234+
* "message_reaction_count" in the list of allowedUpdates to receive these updates.
235+
* The updates are grouped and can be sent with delay up to a few minutes.
236+
*
237+
* @param listener Listener.
238+
*/
239+
inline void onMessageReactionCount(const MessageReactionCountUpdatedListener& listener){
240+
_onMessageReactionCountUpdatedListeners.push_back(listener);
241+
}
242+
243+
/**
244+
* @brief Registers listener which receives business connection updates.
245+
*
246+
* The bot was connected to or disconnected from a business account,
247+
* or a user edited an existing connection with the bot.
248+
*
249+
* @param listener Listener.
250+
*/
251+
inline void onBusinessConnection(const BusinessConnectionListener& listener){
252+
_onBusinessConnectionListeners.push_back(listener);
253+
}
254+
255+
/**
256+
* @brief Registers listener which receives business message updates.
257+
*
258+
* New non-service message from a connected business account.
259+
*
260+
* @param listener Listener.
261+
*/
262+
inline void onBusinessMessage(const MessageListener& listener){
263+
_onBusinessMessageListeners.push_back(listener);
264+
}
265+
266+
/**
267+
* @brief Registers listener which receives edited business message updates.
268+
*
269+
* New version of a message from a connected business account.
270+
*
271+
* @param listener Listener.
272+
*/
273+
inline void onEditedBusinessMessage(const MessageListener& listener){
274+
_onEditedBusinessMessageListeners.push_back(listener);
275+
}
276+
277+
/**
278+
* @brief Registers listener which receives business messages deleted updates.
279+
*
280+
* Messages were deleted from a connected business account.
281+
*
282+
* @param listener Listener.
283+
*/
284+
inline void onDeletedBusinessMessages(const BusinessMessagesDeletedListener& listener){
285+
_onDeletedBusinessMessagesListeners.push_back(listener);
286+
}
287+
288+
/**
289+
* @brief Registers listener which receives chat boost updates.
290+
*
291+
* A chat boost was added or changed. The bot must be an administrator
292+
* in the chat to receive these updates.
293+
*
294+
* @param listener Listener.
295+
*/
296+
inline void onChatBoost(const ChatBoostUpdatedListener& listener){
297+
_onChatBoostUpdatedListeners.push_back(listener);
298+
}
299+
300+
/**
301+
* @brief Registers listener which receives removed chat boost updates.
302+
*
303+
* A boost was removed from a chat. The bot must be an administrator
304+
* in the chat to receive these updates.
305+
*
306+
* @param listener Listener.
307+
*/
308+
inline void onRemovedChatBoost(const ChatBoostRemovedListener& listener){
309+
_onRemovedChatBoostListeners.push_back(listener);
310+
}
311+
205312
private:
206313
template<typename ListenerType, typename ObjectType>
207314
inline void broadcast(const std::vector<ListenerType>& listeners, const ObjectType object) const {
@@ -278,6 +385,38 @@ friend EventHandler;
278385
broadcast<ChatJoinRequestListener, ChatJoinRequest::Ptr>(_onChatJoinRequestListeners, result);
279386
}
280387

388+
inline void broadcastMessageReactionUpdated(const MessageReactionUpdated::Ptr& result) const {
389+
broadcast<MessageReactionUpdatedListener, MessageReactionUpdated::Ptr>(_onMessageReactionUpdatedListeners, result);
390+
}
391+
392+
inline void broadcastMessageReactionCountUpdated(const MessageReactionCountUpdated::Ptr& result) const {
393+
broadcast<MessageReactionCountUpdatedListener, MessageReactionCountUpdated::Ptr>(_onMessageReactionCountUpdatedListeners, result);
394+
}
395+
396+
inline void broadcastBusinessConnection(const BusinessConnection::Ptr& result) const {
397+
broadcast<BusinessConnectionListener, BusinessConnection::Ptr>(_onBusinessConnectionListeners, result);
398+
}
399+
400+
inline void broadcastBusinessMessage(const Message::Ptr& message) const {
401+
broadcast<MessageListener, Message::Ptr>(_onBusinessMessageListeners, message);
402+
}
403+
404+
inline void broadcastEditedBusinessMessage(const Message::Ptr& message) const {
405+
broadcast<MessageListener, Message::Ptr>(_onEditedBusinessMessageListeners, message);
406+
}
407+
408+
inline void broadcastDeletedBusinessMessages(const BusinessMessagesDeleted::Ptr& result) const {
409+
broadcast<BusinessMessagesDeletedListener, BusinessMessagesDeleted::Ptr>(_onDeletedBusinessMessagesListeners, result);
410+
}
411+
412+
inline void broadcastChatBoostUpdated(const ChatBoostUpdated::Ptr& result) const {
413+
broadcast<ChatBoostUpdatedListener, ChatBoostUpdated::Ptr>(_onChatBoostUpdatedListeners, result);
414+
}
415+
416+
inline void broadcastRemovedChatBoost(const ChatBoostRemoved::Ptr& result) const {
417+
broadcast<ChatBoostRemovedListener, ChatBoostRemoved::Ptr>(_onRemovedChatBoostListeners, result);
418+
}
419+
281420
std::vector<MessageListener> _onAnyMessageListeners;
282421
std::unordered_map<std::string, MessageListener> _onCommandListeners;
283422
std::vector<MessageListener> _onUnknownCommandListeners;
@@ -293,6 +432,14 @@ friend EventHandler;
293432
std::vector<ChatMemberUpdatedListener> _onMyChatMemberListeners;
294433
std::vector<ChatMemberUpdatedListener> _onChatMemberListeners;
295434
std::vector<ChatJoinRequestListener> _onChatJoinRequestListeners;
435+
std::vector<MessageReactionUpdatedListener> _onMessageReactionUpdatedListeners;
436+
std::vector<MessageReactionCountUpdatedListener> _onMessageReactionCountUpdatedListeners;
437+
std::vector<BusinessConnectionListener> _onBusinessConnectionListeners;
438+
std::vector<MessageListener> _onBusinessMessageListeners;
439+
std::vector<MessageListener> _onEditedBusinessMessageListeners;
440+
std::vector<BusinessMessagesDeletedListener> _onDeletedBusinessMessagesListeners;
441+
std::vector<ChatBoostUpdatedListener> _onChatBoostUpdatedListeners;
442+
std::vector<ChatBoostRemovedListener> _onRemovedChatBoostListeners;
296443
};
297444

298445
}

src/EventHandler.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ void EventHandler::handleUpdate(const Update::Ptr& update) const {
1616
if (update->editedChannelPost != nullptr) {
1717
_broadcaster->broadcastEditedMessage(update->editedChannelPost);
1818
}
19+
if (update->businessConnection != nullptr) {
20+
_broadcaster->broadcastBusinessConnection(update->businessConnection);
21+
}
22+
if (update->businessMessage != nullptr) {
23+
_broadcaster->broadcastBusinessMessage(update->businessMessage);
24+
}
25+
if (update->editedBusinessMessage != nullptr) {
26+
_broadcaster->broadcastEditedBusinessMessage(update->editedBusinessMessage);
27+
}
28+
if (update->deletedBusinessMessages != nullptr) {
29+
_broadcaster->broadcastDeletedBusinessMessages(update->deletedBusinessMessages);
30+
}
31+
if (update->messageReaction != nullptr) {
32+
_broadcaster->broadcastMessageReactionUpdated(update->messageReaction);
33+
}
34+
if (update->messageReactionCount != nullptr) {
35+
_broadcaster->broadcastMessageReactionCountUpdated(update->messageReactionCount);
36+
}
1937
if (update->inlineQuery != nullptr) {
2038
_broadcaster->broadcastInlineQuery(update->inlineQuery);
2139
}
@@ -46,6 +64,12 @@ void EventHandler::handleUpdate(const Update::Ptr& update) const {
4664
if (update->chatJoinRequest != nullptr) {
4765
_broadcaster->broadcastChatJoinRequest(update->chatJoinRequest);
4866
}
67+
if (update->chatBoost != nullptr) {
68+
_broadcaster->broadcastChatBoostUpdated(update->chatBoost);
69+
}
70+
if (update->removedChatBoost != nullptr) {
71+
_broadcaster->broadcastRemovedChatBoost(update->removedChatBoost);
72+
}
4973
}
5074

5175
void EventHandler::handleMessage(const Message::Ptr& message) const {

0 commit comments

Comments
 (0)