mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-21 22:56:38 +01:00
introduce constants module (#342)
This commit is contained in:
parent
81a755a7d8
commit
04a871aff5
4 changed files with 130 additions and 1 deletions
7
docs/source/telegram.constants.rst
Normal file
7
docs/source/telegram.constants.rst
Normal file
|
@ -0,0 +1,7 @@
|
|||
telegram.constants module
|
||||
=========================
|
||||
|
||||
.. automodule:: telegram.constants
|
||||
:members:
|
||||
:undoc-members:
|
||||
:show-inheritance:
|
|
@ -80,6 +80,10 @@ from .inputvenuemessagecontent import InputVenueMessageContent
|
|||
from .inputcontactmessagecontent import InputContactMessageContent
|
||||
from .update import Update
|
||||
from .bot import Bot
|
||||
from .constants import (MAX_MESSAGE_LENGTH, MAX_CAPTION_LENGTH, SUPPORTED_WEBHOOK_PORTS,
|
||||
MAX_FILESIZE_DOWNLOAD, MAX_FILESIZE_UPLOAD,
|
||||
MAX_MESSAGES_PER_SECOND_PER_CHAT, MAX_MESSAGES_PER_SECOND,
|
||||
MAX_MESSAGES_PER_MINUTE_PER_GROUP)
|
||||
|
||||
__author__ = 'devs@python-telegram-bot.org'
|
||||
__version__ = '4.3.4'
|
||||
|
@ -99,7 +103,9 @@ __all__ = ['Audio', 'Bot', 'Chat', 'ChatMember', 'ChatAction', 'ChosenInlineResu
|
|||
'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', 'SUPPORTED_WEBHOOK_PORTS',
|
||||
'MAX_FILESIZE_DOWNLOAD', 'MAX_FILESIZE_UPLOAD', 'MAX_MESSAGES_PER_SECOND_PER_CHAT',
|
||||
'MAX_MESSAGES_PER_SECOND', 'MAX_MESSAGES_PER_MINUTE_PER_GROUP']
|
||||
|
||||
if version_info < (2, 7):
|
||||
from warnings import warn
|
||||
|
|
47
telegram/constants.py
Normal file
47
telegram/constants.py
Normal file
|
@ -0,0 +1,47 @@
|
|||
# python-telegram-bot - a Python interface to the Telegram Bot API
|
||||
# Copyright (C) 2015-2016
|
||||
# by the python-telegram-bot contributors <devs@python-telegram-bot.org>
|
||||
#
|
||||
# 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.
|
||||
|
||||
Attributes:
|
||||
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 <https://core.telegram.org/bots/faq>`_.
|
||||
|
||||
Attributes:
|
||||
SUPPORTED_WEBHOOK_PORTS (List[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.
|
||||
MAX_MESSAGES_PER_SECOND (int)
|
||||
MAX_MESSAGES_PER_MINUTE_PER_GROUP (int)
|
||||
"""
|
||||
|
||||
MAX_MESSAGE_LENGTH = 4096
|
||||
MAX_CAPTION_LENGTH = 200
|
||||
|
||||
# constants above this line are tested
|
||||
|
||||
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
|
69
tests/test_constants.py
Normal file
69
tests/test_constants.py
Normal file
|
@ -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 <devs@python-telegram-bot.org>
|
||||
#
|
||||
# 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()
|
Loading…
Reference in a new issue