python - Telegram Bot "chat not found"

标签 python python-requests telegram telegram-bot

我在 Python 中有以下代码,用于从机器人向我自己发送消息。

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)

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

我做错了什么?我通过将 /getid 发送到 @myidbot 获得了 myuserid 并且我从 @BotFather

获得了我的 token

最佳答案

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

关于python - Telegram Bot "chat not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41174831/

相关文章:

c# - C# 中的 Telegram Bot 自定义键盘

python - 使用 Python 请求和 REST API 发布 WordPress 帖子

python - 带有嵌套数组的 Scrapy

python-3.x - 通过 enctype ="multipart/form-data"上传多个文件,第一个上传的文件大小始终为0,其余都可以

python - 将字节对象列表转换为字典

python - 带有请求模块的 ConnectionResetError 在异常内部引发异常,导致程序崩溃

Telegram Bot : Is it possible to get userid using username?

python - PyGTK 检查按钮的标签位于左侧

python - Django 模型多项选择

webhooks - 如何在机器人父亲中为 Telegram Bot 创建菜单?