1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00
This commit is contained in:
Badiboy 2022-07-12 22:44:25 +03:00
parent a3a55e7393
commit 16703161aa

View File

@ -9,7 +9,6 @@ import time
import traceback import traceback
from typing import Any, Callable, List, Optional, Union from typing import Any, Callable, List, Optional, Union
# these imports are used to avoid circular import error # these imports are used to avoid circular import error
import telebot.util import telebot.util
import telebot.types import telebot.types
@ -17,12 +16,10 @@ import telebot.types
# storage # storage
from telebot.storage import StatePickleStorage, StateMemoryStorage from telebot.storage import StatePickleStorage, StateMemoryStorage
# random module to generate random string # random module to generate random string
import random import random
import string import string
import ssl import ssl
logger = logging.getLogger('TeleBot') logger = logging.getLogger('TeleBot')
@ -93,7 +90,6 @@ class TeleBot:
See more examples in examples/ directory: See more examples in examples/ directory:
https://github.com/eternnoir/pyTelegramBotAPI/tree/master/examples https://github.com/eternnoir/pyTelegramBotAPI/tree/master/examples
""" """
def __init__( def __init__(
@ -170,8 +166,6 @@ class TeleBot:
'ENABLE_MIDDLEWARE set to True. This is not recommended.' 'ENABLE_MIDDLEWARE set to True. This is not recommended.'
) )
self.middlewares = [] if use_class_middlewares else None self.middlewares = [] if use_class_middlewares else None
self.threaded = threaded self.threaded = threaded
if self.threaded: if self.threaded:
self.worker_pool = util.ThreadPool(self, num_threads=num_threads) self.worker_pool = util.ThreadPool(self, num_threads=num_threads)
@ -299,7 +293,6 @@ class TeleBot:
return apihelper.set_webhook(self.token, url, certificate, max_connections, allowed_updates, ip_address, return apihelper.set_webhook(self.token, url, certificate, max_connections, allowed_updates, ip_address,
drop_pending_updates, timeout, secret_token) drop_pending_updates, timeout, secret_token)
def run_webhooks(self, def run_webhooks(self,
listen: Optional[str]="127.0.0.1", listen: Optional[str]="127.0.0.1",
port: Optional[int]=443, port: Optional[int]=443,
@ -331,6 +324,8 @@ class TeleBot:
:param drop_pending_updates: Pass True to drop all pending updates :param drop_pending_updates: Pass True to drop all pending updates
:param timeout: Integer. Request connection timeout :param timeout: Integer. Request connection timeout
:param secret_token: Secret token to be used to verify the webhook request. :param secret_token: Secret token to be used to verify the webhook request.
:param secret_token_length:
:param debug:
:return: :return:
""" """
@ -338,13 +333,10 @@ class TeleBot:
if not secret_token: if not secret_token:
secret_token = ''.join(random.choices(string.ascii_uppercase + string.digits, k=secret_token_length)) secret_token = ''.join(random.choices(string.ascii_uppercase + string.digits, k=secret_token_length))
if not url_path: if not url_path:
url_path = self.token + '/' url_path = self.token + '/'
if url_path[-1] != '/': url_path += '/' if url_path[-1] != '/': url_path += '/'
protocol = "https" if certificate else "http" protocol = "https" if certificate else "http"
if not webhook_url: if not webhook_url:
webhook_url = "{}://{}:{}/{}".format(protocol, listen, port, url_path) webhook_url = "{}://{}:{}/{}".format(protocol, listen, port, url_path)
@ -352,8 +344,6 @@ class TeleBot:
if certificate and certificate_key: if certificate and certificate_key:
ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_ctx.load_cert_chain(certificate, certificate_key) ssl_ctx.load_cert_chain(certificate, certificate_key)
else:
ssl_ctx = None
# open certificate if it exists # open certificate if it exists
cert_file = open(certificate, 'rb') if certificate else None cert_file = open(certificate, 'rb') if certificate else None
@ -378,8 +368,6 @@ class TeleBot:
self.webhook_listener = SyncWebhookListener(self, secret_token, listen, port, ssl_context, '/'+url_path, debug) self.webhook_listener = SyncWebhookListener(self, secret_token, listen, port, ssl_context, '/'+url_path, debug)
self.webhook_listener.run_app() self.webhook_listener.run_app()
def delete_webhook(self, drop_pending_updates=None, timeout=None): def delete_webhook(self, drop_pending_updates=None, timeout=None):
""" """
Use this method to remove webhook integration if you decide to switch back to getUpdates. Use this method to remove webhook integration if you decide to switch back to getUpdates.
@ -487,7 +475,6 @@ class TeleBot:
if update.update_id > self.last_update_id: self.last_update_id = update.update_id if update.update_id > self.last_update_id: self.last_update_id = update.update_id
continue continue
if update.update_id > self.last_update_id: if update.update_id > self.last_update_id:
self.last_update_id = update.update_id self.last_update_id = update.update_id
if update.message: if update.message:
@ -1519,9 +1506,6 @@ class TeleBot:
allow_sending_without_reply: Optional[bool]=None, allow_sending_without_reply: Optional[bool]=None,
protect_content: Optional[bool]=None) -> types.Message: protect_content: Optional[bool]=None) -> types.Message:
""" """
As of v.4.0, Telegram clients support rounded square mp4 videos of up to 1 minute long. Use this method to send
video messages.
Telegram documentation: https://core.telegram.org/bots/api#sendvideonote Telegram documentation: https://core.telegram.org/bots/api#sendvideonote
:param chat_id: Integer : Unique identifier for the message recipient User or GroupChat id :param chat_id: Integer : Unique identifier for the message recipient User or GroupChat id
@ -1742,7 +1726,6 @@ class TeleBot:
:param protect_content: :param protect_content:
:return: :return:
""" """
return types.Message.de_json( return types.Message.de_json(
apihelper.send_contact( apihelper.send_contact(
self.token, chat_id, phone_number, first_name, last_name, vcard, self.token, chat_id, phone_number, first_name, last_name, vcard,
@ -2158,11 +2141,9 @@ class TeleBot:
:param chat_id: Unique identifier for the target private chat. :param chat_id: Unique identifier for the target private chat.
If not specified, default bot's menu button will be changed. If not specified, default bot's menu button will be changed.
:param menu_button: A JSON-serialized object for the new bot's menu button. Defaults to MenuButtonDefault :param menu_button: A JSON-serialized object for the new bot's menu button. Defaults to MenuButtonDefault
""" """
return apihelper.set_chat_menu_button(self.token, chat_id, menu_button) return apihelper.set_chat_menu_button(self.token, chat_id, menu_button)
def get_chat_menu_button(self, chat_id: Union[int, str]=None) -> types.MenuButton: def get_chat_menu_button(self, chat_id: Union[int, str]=None) -> types.MenuButton:
""" """
Use this method to get the current value of the bot's menu button Use this method to get the current value of the bot's menu button
@ -2174,11 +2155,9 @@ class TeleBot:
:param chat_id: Unique identifier for the target private chat. :param chat_id: Unique identifier for the target private chat.
If not specified, default bot's menu button will be returned. If not specified, default bot's menu button will be returned.
:return: types.MenuButton :return: types.MenuButton
""" """
return types.MenuButton.de_json(apihelper.get_chat_menu_button(self.token, chat_id)) return types.MenuButton.de_json(apihelper.get_chat_menu_button(self.token, chat_id))
def set_my_default_administrator_rights(self, rights: types.ChatAdministratorRights=None, def set_my_default_administrator_rights(self, rights: types.ChatAdministratorRights=None,
for_channels: bool=None) -> bool: for_channels: bool=None) -> bool:
""" """
@ -2193,10 +2172,8 @@ class TeleBot:
:param rights: A JSON-serialized object describing new default administrator rights. If not specified, the default administrator rights will be cleared. :param rights: A JSON-serialized object describing new default administrator rights. If not specified, the default administrator rights will be cleared.
:param for_channels: Pass True to change the default administrator rights of the bot in channels. Otherwise, the default administrator rights of the bot for groups and supergroups will be changed. :param for_channels: Pass True to change the default administrator rights of the bot in channels. Otherwise, the default administrator rights of the bot for groups and supergroups will be changed.
""" """
return apihelper.set_my_default_administrator_rights(self.token, rights, for_channels) return apihelper.set_my_default_administrator_rights(self.token, rights, for_channels)
def get_my_default_administrator_rights(self, for_channels: bool=None) -> types.ChatAdministratorRights: def get_my_default_administrator_rights(self, for_channels: bool=None) -> types.ChatAdministratorRights:
""" """
Use this method to get the current default administrator rights of the bot. Use this method to get the current default administrator rights of the bot.
@ -2207,10 +2184,8 @@ class TeleBot:
:param for_channels: Pass True to get the default administrator rights of the bot in channels. Otherwise, the default administrator rights of the bot for groups and supergroups will be returned. :param for_channels: Pass True to get the default administrator rights of the bot in channels. Otherwise, the default administrator rights of the bot for groups and supergroups will be returned.
:return: types.ChatAdministratorRights :return: types.ChatAdministratorRights
""" """
return types.ChatAdministratorRights.de_json(apihelper.get_my_default_administrator_rights(self.token, for_channels)) return types.ChatAdministratorRights.de_json(apihelper.get_my_default_administrator_rights(self.token, for_channels))
def set_my_commands(self, commands: List[types.BotCommand], def set_my_commands(self, commands: List[types.BotCommand],
scope: Optional[types.BotCommandScope]=None, scope: Optional[types.BotCommandScope]=None,
language_code: Optional[str]=None) -> bool: language_code: Optional[str]=None) -> bool:
@ -2551,7 +2526,6 @@ class TeleBot:
max_tip_amount, suggested_tip_amounts, protect_content) max_tip_amount, suggested_tip_amounts, protect_content)
return types.Message.de_json(result) return types.Message.de_json(result)
def create_invoice_link(self, def create_invoice_link(self,
title: str, description: str, payload:str, provider_token: str, title: str, description: str, payload:str, provider_token: str,
currency: str, prices: List[types.LabeledPrice], currency: str, prices: List[types.LabeledPrice],
@ -2612,8 +2586,6 @@ class TeleBot:
send_email_to_provider, is_flexible) send_email_to_provider, is_flexible)
return result return result
# noinspection PyShadowingBuiltins # noinspection PyShadowingBuiltins
# TODO: rewrite this method like in API # TODO: rewrite this method like in API
def send_poll( def send_poll(
@ -2940,11 +2912,9 @@ class TeleBot:
:param result: A JSON-serialized object describing the message to be sent :param result: A JSON-serialized object describing the message to be sent
:return: :return:
""" """
return apihelper.answer_web_app_query(self.token, web_app_query_id, result) return apihelper.answer_web_app_query(self.token, web_app_query_id, result)
def register_for_reply( def register_for_reply(self, message: types.Message, callback: Callable, *args, **kwargs) -> None:
self, message: types.Message, callback: Callable, *args, **kwargs) -> None:
""" """
Registers a callback function to be notified when a reply to `message` arrives. Registers a callback function to be notified when a reply to `message` arrives.
@ -2984,8 +2954,7 @@ class TeleBot:
for handler in handlers: for handler in handlers:
self._exec_task(handler["callback"], message, *handler["args"], **handler["kwargs"]) self._exec_task(handler["callback"], message, *handler["args"], **handler["kwargs"])
def register_next_step_handler( def register_next_step_handler(self, message: types.Message, callback: Callable, *args, **kwargs) -> None:
self, message: types.Message, callback: Callable, *args, **kwargs) -> None:
""" """
Registers a callback function to be notified when new message arrives after `message`. Registers a callback function to be notified when new message arrives after `message`.
@ -2999,7 +2968,6 @@ class TeleBot:
chat_id = message.chat.id chat_id = message.chat.id
self.register_next_step_handler_by_chat_id(chat_id, callback, *args, **kwargs) self.register_next_step_handler_by_chat_id(chat_id, callback, *args, **kwargs)
def setup_middleware(self, middleware: BaseMiddleware): def setup_middleware(self, middleware: BaseMiddleware):
""" """
Register middleware Register middleware
@ -3012,8 +2980,6 @@ class TeleBot:
return return
self.middlewares.append(middleware) self.middlewares.append(middleware)
def set_state(self, user_id: int, state: Union[int, str, State], chat_id: int=None) -> None: def set_state(self, user_id: int, state: Union[int, str, State], chat_id: int=None) -> None:
""" """
Sets a new state of a user. Sets a new state of a user.
@ -3035,8 +3001,8 @@ class TeleBot:
""" """
if chat_id is None: if chat_id is None:
chat_id = user_id chat_id = user_id
self.current_states.reset_data(chat_id, user_id) self.current_states.reset_data(chat_id, user_id)
def delete_state(self, user_id: int, chat_id: int=None) -> None: def delete_state(self, user_id: int, chat_id: int=None) -> None:
""" """
Delete the current state of a user. Delete the current state of a user.
@ -3449,7 +3415,6 @@ class TeleBot:
**kwargs) **kwargs)
self.add_edited_message_handler(handler_dict) self.add_edited_message_handler(handler_dict)
def channel_post_handler(self, commands=None, regexp=None, func=None, content_types=None, **kwargs): def channel_post_handler(self, commands=None, regexp=None, func=None, content_types=None, **kwargs):
""" """
Channel post handler decorator Channel post handler decorator
@ -4087,7 +4052,6 @@ class TeleBot:
elif isinstance(result, SkipHandler) and skip_handler is False: elif isinstance(result, SkipHandler) and skip_handler is False:
skip_handler = True skip_handler = True
try: try:
if handlers and not skip_handler: if handlers and not skip_handler:
for handler in handlers: for handler in handlers:
@ -4122,10 +4086,8 @@ class TeleBot:
return return
handler["function"](message, **data_copy) handler["function"](message, **data_copy)
except Exception as e: except Exception as e:
handler_error = e handler_error = e
if self.exception_handler: if self.exception_handler:
self.exception_handler.handle(e) self.exception_handler.handle(e)
else: logging.error(str(e)) else: logging.error(str(e))
@ -4134,9 +4096,6 @@ class TeleBot:
for middleware in middlewares: for middleware in middlewares:
middleware.post_process(message, data, handler_error) middleware.post_process(message, data, handler_error)
def _notify_command_handlers(self, handlers, new_messages, update_type): def _notify_command_handlers(self, handlers, new_messages, update_type):
""" """
Notifies command handlers. Notifies command handlers.