如何通过内联电报命令发送随机字符串?
How to send a random string via Inline Telegram commands?
我创建了一个机器人(使用 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
时,它按预期工作,但是无论我在与另一个用户或群组的聊天中使用内联命令,随机选择显然都停止了 working.How 我能得到吗围绕这个问题?
我找到了问题的答案。显然,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)
我创建了一个机器人(使用 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
时,它按预期工作,但是无论我在与另一个用户或群组的聊天中使用内联命令,随机选择显然都停止了 working.How 我能得到吗围绕这个问题?
我找到了问题的答案。显然,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)