Telegram-Bot 没有正确响应
Telegram-Bot does not respond correctly
我正在做一个电报机器人,我有一个合并,即除法不起作用,但如果起作用则添加,有人知道为什么吗?
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()
我的bot有两个功能,一个可以用,一个不行,结构一样,但是不生效。
我把我的令牌留给你,这样你就可以做测试了,如果你想要机器人的名字是:
@moha_m03_1bot
是'division'不是'dividir'不要尝试电报中的函数名称尝试'division'
botm3.add_handler(CommandHandler("Division", dividir))
它非常适合我
我认为命令的名称令人困惑:
- '/Sumar 2 2' -> La suma es 4
- '/Division 4 2' -> La division da 2.0
- '/Dividir 2 2' -> 不匹配任何命令
这些命令被称为 Sumar
和 Division
,也许你的意思是它们被称为 Sumar
和 Dividir
我正在做一个电报机器人,我有一个合并,即除法不起作用,但如果起作用则添加,有人知道为什么吗?
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()
我的bot有两个功能,一个可以用,一个不行,结构一样,但是不生效。 我把我的令牌留给你,这样你就可以做测试了,如果你想要机器人的名字是: @moha_m03_1bot
是'division'不是'dividir'不要尝试电报中的函数名称尝试'division'
botm3.add_handler(CommandHandler("Division", dividir))
它非常适合我
我认为命令的名称令人困惑:
- '/Sumar 2 2' -> La suma es 4
- '/Division 4 2' -> La division da 2.0
- '/Dividir 2 2' -> 不匹配任何命令
这些命令被称为 Sumar
和 Division
,也许你的意思是它们被称为 Sumar
和 Dividir