繁体   English   中英

如何编写/ back命令Python电报Bot?

[英]How to code /back command Python telegram bot?

我正在开发一个电报机器人,该机器人可以在导航场景中询问不同的问题,并且希望用户能够随时返回到宝贵的一步。 我想出一个位置值,它是一个简单的变量。 但这没有用,我没有其他想法,可能有人可以查看我的代码并弄清楚我做错了什么,或者有其他解决方案吗?

先感谢您!

import telebot

bot = telebot.TeleBot("Token")

@bot.message_handler(commands=['start'])
def handle_start(message):
   menu = telebot.types.ReplyKeyboardMarkup(True, False)
   menu.row('Chat')
   menu.row('Not today')

   bot.send_message(message.from_user.id, 'Hi there!', reply_markup=menu)
   position = 0
   return (position)

@bot.message_handler(content_types=['text'])
def handle_text(message):
   if message.text == 'Chat':
      main_menu_1 = telebot.types.ReplyKeyboardMarkup(True, False)
      main_menu_1.row('Good')
      main_menu_1.row('Bad')

      bot.send_message(message.from_user.id, 'How are you today?', reply_markup=main_menu_1)
      position = 1.1
      return (position)

   elif message.text == 'Good':
      sub_menu_1_1 = telebot.types.ReplyKeyboardMarkup(True, False)
      sub_menu_1_1.row('How are you?')
      sub_menu_1_1.row('Bye')

      bot.send_message(message.from_user.id, 'I am glad for you', reply_markup=sub_menu_1_1)
      position = 2.1
      return (position)

   elif message.text == 'Bad':
      sub_menu_1_2 = telebot.types.ReplyKeyboardMarkup(True, False)
      sub_menu_1_2.row('Alright')
      sub_menu_1_2.row('Why?')

      bot.send_message(message.from_user.id, 'I dont care)', reply_markup=sub_menu_1_2)
      position = 2.2
      return (position)

   elif message.text == 'Not today':
      main_menu_2 = telebot.types.ReplyKeyboardMarkup(True, False)
      main_menu_2.row('Not in a mood')
      main_menu_2.row('You are just a machine')

      bot.send_message(message.from_user.id, 'Why not?', reply_markup=main_menu_2)
      position = 1.2
      return (position)

   elif message.text == 'Not in a mood':
      sub_menu_2_1 = telebot.types.ReplyKeyboardMarkup(True, False)
      sub_menu_2_1.row('Its all good')
      sub_menu_2_1.row('See you')

      bot.send_message(message.from_user.id, 'Sorry to hear that(', reply_markup=sub_menu_2_1)
      position = 2.4
      return (position)

   elif message.text == 'You are just a machine':
      sub_menu_2_2 = telebot.types.ReplyKeyboardMarkup(True, False)
      sub_menu_2_2.row('Not funny')
      sub_menu_2_2.row('Funny')

      bot.send_message(message.from_user.id, 'You are just a human', reply_markup=sub_menu_2_2)
      position = 2.5
      return (position)

@bot.message_handler(commands=['back'])
def handle_back(message):
   if position.handle_text == 0:
      bot.send_message(message.from_user.id, 'You are in the main menu already', reply_markup=main_menu)
   elif 1 >= position < 2:
    bot.send_message(message.from_user.id, 'You have returned to the main menu',reply_markup=main_menu)
   elif 2 >= position < 3:
    bot.send_message(message.from_user.id, 'You have returned to a submenu', reply_markup=main_flavor_menu)

bot.polling(none_stop=True)

代替使用back命令,您必须使用back按钮并以这种方式重写代码逻辑

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM