python - 如何使用 Python post 请求和 Telegram 机器人在 Telegram 上发送具有 InlineKeyboardMarkup 的照片

标签 python python-3.x telegram telegram-bot

我有以下文件:

# get_request.py

import requests 

API_TOKEN = "fake:APITOken"

# api-endpoint 
URL = "https://api.telegram.org/bot" + API_TOKEN + "/sendphoto"

multipart_form_data = {
    'photo': ('pic.png', open('pic.png', 'rb')),
    'action': (None, 'send'),
    'chat_id': (None, <MY-ID>),
    'caption': (None, 'This is a *description* of the _image_'),
    'parse_mode': (None, 'Markdown'),
    'reply_to_message_id': (None, 103),
    # 'reply_markup': (None, [[{"text": "Hello"}, {"text": "Bye"}, {"text": "I love you"}]])
}

response = requests.post(URL, files=multipart_form_data)

data = response.json() 

import json
print(json.dumps(data, sort_keys=True, indent=4))

问题是我想取消注释 reply_markup行,因为我想向图像添加一些按钮,但是取消注释该行可以得到:

Traceback (most recent call last):
  File "get_request.py", line 32, in <module>
    response = requests.post(URL, files=multipart_form_data)
  File "/usr/local/lib/python3.7/site-packages/requests/api.py", line 116, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 519, in request
    prep = self.prepare_request(req)
  File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 462, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/usr/local/lib/python3.7/site-packages/requests/models.py", line 316, in prepare
    self.prepare_body(data, files, json)
  File "/usr/local/lib/python3.7/site-packages/requests/models.py", line 504, in prepare_body
    (body, content_type) = self._encode_files(files, data)
  File "/usr/local/lib/python3.7/site-packages/requests/models.py", line 169, in _encode_files
    body, content_type = encode_multipart_formdata(new_fields)
  File "/usr/local/lib/python3.7/site-packages/urllib3/filepost.py", line 90, in encode_multipart_formdata
    body.write(data)
TypeError: a bytes-like object is required, not 'list'

如何解决这个问题?

最佳答案

您缺少 inline_keyboard key 。还可以使用 json.dumps 为 form_data 创建适当的 obj;

别忘了添加一些CallBack data如果有人点击它们,则可以通过按钮来区分它们;

{"text": "Hello", "callback_data": "x"}

更新了代码;

# get_request.py

import requests 

API_TOKEN = "fake:APITOken"

# api-endpoint 
URL = "https://api.telegram.org/bot" + API_TOKEN + "/sendphoto"

keyboard=json.dumps({ "inline_keyboard": [ [ {"text": "Hello", "callback_data": "x"}, {"text": "Bye", "callback_data": "x"}, {"text": "I love you", "callback_data": "x"} ] ] })

multipart_form_data = {
    'photo': ('pic.png', open('pic.png', 'rb')),
    'action': (None, 'send'),
    'chat_id': (None, <ID>),
    'caption': (None, 'This is a *description* of the _image_'),
    'parse_mode': (None, 'Markdown'),
    'reply_to_message_id': (None, 103),
    'reply_markup': (None, keyboard )
}

response = requests.post(URL, files=multipart_form_data)

data = response.json() 

import json
print(json.dumps(data, sort_keys=True, indent=4))

enter image description here

关于python - 如何使用 Python post 请求和 Telegram 机器人在 Telegram 上发送具有 InlineKeyboardMarkup 的照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60253349/

相关文章:

algorithm - 我可以降低它的计算复杂度吗?

encryption - 如何解密 Telegram 网络请求

php - 使用 php bot 在 Telegram 上发送带有照片的长消息

python - 如何从图像中找到各种颜色而不给出颜色范围

python - Django 重定向无法解析 URL

python-3.x - 获取垂直线 numpy 的线坐标

python - 如何在 Google Cloud Platform 中为 Node 应用程序安装 Python3

python - XgBoost : The least populated class in y has only 1 members, 太少

python - 方法调用本身没有方法名?

php - 如何将大文件从 URL 发送到 Telegram 机器人?