Skip to content

Commit 04a871a

Browse files
committed
introduce constants module (python-telegram-bot#342)
1 parent 81a755a commit 04a871a

File tree

4 files changed

+130
-1
lines changed

4 files changed

+130
-1
lines changed

docs/source/telegram.constants.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
telegram.constants module
2+
=========================
3+
4+
.. automodule:: telegram.constants
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:

telegram/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@
8080
from .inputcontactmessagecontent import InputContactMessageContent
8181
from .update import Update
8282
from .bot import Bot
83+
from .constants import (MAX_MESSAGE_LENGTH, MAX_CAPTION_LENGTH, SUPPORTED_WEBHOOK_PORTS,
84+
MAX_FILESIZE_DOWNLOAD, MAX_FILESIZE_UPLOAD,
85+
MAX_MESSAGES_PER_SECOND_PER_CHAT, MAX_MESSAGES_PER_SECOND,
86+
MAX_MESSAGES_PER_MINUTE_PER_GROUP)
8387

8488
__author__ = 'devs@python-telegram-bot.org'
8589
__version__ = '4.3.4'
@@ -99,7 +103,9 @@
99103
'KeyboardButton', 'Location', 'Message', 'MessageEntity', 'NullHandler', 'ParseMode',
100104
'PhotoSize', 'ReplyKeyboardHide', 'ReplyKeyboardMarkup', 'ReplyMarkup', 'Sticker',
101105
'TelegramError', 'TelegramObject', 'Update', 'User', 'UserProfilePhotos', 'Venue',
102-
'Video', 'Voice']
106+
'Video', 'Voice', 'MAX_MESSAGE_LENGTH', 'MAX_CAPTION_LENGTH', 'SUPPORTED_WEBHOOK_PORTS',
107+
'MAX_FILESIZE_DOWNLOAD', 'MAX_FILESIZE_UPLOAD', 'MAX_MESSAGES_PER_SECOND_PER_CHAT',
108+
'MAX_MESSAGES_PER_SECOND', 'MAX_MESSAGES_PER_MINUTE_PER_GROUP']
103109

104110
if version_info < (2, 7):
105111
from warnings import warn

telegram/constants.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# python-telegram-bot - a Python interface to the Telegram Bot API
2+
# Copyright (C) 2015-2016
3+
# by the python-telegram-bot contributors <devs@python-telegram-bot.org>
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU Lesser Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU Lesser Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Lesser Public License
16+
# along with this program. If not, see [http://www.gnu.org/licenses/].
17+
"""Constants in the Telegram network.
18+
19+
Attributes:
20+
MAX_MESSAGE_LENGTH (int): from
21+
https://core.telegram.org/method/messages.sendMessage#return-errors
22+
MAX_CAPTION_LENGTH (int): from https://core.telegram.org/bots/api#sendphoto
23+
24+
The following constants were extracted from the
25+
`Telegram Bots FAQ <https://core.telegram.org/bots/faq>`_.
26+
27+
Attributes:
28+
SUPPORTED_WEBHOOK_PORTS (List[int])
29+
MAX_FILESIZE_DOWNLOAD (int): In bytes.
30+
MAX_FILESIZE_UPLOAD (int): Official limit, the actual limit can be a bit higher.
31+
MAX_MESSAGES_PER_SECOND_PER_CHAT (int): Telegram may allow short bursts that go over this
32+
limit, but eventually you'll begin receiving 429 errors.
33+
MAX_MESSAGES_PER_SECOND (int)
34+
MAX_MESSAGES_PER_MINUTE_PER_GROUP (int)
35+
"""
36+
37+
MAX_MESSAGE_LENGTH = 4096
38+
MAX_CAPTION_LENGTH = 200
39+
40+
# constants above this line are tested
41+
42+
SUPPORTED_WEBHOOK_PORTS = [443, 80, 88, 8443]
43+
MAX_FILESIZE_DOWNLOAD = int(20E6) # (20MB)
44+
MAX_FILESIZE_UPLOAD = int(50E6) # (50MB)
45+
MAX_MESSAGES_PER_SECOND_PER_CHAT = 1
46+
MAX_MESSAGES_PER_SECOND = 30
47+
MAX_MESSAGES_PER_MINUTE_PER_GROUP = 20

tests/test_constants.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# python-telegram-bot - a Python interface to the Telegram Bot API
2+
# Copyright (C) 2015-2016
3+
# by the python-telegram-bot contributors <devs@python-telegram-bot.org>
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU Lesser Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU Lesser Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Lesser Public License
16+
# along with this program. If not, see [http://www.gnu.org/licenses/].
17+
"""Test the Telegram constants."""
18+
19+
import sys
20+
21+
from flaky import flaky
22+
23+
if sys.version_info[0:2] == (2, 6):
24+
import unittest2 as unittest
25+
else:
26+
import unittest
27+
28+
sys.path.append('.')
29+
30+
import telegram
31+
from telegram.error import BadRequest
32+
from tests.base import BaseTest, timeout
33+
34+
35+
class ConstantsTest(BaseTest, unittest.TestCase):
36+
37+
@flaky(3, 1)
38+
@timeout(10)
39+
def testMaxMessageLength(self):
40+
self._bot.sendMessage(chat_id=self._chat_id,
41+
text='a' * telegram.constants.MAX_MESSAGE_LENGTH)
42+
43+
try:
44+
self._bot.sendMessage(chat_id=self._chat_id,
45+
text='a' * (telegram.constants.MAX_MESSAGE_LENGTH + 1))
46+
except BadRequest as e:
47+
err = str(e)
48+
49+
self.assertTrue("too long" in err) # BadRequest: 'Message is too long'
50+
51+
@flaky(3, 1)
52+
@timeout(10)
53+
def testMaxCaptionLength(self):
54+
self._bot.sendPhoto(photo=open('tests/data/telegram.png', 'rb'),
55+
caption='a' * telegram.constants.MAX_CAPTION_LENGTH,
56+
chat_id=self._chat_id)
57+
58+
try:
59+
self._bot.sendPhoto(photo=open('tests/data/telegram.png', 'rb'),
60+
caption='a' * (telegram.constants.MAX_CAPTION_LENGTH + 1),
61+
chat_id=self._chat_id)
62+
except BadRequest as e:
63+
err = str(e)
64+
65+
self.assertTrue("TOO_LONG" in err) # BadRequest: 'MEDIA_CAPTION_TOO_LONG'
66+
67+
68+
if __name__ == '__main__':
69+
unittest.main()

0 commit comments

Comments
 (0)
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