如何在 Telegram 机器人 (Python) 中使用 inline_keyboard 而不是键盘?
How to use inline_keyboard instead of keyboard in Telegram bot (Python)?
我在 Telegram 机器人中使用了这部分代码 (Python):
def reply(msg=None, img=None):
if msg:
resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
'chat_id': str(chat_id),
'text': msg.encode('utf-8'),
'disable_web_page_preview': 'true',
# 'reply_to_message_id': str(message_id),
'reply_markup': json.dumps({'keyboard': [bline1, bline2], 'resize_keyboard': True}),
})).read()
这部分一切正常。问题是:如何使用 inline_keyboard 而不是常规键盘?
我知道这是一个菜鸟问题,但如果有人能帮助我就太好了。
谢谢!
因为 Inline Keyboard 只是一个不同的 json 对象,我想说你只需要用 json.dumps
来构建它而不是你当前的构建。按照你的例子,像这样的事情应该可以解决问题:
def reply(msg=None, img=None):
if msg:
resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
'chat_id': str(chat_id),
'text': msg.encode('utf-8'),
'disable_web_page_preview': 'true',
# 'reply_to_message_id': str(message_id),
'reply_markup': json.dumps({'inline_keyboard': [[{'text': bline1, 'callback_data': bline1}, {'text': bline2, 'callback_data': bline2}]]}),
})).read()
我在 Telegram 机器人中使用了这部分代码 (Python):
def reply(msg=None, img=None):
if msg:
resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
'chat_id': str(chat_id),
'text': msg.encode('utf-8'),
'disable_web_page_preview': 'true',
# 'reply_to_message_id': str(message_id),
'reply_markup': json.dumps({'keyboard': [bline1, bline2], 'resize_keyboard': True}),
})).read()
这部分一切正常。问题是:如何使用 inline_keyboard 而不是常规键盘?
我知道这是一个菜鸟问题,但如果有人能帮助我就太好了。
谢谢!
因为 Inline Keyboard 只是一个不同的 json 对象,我想说你只需要用 json.dumps
来构建它而不是你当前的构建。按照你的例子,像这样的事情应该可以解决问题:
def reply(msg=None, img=None):
if msg:
resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
'chat_id': str(chat_id),
'text': msg.encode('utf-8'),
'disable_web_page_preview': 'true',
# 'reply_to_message_id': str(message_id),
'reply_markup': json.dumps({'inline_keyboard': [[{'text': bline1, 'callback_data': bline1}, {'text': bline2, 'callback_data': bline2}]]}),
})).read()