Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何处理启用电报bot的错误?_Python_Python 3.x_Telegram - Fatal编程技术网

Python 如何处理启用电报bot的错误?

Python 如何处理启用电报bot的错误?,python,python-3.x,telegram,Python,Python 3.x,Telegram,我有一个电报机器人,但随着时间的推移,或在启动后立即出现连接错误。要编写机器人,我使用“telebot”库 requests.exceptions.ConnectTimeout:HTTPSConnectionPool(host='api.telegram.org',port=443):url超过最大重试次数:/bottoken/getUpdates?offset=1&timeout=20(由ConnectTimeoutError(,'Connection to api.telegram.org

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

requests.exceptions.ConnectTimeout:HTTPSConnectionPool(host='api.telegram.org',port=443):url超过最大重试次数:/bottoken/getUpdates?offset=1&timeout=20(由ConnectTimeoutError(,'Connection to api.telegram.org timeout.(connect timeout=3.5')引起)

如何处理此错误并尝试打开bot直到其打开?

尝试以下操作-使用
请求中的
连接错误
。eceptions

from requests.exceptions import ConnectionError

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

try/except
并在except块中捕获此特定异常。您可以启动您的机器人程序,它应该是什么<代码>重新连接=错误,而重新连接=正确:try:res_connect=False bot=telebot.telebot(令牌)异常除外,因为err:res_connect=False
我不确定电报bot如何工作,但只要连接打开,您就不需要时间了。但是,请检查下面的答案,以获得“尝试捕捉”建议。
from requests.exceptions import ConnectionError

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