Skip to content

Commit 594b81e

Browse files
committed
start_polling(): new argument - bootstrap_retries
refs python-telegram-bot#196
1 parent 41d0d45 commit 594b81e

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

telegram/ext/updater.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from signal import signal, SIGINT, SIGTERM, SIGABRT
3131
from telegram import Bot, TelegramError, NullHandler
3232
from telegram.ext import dispatcher, Dispatcher, JobQueue, UpdateQueue
33+
from telegram.error import Unauthorized, InvalidToken
3334
from telegram.utils.webhookhandler import (WebhookServer, WebhookHandler)
3435

3536
logging.getLogger(__name__).addHandler(NullHandler())
@@ -94,7 +95,7 @@ def __init__(self,
9495
""":type: list[Thread]"""
9596

9697
def start_polling(self, poll_interval=0.0, timeout=10, network_delay=2,
97-
clean=False):
98+
clean=False, bootstrap_retries=0):
9899
"""
99100
Starts polling updates from Telegram.
100101
@@ -106,6 +107,11 @@ def start_polling(self, poll_interval=0.0, timeout=10, network_delay=2,
106107
clean (Optional[bool]): Whether to clean any pending updates on
107108
Telegram servers before actually starting to poll. Default is
108109
False.
110+
bootstrap_retries (Optional[int[): Whether the bootstrapping phase
111+
of the `Updater` will retry on failures on the Telegram server.
112+
< 0 - retry indefinitely
113+
0 - no retries (default)
114+
> 0 - retry up to X times
109115
110116
Returns:
111117
Queue: The update queue that can be filled from the main thread
@@ -120,7 +126,8 @@ def start_polling(self, poll_interval=0.0, timeout=10, network_delay=2,
120126
# Create & start threads
121127
self._init_thread(self.dispatcher.start, "dispatcher")
122128
self._init_thread(self._start_polling, "updater",
123-
poll_interval, timeout, network_delay)
129+
poll_interval, timeout, network_delay,
130+
bootstrap_retries)
124131

125132
# Return the update queue so the main thread can insert updates
126133
return self.update_queue
@@ -185,18 +192,18 @@ def start_webhook(self,
185192
# Return the update queue so the main thread can insert updates
186193
return self.update_queue
187194

188-
def _start_polling(self, poll_interval, timeout, network_delay):
195+
def _start_polling(self, poll_interval, timeout, network_delay,
196+
bootstrap_retries):
189197
"""
190198
Thread target of thread 'updater'. Runs in background, pulls
191199
updates from Telegram and inserts them in the update queue of the
192200
Dispatcher.
193-
"""
194201
202+
"""
195203
cur_interval = poll_interval
196204
self.logger.debug('Updater thread started')
197205

198-
# Remove webhook
199-
self.bot.setWebhook(webhook_url=None)
206+
self._set_webhook(None, bootstrap_retries)
200207

201208
while self.running:
202209
try:
@@ -228,6 +235,27 @@ def _start_polling(self, poll_interval, timeout, network_delay):
228235

229236
sleep(cur_interval)
230237

238+
def _set_webhook(self, webhook_url, max_retries):
239+
retries = 0
240+
while 1:
241+
try:
242+
# Remove webhook
243+
self.bot.setWebhook(webhook_url=webhook_url)
244+
except (Unauthorized, InvalidToken):
245+
raise
246+
except TelegramError:
247+
msg = 'failed to set webhook; try={0} max_retries={1}'.format(
248+
retries, max_retries)
249+
if max_retries < 0 or retries < max_retries:
250+
self.logger.info(msg)
251+
retries += 1
252+
else:
253+
self.logger.exception(msg)
254+
raise
255+
else:
256+
break
257+
sleep(1)
258+
231259
@staticmethod
232260
def _increase_poll_interval(current_interval):
233261
# increase waiting times on subsequent errors up to 30secs

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy