簡體   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