Telegram Bot "chat not found"

129.5k views Asked by At

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

11

There are 11 answers

3
Bijan On BEST ANSWER

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

0
kashlo On

For me it worked only with @ prefix before channel id

0
Subhash Sukumaran Nair On

Telegram bots can't send messages to user, if that user hasn't started conversation with bot yet, or bot is not present in chat (if it's a group chat). This issue is not related to the library, this is simply Telegram restriction, so that bots can't spam users without their permission.

you need to first send a message to the bot before the bot can send messages to you.

3
NKM On

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
2
StevenMalai On

I had some trouble with this after upgrading to a supergroup. The chat_id was updated and it was a bit harder to find this new id.

In the end I solved this with this by following this comment: https://stackoverflow.com/a/56078309/14213187

0
vahid esmaily On

If you use a username, it does not require any prefix. That means the following are incorrect:

https://t.me/vahid_esmaily_ie
t.me/vahid_esmaily_ie

And this is the correct case:

vahid_esmaily_ie
0
M Imam Pratama On

Make sure you use user id instead of username if you're sending message to user.

From the docs:

Parameter Type Required Description
chat_id Integer or String Yes Unique identifier for the target chat or username of the target channel (in the format @channelusername)
... ... ... ...

To get the chat id programmatically, see Getting updates for ways to get Update JSON object. For example, when a user sends a message, the Update object will have a Message object inside it which has a Chat object which has the chat id. Turns out the chat id is the same as the user id.

0
Tan Nguyen On

If you want to use a bot message to the channel, you can refer step here

Steps:

  1. Create a Telegram public channel
  2. Create a Telegram BOT (for example x_bot) via BotFather
  3. Set the x_bot as an administrator in your channel

the chat_id is @x_bot, it's a part of https://t.me/x_bot that does not add your channel name.

1
Martin S. On

There is a way to send notifications messages to telegram. It's a bit tricky but the tutorial is great!

http://bernaerts.dyndns.org/linux/75-debian/351-debian-send-telegram-notification

I just sended a message of my apache state to a privat channel. Works also on public channel but it's not what i wantet. As you call a script (bash) you can prepare the parameters in any script language.

Hope that helps.

0
jedidiah On

using the user_id in int() form worked for me

3
Dan Walters On

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"