首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Telebot如何使机器人在“是”之后做出回答而不发送另一条消息

Telebot如何使机器人在“是”之后做出回答而不发送另一条消息
EN

Stack Overflow用户
提问于 2022-07-11 10:07:49
回答 1查看 674关注 0票数 0

我用pyTelegramBotAPI编写了一个简单的电报机器人。我需要的对话框应该如下所示:

  1. 你:“某物”
  2. 机器人:你好,我是机器人的榜样。你叫什么名字?
  3. 你:“某个名字”
  4. 机器人:“选择你的号码”(显示有数字的键盘)
  5. 你:选择号码
  6. 机器人:“你选择了1,你想重复吗?”
  7. 你:“是的”
  8. 机器人:“选择你的号码”(显示有数字的键盘)

但是,由于某些原因,在选择“是”选项后,bot不显示键盘,而是等待我再次写入它(步骤8)。看起来是这样的:

  1. 你:“某物”
  2. 机器人:你好,我是机器人的榜样。你叫什么名字?
  3. 你:“某个名字”
  4. 机器人:“选择你的号码”(显示有数字的键盘)
  5. 你:选择号码
  6. 机器人:“你选择了1,你想重复吗?”
  7. 你:“是的”
  8. 你:“某物”
  9. 机器人:“选择你的号码”(显示有数字的键盘)

我怎样才能让机器人在“是”之后立即回答,而不需要向它发送另一个信息呢?

代码:

代码语言:javascript
运行
复制
import telebot
from telebot import types

API_TOKEN = 'place_token_here'
bot = telebot.TeleBot(API_TOKEN)

@bot.message_handler(func=lambda m: True)
def send_welcome(message):
    msg = bot.send_message(message.from_user.id,  """\
Hi there, I am Example bot.
What's your name?
""", allow_sending_without_reply=True)
    bot.register_next_step_handler(msg, another_test_step)

def another_test_step(message):
    markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
    markup.add('1', '2', '3')
    bot.send_message(message.from_user.id, 'Choose your number', reply_markup=markup, allow_sending_without_reply=True)
    bot.register_next_step_handler(message, after_key)

def after_key(message):
    message = bot.send_message(message.from_user.id, 'You chose ' + message.text + '. Do you want to repeat?')
    bot.register_next_step_handler(message, yet2_test_step)


def yet2_test_step(message):
    if message.text == "yes":
        bot.register_next_step_handler(message, another_test_step)
    elif message.text == 'no':
        bot.register_next_step_handler(message, send_welcome)


bot.enable_save_next_step_handlers(delay=2)

bot.load_next_step_handlers()

bot.infinity_polling()

现在在电报中是如何工作的

还建议我在yet2_test_step中手动调用函数another_test_step来更改yet2_test_step函数。所以代码是:

代码语言:javascript
运行
复制
import telebot
from telebot import types

API_TOKEN = ''
bot = telebot.TeleBot(API_TOKEN)

@bot.message_handler(func=lambda m: True)
def send_welcome(message):
    msg = bot.send_message(message.from_user.id,  """\
Hi there, I am Example bot.
What's your name?
""", allow_sending_without_reply=True)
    bot.register_next_step_handler(msg, another_test_step)


def another_test_step(message):
    markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
    markup.add('1', '2', '3')
    bot.send_message(message.from_user.id, 'Choose your number', reply_markup=markup, allow_sending_without_reply=True)
    bot.register_next_step_handler(message, after_key)


def after_key(message):
    message = bot.send_message(message.from_user.id, 'You chose ' + message.text + '. Do you want to repeat?')
    bot.register_next_step_handler(message, yet2_test_step)


def yet2_test_step(message):
    if message.text == "yes":
        another_test_step(message)
        bot.register_next_step_handler(message, after_key)
    elif message.text == 'no':
        bot.register_next_step_handler(message, send_welcome)

此选项可根据我的需要工作,但会导致https://ibb.co/LxtH64z错误。所以我还是不知道怎么处理这个问题

EN

回答 1

Stack Overflow用户

发布于 2022-07-11 10:37:04

处理程序函数只有在收到消息时才能工作。因此,您只注册"another_test_step",但不要在下一条消息之前调用它。您只需在yet2_test_step中手动调用此函数,并将after_key注册为next (这将执行您的after_key)。

代码语言:javascript
运行
复制
import telebot
from telebot import types

API_TOKEN = 'place_token_here'
bot = telebot.TeleBot(API_TOKEN)

@bot.message_handler(func=lambda m: True)
def send_welcome(message):
    msg = bot.send_message(message.from_user.id,  """\
Hi there, I am Example bot.
What's your name?
""", allow_sending_without_reply=True)
    bot.register_next_step_handler(msg, another_test_step)

def another_test_step(message):
    markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
    markup.add('1', '2', '3')
    bot.send_message(message.from_user.id, 'Choose your number', reply_markup=markup, allow_sending_without_reply=True)
    bot.register_next_step_handler(message, after_key)

def after_key(message):
    message = bot.send_message(message.from_user.id, 'You chose ' + message.text + '. Do you want to repeat?')
    bot.register_next_step_handler(message, yet2_test_step)


def yet2_test_step(message):
    if message.text == "yes":
        # CHANGES HERE
        another_test_step(message)
    elif message.text == 'no':
        bot.register_next_step_handler(message, send_welcome)


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

https://stackoverflow.com/questions/72936843

复制
相关文章

相似问题

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