[英]Python Telegram Bot get the bot to respond to message
我目前正在使用python-telegram-bot
庫來制作電報機器人。 我的問題是我試圖讓我的機器人在使用內聯命令時做出回應。 因此,當用戶發送機器人@botname 'text'
,我希望將'text'
存儲為string
,然后讓我的機器人用該變量發回一些東西。
出於某種原因,我無法讓它發揮作用。 我嘗試了下面的代碼,但它不起作用......我還從 github 上發布了這個例子,它可以工作但不是我想要的方式。
我的代碼
def inlinequery(update, context):
"""Handle the inline query."""
query = update.inline_query.query
text = query.message_text
print(text)
update.message.reply_text(text)
示例代碼
#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
對象向他們發送消息。 請記住,用戶必須先與機器人開始私人聊天,然后機器人才能向他們發送消息。
我修改了你的嘗試。 它應該可以工作,但我還沒有測試過:
def inlinequery(update, context):
"""Handle the inline query."""
query = update.inline_query
text = query.query
print(text)
query.from_user.send_message(text)
相關文檔:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.