我正在寻找一种方式,一个机器人等待一个用户的答复后,一个命令。例如,首先输入"/ask",然后机器人等待来自用户的普通消息(而不是命令),在用户回复后将他/她的答复存储在变量中。
我肯定这很简单,但是我看到的所有教程都是俄文的,而且python电报api的文档也很混乱,如果我是哑巴的话,我不是最高级的,对不起,请帮助一个初学者。
发布于 2021-05-01 11:25:00
好吧这是毫无意义的。我以为你不会用论据,但我读的那篇文章只有5岁,所以.我太傻了。我只是用论点来代替,谢谢你的帮助,真的很感激
发布于 2021-05-01 09:05:31
下面是接受用户输入并将其存储在"store_user_input“变量中的代码。
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()您可以通过添加以下内容来检查该值:
print("store_user_input")在store_user_input线之后。
https://stackoverflow.com/questions/67344301
复制相似问题