Skip to content

Too Many Requests error when start_polling(clean=True) #422

@gelin

Description

@gelin

Steps to reproduce

Case 1

  1. updater.start_polling(clean=True)
  2. Telegram API returns Too Many Requests: retry after 1 (429)

Case 2

  1. updater.start_polling(clean=False)
  2. No errors

Expected behaviour

No exception or errors from Telegram API with clean True or False

Actual behaviour

Too Many Requests from Telegram API if clean=True
No errors if clean=False

Configuration

Operating System:
Ubuntu 16.04, Debian Jessie

Version of Python, python-telegram-bot & dependencies:
$ python -m telegram

python-telegram-bot 5.1.0
urllib3 1.16
certifi 2016.08.08
future 0.15.2
Python 3.5.2 (default, Jul  5 2016, 12:43:10)  [GCC 5.4.0 20160609]
python-telegram-bot 5.0.0
urllib3 1.16
certifi 2016.02.28
future 0.15.2
Python 3.4.2 (default, Oct  8 2014, 10:45:20)  [GCC 4.9.1]

Logs

2016-09-25 00:03:46,616 - telegram.ext.updater - ERROR - error in bootstrap phase; try=0 max_retries=0
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/telegram/ext/updater.py", line 331, in _bootstrap
    self.bot.setWebhook(webhook_url=webhook_url, certificate=cert)
  File "/usr/local/lib/python3.5/dist-packages/telegram/bot.py", line 126, in decorator
    result = func(self, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/telegram/bot.py", line 1264, in setWebhook
    result = self._request.post(url, data, timeout=kwargs.get('timeout'))
  File "/usr/local/lib/python3.5/dist-packages/telegram/utils/request.py", line 201, in post
    **urlopen_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/telegram/utils/request.py", line 152, in _request_wrapper
    raise NetworkError('{0} ({1})'.format(message, resp.status))
telegram.error.NetworkError: Too Many Requests: retry after 1 (429)
2016-09-25 00:03:46,617 - telegram.ext.updater - ERROR - unhandled exception
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/telegram/ext/updater.py", line 117, in _thread_wrapper
    target(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/telegram/ext/updater.py", line 228, in _start_polling
    self._bootstrap(bootstrap_retries, clean=clean, webhook_url='')
  File "/usr/local/lib/python3.5/dist-packages/telegram/ext/updater.py", line 331, in _bootstrap
    self.bot.setWebhook(webhook_url=webhook_url, certificate=cert)
  File "/usr/local/lib/python3.5/dist-packages/telegram/bot.py", line 126, in decorator
    result = func(self, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/telegram/bot.py", line 1264, in setWebhook
    result = self._request.post(url, data, timeout=kwargs.get('timeout'))
  File "/usr/local/lib/python3.5/dist-packages/telegram/utils/request.py", line 201, in post
    **urlopen_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/telegram/utils/request.py", line 152, in _request_wrapper
    raise NetworkError('{0} ({1})'.format(message, resp.status))
telegram.error.NetworkError: Too Many Requests: retry after 1 (429)
Exception in thread updater:
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.5/dist-packages/telegram/ext/updater.py", line 117, in _thread_wrapper
    target(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/telegram/ext/updater.py", line 228, in _start_polling
    self._bootstrap(bootstrap_retries, clean=clean, webhook_url='')
  File "/usr/local/lib/python3.5/dist-packages/telegram/ext/updater.py", line 331, in _bootstrap
    self.bot.setWebhook(webhook_url=webhook_url, certificate=cert)
  File "/usr/local/lib/python3.5/dist-packages/telegram/bot.py", line 126, in decorator
    result = func(self, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/telegram/bot.py", line 1264, in setWebhook
    result = self._request.post(url, data, timeout=kwargs.get('timeout'))
  File "/usr/local/lib/python3.5/dist-packages/telegram/utils/request.py", line 201, in post
    **urlopen_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/telegram/utils/request.py", line 152, in _request_wrapper
    raise NetworkError('{0} ({1})'.format(message, resp.status))
telegram.error.NetworkError: Too Many Requests: retry after 1 (429)

Additional information

Bot Support said:

This kind of "Too many requests" are usually it is caused because you call setWebhook too often

Actually updater.py calls setWebhook twice:

                if clean:
                    # Disable webhook for cleaning
                    self.bot.setWebhook(webhook_url='')
                    self._clean_updates()

                self.bot.setWebhook(webhook_url=webhook_url, certificate=cert)

Maybe it's not necessary to do setWebhook when polling. Or some timeout should be set between these two calls.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions