【问题标题】:Telegram-Bot does not respond correctlyTelegram-Bot 未正确响应
【发布时间】:2020-09-15 19:54:06
【问题描述】:

我正在做一个电报机器人,我有一个合并,即拆分不起作用但如果它起作用则添加,有​​人知道为什么吗?

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

【问题讨论】:

  • def sumar = add like 5 + 5
  • dividr = 除以 100 / 2

标签: python logging bots telegram telegram-bot


【解决方案1】:

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

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

它对我来说非常有用

【讨论】:

  • 不适合我。并知道为什么
【解决方案2】:

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

  • '/Sumar 2 2' -> La suma es 4
  • '/Division 4 2' -> La Division da 2.0
  • '/Dividir 2 2' -> 不匹配任何命令

这些命令被称为Sumar 和Division,也许你的意思是它们被称为Sumar 和Dividir

【讨论】:

    猜你喜欢
    • 2016-07-15
    • 1970-01-01
    • 2021-10-03
    • 2018-11-19
    • 2016-10-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2017-01-05
    相关资源
    最近更新 更多