电报机器人:如何获得内联结果

阿里·谢拉法特(Ali Sherafat)

我正在发送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适用于上述代码段),然后将适当的响应发送给用户。

阿里·谢拉法特(Ali Sherafat)

好,我从这里得到答案

处理用户选择的结果:

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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章