使用纯 API 在电报机器人中制作自定义键盘

Making Custom keyboards in telegram bot using pure API

我正在尝试仅使用 API 而没有外部包来创建电报机器人。 我已经完成了基本功能的创建,我的机器人可以发送短信了。

我只是想知道如何使用 ReplyKeyboardMarkup。 正如文档中提到的,我应该使用一些参数,但我不知道如何使用它们,或者我是否应该在请求中发送它们。

有人能告诉我我应该怎么做才能在我的代码中使用这个 API 东西而不是使用外部包,例如 telebot。

使用 inlinekeyboardbutton send through reply_markup parameter from the sendMessage 方法的小例子。

如文档中所述,此方法与 replykeyboardmarkup

所需的方法相同
import json
import requests

# Create sendMessage url
bottoken = "94924.............."
url = "https://api.telegram.org/bot" + bottoken + "/sendMessage"

# Create keyboard, convert dic to json with json.dumps
kb=json.dumps(
    { "inline_keyboard":
        [
            [
                { "text": "Yes", "callback_data": "x" },
                { "text": "No", "callback_data": "x" }
            ]
        ]
    }
)

# Create data dict
data = {
    'text': (None, 'Hi!'),
    'chat_id': (None, 12345678),
    'parse_mode': (None, 'Markdown'),
    'reply_markup': (None, kb )
}

# Send
res=requests.post(url=url, headers={}, files=data)
print(res.text.encode('utf8'))