3

I'm using python-telegram-bot to build a bot that answers to inline queries. The query result is kind of complex to process. I'm using @run_async.

As a user types an inline query, the client produces several queries, every one of them spawning a handler on my bot that takes time to process.

Of all of those queries, only the latest one is actually important. For example, if I query for "Beach Voley", the bot will receive successive queries for:

  • Bea
  • Beach
  • Beach Vo
  • Beach Voley

as I type, pause and continue typing.

Then, my bot lags processing the incomplete queries before processing the actually important one, and telegram gives me invalid id errors for the outdated ones, and timeout for the actually important query.

I would like to, upon receive a query, cancel any other inline queries being processed for that user with an older timestamp, killing their threads or something.

Telepot, another telegram bot library for python I used, had this included as a feature. From what I could understand of the source code, it keeps a running tasks queue internally.

How could I mimic this behavior in python-telegram-bot? Is this a feaure that I'm just not finding?

  • 1
    Welcome to StackOverflow! Please include your attempts so far in the question – Andreas Nov 26 '18 at 05:52
  • Well, I couldn't really attemp to write it. I don't understand how python-telegram-bot handles the currently ongoing tasks and telepot's Answerer was black magic for me. I tried searching for similar questions and reasearching on python-telegram-bot documentation and examples, but I've had no success so far. I considered adding a code snippet, but it's really not different from the standard example for an inline bot. – QuanticPotato Nov 26 '18 at 06:08

1 Answers1

0

You can set query to context.user_data['query'] in handler every time overwriting the value. Finally, when you'll need to use it - you'll have the last one.

wowkin2
  • 5,895
  • 5
  • 23
  • 66