skip to Main Content

I am doing a telegram bot, I have a merger that is to divide that does not work but to add if it does, does anyone know why?

import logging

from telegram.ext import Updater, CommandHandler, MessageHandler, Filters

# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)

logger = logging.getLogger(__name__)

def start(update, context):
    update.message.reply_text('Hola!')


def help(update, context):
    update.message.reply_text('Help!')

def sumar(update, context):
    try:
        numero1 = int(context.args[0])
        numero2 = int(context.args[1])

        suma = numero1 + numero2
        update.message.reply_text('La suma es '+str(suma))

    except (IndexError, ValueError):
        update.message.reply_text('Por favor utiliza dos numeros')    

def dividir(update, context):
    try:
        numero1 = int(context.args[0])
        numero2 = int(context.args[1])

        div= numero1 / numero2
        update.message.reply_text('La division da '+str(div))

    except (IndexError, ValueError):
        update.message.reply_text('Por favor utiliza dos numeros')

def echo(update, context):
    """Echo the user message."""
    update.message.reply_text(update.message.text)


def error(update, context):
    """Log Errors caused by Updates."""
    logger.warning('Update "%s" caused error "%s"', update, context.error)


def main():
    """Start the bot."""
    # Create the Updater and pass it your bot's token.
    # Make sure to set use_context=True to use the new context based callbacks
    # Post version 12 this will no longer be necessary
    updater = Updater("1225696978:AAFsJYex51HMRbKL814tLJJPczJMu3nLlYY", use_context=True)

    # Get the dispatcher to register handlers
    botm3 = updater.dispatcher

    # on different commands - answer in Telegram
    botm3.add_handler(CommandHandler("start", start))
    botm3.add_handler(CommandHandler("help", help))
    botm3.add_handler(CommandHandler("Sumar", sumar))
    botm3.add_handler(CommandHandler("Division", dividir))

    # on noncommand i.e message - echo the message on Telegram
    botm3.add_handler(MessageHandler(Filters.text, echo))

    # log all errors
    botm3.add_error_handler(error)

    # Start the Bot
    updater.start_polling()

    # Run the bot until you press Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()


if __name__ == '__main__':
    main()

I have two functions for the bot, one works the other does not and they are the same structure but it does not take effect.
I leave you my token so you can do the tests if you want the name of the bot is:
@ moha_m03_1bot

2

Answers


  1. it is ‘division’ not ‘dividir’ don’t try the function name in telegram try ‘division’

    botm3.add_handler(CommandHandler("Division", dividir))
    

    It is working perfectly for me

    Login or Signup to reply.
  2. I think the name of the command is confusing:

    • ‘/Sumar 2 2’ -> La suma es 4
    • ‘/Division 4 2’ -> La division da 2.0
    • ‘/Dividir 2 2’ -> doesnt match any command

    The commands are called Sumar and Division, maybe you meant them to be called Sumar and Dividir

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search