Skip to content

Commit bf4446c

Browse files
authored
Merge pull request #2219 from artyl/master
Example bot using message reactions (emoji)
2 parents ecf83f0 + e8074eb commit bf4446c

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/python3
2+
3+
# This is a simple bot using message reactions (emoji)
4+
# https://core.telegram.org/bots/api#reactiontype
5+
# https://core.telegram.org/bots/api#update
6+
# allowed_updates: Specify an empty list to receive all update types except, chat_member, message_reaction, and message_reaction_count.
7+
# If you want to receive message_reaction events, you cannot simply leave the allowed_updates=None default value.
8+
# The default list of events does not include chat_member, message_reaction, and message_reaction_count events.
9+
# You must explicitly specify all the events you wish to receive and add message_reaction to this list.
10+
# It’s also important to note that after using allowed_updates=[...], in the future, using allowed_updates=None will mean
11+
# that the list of events you will receive will consist of the events you last explicitly specified.
12+
13+
import random
14+
from telebot.async_telebot import AsyncTeleBot
15+
from telebot.types import ReactionTypeEmoji
16+
17+
API_TOKEN = '<api_token>'
18+
bot = AsyncTeleBot(API_TOKEN)
19+
20+
21+
22+
# Send a reactions to all messages with content_type 'text' (content_types defaults to ['text'])
23+
@bot.message_handler(func=lambda message: True)
24+
async def send_reaction(message):
25+
emo = ["\U0001F525", "\U0001F917", "\U0001F60E"] # or use ["🔥", "🤗", "😎"]
26+
await bot.set_message_reaction(message.chat.id, message.id, [ReactionTypeEmoji(random.choice(emo))], is_big=False)
27+
28+
29+
@bot.message_reaction_handler(func=lambda message: True)
30+
async def get_reactions(message):
31+
await bot.reply_to(message, f"You changed the reaction from {[r.emoji for r in message.old_reaction]} to {[r.emoji for r in message.new_reaction]}")
32+
33+
34+
import asyncio
35+
asyncio.run(bot.infinity_polling(allowed_updates=['message', 'message_reaction']))

examples/message_reaction_example.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/python3
2+
3+
# This is a simple bot using message reactions (emoji)
4+
# https://core.telegram.org/bots/api#reactiontype
5+
# https://core.telegram.org/bots/api#update
6+
# allowed_updates: Specify an empty list to receive all update types except, chat_member, message_reaction, and message_reaction_count.
7+
# If you want to receive message_reaction events, you cannot simply leave the allowed_updates=None default value.
8+
# The default list of events does not include chat_member, message_reaction, and message_reaction_count events.
9+
# You must explicitly specify all the events you wish to receive and add message_reaction to this list.
10+
# It’s also important to note that after using allowed_updates=[...], in the future, using allowed_updates=None will mean
11+
# that the list of events you will receive will consist of the events you last explicitly specified.
12+
13+
import random
14+
import telebot
15+
from telebot.types import ReactionTypeEmoji
16+
17+
API_TOKEN = '<api_token>'
18+
bot = telebot.TeleBot(API_TOKEN)
19+
20+
21+
# Send a reactions to all messages with content_type 'text' (content_types defaults to ['text'])
22+
@bot.message_handler(func=lambda message: True)
23+
def send_reaction(message):
24+
emo = ["\U0001F525", "\U0001F917", "\U0001F60E"] # or use ["🔥", "🤗", "😎"]
25+
bot.set_message_reaction(message.chat.id, message.id, [ReactionTypeEmoji(random.choice(emo))], is_big=False)
26+
27+
28+
@bot.message_reaction_handler(func=lambda message: True)
29+
def get_reactions(message):
30+
bot.reply_to(message, f"You changed the reaction from {[r.emoji for r in message.old_reaction]} to {[r.emoji for r in message.new_reaction]}")
31+
32+
33+
bot.infinity_polling(allowed_updates=['message', 'message_reaction'])

0 commit comments

Comments
 (0)