Fix #49. Introducing Telegram message length limit and trimming descriptions which exceed it.
This commit is contained in:
parent
20acceed8c
commit
aedf80a4df
14
telegram.py
14
telegram.py
|
@ -108,6 +108,8 @@ class Notifier:
|
||||||
|
|
||||||
# https://core.telegram.org/bots/faq#my-bot-is-hitting-limits-how-do-i-avoid-this
|
# https://core.telegram.org/bots/faq#my-bot-is-hitting-limits-how-do-i-avoid-this
|
||||||
BATCH_LIMIT: int = 25
|
BATCH_LIMIT: int = 25
|
||||||
|
# https://core.telegram.org/bots/api#sendmessage
|
||||||
|
MESSAGE_LENGTH_LIMIT: int = 4096
|
||||||
|
|
||||||
sent_counter: int = 0
|
sent_counter: int = 0
|
||||||
|
|
||||||
|
@ -167,12 +169,20 @@ class Notifier:
|
||||||
if item.date is not None:
|
if item.date is not None:
|
||||||
date_string = item.date.strftime('%m.%d.%Y %H:%M')
|
date_string = item.date.strftime('%m.%d.%Y %H:%M')
|
||||||
|
|
||||||
return (
|
header = (
|
||||||
f"<strong><a href=\"{item.url}\">{item.title}</a></strong>\n"
|
f"<strong><a href=\"{item.url}\">{item.title}</a></strong>\n"
|
||||||
f"{date_string}\n\n"
|
f"{date_string}\n\n"
|
||||||
f"{self.__sanitize_html(item.description)}"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
sanitized_description = self.__sanitize_html(item.description)
|
||||||
|
|
||||||
|
if len(sanitized_description) > (self.MESSAGE_LENGTH_LIMIT - len(header)):
|
||||||
|
cut = '<...>'
|
||||||
|
trim_index = self.MESSAGE_LENGTH_LIMIT - len(header) - len(cut) - 1
|
||||||
|
sanitized_description = sanitized_description[:trim_index] + cut
|
||||||
|
|
||||||
|
return header + sanitized_description
|
||||||
|
|
||||||
def __sanitize_html(self, html: str) -> str:
|
def __sanitize_html(self, html: str) -> str:
|
||||||
if not html:
|
if not html:
|
||||||
return ''
|
return ''
|
||||||
|
|
Loading…
Reference in a new issue