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
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `Anton Tagunov <https://github.com/anton-tagunov>`_
- `Balduro <https://github.com/Balduro>`_
- `bimmlerd <https://github.com/bimmlerd>`_
- `d-qoi <https://github.com/d-qoi>`
- `daimajia <https://github.com/daimajia>`_
- `Eli Gao <https://github.com/eligao>`_
- `ErgoZ Riftbit Vaper <https://github.com/ergoz>`_
Expand Down
15 changes: 14 additions & 1 deletion telegram/ext/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class Updater(object):
bot (Optional[Bot]): A pre-initialized bot instance. If a pre-initizlied bot is used, it is
the user's responsibility to create it using a `Request` instance with a large enough
connection pool.
user_sig_handler (Optional[function]): Takes ``signum, frame`` as positional arguments.
This will be called when a signal is received, defaults are (SIGINT, SIGTERM, SIGABRT)
setable with Updater.idle(stop_signals=(signals))
request_kwargs (Optional[dict]): Keyword args to control the creation of a request object
(ignored if `bot` argument is used).

Expand All @@ -71,7 +74,14 @@ class Updater(object):
"""
_request = None

def __init__(self, token=None, base_url=None, workers=4, bot=None, request_kwargs=None):
def __init__(self,
token=None,
base_url=None,
workers=4,
bot=None,
user_sig_handler=None,
request_kwargs=None):

if (token is None) and (bot is None):
raise ValueError('`token` or `bot` must be passed')
if (token is not None) and (bot is not None):
Expand All @@ -92,6 +102,7 @@ def __init__(self, token=None, base_url=None, workers=4, bot=None, request_kwarg
request_kwargs['con_pool_size'] = workers + 4
self._request = Request(**request_kwargs)
self.bot = Bot(token, base_url, request=self._request)
self.user_sig_handler = user_sig_handler
self.update_queue = Queue()
self.job_queue = JobQueue(self.bot)
self.__exception_event = Event()
Expand Down Expand Up @@ -422,6 +433,8 @@ def signal_handler(self, signum, frame):
self.is_idle = False
if self.running:
self.stop()
if self.user_sig_handler:
self.user_sig_handler(signum, frame)
else:
self.logger.warning('Exiting immediately!')
import os
Expand Down
17 changes: 17 additions & 0 deletions tests/test_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,23 @@ def test_idle(self):
sleep(1)
self.assertFalse(self.updater.running)

def test_userSignal(self):
self._setup_updater('Test7', messages=0)

tempVar = {'a': 0}

def userSignalInc(signum, frame):
tempVar['a'] = 1

self.updater.user_sig_handler = userSignalInc
self.updater.start_polling(poll_interval=0.01)
Thread(target=self.signalsender).start()
self.updater.idle()
# If we get this far, idle() ran through
sleep(1)
self.assertFalse(self.updater.running)
self.assertTrue(tempVar['a'] != 0)

def test_createBot(self):
self.updater = Updater('123:abcd')
self.assertIsNotNone(self.updater.bot)
Expand Down
X Tutup