【问题标题】:Telegram bot: How to get chosen inline result电报机器人:如何获得选择的内联结果
【发布时间】:2017-08-15 04:35:54
【问题描述】:

我正在向客户发送InlineQueryResultArticle,我想知道如何获得选择的结果及其数据(如result_id,...)。

这里是发送结果的代码:

token = 'Bot token'
bot = telegram.Bot(token)
updater = Updater(token)
dispatcher = updater.dispatcher

def get_inline_results(bot, update):
    query = update.inline_query.query
    results = list()

    results.append(InlineQueryResultArticle(id='1000',
                                            title="Book 1",
                                            description='Description of this book, author ...',
                                            thumb_url='https://fakeimg.pl/100/?text=book%201',
                                            input_message_content=InputTextMessageContent(
                                                'chosen book:')))

    results.append(InlineQueryResultArticle(id='1001',
                                            title="Book 2",
                                            description='Description of the book, author...',
                                            thumb_url='https://fakeimg.pl/300/?text=book%202',
                                            input_message_content=InputTextMessageContent(
                                                'chosen book:')
                                            ))

    update.inline_query.answer(results)


inline_query_handler = InlineQueryHandler(get_inline_results)
dispatcher.add_handler(inline_query_handler)

我正在寻找像on_inline_chosen(data) 这样的方法来获取所选项目的ID。 (1000 or 1001 上面的 sn-p)然后向用户发送适当的响应。

【问题讨论】:

    标签: telegram-bot python-telegram-bot


    【解决方案1】:

    你应该在@BotFather中设置/setinlinefeedback,然后你会得到这个更新

    【讨论】:

    • 谢谢,但我想知道如何在代码中实现on_inline_result_selected()
    【解决方案2】:

    好的,我的回答来自here

    处理用户选择的结果:

    from telegram.ext import ChosenInlineResultHandler
    
    def on_result_chosen(bot, update):
        print(update.to_dict())
        result = update.chosen_inline_result
        result_id = result.result_id
        query = result.query
        user = result.from_user.id
        print(result_id)
        print(user)
        print(query)
        print(result.inline_message_id)
        bot.send_message(user, text='fetching book data with id:' + result_id)
    
    
    result_chosen_handler = ChosenInlineResultHandler(on_result_chosen)
    dispatcher.add_handler(result_chosen_handler)
    

    【讨论】:

      猜你喜欢
      • 2016-09-11
      • 2023-03-13
      • 2017-12-10
      • 2017-08-18
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 2015-09-11
      • 2017-01-06
      相关资源
      最近更新 更多