Skip to content

telegram_bot: better logging when a message is not sent#8248

Merged
pvizeli merged 1 commit into
home-assistant:devfrom
azogue:telegram-log
Jun 29, 2017
Merged

telegram_bot: better logging when a message is not sent#8248
pvizeli merged 1 commit into
home-assistant:devfrom
azogue:telegram-log

Conversation

@azogue
Copy link
Copy Markdown
Member

@azogue azogue commented Jun 29, 2017

Description:

The telegram service parses the text messages for formatting in markup or html styles, but this is very prone to errors, and when they occur, the log is not much of help there. Other errors, like bad metadata args also will be better debugged with this change.

The error log now:

2017-06-29 15:28:53 ERROR (Thread-21) [homeassistant.components.telegram_bot] Error sending message
Traceback (most recent call last):
  File "/Users/uge/Dropbox/PYTHON/PYPROJECTS/contrib/hass/home-assistant/homeassistant/components/telegram_bot/__init__.py", line 430, in _send_msg
    out = func_send(*args_msg, **kwargs_msg)
  File "/Users/uge/Dropbox/PYTHON/PYPROJECTS/contrib/ha_test1/deps/telegram/bot.py", line 52, in decorator
    result = func(self, *args, **kwargs)
  File "/Users/uge/Dropbox/PYTHON/PYPROJECTS/contrib/ha_test1/deps/telegram/bot.py", line 64, in decorator
    return self._message_wrapper(url, data, *args, **kwargs)
  File "/Users/uge/Dropbox/PYTHON/PYPROJECTS/contrib/ha_test1/deps/telegram/bot.py", line 156, in _message_wrapper
    result = self._request.post(url, data, timeout=kwargs.get('timeout'))
  File "/Users/uge/Dropbox/PYTHON/PYPROJECTS/contrib/ha_test1/deps/telegram/utils/request.py", line 252, in post
    **urlopen_kwargs)
  File "/Users/uge/Dropbox/PYTHON/PYPROJECTS/contrib/ha_test1/deps/telegram/utils/request.py", line 194, in _request_wrapper
    raise BadRequest(message)
telegram.error.BadRequest: Can't parse entities in message text: can't find end of the entity starting at byte offset 18

A better approach to help debugging the error, simply showing the args and kwargs for the failed message:

2017-06-29 15:28:53 ERROR (Thread-21) [homeassistant.components.telegram_bot] Error sending message: Can't parse entities in message text: can't find end of the entity starting at byte offset 18. Args: (-12345678, '*Asterisks* go in *pairs\nThe _underscores_ also go in _pairs, so this is going to fail.'), kwargs: {'reply_to_message_id': None, 'parse_mode': 'Markdown', 'disable_web_page_preview': None, 'disable_notification': False, 'timeout': None, 'reply_markup': None}

Example entry for configuration.yaml (if applicable):

telegram_bot:
  platform: broadcast
  api_key: !secret telegram_bot_api_key
  allowed_chat_ids:
    - -12345678

notify:
  - platform: telegram
    name: telegram_bot
    chat_id: -12345678

script:
  test_bad_parsing:
    alias: Try to send a wrong formatted message
    sequence:
      - service: notify.telegram_bot
        data_template:
          title: "*Asterisks* go in *pairs"
          message: "The _underscores_ also go in _pairs, so this is going to fail."

Checklist:

If the code communicates with devices, web services, or third-party tools:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass

@mention-bot
Copy link
Copy Markdown

@azogue, thanks for your PR! By analyzing the history of the files in this pull request, we identified @fabaff, @sander76 and @pvizeli to be potential reviewers.

@pvizeli pvizeli merged commit 1fb4eef into home-assistant:dev Jun 29, 2017
@balloob balloob mentioned this pull request Jul 1, 2017
@azogue azogue deleted the telegram-log branch July 30, 2017 09:33
dethpickle pushed a commit to dethpickle/home-assistant that referenced this pull request Aug 18, 2017
@home-assistant home-assistant locked and limited conversation to collaborators Dec 11, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants