【问题标题】:Python-telegram-bot How to send InlineKeyboard via urlPython-telegram-bot 如何通过 url 发送 InlineKeyboard
【发布时间】:2018-08-20 10:22:22
【问题描述】:

尝试发送带有回调键盘的消息,机器人不好。告诉我

TypeError: must be str, not ReplyKeyboardMarkup

找不到任何正确的例子。

keyboard = [[InlineKeyboardButton("Выполнено", callback_data='Done')],
                [InlineKeyboardButton("MAC", callback_data='MAC'),
                 InlineKeyboardButton("Phone", callback_data='Phone'),
                 InlineKeyboardButton("История", callback_data='History')]]
    reply_markup = ReplyKeyboardMarkup(keyboard)
    requests.post(url='https://api.telegram.org/bot{blah}/'
                      'sendMessage?chat_id=' + str(query.message.chat_id) + '&text="TEST"&reply_markup=' + reply_markup)

【问题讨论】:

    标签: python telegram telegram-bot python-telegram-bot


    【解决方案1】:

    首先,您应该使用InlineKeyboardMarkup 而不是ReplyKeyboardMarkup 来创建由InlineKeyboardButtons 组成的标记对象。

    那么,您可能应该简单地使用bot 对象来发送带有bot.send_message(query.message.chat_id, 'TEST', reply_markup=reply_markup) 的消息。

    最后,如果你真的需要使用requests进行手动HTTP请求,你应该提供requests.post()data中的参数。

    import json
    import requests
    from telegram import InlineKeyboardButton, InlineKeyboardMarkup
    
    keyboard = [[InlineKeyboardButton("Выполнено", callback_data='Done')],
                [InlineKeyboardButton("MAC", callback_data='MAC'),
                 InlineKeyboardButton("Phone", callback_data='Phone'),
                 InlineKeyboardButton("История", callback_data='History')]]
    reply_markup = InlineKeyboardMarkup(keyboard)
    
    data = {"chat_id": query.message.chat_id,
            "text": "TEST", 
            "reply_markup": json.dumps(reply_markup.to_dict())}
    
    requests.post(url='https://api.telegram.org/bot{blah}/sendMessage', data=data)
    

    【讨论】:

      猜你喜欢
      • 2019-08-07
      • 1970-01-01
      • 2019-08-23
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      • 2017-06-06
      • 2016-05-09
      相关资源
      最近更新 更多