电报机器人没有正确响应

2024-04-29 16:13:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做一个电报机器人,我有一个合并,就是拆分不起作用的东西,但如果它起作用了,谁知道为什么

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()

我有两个机器人的功能,一个工作,另一个不工作,他们是相同的结构,但它不生效。 我将我的令牌留给您,以便您可以在需要时进行测试机器人的名称为: @moha_m03_1bot


Tags: thetextaddmessageloggingdefcontextupdate
2条回答

它是'division'而不是'dividir'不要尝试电报中的函数名尝试'division'

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

这对我来说非常有效

我认为命令的名称令人困惑:

  • “/Sumar 2 2”->;拉苏马群岛4号
  • “/第4部分2”->;洛杉矶分区da 2.0
  • “/Dividir 2 2”->;不匹配任何命令

这些命令被称为SumarDivision,也许您的意思是将它们称为SumarDividir

相关问题 更多 >