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
reactionofsetMessageReactiondoes expect an array of objects of typeReactionTyperather than a simple string. So you should defineTelegram does provide a error message that helps to find this issue as part of the
response. If you printresponse.json()in the exception as case as well, you'll see the infoMoreover, 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.