Skip to content

Commit

Permalink
Merge pull request eternnoir#1269 from Badiboy/master
Browse files Browse the repository at this point in the history
Check and update for full compatibility to Bot API up to 5.0
  • Loading branch information
Badiboy authored Aug 18, 2021
2 parents e818e38 + fabcd93 commit 3232811
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 323 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ You can use proxy for request. `apihelper.proxy` object will use by call `reques
```python
from telebot import apihelper

apihelper.proxy = {'http':'http://10.10.1.10:3128'}
apihelper.proxy = {'http':'http://127.0.0.1:3128'}
```

If you want to use socket5 proxy you need install dependency `pip install requests[socks]` and make sure, that you have the latest version of `gunicorn`, `PySocks`, `pyTelegramBotAPI`, `requests` and `urllib3`.
Expand All @@ -605,8 +605,14 @@ apihelper.proxy = {'https':'socks5://userproxy:password@proxy_address:port'}

_Checking is in progress..._

[Bot API 4.5](https://core.telegram.org/bots/api-changelog#december-31-2019) _- To be checked..._
[Bot API 5.1](https://core.telegram.org/bots/api#march-9-2021) _- To be checked..._

*[Bot API 5.0](https://core.telegram.org/bots/api-changelog#november-4-2020)
*[Bot API 4.9](https://core.telegram.org/bots/api-changelog#june-4-2020)
*[Bot API 4.8](https://core.telegram.org/bots/api-changelog#april-24-2020)
*[Bot API 4.7](https://core.telegram.org/bots/api-changelog#march-30-2020)
*[Bot API 4.6](https://core.telegram.org/bots/api-changelog#january-23-2020)
*[Bot API 4.5](https://core.telegram.org/bots/api-changelog#december-31-2019) - No nested MessageEntities and Markdown2 support.
*[Bot API 4.4](https://core.telegram.org/bots/api-changelog#july-29-2019)
*[Bot API 4.3](https://core.telegram.org/bots/api-changelog#may-31-2019)
*[Bot API 4.2](https://core.telegram.org/bots/api-changelog#april-14-2019)
Expand Down
72 changes: 43 additions & 29 deletions telebot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class TeleBot:
def __init__(
self, token, parse_mode=None, threaded=True, skip_pending=False, num_threads=2,
next_step_backend=None, reply_backend=None, exception_handler=None, last_update_id=0,
suppress_middleware_excepions=False # <- Typo in exceptions
suppress_middleware_excepions=False
):
"""
:param token: bot API token
Expand Down Expand Up @@ -587,8 +587,9 @@ def infinity_polling(self, timeout=20, skip_pending=False, long_polling_timeout=
if logger_level and logger_level >= logging.INFO:
logger.error("Break infinity polling")

def polling(self, none_stop: bool=False, skip_pending=False, interval: int=0, timeout: int=20,
long_polling_timeout: int=20, allowed_updates: Optional[List[str]]=None):
def polling(self, non_stop: bool=False, skip_pending=False, interval: int=0, timeout: int=20,
long_polling_timeout: int=20, allowed_updates: Optional[List[str]]=None,
none_stop: Optional[bool]=None):
"""
This function creates a new Thread that calls an internal __retrieve_updates function.
This allows the bot to retrieve Updates automagically and notify listeners and message handlers accordingly.
Expand All @@ -597,7 +598,7 @@ def polling(self, none_stop: bool=False, skip_pending=False, interval: int=0, ti
Always get updates.
:param interval: Delay between two update retrivals
:param none_stop: Do not stop polling when an ApiException occurs.
:param non_stop: Do not stop polling when an ApiException occurs.
:param timeout: Request connection timeout
:param skip_pending: skip old updates
:param long_polling_timeout: Timeout in seconds for long polling (see API docs)
Expand All @@ -609,15 +610,19 @@ def polling(self, none_stop: bool=False, skip_pending=False, interval: int=0, ti
Please note that this parameter doesn't affect updates created before the call to the get_updates,
so unwanted updates may be received for a short period of time.
:param none_stop: Deprecated, use non_stop. Old typo f***up compatibility
:return:
"""
if none_stop is not None:
non_stop = none_stop

if skip_pending:
self.__skip_updates()

if self.threaded:
self.__threaded_polling(none_stop, interval, timeout, long_polling_timeout, allowed_updates)
self.__threaded_polling(non_stop, interval, timeout, long_polling_timeout, allowed_updates)
else:
self.__non_threaded_polling(none_stop, interval, timeout, long_polling_timeout, allowed_updates)
self.__non_threaded_polling(non_stop, interval, timeout, long_polling_timeout, allowed_updates)

def __threaded_polling(self, non_stop=False, interval=0, timeout = None, long_polling_timeout = None, allowed_updates=None):
logger.info('Started polling.')
Expand Down Expand Up @@ -1119,30 +1124,34 @@ def send_document(
thumb: Optional[Union[Any, str]]=None,
caption_entities: Optional[List[types.MessageEntity]]=None,
allow_sending_without_reply: Optional[bool]=None,
visible_file_name: Optional[str]=None) -> types.Message:
visible_file_name: Optional[str]=None,
disable_content_type_detection: Optional[bool]=None) -> types.Message:
"""
Use this method to send general files.
:param chat_id:
:param data:
:param reply_to_message_id:
:param caption:
:param chat_id: Unique identifier for the target chat or username of the target channel (in the format @channelusername)
:param data: (document) File to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data
:param reply_to_message_id: If the message is a reply, ID of the original message
:param caption: Document caption (may also be used when resending documents by file_id), 0-1024 characters after entities parsing
:param reply_markup:
:param parse_mode:
:param disable_notification:
:param parse_mode: Mode for parsing entities in the document caption
:param disable_notification: Sends the message silently. Users will receive a notification with no sound.
:param timeout:
:param thumb: InputFile or String : Thumbnail of the file sent
:param thumb: InputFile or String : Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supported server-side. The thumbnail should be in JPEG format and less than 200 kB in size. A thumbnail's width and height should not exceed 320. Ignored if the file is not uploaded using multipart/form-data. Thumbnails can't be reused and can be only uploaded as a new file, so you can pass “attach://<file_attach_name>” if the thumbnail was uploaded using multipart/form-data under <file_attach_name>
:param caption_entities:
:param allow_sending_without_reply:
:param visible_file_name: allows to define file name that will be visible in the Telegram instead of original file name
:param disable_content_type_detection: Disables automatic server-side content type detection for files uploaded using multipart/form-data
:return: API reply.
"""
parse_mode = self.parse_mode if (parse_mode is None) else parse_mode

return types.Message.de_json(
apihelper.send_data(
self.token, chat_id, data, 'document', reply_to_message_id, reply_markup,
parse_mode, disable_notification, timeout, caption, thumb, caption_entities,
allow_sending_without_reply, visible_file_name))
self.token, chat_id, data, 'document',
reply_to_message_id = reply_to_message_id, reply_markup = reply_markup, parse_mode = parse_mode,
disable_notification = disable_notification, timeout = timeout, caption = caption, thumb = thumb,
caption_entities = caption_entities, allow_sending_without_reply = allow_sending_without_reply,
disable_content_type_detection = disable_content_type_detection, visible_file_name = visible_file_name))

def send_sticker(
self, chat_id: Union[int, str], data: Union[Any, str],
Expand All @@ -1164,7 +1173,7 @@ def send_sticker(
"""
return types.Message.de_json(
apihelper.send_data(
self.token, chat_id=chat_id, data=data, data_type='sticker',
self.token, chat_id, data, 'sticker',
reply_to_message_id=reply_to_message_id, reply_markup=reply_markup,
disable_notification=disable_notification, timeout=timeout,
allow_sending_without_reply=allow_sending_without_reply))
Expand Down Expand Up @@ -1638,8 +1647,8 @@ def create_chat_invite_link(
:param chat_id: Id: Unique identifier for the target chat or username of the target channel
(in the format @channelusername)
:expire_date: Point in time (Unix timestamp) when the link will expire
:member_limit: Maximum number of users that can be members of the chat simultaneously
:param expire_date: Point in time (Unix timestamp) when the link will expire
:param member_limit: Maximum number of users that can be members of the chat simultaneously
:return:
"""
return types.ChatInviteLink.de_json(
Expand All @@ -1654,11 +1663,12 @@ def edit_chat_invite_link(
Use this method to edit a non-primary invite link created by the bot.
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
:param invite_link:
:param chat_id: Id: Unique identifier for the target chat or username of the target channel
(in the format @channelusername)
:invite_link: The invite link to edit
:expire_date: Point in time (Unix timestamp) when the link will expire
:member_limit: Maximum number of users that can be members of the chat simultaneously
:param invite_link: The invite link to edit
:param expire_date: Point in time (Unix timestamp) when the link will expire
:param member_limit: Maximum number of users that can be members of the chat simultaneously
:return:
"""
return types.ChatInviteLink.de_json(
Expand All @@ -1674,7 +1684,7 @@ def revoke_chat_invite_link(
:param chat_id: Id: Unique identifier for the target chat or username of the target channel
(in the format @channelusername)
:invite_link: The invite link to revoke
:param invite_link: The invite link to revoke
:return:
"""
return types.ChatInviteLink.de_json(
Expand Down Expand Up @@ -1832,7 +1842,8 @@ def edit_message_text(
message_id: Optional[int]=None,
inline_message_id: Optional[str]=None,
parse_mode: Optional[str]=None,
disable_web_page_preview: Optional[bool]=None,
entities: Optional[List[types.MessageEntity]]=None,
disable_web_page_preview: Optional[bool]=None,
reply_markup: Optional[REPLY_MARKUP_TYPES]=None) -> Union[types.Message, bool]:
"""
Use this method to edit text and game messages.
Expand All @@ -1841,14 +1852,15 @@ def edit_message_text(
:param message_id:
:param inline_message_id:
:param parse_mode:
:param entities:
:param disable_web_page_preview:
:param reply_markup:
:return:
"""
parse_mode = self.parse_mode if (parse_mode is None) else parse_mode

result = apihelper.edit_message_text(self.token, text, chat_id, message_id, inline_message_id, parse_mode,
disable_web_page_preview, reply_markup)
entities, disable_web_page_preview, reply_markup)
if type(result) == bool: # if edit inline message return is bool not Message.
return result
return types.Message.de_json(result)
Expand Down Expand Up @@ -2024,6 +2036,7 @@ def send_invoice(
max_tip_amount, suggested_tip_amounts)
return types.Message.de_json(result)

# noinspection PyShadowingBuiltins
def send_poll(
self, chat_id: Union[int, str], question: str, options: List[str],
is_anonymous: Optional[bool]=None, type: Optional[str]=None,
Expand All @@ -2034,7 +2047,7 @@ def send_poll(
open_period: Optional[int]=None,
close_date: Optional[Union[int, datetime]]=None,
is_closed: Optional[bool]=None,
disable_notifications: Optional[bool]=False,
disable_notification: Optional[bool]=False,
reply_to_message_id: Optional[int]=None,
reply_markup: Optional[REPLY_MARKUP_TYPES]=None,
allow_sending_without_reply: Optional[bool]=None,
Expand All @@ -2054,7 +2067,7 @@ def send_poll(
:param open_period:
:param close_date:
:param is_closed:
:param disable_notifications:
:param disable_notification:
:param reply_to_message_id:
:param allow_sending_without_reply:
:param reply_markup:
Expand All @@ -2072,7 +2085,7 @@ def send_poll(
question, options,
is_anonymous, type, allows_multiple_answers, correct_option_id,
explanation, explanation_parse_mode, open_period, close_date, is_closed,
disable_notifications, reply_to_message_id, allow_sending_without_reply,
disable_notification, reply_to_message_id, allow_sending_without_reply,
reply_markup, timeout, explanation_entities))

def stop_poll(
Expand Down Expand Up @@ -2127,6 +2140,7 @@ def edit_message_caption(
:param message_id:
:param inline_message_id:
:param parse_mode:
:param caption_entities:
:param reply_markup:
:return:
"""
Expand Down
10 changes: 6 additions & 4 deletions telebot/apihelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ def unpin_all_chat_messages(token, chat_id):
# Updating messages

def edit_message_text(token, text, chat_id=None, message_id=None, inline_message_id=None, parse_mode=None,
disable_web_page_preview=None, reply_markup=None):
entities = None, disable_web_page_preview=None, reply_markup=None):
method_url = r'editMessageText'
payload = {'text': text}
if chat_id:
Expand All @@ -1115,6 +1115,8 @@ def edit_message_text(token, text, chat_id=None, message_id=None, inline_message
payload['inline_message_id'] = inline_message_id
if parse_mode:
payload['parse_mode'] = parse_mode
if entities:
payload['entities'] = json.dumps(types.MessageEntity.to_list_of_dicts(entities))
if disable_web_page_preview is not None:
payload['disable_web_page_preview'] = disable_web_page_preview
if reply_markup:
Expand Down Expand Up @@ -1483,7 +1485,7 @@ def send_poll(
question, options,
is_anonymous = None, type = None, allows_multiple_answers = None, correct_option_id = None,
explanation = None, explanation_parse_mode=None, open_period = None, close_date = None, is_closed = None,
disable_notifications=False, reply_to_message_id=None, allow_sending_without_reply=None,
disable_notification=False, reply_to_message_id=None, allow_sending_without_reply=None,
reply_markup=None, timeout=None, explanation_entities=None):
method_url = r'sendPoll'
payload = {
Expand Down Expand Up @@ -1513,8 +1515,8 @@ def send_poll(
if is_closed is not None:
payload['is_closed'] = is_closed

if disable_notifications:
payload['disable_notification'] = disable_notifications
if disable_notification:
payload['disable_notification'] = disable_notification
if reply_to_message_id is not None:
payload['reply_to_message_id'] = reply_to_message_id
if allow_sending_without_reply is not None:
Expand Down
Loading

0 comments on commit 3232811

Please sign in to comment.