简体   繁体   中英

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

正如@maak 指出的那样,您需要先向机器人发送消息,然后机器人才能向您发送消息。

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"

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.

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

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

对我来说,它只在频道 id 之前使用@前缀

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM