[英]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()
我有两个机器人功能,一个可以工作,另一个不能工作,它们的结构相同,但没有生效。 我把我的令牌留给你,如果你想要机器人的名字,你可以做测试:@ moha_m03_1bot
这是“除法”而不是“除法”不要尝试电报中的 function 名称尝试“除法”
botm3.add_handler(CommandHandler("Division", dividir))
它对我来说非常有用
我认为命令的名称令人困惑:
这些命令称为Sumar
和Division
,也许您的意思是它们被称为Sumar
和Dividir
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.