【问题标题】:How to handle the error of enabling telegram bot?如何处理启用电报机器人的错误?
【发布时间】:2021-08-21 03:12:37
【问题描述】:

我有一个电报机器人,但随着时间的推移或在启动后立即出现连接错误。为了编写机器人,我使用“telebot”库。

requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bottoken/getUpdates?offset=1&timeout=20 (Caused by ConnectTimeoutError(<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x02FB6910>, 'Connection to api.telegram.org timed out. (connect timeout=3.5)'))

如何处理这个错误并尝试开启机器人直到它开启?

【问题讨论】:

  • try/except 并在 except 块中捕获此特定异常,您可以启动您的机器人
  • 应该是吗? res_connect=False while res_connect!=True: try: res_connect=False bot = telebot.TeleBot(token) except Exception as err: res_connect=False
  • 我不确定电报机器人是如何工作的,但只要连接打开,你就不需要这个。但是,请查看下面的答案以获取 try catch 建议。

标签: python python-3.x telegram


【解决方案1】:

尝试以下操作 - 使用 requests.eceptions 中的 ConnectionError

from requests.exceptions import ConnectionError

try:
   Bot working and doing stuff
except ConnectionError as e:
   log e
   start Bot()

【讨论】:

    猜你喜欢
    • 2018-03-31
    • 1970-01-01
    • 2015-12-12
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    相关资源
    最近更新 更多