Merge pull request 'Fix #49 - message length limit error' (#50) from fix_49 into master
All checks were successful
continuous-integration/drone/push Build is passing

Reviewed-on: #50
Reviewed-by: Miroslavsckaya <miroslavsckaya@noreply.git.skobk.in>
This commit is contained in:
Miroslavsckaya 2023-07-01 18:23:14 +00:00
commit e66c5a7306

View file

@ -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 ''