Telegram Bot “chat not found”

2020-08-13 05:30发布

问题:

I have the following code in Python to send a message to myself from a bot.

import requests

token = '320835125:AAFUUC-fdo_EFzsCjvmxu8HBk7qVzZXXXXX'
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

回答1:

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



回答2:

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.



回答3:

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"