【问题标题】:pyTelegramBotAPI(telebot) exit from while by /stop commandpyTelegramBotAPI(telebot) 通过 /stop 命令退出 while
【发布时间】:2020-08-12 11:09:15
【问题描述】:

我编写了一个机器人来检查 snmp 上的负载平均值,使用以下命令:snmpwalk -v3 -l authNoPriv -u admin -a SHA -A "admin" ' + server + ' .1.3.6.1.4.1.2021.10.1.3 .1.

而且我无法通过 stop 命令退出 while 循环。

bot = telebot.TeleBot('xxxxx')

@bot.message_handler(commands=['start'])
def start(message):
    sent = bot.send_message(message.chat.id, 'Please input server (example 192.168.0.1)')
    bot.register_next_step_handler(sent, cores)

def cores(message):
    global server
    server = (message.text)
    sent2 = bot.send_message(message.chat.id, 'Input number of cores')
    bot.register_next_step_handler(sent2, botstarted)

def botstarted(message):
    global core
    global bye
    core = (message.text)
    cores = float(core)
    bot.send_message(message.chat.id, 'Check started, if load average more then number of cores you get Warning message to stop enter /stop')
    while (True):
        b = os.popen('snmpwalk -v3 -l authNoPriv -u admin -a SHA -A "admin" ' + server + ' .1.3.6.1.4.1.2021.10.1.3.1').read()
        d = (b[39:43])
        c = float(b[39:43])
        if c > cores:
            bot.send_message(message.chat.id, 'Warning, huge load average: ' + d)
        print(c)
        time.sleep(10)

@bot.message_handler(commands=['stop'])
def stop(message):
    sent3 = bot.send_message(message.chat.id, 'bye')
    print(sent3)

bot.polling(none_stop=True)

【问题讨论】:

    标签: python while-loop py-telegram-bot-api


    【解决方案1】:
    bot.stop_polling()  but this = exit
    

    【讨论】:

    • 用更多细节扩展你的答案是值得的。通常只有代码的答案虽然可能是正确的,但被否决了。
    猜你喜欢
    • 2023-02-24
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 2017-11-22
    • 2020-01-29
    • 1970-01-01
    相关资源
    最近更新 更多