Skip to content

Commit

Permalink
Respect Telegram max text length
Browse files Browse the repository at this point in the history
  • Loading branch information
twsl committed Feb 16, 2022
1 parent 90d71cf commit 770fb56
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions whos_there/senders/telegram.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import telegram
from telegram import Bot

from whos_there.senders.base import Sender

Expand All @@ -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)

0 comments on commit 770fb56

Please sign in to comment.