我刚开始学习使用telegram-bot-api-python模块构建Telegram机器人。我做了三个功能,其中一个/start
命令功能可以开始通话,此功能会询问用户电话号码并将其发送给其他处理程序或函数reply_with_number
,在该处理程序或函数中,将使用if
else
语句验证用户的答复,如果用户的答复有效,则将其发送至另一个称为ask_link
this的处理程序或函数是最后一个,他将通过链接答复我。一切都很好,但是当用户输入诸如“ thisstring
”之类的字符串而不是电话号码时,该功能reply_with_number
应继续运行或继续询问有效的电话号码,直到他键入有效的电话号码为止。但是我只是摆脱这种情况,reply_with_number
然后运行下一个ask_link
处理程序,无需等待回复。如何解决这个问题?我的代码:
import time
from telegram import MessageEntity,Bot
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler
from telegram import KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove
updater = Updater(token=token, use_context=True)
dispatcher = updater.dispatcher
def start(update, context):
username = update.message.from_user.first_name
context.bot.send_message(chat_id=update.effective_chat.id,
text=f"Hello {username}, Welcome to my bot!")
context.bot.send_message(chat_id=update.effective_chat.id,
text='May i know your phone no ?')
def reply_with_number(update, context):
if update.message.text and len(update.message.text) <= 13:
try:
if update.message.text[0:3] == '+91':
phone_number = int(update.message.text[3:])
elif update.message.text[0:2] == '91':
phone_number = int(update.message.text[2:])
elif update.message.text[0] == '0':
phone_number = int(update.message.text[1:])
else:
phone_number = int(update.message.text)
context.bot.send_message(chat_id=update.effective_chat.id,
text='Looks good!',
reply_markup=reply_markup)
except Exception as e:
update.message.reply_text('Please enter phone number not strings!')
def ask_link(update, context):
link = update.message.text
neat_url = strip(link)
if neat_url != None:
context.bot.send_message(chat_id=update.effective_chat.id,
text='Link looks good')
elif neat_url == None:
context.bot.send_message(chat_id=update.effective_chat.id,
text='Its invalid!')
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
reply_handler = MessageHandler(Filters.contact | Filters.text, reply_with_number)
dispatcher.add_handler(reply_handler)
ask_for_link = MessageHandler(Filters.text, ask_link)
dispatcher.add_handler(ask_for_link)
updater.start_polling()
在ConversationHandler
这种情况下,您应该使用,并仅返回正确的消息供用户重试,并返回正确的消息,以便漫游器知道用户仍在对话处理程序的同一步骤中,例如:
from telegram.ext import CommandHandler, MessageHandler, Filters, ConversationHandler
FIRST_STEP, SECOND_STEP = range(2)
conversation_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
FIRST_STEP: [MessageHandler(Filters.text & ~Filters.command, first_step)],
SECOND_STEP: [MessageHandler(Filters.text & ~Filters.command, second_step)],
},
fallbacks=[CommandHandler('cancel', cancel)]
)
def start(bot, update):
bot.send_message(
chat_id=update.message.chat_id,
text='2 + 2 = ?',
parse_mode='markdown')
return FIRST_STEP
def cancel(bot, update):
bot.send_message(
chat_id=update.message.chat_id,
text='2 + 2 = ?')
return ConversationHandler.END
def first_step(bot, update):
if update.message.text != '4':
bot.send_message(
chat_id=update.message.chat_id,
text='Sorry try again to enter a valid input.')
bot.send_message(
chat_id=update.message.chat_id,
text='2 + 2 = ?')
return FIRST_STEP
else:
bot.send_message(
chat_id=update.message.chat_id,
text='The answer is right, you are in the next step')
return SECOND_STEP
def second_step(bot, update):
bot.send_message(
chat_id=update.message.chat_id,
text='Correct answer',
parse_mode='markdown')
return ConversationHandler.END
参考文档:https : //python-telegram-bot.readthedocs.io/en/stable/telegram.ext.conversationhandler.html? highlight =ConversationHandler
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句