如何通知电视机错误?

时间:2018-03-19 08:35:08

标签: python telegram telepot

我的电报机器人包含电话DelegatorBot。当互联网连接断开,电话通知,发出错误,捕获它(我假设)并重试。
我怎么能注意到发生了错误并相应地记录,而不只是将stdout重定向到文件?

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',))

Read about Java's heap size似乎没有涵盖MaxRetryError

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

无论何时发生错误,我都可能尝试使用try except子句向自己发送带有调试文本的电报消息。我的意思是,我们已经在电报上了,是吧?

try:
    # whatever may cause an error
except:
    bot.sendMessage("YOUR_USER_ID","THE_ERROR_MESSAGE")