Search code examples
pythontelebot

how can i make this telegram bot calculator work


i am trying to make this calculator work but it automatically sends a message please provide a valid number when i run the command /multiply any help would be muchly appreciated

type here
import telebot

bot = telebot.TeleBot('my token ')

first_number = None
second_number = None

@bot.message_handler(commands=['multiply'])
def multiply(message):
    global first_number
    global second_number
    if first_number is None:
        try:
            first_number = int(message.text)
            bot.send_message(message.chat.id, 'Please provide the second number')
        except ValueError:
            bot.send_message(message.chat.id, 'Please provide a valid number')
    elif second_number is None:
        try:
            second_number = int(message.text)
            result = first_number * second_number
            bot.send_message(message.chat.id, 'Result: {}'.format(result))
            first_number = None
            second_number = None
        except ValueError:
            bot.send_message(message.chat.id, 'Please provide a valid number')

bot.polling()

i am trying to make it work


Solution

  • The problem with your code was:

    1. You were trying to convert the command message itself to integer.

    2. Your bot did only handle /multiply command but you also want to handle the further input messages too.

    Based on what you said in comments, you wanted the bot to interact in this way

    User types /multiply
    bot asks for first number
    user enter valid first number, if not prompt again to enter a valid number
    bot asks for second number, if not prompt again to enter a valid number
    first number * second number is displayed
    

    You can use another global variable to denote in which state your bot is. Whether it's in ready state to receive number inputs or not. Also this state is activated when user uses /multiply command and should be deactivated when a calculation is done.

    So you can do something like this:

    import telebot
    
    bot = telebot.TeleBot('my token')
    first_number = None
    second_number = None
    ready_to_recieve = False
    
    @bot.message_handler(commands=['multiply'])
    def multiply_command_handler(message):
        global ready_to_recieve
        if not ready_to_recieve:
            ready_to_recieve = True
            bot.send_message(message.chat.id, 'Please provide the first number')
        else:
            bot.send_message(message.chat.id, 'You have already requested for a calculation')
    
    @bot.message_handler(func=lambda m: ready_to_recieve)
    def multiply(message):
        global ready_to_recieve
        global first_number
        global second_number
    
        if first_number is None:
            try:
                first_number = int(message.text)
                bot.send_message(message.chat.id, 'Please provide the second number')
            except ValueError:
                bot.send_message(message.chat.id, 'Please provide a valid number')
        elif second_number is None:
            try:
                second_number = int(message.text)
                result = first_number * second_number
                bot.send_message(message.chat.id, 'Result: {}'.format(result))
                first_number = None
                second_number = None
                ready_to_recieve = False
            except ValueError:
                bot.send_message(message.chat.id, 'Please provide a valid number')
    bot.polling()
    

    @bot.message_handler(func=lambda m: ready_to_recieve) this will only get handled when ready_to_recieve is True.