From 66f2b0e4725efa37b45773e92c582c32a3904909 Mon Sep 17 00:00:00 2001 From: Rahiel Kasim Date: Sat, 9 Jul 2016 16:42:21 +0200 Subject: [PATCH 1/4] introduce constants module --- telegram/__init__.py | 3 +- telegram/constants.py | 23 ++++++++++++++ tests/test_constants.py | 69 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 telegram/constants.py create mode 100644 tests/test_constants.py diff --git a/telegram/__init__.py b/telegram/__init__.py index 4b1b74f1105..ba807fbe586 100644 --- a/telegram/__init__.py +++ b/telegram/__init__.py @@ -80,6 +80,7 @@ from .inputcontactmessagecontent import InputContactMessageContent from .update import Update from .bot import Bot +from .constants import MAX_MESSAGE_LENGTH, MAX_CAPTION_LENGTH __author__ = 'devs@python-telegram-bot.org' __version__ = '4.3.3' @@ -99,7 +100,7 @@ 'KeyboardButton', 'Location', 'Message', 'MessageEntity', 'NullHandler', 'ParseMode', 'PhotoSize', 'ReplyKeyboardHide', 'ReplyKeyboardMarkup', 'ReplyMarkup', 'Sticker', 'TelegramError', 'TelegramObject', 'Update', 'User', 'UserProfilePhotos', 'Venue', - 'Video', 'Voice'] + 'Video', 'Voice', 'MAX_MESSAGE_LENGTH', 'MAX_CAPTION_LENGTH'] if version_info < (2, 7): from warnings import warn diff --git a/telegram/constants.py b/telegram/constants.py new file mode 100644 index 00000000000..30c3e1bb3e9 --- /dev/null +++ b/telegram/constants.py @@ -0,0 +1,23 @@ +# python-telegram-bot - a Python interface to the Telegram Bot API +# Copyright (C) 2015-2016 +# by the python-telegram-bot contributors +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser Public License for more details. +# +# You should have received a copy of the GNU Lesser Public License +# along with this program. If not, see [http://www.gnu.org/licenses/]. +"""Constants in the Telegram network.""" + +# https://core.telegram.org/method/messages.sendMessage +MAX_MESSAGE_LENGTH = 4096 + +# https://core.telegram.org/bots/api#sendphoto +MAX_CAPTION_LENGTH = 200 diff --git a/tests/test_constants.py b/tests/test_constants.py new file mode 100644 index 00000000000..2fa1ac77f7d --- /dev/null +++ b/tests/test_constants.py @@ -0,0 +1,69 @@ +# python-telegram-bot - a Python interface to the Telegram Bot API +# Copyright (C) 2015-2016 +# by the python-telegram-bot contributors +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser Public License for more details. +# +# You should have received a copy of the GNU Lesser Public License +# along with this program. If not, see [http://www.gnu.org/licenses/]. +"""Test the Telegram constants.""" + +import sys + +from flaky import flaky + +if sys.version_info[0:2] == (2, 6): + import unittest2 as unittest +else: + import unittest + +sys.path.append('.') + +import telegram +from telegram.error import BadRequest +from tests.base import BaseTest, timeout + + +class ConstantsTest(BaseTest, unittest.TestCase): + + @flaky(3, 1) + @timeout(10) + def testMaxMessageLength(self): + self._bot.sendMessage(chat_id=self._chat_id, + text='a' * telegram.constants.MAX_MESSAGE_LENGTH) + + try: + self._bot.sendMessage(chat_id=self._chat_id, + text='a' * (telegram.constants.MAX_MESSAGE_LENGTH + 1)) + except BadRequest as e: + err = str(e) + + self.assertTrue("too long" in err) # BadRequest: 'Message is too long' + + @flaky(3, 1) + @timeout(10) + def testMaxCaptionLength(self): + self._bot.sendPhoto(photo=open('tests/data/telegram.png', 'rb'), + caption='a' * telegram.constants.MAX_CAPTION_LENGTH, + chat_id=self._chat_id) + + try: + self._bot.sendPhoto(photo=open('tests/data/telegram.png', 'rb'), + caption='a' * (telegram.constants.MAX_CAPTION_LENGTH + 1), + chat_id=self._chat_id) + except BadRequest as e: + err = str(e) + + self.assertTrue("TOO_LONG" in err) # BadRequest: 'MEDIA_CAPTION_TOO_LONG' + + +if __name__ == '__main__': + unittest.main() From 242de3c7afecd3f7b5fda4f14b63250d181cee92 Mon Sep 17 00:00:00 2001 From: Rahiel Kasim Date: Thu, 14 Jul 2016 18:57:49 +0200 Subject: [PATCH 2/4] more constants --- telegram/constants.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/telegram/constants.py b/telegram/constants.py index 30c3e1bb3e9..c6889d735ed 100644 --- a/telegram/constants.py +++ b/telegram/constants.py @@ -21,3 +21,14 @@ # https://core.telegram.org/bots/api#sendphoto MAX_CAPTION_LENGTH = 200 + +# constants above this line are tested + +MAX_MESSAGES_PER_SECOND = 30 +MAX_MESSAGES_PER_SECOND_PER_CHAT = 1 +MAX_MESSAGES_PER_MINUTE_PER_GROUP = 20 + +MAX_FILESIZE_UPLOAD = 50E6 # (50MB) +MAX_FILESIZE_DOWNLOAD = 20E6 # (20MB) + +SUPPORTED_WEBHOOK_PORTS = [443, 80, 88, 8443] From 02b505257899d31ad84978401e881dcbffbf693e Mon Sep 17 00:00:00 2001 From: Rahiel Kasim Date: Thu, 14 Jul 2016 21:23:10 +0200 Subject: [PATCH 3/4] document constants --- docs/source/telegram.constants.rst | 7 +++++++ telegram/constants.py | 25 ++++++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 docs/source/telegram.constants.rst diff --git a/docs/source/telegram.constants.rst b/docs/source/telegram.constants.rst new file mode 100644 index 00000000000..c14267b118f --- /dev/null +++ b/docs/source/telegram.constants.rst @@ -0,0 +1,7 @@ +telegram.constants module +========================= + +.. automodule:: telegram.constants + :members: + :undoc-members: + :show-inheritance: diff --git a/telegram/constants.py b/telegram/constants.py index c6889d735ed..4f98a53f29a 100644 --- a/telegram/constants.py +++ b/telegram/constants.py @@ -14,7 +14,19 @@ # # You should have received a copy of the GNU Lesser Public License # along with this program. If not, see [http://www.gnu.org/licenses/]. -"""Constants in the Telegram network.""" +"""Constants in the Telegram network. + +Attributes: + MAX_MESSAGE_LENGTH (int) + MAX_CAPTION_LENGTH (int) + SUPPORTED_WEBHOOK_PORTS (List[int]) + MAX_FILESIZE_DOWNLOAD (int) + MAX_FILESIZE_UPLOAD (int): Official limit, the actual limit can be a bit higher. + MAX_MESSAGES_PER_SECOND_PER_CHAT (int): Telegram may allow short bursts that go over this + limit, but eventually you'll begin receiving 429 errors. + MAX_MESSAGES_PER_SECOND (int) + MAX_MESSAGES_PER_MINUTE_PER_GROUP (int) +""" # https://core.telegram.org/method/messages.sendMessage MAX_MESSAGE_LENGTH = 4096 @@ -24,11 +36,10 @@ # constants above this line are tested -MAX_MESSAGES_PER_SECOND = 30 +# https://core.telegram.org/bots/faq +SUPPORTED_WEBHOOK_PORTS = [443, 80, 88, 8443] +MAX_FILESIZE_DOWNLOAD = int(20E6) # (20MB) +MAX_FILESIZE_UPLOAD = int(50E6) # (50MB) MAX_MESSAGES_PER_SECOND_PER_CHAT = 1 +MAX_MESSAGES_PER_SECOND = 30 MAX_MESSAGES_PER_MINUTE_PER_GROUP = 20 - -MAX_FILESIZE_UPLOAD = 50E6 # (50MB) -MAX_FILESIZE_DOWNLOAD = 20E6 # (20MB) - -SUPPORTED_WEBHOOK_PORTS = [443, 80, 88, 8443] From 08d56f3f9b5872b49f464d08a9220273300361f2 Mon Sep 17 00:00:00 2001 From: Rahiel Kasim Date: Thu, 14 Jul 2016 21:34:37 +0200 Subject: [PATCH 4/4] last docs changes --- telegram/constants.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/telegram/constants.py b/telegram/constants.py index 4f98a53f29a..338e3b90307 100644 --- a/telegram/constants.py +++ b/telegram/constants.py @@ -17,10 +17,16 @@ """Constants in the Telegram network. Attributes: - MAX_MESSAGE_LENGTH (int) - MAX_CAPTION_LENGTH (int) + MAX_MESSAGE_LENGTH (int): from + https://core.telegram.org/method/messages.sendMessage#return-errors + MAX_CAPTION_LENGTH (int): from https://core.telegram.org/bots/api#sendphoto + +The following constants were extracted from the +`Telegram Bots FAQ `_. + +Attributes: SUPPORTED_WEBHOOK_PORTS (List[int]) - MAX_FILESIZE_DOWNLOAD (int) + MAX_FILESIZE_DOWNLOAD (int): In bytes. MAX_FILESIZE_UPLOAD (int): Official limit, the actual limit can be a bit higher. MAX_MESSAGES_PER_SECOND_PER_CHAT (int): Telegram may allow short bursts that go over this limit, but eventually you'll begin receiving 429 errors. @@ -28,15 +34,11 @@ MAX_MESSAGES_PER_MINUTE_PER_GROUP (int) """ -# https://core.telegram.org/method/messages.sendMessage MAX_MESSAGE_LENGTH = 4096 - -# https://core.telegram.org/bots/api#sendphoto MAX_CAPTION_LENGTH = 200 # constants above this line are tested -# https://core.telegram.org/bots/faq SUPPORTED_WEBHOOK_PORTS = [443, 80, 88, 8443] MAX_FILESIZE_DOWNLOAD = int(20E6) # (20MB) MAX_FILESIZE_UPLOAD = int(50E6) # (50MB) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy