From 20e3f731f7f3d3ebb2d6cd6fcf1eee5946e8817c Mon Sep 17 00:00:00 2001 From: pieter Date: Sat, 25 Jul 2015 20:59:36 +0200 Subject: [PATCH 1/2] 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. --- telebot/__init__.py | 4 ++++ telebot/apihelper.py | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 5dcfe72..f4bad7f 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -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: diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 1450dbe..655b881 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -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. From f41cc8134aa999ca703c2b5945229d11298ef60b Mon Sep 17 00:00:00 2001 From: pieter Date: Sat, 25 Jul 2015 21:41:19 +0200 Subject: [PATCH 2/2] 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. And added build status to README for fun. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 6a2fdc5..ee54c63 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ A Python implementation for the Telegram Bot API. See [https://core.telegram.org/bots/api](https://core.telegram.org/bots/api) +[![Build Status](https://travis-ci.org/eternnoir/pyTelegramBotAPI.svg?branch=master)](https://travis-ci.org/eternnoir/pyTelegramBotAPI) + ## How to install Python 2 or Python 3 is required.