电报机器人“找不到聊天”

2024-04-28 22:44:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Python编写了以下代码,用于从机器人向自己发送消息。

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)

但这会返回{'description': 'Bad Request: chat not found', 'error_code': 400, 'ok': False}

我做错什么了?我通过发送/getid@myidbot获得myuserid,我从@BotFather获得我的令牌


Tags: 代码importtoken消息responsechat机器人post
2条回答

有一种方法可以将通知消息发送到电报。这有点棘手,但教程是伟大的!

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

我刚刚向privat频道发送了一条apache状态的消息。 也在公共频道工作,但这不是我想要的。在调用脚本(bash)时,可以用任何脚本语言准备参数。

希望能有所帮助。

正如@maak所指出的,在机器人向您发送消息之前,您需要先向机器人发送消息。

相关问题 更多 >