简体   繁体   English

如何使用 Python 发布请求和 Telegram bot 在具有 InlineKeyboardMarkup 的 Telegram 上发送照片

[英]How to send a photo on Telegram that has InlineKeyboardMarkup using Python post request and a Telegram bot

I have the following file:我有以下文件:

# 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))

The problem is that I want to uncomment the reply_markup line, because I want to add some buttons to the image, but uncommenting this line gives me:问题是我想取消对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'

How can I solve this issue?我该如何解决这个问题?

Your missing the inline_keyboard key.您缺少inline_keyboard键。 Also use json.dumps to create an proper obj for the form_data;还使用json.dumpsjson.dumps创建一个合适的 obj;

Don't forget to add some CallBack data to the button to tell them apart if somebody clicks them;不要忘记向按钮添加一些回调数据,以便在有人点击它们时区分它们;

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

Updated code;更新代码;

# 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))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM