I am implementing the function of setting the bot's response to a user's message (a new version of the bot api was recently released in Telegram, where this function appeared).
def send_react(message):
global TOKEN
chat_id = message.chat.id
message_id = message.message_id
reaction = '👍'
url = f'https://api.telegram.org/bot{TOKEN}/setMessageReaction'
data = {
'chat_id': chat_id,
'message_id': message_id,
'reaction': reaction,
'is_big': False
}
response = requests.post(url, json=data)
if response.status_code == 200:
result = response.json()
if result['ok']:
print('ok')
else:
print('not ok:', result['description'])
else:
print('error:', response.status_code)
return
However, I get an error: Error: 400 What could it be?
I also checked that when deleting the reaction and is_big fields, the request is successful, but in this case, the function is useless.
The root cause of your problem is that the reaction
of setMessageReaction
does expect an array of objects of type ReactionType
rather than a simple string. So you should define
reaction = [{"type": "emoji", "emoji": "👍"}]
Telegram does provide a error message that helps to find this issue as part of the response
. If you print response.json()
in the exception as case as well, you'll see the info
{'ok': False, 'error_code': 400, 'description': "Bad Request: can't parse reaction types JSON object"}
Moreover, let me point out that you avoid the use of a global variable for the token by using message.get_bot().token
- see here and here for docs on this.
Disclaimer: I'm currently the maintainer of python-telegram-bot
.