mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-26 00:27:12 +01:00
parent
41d0d45e2f
commit
594b81e463
1 changed files with 34 additions and 6 deletions
|
@ -30,6 +30,7 @@ import subprocess
|
||||||
from signal import signal, SIGINT, SIGTERM, SIGABRT
|
from signal import signal, SIGINT, SIGTERM, SIGABRT
|
||||||
from telegram import Bot, TelegramError, NullHandler
|
from telegram import Bot, TelegramError, NullHandler
|
||||||
from telegram.ext import dispatcher, Dispatcher, JobQueue, UpdateQueue
|
from telegram.ext import dispatcher, Dispatcher, JobQueue, UpdateQueue
|
||||||
|
from telegram.error import Unauthorized, InvalidToken
|
||||||
from telegram.utils.webhookhandler import (WebhookServer, WebhookHandler)
|
from telegram.utils.webhookhandler import (WebhookServer, WebhookHandler)
|
||||||
|
|
||||||
logging.getLogger(__name__).addHandler(NullHandler())
|
logging.getLogger(__name__).addHandler(NullHandler())
|
||||||
|
@ -94,7 +95,7 @@ class Updater:
|
||||||
""":type: list[Thread]"""
|
""":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):
|
clean=False, bootstrap_retries=0):
|
||||||
"""
|
"""
|
||||||
Starts polling updates from Telegram.
|
Starts polling updates from Telegram.
|
||||||
|
|
||||||
|
@ -106,6 +107,11 @@ class Updater:
|
||||||
clean (Optional[bool]): Whether to clean any pending updates on
|
clean (Optional[bool]): Whether to clean any pending updates on
|
||||||
Telegram servers before actually starting to poll. Default is
|
Telegram servers before actually starting to poll. Default is
|
||||||
False.
|
False.
|
||||||
|
bootstrap_retries (Optional[int[): Whether the bootstrapping phase
|
||||||
|
of the `Updater` will retry on failures on the Telegram server.
|
||||||
|
< 0 - retry indefinitely
|
||||||
|
0 - no retries (default)
|
||||||
|
> 0 - retry up to X times
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Queue: The update queue that can be filled from the main thread
|
Queue: The update queue that can be filled from the main thread
|
||||||
|
@ -120,7 +126,8 @@ class Updater:
|
||||||
# Create & start threads
|
# Create & start threads
|
||||||
self._init_thread(self.dispatcher.start, "dispatcher")
|
self._init_thread(self.dispatcher.start, "dispatcher")
|
||||||
self._init_thread(self._start_polling, "updater",
|
self._init_thread(self._start_polling, "updater",
|
||||||
poll_interval, timeout, network_delay)
|
poll_interval, timeout, network_delay,
|
||||||
|
bootstrap_retries)
|
||||||
|
|
||||||
# Return the update queue so the main thread can insert updates
|
# Return the update queue so the main thread can insert updates
|
||||||
return self.update_queue
|
return self.update_queue
|
||||||
|
@ -185,18 +192,18 @@ class Updater:
|
||||||
# Return the update queue so the main thread can insert updates
|
# Return the update queue so the main thread can insert updates
|
||||||
return self.update_queue
|
return self.update_queue
|
||||||
|
|
||||||
def _start_polling(self, poll_interval, timeout, network_delay):
|
def _start_polling(self, poll_interval, timeout, network_delay,
|
||||||
|
bootstrap_retries):
|
||||||
"""
|
"""
|
||||||
Thread target of thread 'updater'. Runs in background, pulls
|
Thread target of thread 'updater'. Runs in background, pulls
|
||||||
updates from Telegram and inserts them in the update queue of the
|
updates from Telegram and inserts them in the update queue of the
|
||||||
Dispatcher.
|
Dispatcher.
|
||||||
"""
|
|
||||||
|
|
||||||
|
"""
|
||||||
cur_interval = poll_interval
|
cur_interval = poll_interval
|
||||||
self.logger.debug('Updater thread started')
|
self.logger.debug('Updater thread started')
|
||||||
|
|
||||||
# Remove webhook
|
self._set_webhook(None, bootstrap_retries)
|
||||||
self.bot.setWebhook(webhook_url=None)
|
|
||||||
|
|
||||||
while self.running:
|
while self.running:
|
||||||
try:
|
try:
|
||||||
|
@ -228,6 +235,27 @@ class Updater:
|
||||||
|
|
||||||
sleep(cur_interval)
|
sleep(cur_interval)
|
||||||
|
|
||||||
|
def _set_webhook(self, webhook_url, max_retries):
|
||||||
|
retries = 0
|
||||||
|
while 1:
|
||||||
|
try:
|
||||||
|
# Remove webhook
|
||||||
|
self.bot.setWebhook(webhook_url=webhook_url)
|
||||||
|
except (Unauthorized, InvalidToken):
|
||||||
|
raise
|
||||||
|
except TelegramError:
|
||||||
|
msg = 'failed to set webhook; try={0} max_retries={1}'.format(
|
||||||
|
retries, max_retries)
|
||||||
|
if max_retries < 0 or retries < max_retries:
|
||||||
|
self.logger.info(msg)
|
||||||
|
retries += 1
|
||||||
|
else:
|
||||||
|
self.logger.exception(msg)
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
sleep(1)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _increase_poll_interval(current_interval):
|
def _increase_poll_interval(current_interval):
|
||||||
# increase waiting times on subsequent errors up to 30secs
|
# increase waiting times on subsequent errors up to 30secs
|
||||||
|
|
Loading…
Add table
Reference in a new issue