我是 Python 新手和 Stack Overflow 新手,对我的错误感到抱歉...
我正在使用 Telepot 为 Telegram 编写一个机器人。
在处理内联结果时,我只能让 on_chosen_inline_result 中的代码针对我选择的每篇文章执行,而不是第一个创建的文章。
def on_inline_query(msg):
query_id, from_id, query_string = telepot.glance(msg, flavor='inline_query')
tastiera_inline = InlineKeyboardMarkup(*some_keyboard*)
articles = []
if query_string:
found_conte = fun.get_conta(titolo=query_string)
if found_conte == 1:
return
for conta_inline in found_conte:
articles.append(InlineQueryResultArticle(
id=str(conta_inline['id']),
title=conta_inline['titolo'],
input_message_content=InputTextMessageContent(*some_content*),
reply_markup=tastiera_inline,
description=conta_inline['testo'],
thumb_url=*some_url*,
))
else:
if fun.get_conta('all') == 1:
return
for conta_inline in fun.get_conta('all'):
articles.append(InlineQueryResultArticle(
id=str(conta_inline['id']),
title=conta_inline['titolo'],
input_message_content=InputTextMessageContent(*some_content*),
reply_markup=tastiera_inline,
description=conta_inline['testo'],
thumb_url=*some_url*,
))
bot.answerInlineQuery(query_id, articles)
def on_chosen_inline_result(msg):
print(msg)
bot = telepot.Bot(TOKEN)
bot.message_loop({'chat': on_chat_message,
'inline_query': on_inline_query,
'chosen_inline_result': on_chosen_inline_result
},
run_forever='Listening ...')
print(msg) 对于我从内联查询的答案列表中选择的每篇文章都能正常工作,除了创建的第一篇文章。 我真的很困惑......
真正令人困惑的是,当我从 Telegram 的列表中选择一篇文章时,我认为无法控制会发生什么,所以有时它会触发 on_chosen_inline_result 代码似乎很奇怪而其他时候则不然。
编辑:似乎如果 InlineQueryResultArticle 中的 id='0' 所选结果不会触发 on_chosen_inline_result 中的代码,但是我不是 100% 确定这是原因,而且我真的不知道为什么,无论如何...
PS:我不知道需要多少代码才能理解这个问题而不复制这里的所有内容。如果需要,我当然可以编辑问题并添加更多代码。
请您参考如下方法:
从我自己的测试来看,你的猜测似乎是正确的。我尝试了多种 id 字符串并依次选择了它们中的每一个:
00→ 是(chosen_inline_result得到)$0→ 是0→ 否1→ 是
作为 telepot 的作者,我想不出任何地方 0 的结果 ID 可能会被无意中过滤掉。所以我假设这是一个 Telegram 问题。
如上图所示,交通便利。您几乎可以在前面添加任何字符来取回 chosen_inline_result。
如果有人感兴趣,这是我的测试代码:
import sys
import threading
import telepot
from telepot.namedtuple import InlineQueryResultArticle, InputTextMessageContent
def on_inline_query(msg):
def compute():
query_id, from_id, query_string = telepot.glance(msg, flavor='inline_query')
print('%s: Computing for: %s' % (threading.current_thread().name, query_string))
articles = [InlineQueryResultArticle(
id='00', title='ID=00',
input_message_content=InputTextMessageContent(
message_text='ID=00')),
InlineQueryResultArticle(
id='$0', title='ID=$0',
input_message_content=InputTextMessageContent(
message_text='ID=$0')),
InlineQueryResultArticle(
id='0', title='ID=0',
input_message_content=InputTextMessageContent(
message_text='ID=0')),
InlineQueryResultArticle(
id='1', title='ID=1',
input_message_content=InputTextMessageContent(
message_text='ID=1')),]
return dict(results=articles)
answerer.answer(msg, compute)
def on_chosen_inline_result(msg):
result_id, from_id, query_string = telepot.glance(msg, flavor='chosen_inline_result')
print('Chosen Inline Result:', result_id, from_id, query_string)
TOKEN = sys.argv[1] # get token from command-line
bot = telepot.Bot(TOKEN)
answerer = telepot.helper.Answerer(bot)
bot.message_loop({'inline_query': on_inline_query,
'chosen_inline_result': on_chosen_inline_result},
run_forever='Listening ...')
