0

我的电报机器人由一个 telepot 组成DelegatorBot。当互联网连接中断时,telepot 会通知,抛出错误,捕获它(所以我假设)并重试。
我怎样才能注意到发生了错误并相应地记录,而不仅仅是将标准输出重定向到文件?

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/telepot/loop.py", line 60, in run_forever
    allowed_updates=allowed_updates)
  File "/usr/local/lib/python2.7/dist-packages/telepot/__init__.py", line 993, in getUpdates
    return self._api_request('getUpdates', _rectify(p))
  File "/usr/local/lib/python2.7/dist-packages/telepot/__init__.py", line 491, in _api_request
    return api.request((self._token, method, params, files), **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/telepot/api.py", line 154, in request
    r = fn(*args, **kwargs)  # `fn` must be thread-safe
  File "/usr/local/lib/python2.7/dist-packages/urllib3/request.py", line 148, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "/usr/local/lib/python2.7/dist-packages/urllib3/poolmanager.py", line 321, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "/usr/local/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 668, in urlopen
    **response_kw)
  File "/usr/local/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 668, in urlopen
    **response_kw)
  File "/usr/local/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 668, in urlopen
    **response_kw)
  File "/usr/local/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/local/lib/python2.7/dist-packages/urllib3/util/retry.py", line 388, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
MaxRetryError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot516341178:AAFcm4EYHvQerSCmzhcRhngEEd0he2GF07Q/getUpdates (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x75c26c90>: Failed to establish a new connection: [Errno -2] Name or service not known',))

文档似乎没有涵盖MaxRetryError

4

2 回答 2

0

我不知道你是否解决了你的问题,但我昨天还在做类似的事情。

我得到错误的代码是:

try:
    browserSelenium.startThreadBrowserStartLiking(chat_id, hoursToCatchUp)
    ex = ''
except Exception as e:
    print(e)
    ex = '%s\nIF YOU SAW THIS MESSAGE, PLEASE COPY THIS TEXT AND CONTACT AN ADMIN!!!' % e

当我发送一条消息时,我总是发送ex到消息的末尾。

except您可以使用类似的东西将通知消息实现到

bot.sendMessage(chat_id, 'Error: \n%s' % ex)
于 2019-03-22T09:49:25.597 回答
0

每当发生错误时,我可能会尝试仅使用try except分句向自己发送带有调试文本的 Telegram 消息。我的意思是,我们已经在 Telegram 上了,对吧?

try:
    # whatever may cause an error
except:
    bot.sendMessage("YOUR_USER_ID","THE_ERROR_MESSAGE")
于 2019-07-22T14:49:16.560 回答