From 770fb56a339397480076898eff3bdf1415ac0558 Mon Sep 17 00:00:00 2001 From: twsl <45483159+twsl@users.noreply.github.com> Date: Wed, 16 Feb 2022 20:32:08 +0000 Subject: [PATCH] Respect Telegram max text length --- whos_there/senders/telegram.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/whos_there/senders/telegram.py b/whos_there/senders/telegram.py index 137f6dcf..1d1b6bcb 100644 --- a/whos_there/senders/telegram.py +++ b/whos_there/senders/telegram.py @@ -1,4 +1,4 @@ -import telegram +from telegram import Bot from whos_there.senders.base import Sender @@ -14,13 +14,16 @@ def __init__(self, token: str, chat_id: int) -> None: super().__init__() self.token = token self.chat_id = chat_id - self._bot: telegram.Bot = None + self._bot: Bot = None @property def bot(self): if not self._bot: - self._bot = telegram.Bot(token=self.token) + self._bot = Bot(token=self.token) return self._bot def send(self, text: str) -> None: - return self.bot.send_message(chat_id=self.chat_id, text=text) + length = 4096 + chunks = [text[0 + i : length + i] for i in range(0, len(text), length)] + for chunk in chunks: + self.bot.send_message(chat_id=self.chat_id, text=chunk)