保留 python 电报机器人 运行

Keep python telegram bot running

我使用 python 和 pyTelegramBotAPI api 制作了一个电报机器人。问题是,当我 运行 我的机器人超过 5 分钟时,它会抛出一个错误 "conection reset by peer"。所以我决定进行无限轮询。无限轮询每 10 分钟停止工作 1 分钟投掷:

RuntimeError: maximum recursion depth exceeded

之后,它 运行s 又持续了 10 分钟,但出现此错误:

    2018-08-01 21:16:27,187 (util.py:65 PollingThread) ERROR - TeleBot: "ApiException occurred, args=('A request to the Telegram API was unsuccessful. The server returned HTTP 409 Conflict. Response
 body:\n[{"ok":false,"error_code":409,"description":"Conflict: terminated by other getUpdates request; make sure that only one bot instance is running"}]',)

如果我决定用电报发送一些东西,有时机器人会做出双重响应,似乎它们是 运行 两个机器人实例,但为什么呢?

有办法解决吗?也许 shell 脚本使用每 n 分钟或类似的时间安排来终止进程。

我也试过使用 heroku 部署但没有成功。

欢迎任何保持该机器人 运行ning 的想法。这是我的投票:

def principal():
    while True:
        try:
            bot.infinity_polling(True)
            bot.polling(none_stop=True)
        except:
            time.sleep(10)


principal()

您 运行 遇到的问题似乎是 pyTelegramBotAPI 包的固有问题——它们并非特定于您的代码。其他用户遇到了同样的问题:

这些问题似乎是图书馆固有的,图书馆的作者似乎没有解决。您最好的选择可能是使用另一个库。如果做不到这一点,一些用户报告说禁用线程可能会有所帮助。