X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions telegram/ext/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ def start_webhook(self,
key=None,
clean=False,
bootstrap_retries=0,
webhook_url=None):
webhook_url=None,
allowed_updates=None):
"""
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
Expand All @@ -215,9 +216,10 @@ def start_webhook(self,
| < 0 - retry indefinitely
| 0 - no retries (default)
| > 0 - retry up to X times
webhook_url (Optional[str]): Explicitly specifiy the webhook url.
webhook_url (Optional[str]): Explicitly specify the webhook url.
Useful behind NAT, reverse proxy, etc. Default is derived from
`listen`, `port` & `url_path`.
allowed_updates (Optional[list[str]]): Passed to Bot.setWebhook

Returns:
Queue: The update queue that can be filled from the main thread
Expand All @@ -231,7 +233,7 @@ def start_webhook(self,
self.job_queue.start()
self._init_thread(self.dispatcher.start, "dispatcher"),
self._init_thread(self._start_webhook, "updater", listen, port, url_path, cert,
key, bootstrap_retries, clean, webhook_url)
key, bootstrap_retries, clean, webhook_url, allowed_updates)

# Return the update queue so the main thread can insert updates
return self.update_queue
Expand All @@ -247,7 +249,7 @@ def _start_polling(self, poll_interval, timeout, read_latency, bootstrap_retries
cur_interval = poll_interval
self.logger.debug('Updater thread started')

self._bootstrap(bootstrap_retries, clean=clean, webhook_url='')
self._bootstrap(bootstrap_retries, clean=clean, webhook_url='', allowed_updates=None)

while self.running:
try:
Expand Down Expand Up @@ -295,7 +297,7 @@ def _increase_poll_interval(current_interval):
return current_interval

def _start_webhook(self, listen, port, url_path, cert, key, bootstrap_retries, clean,
webhook_url):
webhook_url, allowed_updates):
self.logger.debug('Updater thread started')
use_ssl = cert is not None and key is not None
if not url_path.startswith('/'):
Expand All @@ -316,7 +318,8 @@ def _start_webhook(self, listen, port, url_path, cert, key, bootstrap_retries, c
max_retries=bootstrap_retries,
clean=clean,
webhook_url=webhook_url,
cert=open(cert, 'rb'))
cert=open(cert, 'rb'),
allowed_updates=allowed_updates)
elif clean:
self.logger.warning("cleaning updates is not supported if "
"SSL-termination happens elsewhere; skipping")
Expand Down Expand Up @@ -346,7 +349,7 @@ def _check_ssl_cert(self, cert, key):
def _gen_webhook_url(listen, port, url_path):
return 'https://{listen}:{port}{path}'.format(listen=listen, port=port, path=url_path)

def _bootstrap(self, max_retries, clean, webhook_url, cert=None):
def _bootstrap(self, max_retries, clean, webhook_url, allowed_updates, cert=None):
retries = 0
while 1:

Expand All @@ -357,7 +360,8 @@ def _bootstrap(self, max_retries, clean, webhook_url, cert=None):
self._clean_updates()
sleep(1)

self.bot.setWebhook(url=webhook_url, certificate=cert)
self.bot.setWebhook(
url=webhook_url, certificate=cert, allowed_updates=allowed_updates)
except (Unauthorized, InvalidToken):
raise
except TelegramError:
Expand Down
5 changes: 3 additions & 2 deletions tests/test_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,8 @@ def test_webhook_invalid_posts(self):
ip = '127.0.0.1'
port = randrange(1024, 49152) # select random port for travis
thr = Thread(
target=self.updater._start_webhook, args=(ip, port, '', None, None, 0, False, None))
target=self.updater._start_webhook,
args=(ip, port, '', None, None, 0, False, None, None))
thr.start()

sleep(0.5)
Expand Down Expand Up @@ -832,7 +833,7 @@ def mockUpdate(self, text):

return update

def setWebhook(self, url=None, certificate=None):
def setWebhook(self, url=None, certificate=None, allowed_updates=None):
if self.bootstrap_retries is None:
return

Expand Down
X Tutup