【问题标题】:Send Image or Document with Inline Menu使用内联菜单发送图像或文档
【发布时间】:2019-06-18 17:24:57
【问题描述】:

我正在使用 python-telegram-bot 编写 Python Telegram Bot。我创建了一个自定义内联菜单。

我希望用户可以按下按钮并获得图片。 send_photo 函数需要更新一个机器人实例。

但我不知道如何将其传递给 CallBackQuery 处理程序。

有人知道如何解决吗?

发送照片功能:

def gvu(bot, update):
    bot.send_photo(update.message.chat_id, photo=open('botpic/gvu.jpg', 'rb'))

主程序中的处理程序:

updater.dispatcher.add_handler(CallbackQueryHandler(pattern="1", callback=gvu))
return self.callback(dispatcher.bot, update, **optional_args)

错误:

TypeError: callback_handler() 得到了一个意外的关键字参数“chat_data”

【问题讨论】:

  • 你能发布完整的错误回溯吗?

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


【解决方案1】:

这对我有用:

    buttonsMenu = [
    [telegram.InlineKeyboardButton("UP", callback_data="UpVote")],
    [telegram.InlineKeyboardButton("DOWN", callback_data="DownVote")],
    ]
    keyboard_markup = telegram.InlineKeyboardMarkup(buttonsMenu)
    context.bot.sendPhoto(chat_id=update.message.chat.id, photo=open('./imgpath.jpg'), 'rb'),caption='messageText', reply_markup=keyboard_markup)
     

这将发送一张图片,带有文本和文本消息下方的 2 个按钮。

现在对于回调查询,我在 main() 中这样做了:

# Callbacks for the msg buttons
dp.add_handler(CallbackQueryHandler(vote, pattern="UpVote"))
dp.add_handler(CallbackQueryHandler(vote, pattern="DownVote"))

其中 vote 是一个运行我想要的回调代码的定义。 希望有意义。

【讨论】:

    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    • 2010-11-15
    • 2018-01-20
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    相关资源
    最近更新 更多