diff --git a/telebot/__init__.py b/telebot/__init__.py index a6a20ff..f166a27 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -45,6 +45,7 @@ class ExceptionHandler: Class for handling exceptions while Polling """ + # noinspection PyMethodMayBeStatic,PyUnusedLocal def handle(self, exception): return False @@ -235,7 +236,8 @@ class TeleBot: """ self.reply_backend.load_handlers(filename, del_file_after_loading) - def set_webhook(self, url=None, certificate=None, max_connections=None, allowed_updates=None, ip_address=None, timeout=None): + def set_webhook(self, url=None, certificate=None, max_connections=None, allowed_updates=None, ip_address=None, + drop_pending_updates = None, timeout=None): """ Use this method to specify a url and receive incoming updates via an outgoing webhook. Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update. @@ -246,10 +248,11 @@ class TeleBot: :param max_connections: Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery, 1-100. Defaults to 40. Use lower values to limit the load on your bot's server, and higher values to increase your bot's throughput. :param allowed_updates: A JSON-serialized list of the update types you want your bot to receive. For example, specify [“message”, “edited_channel_post”, “callback_query”] to only receive updates of these types. See Update for a complete list of available update types. Specify an empty list to receive all updates regardless of type (default). If not specified, the previous setting will be used. :param ip_address: The fixed IP address which will be used to send webhook requests instead of the IP address resolved through DNS + :param drop_pending_updates: Pass True to drop all pending updates :param timeout: Integer. Request connection timeout :return: """ - return apihelper.set_webhook(self.token, url, certificate, max_connections, allowed_updates, ip_address, timeout) + return apihelper.set_webhook(self.token, url, certificate, max_connections, allowed_updates, ip_address, drop_pending_updates, timeout) def delete_webhook(self, drop_pending_updates=None, timeout=None): """ diff --git a/telebot/apihelper.py b/telebot/apihelper.py index f059cb4..1cedbc8 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -206,7 +206,8 @@ def send_message( return _make_request(token, method_url, params=payload, method='post') -def set_webhook(token, url=None, certificate=None, max_connections=None, allowed_updates=None, ip_address=None, timeout=None): +def set_webhook(token, url=None, certificate=None, max_connections=None, allowed_updates=None, ip_address=None, + drop_pending_updates = None, timeout=None): method_url = r'setWebhook' payload = { 'url': url if url else "", @@ -216,10 +217,12 @@ def set_webhook(token, url=None, certificate=None, max_connections=None, allowed files = {'certificate': certificate} if max_connections: payload['max_connections'] = max_connections - if allowed_updates is not None: # Empty lists should pass + if allowed_updates is not None: # Empty lists should pass payload['allowed_updates'] = json.dumps(allowed_updates) - if ip_address is not None: # Empty string should pass + if ip_address is not None: # Empty string should pass payload['ip_address'] = ip_address + if drop_pending_updates is not None: # Any bool value should pass + payload['drop_pending_updates'] = drop_pending_updates if timeout: payload['connect-timeout'] = timeout return _make_request(token, method_url, params=payload, files=files) @@ -228,7 +231,7 @@ def set_webhook(token, url=None, certificate=None, max_connections=None, allowed def delete_webhook(token, drop_pending_updates=None, timeout=None): method_url = r'deleteWebhook' payload = {} - if drop_pending_updates is not None: # None / True / False + if drop_pending_updates is not None: # Any bool value should pass payload['drop_pending_updates'] = drop_pending_updates if timeout: payload['connect-timeout'] = timeout diff --git a/tests/test_telebot.py b/tests/test_telebot.py index 6ca56fb..547d29d 100644 --- a/tests/test_telebot.py +++ b/tests/test_telebot.py @@ -48,6 +48,7 @@ class TestTeleBot: bot = telebot.TeleBot('') msg = self.create_text_message(r'https://web.telegram.org/') + # noinspection PyUnusedLocal @bot.message_handler(regexp=r'((https?):((//)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)') def command_url(message): msg.text = 'got' @@ -60,6 +61,7 @@ class TestTeleBot: bot = telebot.TeleBot('') msg = self.create_text_message(r'lambda_text') + # noinspection PyUnusedLocal @bot.message_handler(func=lambda message: r'lambda' in message.text) def command_url(message): msg.text = 'got' @@ -72,6 +74,7 @@ class TestTeleBot: bot = telebot.TeleBot('') msg = self.create_text_message(r'text') + # noinspection PyUnusedLocal @bot.message_handler(func=lambda message: r'lambda' in message.text) def command_url(message): msg.text = 'got' @@ -84,6 +87,7 @@ class TestTeleBot: bot = telebot.TeleBot('') msg = self.create_text_message(r'web.telegram.org/') + # noinspection PyUnusedLocal @bot.message_handler(regexp=r'((https?):((//)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)') def command_url(message): msg.text = 'got' @@ -522,6 +526,7 @@ class TestTeleBot: tb = telebot.TeleBot('') update = self.create_message_update('/help') + # noinspection PyUnusedLocal @tb.middleware_handler(update_types=['message']) def middleware(tb_instance, message): message.text = 'got' @@ -542,9 +547,10 @@ class TestTeleBot: tb = telebot.TeleBot('') update = self.create_message_update('/help') + # noinspection PyUnusedLocal @tb.middleware_handler() - def middleware(tb_instance, update): - update.message.text = 'got' + def middleware(tb_instance, mw_update): + mw_update.message.text = 'got' @tb.message_handler(func=lambda m: m.text == 'got') def command_handler(message): @@ -556,6 +562,6 @@ class TestTeleBot: def test_chat_permissions(self): return # CHAT_ID is private chat, no permissions can be set - tb = telebot.TeleBot(TOKEN) - permissions = types.ChatPermissions(can_send_messages=True, can_send_polls=False) - msg = tb.set_chat_permissions(CHAT_ID, permissions) + #tb = telebot.TeleBot(TOKEN) + #permissions = types.ChatPermissions(can_send_messages=True, can_send_polls=False) + #msg = tb.set_chat_permissions(CHAT_ID, permissions)