Telegram bot continues to process the request instead of restarting

20 Views Asked by At

There is a working Telegram bot that sends a greeting and offers to submit a request. It sends the result to a specific user in a Telegram chat.

However, if during the request submission process, the user stops 'halfway', clears the history, and restarts the bot with the /start command, the bot does not start from the beginning but continues to process the request.

Also, if the user has submitted a request, there is no possibility to submit a new one.

Please help adjust the code so that if /start is pressed while the bot is performing a task, it starts from the beginning.

And so that when a request is created and sent, there is the possibility to immediately create a new request.

import telebot
from telebot import types

TOKEN = 'TOKENFROMBOTFATHER'
bot = telebot.TeleBot(TOKEN)


user2_chat_id = 'IDFROMCHAT'

class Application:
    @staticmethod
    def display(message):
        bot.send_message(message.chat.id, "Your name:")
        bot.register_next_step_handler(message, process_name_step)

def process_name_step(message):
    global name
    name = message.text
    chat_id = message.chat.id

    markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
    leftside = types.KeyboardButton('ADRESS1')
    rightside = types.KeyboardButton('ADRESS2')
    markup.add(leftside, rightside)

    bot.send_message(message.chat.id, "Choose your adress:", reply_markup=markup)

    bot.register_next_step_handler(message, process_bereg_step)

def process_bereg_step(message):
    global bereg
    chat_id = message.chat.id
    bereg = message.text

    markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
    DHC = types.KeyboardButton('SERVICE1')
    TO = types.KeyboardButton('SERVICE2')
    comp = types.KeyboardButton('SERVICE3')
    buy = types.KeyboardButton('SERVICE4')
    GRM = types.KeyboardButton('SERVICE5')
    doos = types.KeyboardButton('SERVICE6')
    inshe = types.KeyboardButton('SERVICE7')
    markup.add(DHC, TO, comp, buy, GRM, doos, inshe)
    
    bot.send_message(message.chat.id, "Choose your service:", reply_markup=markup)
    bot.register_next_step_handler(message, process_posluga_step)

def process_posluga_step(message):
    global posluga
    chat_id = message.chat.id
    posluga = message.text
    bot.send_message(chat_id, "Your phone number:")
    bot.register_next_step_handler(message, process_phone_step)

def process_phone_step(message):
    global phone
    chat_id = message.chat.id
    phone = message.text
    bot.send_message(chat_id, "Your tech code:")
    bot.register_next_step_handler(message, process_text_step)

def process_text_step(message):
    chat_id = message.chat.id
    text = message.text
    bot.send_message(chat_id, f"Thanks. We'll call you")
    
    bot.send_message(user2_chat_id, f"Name: {name}\n\nAdress: {bereg}\n\nService: {posluga}\n\nPhone: {phone}\n\nCode: {text}")



@bot.message_handler(commands=['start'])
def send_welcome(message):

    photo = open('HelloPicture.jpg', 'rb')
    bot.send_photo(message.chat.id, photo)


   
    bot.send_message(message.chat.id, f"HELLO TEXT", parse_mode="HTML")


    markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
    itembtn1 = types.KeyboardButton('Make request')
    markup.add(itembtn1)

    bot.send_message(message.chat.id, "Push the button for request", reply_markup=markup)

@bot.message_handler(func=lambda message: True)
def echo_all(message):
    if message.text == 'Make request':
        Application.display(message)


bot.polling(none_stop=True)

If /start is pressed while the bot is performing a task, it starts from the beginning.

0

There are 0 best solutions below