1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Added split_string to apihelper.py

Added a warning to send_message to not exceed 5000 characters per message.
Changed send_message to use HTTP POST instead of GET, to stretch the maximum character limit a little more.
This commit is contained in:
pieter 2015-07-25 20:59:36 +02:00
parent 97f2c0c110
commit 20e3f731f7
2 changed files with 17 additions and 1 deletions

View File

@ -188,6 +188,10 @@ class TeleBot:
def send_message(self, chat_id, text, disable_web_page_preview=None, reply_to_message_id=None, reply_markup=None):
"""
Use this method to send text messages.
Warning: Do not send more than about 5000 characters each message, otherwise you'll risk an HTTP 414 error.
If you must send more than 5000 characters, use the split_string function in apihelper.py.
:param chat_id:
:param text:
:param disable_web_page_preview:

View File

@ -56,7 +56,7 @@ def send_message(token, chat_id, text, disable_web_page_preview=None, reply_to_m
payload['reply_to_message_id'] = reply_to_message_id
if reply_markup:
payload['reply_markup'] = _convert_markup(reply_markup)
return _make_request(token, method_url, params=payload)
return _make_request(token, method_url, params=payload, method='post')
def get_updates(token, offset=None, limit=None, timeout=None):
@ -179,6 +179,18 @@ def extract_command(text):
"""
return text.split()[0].split('@')[0][1:] if is_command(text) else None
def split_string(text, chars_per_string):
"""
Splits one string into multiple strings, with a maximum amount of `chars_per_string` characters per string.
This is very useful for splitting one giant message into multiples.
:param text: The text to split
:param chars_per_string: The number of characters per line the text is split into.
:return: The splitted text as a list of strings.
"""
return [text[i:i + chars_per_string] for i in range(0, len(text), chars_per_string)]
class ApiException(Exception):
"""
This class represents an Exception thrown when a call to the Telegram API fails.