Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telegram Bot "chat not found"

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

like image 289
Bijan Avatar asked Dec 15 '16 23:12

Bijan


People also ask

How can I find my Telegram bot chat?

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.

Why bot in Telegram is not working?

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.


3 Answers

As @maak pointed out, you need to first send a message to the bot before the bot can send messages to you.

like image 143
Bijan Avatar answered Sep 22 '22 07:09

Bijan


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
like image 28
NKM Avatar answered Sep 18 '22 07:09

NKM


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"
like image 34
Dan Walters Avatar answered Sep 19 '22 07:09

Dan Walters