简体   繁体   English

如何在电报错误时得到通知?

[英]How to be notified on telepot error?

My telegram bot consists of a telepot DelegatorBot . 我的电报机器人由telepot的DelegatorBot When the internet connection goes down, telepot notices, throws an error, catches it (so I assume) and retries. 当互联网连接中断时,电报站会发出通知,引发错误,然后捕获错误(我假设是这样),然后重试。
How can I notice that an error happened and log accordingly, without just redirecting stdout to a file? 我如何注意到发生了错误并相应地进行记录,而不仅仅是将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',))

The docs don't seem to cover MaxRetryError 该文档似乎没有涵盖MaxRetryError

I don't know if you solved your issue but I was working just yesterday on something similar. 我不知道您是否解决了您的问题,但我昨天才开始处理类似的问题。

my code to get the error is : 我得到错误的代码是:

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

and when I sant a message I alway send ex to de end of the message. 当我朗读一条消息时,我总是发送ex到消息的结尾。

You could implement the notification message into the except with something like 你可以实现通知消息到except与像

bot.sendMessage(chat_id, 'Error: \n%s' % ex)

I'd maybe try to just use a try except clausule to send myself a Telegram message with the debugging text whenever the error happens. 每当发生错误时,我可能都会尝试使用clausule try except方式向自己发送带有调试文本的Telegram消息。 I mean, we're already on Telegram, huh? 我的意思是,我们已经在使用Telegram,是吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM