2017-08-11 23:58:41 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2022-01-03 08:15:18 +01:00
|
|
|
# Copyright (C) 2015-2022
|
2017-08-11 23:58:41 +02:00
|
|
|
# Leandro Toledo de Souza <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/].
|
2019-08-28 21:58:58 +02:00
|
|
|
import os
|
2017-06-14 13:42:57 +02:00
|
|
|
import inspect
|
2021-08-29 18:17:06 +02:00
|
|
|
from typing import List
|
2017-06-14 13:42:57 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
import pytest
|
2022-04-24 12:38:09 +02:00
|
|
|
import httpx
|
2017-06-14 13:42:57 +02:00
|
|
|
from bs4 import BeautifulSoup
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
|
2017-06-14 13:42:57 +02:00
|
|
|
import telegram
|
2021-02-13 22:07:37 +01:00
|
|
|
from tests.conftest import env_var_2_bool
|
2017-06-14 13:42:57 +02:00
|
|
|
|
|
|
|
IGNORED_OBJECTS = ('ResponseParameters', 'CallbackGame')
|
2020-10-09 17:22:07 +02:00
|
|
|
IGNORED_PARAMETERS = {
|
|
|
|
'self',
|
|
|
|
'args',
|
2020-11-29 16:20:46 +01:00
|
|
|
'_kwargs',
|
2022-04-24 12:38:09 +02:00
|
|
|
'read_timeout',
|
|
|
|
'write_timeout',
|
|
|
|
'connect_timeout',
|
|
|
|
'pool_timeout',
|
2020-10-09 17:22:07 +02:00
|
|
|
'bot',
|
|
|
|
'api_kwargs',
|
2021-08-19 22:01:10 +02:00
|
|
|
'kwargs',
|
2020-10-09 17:22:07 +02:00
|
|
|
}
|
2017-06-14 13:42:57 +02:00
|
|
|
|
2021-08-29 18:17:06 +02:00
|
|
|
ignored_param_requirements = { # Ignore these since there's convenience params in them (eg. Venue)
|
|
|
|
'send_location': {'latitude', 'longitude'},
|
|
|
|
'edit_message_live_location': {'latitude', 'longitude'},
|
|
|
|
'send_venue': {'latitude', 'longitude', 'title', 'address'},
|
|
|
|
'send_contact': {'phone_number', 'first_name'},
|
|
|
|
}
|
|
|
|
|
2017-06-14 13:42:57 +02:00
|
|
|
|
|
|
|
def find_next_sibling_until(tag, name, until):
|
|
|
|
for sibling in tag.next_siblings:
|
|
|
|
if sibling is until:
|
|
|
|
return
|
|
|
|
if sibling.name == name:
|
|
|
|
return sibling
|
|
|
|
|
|
|
|
|
2021-08-29 18:17:06 +02:00
|
|
|
def parse_table(h4) -> List[List[str]]:
|
|
|
|
"""Parses the Telegram doc table and has an output of a 2D list."""
|
2017-06-14 13:42:57 +02:00
|
|
|
table = find_next_sibling_until(h4, 'table', h4.find_next_sibling('h4'))
|
|
|
|
if not table:
|
|
|
|
return []
|
|
|
|
t = []
|
|
|
|
for tr in table.find_all('tr')[1:]:
|
2017-10-14 20:03:02 +02:00
|
|
|
t.append([td.text for td in tr.find_all('td')])
|
2017-06-14 13:42:57 +02:00
|
|
|
return t
|
|
|
|
|
|
|
|
|
|
|
|
def check_method(h4):
|
2021-08-29 18:17:06 +02:00
|
|
|
name = h4.text # name of the method in telegram's docs.
|
|
|
|
method = getattr(telegram.Bot, name) # Retrieve our lib method
|
2017-06-14 13:42:57 +02:00
|
|
|
table = parse_table(h4)
|
|
|
|
|
|
|
|
# Check arguments based on source
|
|
|
|
sig = inspect.signature(method, follow_wrapped=True)
|
|
|
|
|
|
|
|
checked = []
|
|
|
|
for parameter in table:
|
2017-10-14 20:03:02 +02:00
|
|
|
param = sig.parameters.get(parameter[0])
|
2020-11-23 22:09:29 +01:00
|
|
|
assert param is not None, f"Parameter {parameter[0]} not found in {method.__name__}"
|
2021-08-29 18:17:06 +02:00
|
|
|
|
2017-06-14 13:42:57 +02:00
|
|
|
# TODO: Check type via docstring
|
2021-08-29 18:17:06 +02:00
|
|
|
assert check_required_param(
|
|
|
|
parameter, param.name, sig, method.__name__
|
|
|
|
), f'Param {param.name!r} of method {method.__name__!r} requirement mismatch!'
|
2017-10-14 20:03:02 +02:00
|
|
|
checked.append(parameter[0])
|
2017-06-14 13:42:57 +02:00
|
|
|
|
|
|
|
ignored = IGNORED_PARAMETERS.copy()
|
|
|
|
if name == 'getUpdates':
|
|
|
|
ignored -= {'timeout'} # Has it's own timeout parameter that we do wanna check for
|
2020-12-16 14:28:53 +01:00
|
|
|
elif name in (
|
|
|
|
f'send{media_type}'
|
|
|
|
for media_type in [
|
|
|
|
'Animation',
|
|
|
|
'Audio',
|
|
|
|
'Document',
|
|
|
|
'Photo',
|
|
|
|
'Video',
|
|
|
|
'VideoNote',
|
|
|
|
'Voice',
|
|
|
|
]
|
|
|
|
):
|
|
|
|
ignored |= {'filename'} # Convenience parameter
|
2017-07-25 00:35:22 +02:00
|
|
|
elif name == 'sendContact':
|
|
|
|
ignored |= {'contact'} # Added for ease of use
|
2017-10-14 20:03:02 +02:00
|
|
|
elif name in ['sendLocation', 'editMessageLiveLocation']:
|
2017-07-25 00:35:22 +02:00
|
|
|
ignored |= {'location'} # Added for ease of use
|
|
|
|
elif name == 'sendVenue':
|
|
|
|
ignored |= {'venue'} # Added for ease of use
|
2020-09-27 14:11:49 +02:00
|
|
|
elif name == 'answerInlineQuery':
|
|
|
|
ignored |= {'current_offset'} # Added for ease of use
|
2017-06-14 13:42:57 +02:00
|
|
|
|
|
|
|
assert (sig.parameters.keys() ^ checked) - ignored == set()
|
|
|
|
|
|
|
|
|
|
|
|
def check_object(h4):
|
|
|
|
name = h4.text
|
|
|
|
obj = getattr(telegram, name)
|
|
|
|
table = parse_table(h4)
|
|
|
|
|
2021-08-19 22:01:10 +02:00
|
|
|
# Check arguments based on source. Makes sure to only check __init__'s signature & nothing else
|
|
|
|
sig = inspect.signature(obj.__init__, follow_wrapped=True)
|
2017-06-14 13:42:57 +02:00
|
|
|
|
2021-08-29 18:17:06 +02:00
|
|
|
checked = set()
|
2017-06-14 13:42:57 +02:00
|
|
|
for parameter in table:
|
2017-10-14 20:03:02 +02:00
|
|
|
field = parameter[0]
|
2017-06-14 13:42:57 +02:00
|
|
|
if field == 'from':
|
|
|
|
field = 'from_user'
|
2020-10-09 17:22:07 +02:00
|
|
|
elif (
|
2021-07-01 17:45:19 +02:00
|
|
|
name.startswith('InlineQueryResult')
|
|
|
|
or name.startswith('InputMedia')
|
|
|
|
or name.startswith('BotCommandScope')
|
2020-10-09 17:22:07 +02:00
|
|
|
) and field == 'type':
|
2017-06-14 13:42:57 +02:00
|
|
|
continue
|
2021-08-29 18:17:06 +02:00
|
|
|
elif (name.startswith('ChatMember')) and field == 'status': # We autofill the status
|
2021-07-01 17:45:19 +02:00
|
|
|
continue
|
2021-05-27 09:38:17 +02:00
|
|
|
elif (
|
|
|
|
name.startswith('PassportElementError') and field == 'source'
|
|
|
|
) or field == 'remove_keyboard':
|
2017-06-14 13:42:57 +02:00
|
|
|
continue
|
2021-08-29 18:17:06 +02:00
|
|
|
elif name.startswith('ForceReply') and field == 'force_reply': # this param is always True
|
|
|
|
continue
|
2017-06-14 13:42:57 +02:00
|
|
|
|
|
|
|
param = sig.parameters.get(field)
|
2020-11-23 22:09:29 +01:00
|
|
|
assert param is not None, f"Attribute {field} not found in {obj.__name__}"
|
2017-06-14 13:42:57 +02:00
|
|
|
# TODO: Check type via docstring
|
2021-08-29 18:17:06 +02:00
|
|
|
assert check_required_param(
|
|
|
|
parameter, field, sig, obj.__name__
|
|
|
|
), f"{obj.__name__!r} parameter {param.name!r} requirement mismatch"
|
|
|
|
checked.add(field)
|
2017-06-14 13:42:57 +02:00
|
|
|
|
|
|
|
ignored = IGNORED_PARAMETERS.copy()
|
|
|
|
if name == 'InputFile':
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
return
|
2021-05-27 09:38:17 +02:00
|
|
|
if name == 'InlineQueryResult':
|
2021-07-01 17:45:19 +02:00
|
|
|
ignored |= {'id', 'type'} # attributes common to all subclasses
|
|
|
|
if name == 'ChatMember':
|
|
|
|
ignored |= {'user', 'status'} # attributes common to all subclasses
|
|
|
|
if name == 'BotCommandScope':
|
|
|
|
ignored |= {'type'} # attributes common to all subclasses
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
elif name in ('PassportFile', 'EncryptedPassportElement'):
|
|
|
|
ignored |= {'credentials'}
|
|
|
|
elif name == 'PassportElementError':
|
|
|
|
ignored |= {'message', 'type', 'source'}
|
2021-10-15 18:03:56 +02:00
|
|
|
elif name == 'InputMedia':
|
|
|
|
ignored |= {'caption', 'caption_entities', 'media', 'media_type', 'parse_mode'}
|
2020-12-16 14:28:53 +01:00
|
|
|
elif name.startswith('InputMedia'):
|
|
|
|
ignored |= {'filename'} # Convenience parameter
|
2017-06-14 13:42:57 +02:00
|
|
|
|
|
|
|
assert (sig.parameters.keys() ^ checked) - ignored == set()
|
|
|
|
|
|
|
|
|
2021-08-29 18:17:06 +02:00
|
|
|
def check_required_param(
|
|
|
|
param_desc: List[str], param_name: str, sig: inspect.Signature, method_or_obj_name: str
|
|
|
|
) -> bool:
|
|
|
|
"""Checks if the method/class parameter is a required/optional param as per Telegram docs."""
|
|
|
|
if len(param_desc) == 4: # this means that there is a dedicated 'Required' column present.
|
|
|
|
# Handle cases where we provide convenience intentionally-
|
|
|
|
if param_name in ignored_param_requirements.get(method_or_obj_name, {}):
|
|
|
|
return True
|
|
|
|
is_required = True if param_desc[2] in {'Required', 'Yes'} else False
|
|
|
|
is_ours_required = sig.parameters[param_name].default is inspect.Signature.empty
|
|
|
|
return is_required is is_ours_required
|
|
|
|
|
|
|
|
if len(param_desc) == 3: # The docs mention the requirement in the description for classes...
|
|
|
|
if param_name in ignored_param_requirements.get(method_or_obj_name, {}):
|
|
|
|
return True
|
|
|
|
is_required = False if param_desc[2].split('.', 1)[0] == 'Optional' else True
|
|
|
|
is_ours_required = sig.parameters[param_name].default is inspect.Signature.empty
|
|
|
|
return is_required is is_ours_required
|
|
|
|
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
argvalues = []
|
|
|
|
names = []
|
2022-04-24 12:38:09 +02:00
|
|
|
request = httpx.get('https://core.telegram.org/bots/api')
|
|
|
|
soup = BeautifulSoup(request.text, 'html.parser')
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
for thing in soup.select('h4 > a.anchor'):
|
|
|
|
# Methods and types don't have spaces in them, luckily all other sections of the docs do
|
|
|
|
# TODO: don't depend on that
|
|
|
|
if '-' not in thing['name']:
|
|
|
|
h4 = thing.parent
|
|
|
|
|
|
|
|
# Is it a method
|
|
|
|
if h4.text[0].lower() == h4.text[0]:
|
|
|
|
argvalues.append((check_method, h4))
|
|
|
|
names.append(h4.text)
|
|
|
|
elif h4.text not in IGNORED_OBJECTS: # Or a type/object
|
|
|
|
argvalues.append((check_object, h4))
|
|
|
|
names.append(h4.text)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(('method', 'data'), argvalues=argvalues, ids=names)
|
2021-02-13 22:07:37 +01:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
not env_var_2_bool(os.getenv('TEST_OFFICIAL')), reason='test_official is not enabled'
|
|
|
|
)
|
2017-08-11 23:58:41 +02:00
|
|
|
def test_official(method, data):
|
|
|
|
method(data)
|