Skip to content

Instantly share code, notes, and snippets.

@behf
Last active May 14, 2020 14:59
Show Gist options
  • Save behf/922c932017878112b528ef64684a67e7 to your computer and use it in GitHub Desktop.
Save behf/922c932017878112b528ef64684a67e7 to your computer and use it in GitHub Desktop.
split a long text into small chunks, it won't cut your words.
def split_message(text, length=4096, offset=200):
return [text[text.find('\n', i - offset, i + 1) if text.find('\n', i - offset, i + 1) != -1 else i:
text.find('\n', i + length - offset, i + length) if text.find('\n', i + length - offset,
i + length) != -1 else i + length] for
i
in
range(0, len(text), length)]
# Usage, Using Pyrogram
text = 'some really long text'
for m in split_message(text):
app.send_message('me', m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment