rahiel / telegram-send

Send messages and files over Telegram from the command-line.
https://www.rahielkasim.com/telegram-send/
GNU General Public License v3.0
938 stars 105 forks source link

Bug: can't find end of the entity #112

Closed frank42hh closed 1 year ago

frank42hh commented 2 years ago

Hi,

I just stumbled upon this possible bug (imho):

Traceback (most recent call last):
  File "/usr/local/bin/telegram-send", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.8/site-packages/telegram_send.py", line 107, in main
    send(messages=[message], conf=c, parse_mode=args.parse_mode, silent=args.silent, disable_web_page_preview=args.disable_web_page_preview)
  File "/usr/local/lib/python3.8/site-packages/telegram_send.py", line 218, in send
    send_message(m)
  File "/usr/local/lib/python3.8/site-packages/telegram_send.py", line 207, in send_message
    return bot.send_message(chat_id=chat_id, text=message, parse_mode=parse_mode, disable_notification=silent, disable_web_page_preview=disable_web_page_preview)
  File "/usr/local/lib/python3.8/site-packages/telegram/bot.py", line 130, in decorator
    result = func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/telegram/bot.py", line 522, in send_message
    return self._message(  # type: ignore[return-value]
  File "/usr/local/lib/python3.8/site-packages/telegram/bot.py", line 336, in _message
    result = self._post(endpoint, data, timeout=timeout, api_kwargs=api_kwargs)
  File "/usr/local/lib/python3.8/site-packages/telegram/bot.py", line 295, in _post
    return self.request.post(
  File "/usr/local/lib/python3.8/site-packages/telegram/utils/request.py", line 361, in post
    result = self._request_wrapper(
  File "/usr/local/lib/python3.8/site-packages/telegram/utils/request.py", line 279, in _request_wrapper
    raise BadRequest(message)
telegram.error.BadRequest: Can't parse entities: can't find end of the entity starting at byte offset 82

While markdown works fine ( echo "birthday Fred" | telegram-send ), the script throws the above exception when finding an uneven amount of markdown characters ( echo "birthday Fred [*1980]" | telegram-send ).

In a manual environment, one can circumvent this by quoting the asterisk ( echo "birthday Fred [\*1980]" | telegram-send ), but there are use cases where this is impossible.

For example, I'm fetching calendar entries per ical from a Google Calendar, and there are entries like "birthday Fred (*1980)" for example. The fetching script can't determine which one of the asterisks is to be quoted. But since the calendar entries are mostly generated, I can't add quotes there either.

So, is there any solution other than "Don't use asterisks in your input data"?

rahiel commented 1 year ago

You can only send valid markdown when using the markdown format (--format markdown), best for your case would be to send without any formatting enabled. If you format manually in your script try using html formatting (--format html).