Search code examples
pythonpython-telegram-bot

How to get different cache_times from different inline queries?


just started to use the python-telegram-bot library and I made my own bot using their examples and documentations, but still can't get my bot to do something that should be rather simple, which is to have different cache_times for different inline queries. This is the involved code:

    def inline_opt(update, context):
        results = [
        InlineQueryResultArticle(
            id=uuid4(),
            title = "QUERY1",
            input_message_content = InputTextMessageContent(
                "blah blah")),

        InlineQueryResultArticle(
            id=uuid4(),
            title = "QUERY2",
            input_message_content = InputTextMessageContent(
                "Blah blah "))
        ]

    update.inline_query.answer(results, cache_time=0)

It works fine, except that I want the first query to have a cache_time of 0 seconds and the other one to have a cache_time of x seconds. Sorry if it's a dumb question but couldn't get an answer on the doc or in the telegram group.


Solution

  • cache_time is a parameter of inline_query.answer() which means you need to filter the queries you receive to create a tailored answer with its particular cache_time.

    import time
    
    def inlinequery(update, context):
        query = update.inline_query.query
        if query=="time":
            results = [
                InlineQueryResultArticle(
                    id=uuid4(),
                    title="time",
                    input_message_content=InputTextMessageContent(
                        "time({!s}): {!s}".format(query,time.asctime(time.localtime()))))
                ]
            seconds = 1;
            update.inline_query.answer(results,cache_time=seconds)
        elif query=="hora":
            results = [
                InlineQueryResultArticle(
                    id=uuid4(),
                    title="hora",
                    input_message_content=InputTextMessageContent(
                        "Time({!s}): {!s}".format(query,time.asctime(time.localtime()))))
                ]
            seconds = 60;
            update.inline_query.answer(results,cache_time=seconds)