I have the following code in Python to send a message to myself from a bot.
import requests
token = '123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHI'
method = 'sendMessage'
myuserid = 1949275XX
response = requests.post(
url='https://api.telegram.org/bot{0}/{1}'.format(token, method),
data={'chat_id': myuserid, 'text': 'hello friend'}
).json()
print(response)
but this returns {'description': 'Bad Request: chat not found', 'error_code': 400, 'ok': False}
What am I doing wrong? I got myuserid
by sending /getid
to @myidbot
and I got my token from @BotFather
Type “ @RawDataBot ” and select “Telegram Bot Raw” from the drop-down list. Click on the “Start” button in the auto-reply message. The Telegram bot will send a message with your account info. Scroll down and find “Chat.” Your chat ID number is listed below, next to “id.
Suggestions: Ensure the Bot is in the group and as an admin. Try adding a new access token (revoke existing API key and create a new one then replace in plugin settings) If you edited the bot with @BotFather, make sure to restore it to the default settings.
As @maak pointed out, you need to first send a message to the bot before the bot can send messages to you.
I was using prefix @ before the value of chat_id as suggested everywhere. I removed it and it started working. Note: if your chat id is 12345678 then you need to prefix it with -100 such that it is -10012345678. Example Postman call:
/sendMessage?chat_id=-10012345678&text=Let's get together
If your trying to send messages to a group, you must add a ‘-‘ in front of your chat ID. For example:
TELEGRAM_REG_CHAT_ID="1949275XX"
should be
TELEGRAM_REG_CHAT_ID="-1949275XX"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With