requests.exceptions.HTTPError:尝试通过 Python 向电报机器人发送消息时引发 400 客户端错误

问题描述 投票:0回答:1

我正在尝试通过 python 请求向电报机器人发送消息。但我一直收到 400 客户端错误。这是代码:


import requests
def send(text):
    token = 'your_token_goes_here'
    params = {'chat_id': xxx, 'text': text, 'parse_mode': 'HTML'}
    resp = requests.post('https://api.telegram.org/bot{}/sendMessage'.format(token), params)
    resp.raise_for_status()
send('hello')

这是错误:

requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: XXXXXXXXXXXXXXXXX

这是我打开 URL 时看到的内容:

{"ok":false,"error_code":400,"description":"Bad Request: message text is empty"}
  1. 我尝试过这个方法。我用的是最新的代理用户字符串
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.79 Safari/537.36 

来自这里。 它不起作用。同样的错误

  1. 我尝试检查是否是requests.post有问题。使用 requests.post 从其他 API 检索数据。它工作得很好。
python-requests python-telegram-bot
1个回答
0
投票

原来我使用了错误的chat_id。

© www.soinside.com 2019 - 2024. All rights reserved.