From f0e7a3316cabbf96e9b402a95cc803aaa64d382a Mon Sep 17 00:00:00 2001 From: Noam Meltzer Date: Tue, 1 Mar 2016 20:20:51 +0200 Subject: [PATCH 1/2] jobqueue: fix docstring --- telegram/jobqueue.py | 3 +-- telegram/updater.py | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/telegram/jobqueue.py b/telegram/jobqueue.py index 03e3b4c25..7e0cc2458 100644 --- a/telegram/jobqueue.py +++ b/telegram/jobqueue.py @@ -41,10 +41,9 @@ class JobQueue(object): Args: bot (Bot): The bot instance that should be passed to the jobs - - Keyword Args: tick_interval (Optional[float]): The interval this queue should check the newest task in seconds. Defaults to 1.0 + """ def __init__(self, bot, tick_interval=1.0): diff --git a/telegram/updater.py b/telegram/updater.py index 651619330..3d502f564 100644 --- a/telegram/updater.py +++ b/telegram/updater.py @@ -58,6 +58,8 @@ class Updater: workers (Optional[int]): Amount of threads in the thread pool for functions decorated with @run_async bot (Optional[Bot]): + job_queue_tick_interval(Optional[float]): The interval the queue should + be checked for new tasks. Defaults to 1.0 Raises: ValueError: If both `token` and `bot` are passed or none of them. From a0a040a9c2346241ce0539338db7f653fbd59c3b Mon Sep 17 00:00:00 2001 From: Noam Meltzer Date: Tue, 1 Mar 2016 21:40:04 +0200 Subject: [PATCH 2/2] updater: allow cleaning updates from Telegram servers before start --- telegram/updater.py | 25 ++++++++++++++++++++++--- tests/test_updater.py | 10 +++++++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/telegram/updater.py b/telegram/updater.py index 3d502f564..68344f841 100644 --- a/telegram/updater.py +++ b/telegram/updater.py @@ -94,7 +94,8 @@ class Updater: self.__threads = [] """:type: list[Thread]""" - def start_polling(self, poll_interval=0.0, timeout=10, network_delay=2): + def start_polling(self, poll_interval=0.0, timeout=10, network_delay=2, + clean=False): """ Starts polling updates from Telegram. @@ -103,14 +104,19 @@ class Updater: updates from Telegram in seconds. Default is 0.0 timeout (Optional[float]): Passed to Bot.getUpdates network_delay (Optional[float]): Passed to Bot.getUpdates + clean (Optional[bool]): Whether to clean any pending updates on + Telegram servers before actually starting to poll. Default is + False. Returns: Queue: The update queue that can be filled from the main thread - """ + """ with self.__lock: if not self.running: self.running = True + if clean: + self._clean_updates() # Create & start threads self._init_thread(self.dispatcher.start, "dispatcher") @@ -142,7 +148,8 @@ class Updater: port=80, url_path='', cert=None, - key=None): + key=None, + clean=False): """ Starts a small http server to listen for updates via webhook. If cert and key are not provided, the webhook will be started directly on @@ -156,6 +163,10 @@ class Updater: url_path (Optional[str]): Path inside url cert (Optional[str]): Path to the SSL certificate file key (Optional[str]): Path to the SSL key file + clean (Optional[bool]): Whether to clean any pending updates on + Telegram servers before actually starting the webhook. Default + is False. + Returns: Queue: The update queue that can be filled from the main thread @@ -164,6 +175,8 @@ class Updater: with self.__lock: if not self.running: self.running = True + if clean: + self._clean_updates() # Create & start threads self._init_thread(self.dispatcher.start, "dispatcher"), @@ -260,6 +273,12 @@ class Updater: self.httpd.serve_forever(poll_interval=1) + def _clean_updates(self): + self.logger.info('Cleaning updates from Telegram server') + updates = self.bot.getUpdates() + while updates: + updates = self.bot.getUpdates(updates[-1].update_id + 1) + def stop(self): """ Stops the polling/webhook thread, the dispatcher and the job queue diff --git a/tests/test_updater.py b/tests/test_updater.py index 5e26b6825..46778dea1 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -301,6 +301,15 @@ class UpdaterTest(BaseTest, unittest.TestCase): sleep(.1) self.assertEqual(self.received_message, 'Test Error 1') + def test_cleanBeforeStart(self): + self._setup_updater('') + d = self.updater.dispatcher + d.addTelegramMessageHandler(self.telegramHandlerTest) + self.updater.start_polling(0.01, clean=True) + sleep(.1) + self.assertEqual(self.message_count, 0) + self.assertIsNone(self.received_message) + def test_errorOnGetUpdates(self): self._setup_updater('', raise_error=True) d = self.updater.dispatcher @@ -396,7 +405,6 @@ class UpdaterTest(BaseTest, unittest.TestCase): self.assertEqual(self.received_message, (('This', 'regex group'), {'testgroup': 'regex group'})) - def test_runAsyncWithAdditionalArgs(self): self._setup_updater('Test6', messages=2) d = self.updater.dispatcher