首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >电报机器人在命令Python后等待用户回复

电报机器人在命令Python后等待用户回复
EN

Stack Overflow用户
提问于 2021-05-01 08:33:09
回答 2查看 1.1K关注 0票数 3

我正在寻找一种方式,一个机器人等待一个用户的答复后,一个命令。例如,首先输入"/ask",然后机器人等待来自用户的普通消息(而不是命令),在用户回复后将他/她的答复存储在变量中。

我肯定这很简单,但是我看到的所有教程都是俄文的,而且python电报api的文档也很混乱,如果我是哑巴的话,我不是最高级的,对不起,请帮助一个初学者。

EN

回答 2

Stack Overflow用户

发布于 2021-05-01 11:25:00

好吧这是毫无意义的。我以为你不会用论据,但我读的那篇文章只有5岁,所以.我太傻了。我只是用论点来代替,谢谢你的帮助,真的很感激

票数 1
EN

Stack Overflow用户

发布于 2021-05-01 09:05:31

下面是接受用户输入并将其存储在"store_user_input“变量中的代码。

代码语言:javascript
运行
复制
import logging
from telegram.ext import Updater, 
CommandHandler, MessageHandler, Filters, 
CallbackContext
from telegram import Update

logging.basicConfig(
format='%(asctime)s - %(name)s - % 
(levelname)s - %(message)s', 
level=logging.INFO
)
logger = logging.getLogger(__name__)

# function to handle the /start command
def startcommand(update:Update, context: 
CallbackContext) -> None:
first_name = update.message.chat.first_name
update.message.reply_text\
        (
        f'Welcome to the bot, {first_name}, 
what are you interested in?'
        )

# function to handle and store user input
def text(update:Update, context: 
CallbackContext) -> None:
text_received = update.message.text
store_user_input = text_received

# function to handle the /help command
def helpcommand(update:Update, context: 
CallbackContext) -> None:
update.message.reply_text('Here is a list of 
all available commands:\n '
                          '/start - start the 
bot\n'
                          '/help - get all 
available commands\n')

# function to handle errors occured in the 
dispatcher
def errormsg(update:Update, context: 
CallbackContext) -> None:
update.message.reply_text('An error 
occured.')

# main function
def main():
# "bot_data is your .txt file with bot API token"
fo = open("bot_data")
fr = fo.read()
updater = Updater(fr)
dispatcher = updater.dispatcher

# add handlers for start and help commands

dispatcher.add_handler(CommandHandler("start", startcommand))
dispatcher.add_handler(CommandHandler("help", helpcommand))

# add an handler for normal text (not commands)
dispatcher.add_handler(MessageHandler(Filters.text, text))

# add an handler for errors
dispatcher.add_error_handler(errormsg)

# start bot
updater.start_polling()

# run the bot
updater.idle()


if __name__ == '__main__':
    main()

您可以通过添加以下内容来检查该值:

代码语言:javascript
运行
复制
print("store_user_input")

在store_user_input线之后。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67344301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档