1

我创建了一个机器人(使用python-telegram-bot),在选择一种查询类型时,机器人应该随机选择一个可用字符串作为回复。

我创建回复的功能如下:

def generate_reply():
    replies = """
              Hello
              Goodbye
              Thanks!
              Your welcome!
              See you around!""".splitlines()
    r = random.choice(replies).strip()
    return r

回复用户的功能如下:

#Inline Reply
def inlinequery(update, context):
    query = update.inline_query.query
    results = [InlineQueryResultArticle(id=uuid4(), title="Interact",
                                        input_message_content=InputTextMessageContent(
                                                generate_reply()))]

    update.inline_query.answer(results)

#Normal reply
def reply(update, context):
   update.message.reply_text(generate_reply())

创建机器人后,我使用以下命令将其添加到机器人中:

dp.add_handler(CommandHandler("reply", reply))
dp.add_handler(InlineQueryHandler(inlinequery))

当我/reply在聊天中使用它时,它按预期工作,但是无论我在与另一个用户或组聊天时使用内联命令,随机选择显然停止工作。我怎样才能解决这个问题?

4

1 回答 1

1

我找到了我的问题的答案。显然,Telegram 将类似内联查询的答案缓存了一段时间。为了让它正常工作,你应该设置cache_time你想要的东西,在我的例子中是 0。

#Inline Reply
def inlinequery(update, context):
    query = update.inline_query.query
    results = [InlineQueryResultArticle(id=uuid4(), title="Interact",
                                        input_message_content=InputTextMessageContent(
                                                generate_reply()))]

    update.inline_query.answer(results, cache_time=0)
于 2020-09-01T06:09:10.080 回答