Ali Sherafat
Ali Sherafat

Reputation: 3855

Telegram bot: How to get chosen inline result

I'm sending InlineQueryResultArticle to clients and i'm wondering how to get chosen result and it's data (like result_id,...).

here is the code to send results:

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)

I'm looking for a method like on_inline_chosen(data) to get id of the chosen item. (1000 or 1001 for snippet above) and then send the appropriate response to user.

Upvotes: 4

Views: 4726

Answers (2)

Ali Sherafat
Ali Sherafat

Reputation: 3855

OK, i got my answer from here

Handling user chosen result:

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)

Upvotes: 3

Sean Wei
Sean Wei

Reputation: 7985

You should set /setinlinefeedback in @BotFather, then you will get this update

Upvotes: 8

Related Questions