我目前正在使用python-telegram-bot
库制作一个电报机器人。我的问题是,当使用inline命令时,我试图让我的bot回复。因此,当用户发送bot @botname 'text'
时,我希望将'text'
存储为string
,然后让我的bot用该变量返回一些内容
由于某种原因,我不能让它工作。我尝试了下面的代码,但不起作用……我还发布了github中的示例,它可以工作,但不是我想要的方式
My code
def inlinequery(update, context):
"""Handle the inline query."""
query = update.inline_query.query
text = query.message_text
print(text)
update.message.reply_text(text)
Example Code
#Sends message when @botname is used
def inlinequery(update, context):
"""Handle the inline query."""
query = update.inline_query.query
results = [
InlineQueryResultArticle(
id=uuid4(),
title="Caps",
input_message_content=InputTextMessageContent(
query.upper())),
InlineQueryResultArticle(
id=uuid4(),
title="Bold",
input_message_content=InputTextMessageContent(
"*{}*".format(escape_markdown(query)),
parse_mode=ParseMode.MARKDOWN)),
InlineQueryResultArticle(
id=uuid4(),
title="Italic",
input_message_content=InputTextMessageContent(
"_{}_".format(escape_markdown(query)),
parse_mode=ParseMode.MARKDOWN))]
update.inline_query.answer(results)
def main():
# Get the dispatcher to register handlers
dp = updater.dispatcher
dp.add_handler(InlineQueryHandler(inlinequery))
# Start the Bot
updater.start_polling()
if __name__ == '__main__':
main()
您可以使用内联查询的
User
对象向他们发送消息。请记住,在机器人向用户发送消息之前,用户必须先与机器人进行私人聊天我修改了你的尝试。它应该可以工作,但我没有测试它:
相关文件:
相关问题 更多 >
编程相关推荐