Telegram 机器人 API 键盘

2024-04-19

我对 Telegram Bot Api 和“ReplyKeyboard”有问题。我正在使用 Python 2.7 并发送发布请求:

TelegramAPI.post(TELEGRAM_URL + "sendMessage", data=dict(chat_id=CHAT_ID, text="", keyboard={'keyboard': keyboard, 'one_time_keyboard': False, 'resize_keyboard': True})

键盘格式如下:

[["A button"], ["B button"]]

但在 Telegram 中我看不到键盘。可能是什么问题?


根据机器人 API 文档 https://core.telegram.org/bots/api#sendmessage,自定义键盘需要reply_markup参数,其值为键盘的 JSON 序列化规范。假设你的TelegramAPI.post()函数不会为您进行 JSON 序列化,我会尝试以下操作:

import json

json_keyboard = json.dumps({'keyboard': [["A button"], ["B button"]], 
                            'one_time_keyboard': False, 
                            'resize_keyboard': True})

TelegramAPI.post(TELEGRAM_URL + "sendMessage", 
                 data=dict(chat_id=CHAT_ID, 
                           text="Has to be non-empty", 
                           reply_markup=json_keyboard))

注意text必须是非空的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Telegram 机器人 API 键盘 的相关文章

随机推荐