-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
_application.py
1530 lines (1285 loc) · 65.7 KB
/
_application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2022
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains the Application class."""
import asyncio
import inspect
import itertools
import logging
import platform
import signal
from collections import defaultdict
from contextlib import AbstractAsyncContextManager
from copy import deepcopy
from pathlib import Path
from types import MappingProxyType, TracebackType
from typing import (
TYPE_CHECKING,
Any,
Callable,
Coroutine,
DefaultDict,
Dict,
Generic,
List,
Mapping,
NoReturn,
Optional,
Sequence,
Set,
Tuple,
Type,
TypeVar,
Union,
)
from telegram._update import Update
from telegram._utils.defaultvalue import DEFAULT_NONE, DEFAULT_TRUE, DefaultValue
from telegram._utils.types import DVInput, ODVInput
from telegram._utils.warnings import warn
from telegram.error import TelegramError
from telegram.ext._basepersistence import BasePersistence
from telegram.ext._contexttypes import ContextTypes
from telegram.ext._extbot import ExtBot
from telegram.ext._handler import BaseHandler
from telegram.ext._updater import Updater
from telegram.ext._utils.stack import was_called_by
from telegram.ext._utils.trackingdict import TrackingDict
from telegram.ext._utils.types import BD, BT, CCT, CD, JQ, UD, ConversationKey, HandlerCallback
if TYPE_CHECKING:
from telegram import Message
from telegram.ext import ConversationHandler
from telegram.ext._applicationbuilder import InitApplicationBuilder
from telegram.ext._jobqueue import Job
DEFAULT_GROUP: int = 0
_AppType = TypeVar("_AppType", bound="Application") # pylint: disable=invalid-name
_RT = TypeVar("_RT")
_STOP_SIGNAL = object()
_logger = logging.getLogger(__name__)
class ApplicationHandlerStop(Exception):
"""
Raise this in a handler or an error handler to prevent execution of any other handler (even in
different groups).
In order to use this exception in a :class:`telegram.ext.ConversationHandler`, pass the
optional :paramref:`state` parameter instead of returning the next state:
.. code-block:: python
async def conversation_callback(update, context):
...
raise ApplicationHandlerStop(next_state)
Note:
Has no effect, if the handler or error handler is run in a non-blocking way.
Args:
state (:obj:`object`, optional): The next state of the conversation.
Attributes:
state (:obj:`object`): Optional. The next state of the conversation.
"""
__slots__ = ("state",)
def __init__(self, state: object = None) -> None:
super().__init__()
self.state = state
class Application(Generic[BT, CCT, UD, CD, BD, JQ], AbstractAsyncContextManager):
"""This class dispatches all kinds of updates to its registered handlers, and is the entry
point to a PTB application.
Tip:
This class may not be initialized directly. Use :class:`telegram.ext.ApplicationBuilder`
or :meth:`builder` (for convenience).
Instances of this class can be used as asyncio context managers, where
.. code:: python
async with application:
# code
is roughly equivalent to
.. code:: python
try:
await application.initialize()
# code
finally:
await application.shutdown()
.. versionchanged:: 20.0
* Initialization is now done through the :class:`telegram.ext.ApplicationBuilder`.
* Removed the attribute ``groups``.
Attributes:
bot (:class:`telegram.Bot`): The bot object that should be passed to the handlers.
update_queue (:class:`asyncio.Queue`): The synchronized queue that will contain the
updates.
updater (:class:`telegram.ext.Updater`): Optional. The updater used by this application.
job_queue (:class:`telegram.ext.JobQueue`): Optional. The :class:`telegram.ext.JobQueue`
instance to pass onto handler callbacks.
chat_data (:obj:`types.MappingProxyType`): A dictionary handlers can use to store data for
the chat. For each integer chat id, the corresponding value of this mapping is
available as :attr:`telegram.ext.CallbackContext.chat_data` in handler callbacks for
updates from that chat.
.. versionchanged:: 20.0
:attr:`chat_data` is now read-only. Note that the values of the mapping are still
mutable, i.e. editing ``context.chat_data`` within a handler callback is possible
(and encouraged), but editing the mapping ``application.chat_data`` itself is not.
.. tip::
Manually modifying :attr:`chat_data` is almost never needed and unadvisable.
user_data (:obj:`types.MappingProxyType`): A dictionary handlers can use to store data for
the user. For each integer user id, the corresponding value of this mapping is
available as :attr:`telegram.ext.CallbackContext.user_data` in handler callbacks for
updates from that user.
.. versionchanged:: 20.0
:attr:`user_data` is now read-only. Note that the values of the mapping are still
mutable, i.e. editing ``context.user_data`` within a handler callback is possible
(and encouraged), but editing the mapping ``application.user_data`` itself is not.
.. tip::
Manually modifying :attr:`user_data` is almost never needed and unadvisable.
bot_data (:obj:`dict`): A dictionary handlers can use to store data for the bot.
persistence (:class:`telegram.ext.BasePersistence`): The persistence class to
store data that should be persistent over restarts.
handlers (Dict[:obj:`int`, List[:class:`telegram.ext.BaseHandler`]]): A dictionary mapping
each handler group to the list of handlers registered to that group.
.. seealso::
:meth:`add_handler`, :meth:`add_handlers`.
error_handlers (Dict[:term:`coroutine function`, :obj:`bool`]): A dictionary where the keys
are error handlers and the values indicate whether they are to be run blocking.
.. seealso::
:meth:`add_error_handler`
context_types (:class:`telegram.ext.ContextTypes`): Specifies the types used by this
dispatcher for the ``context`` argument of handler and job callbacks.
post_init (:term:`coroutine function`): Optional. A callback that will be executed by
:meth:`Application.run_polling` and :meth:`Application.run_webhook` after initializing
the application via :meth:`initialize`.
post_shutdown (:term:`coroutine function`): Optional. A callback that will be executed by
:meth:`Application.run_polling` and :meth:`Application.run_webhook` after shutting down
the application via :meth:`shutdown`.
"""
# Allowing '__weakref__' creation here since we need it for the JobQueue
__slots__ = (
"__create_task_tasks",
"__update_fetcher_task",
"__update_persistence_event",
"__update_persistence_lock",
"__update_persistence_task",
"__weakref__",
"_chat_data",
"_chat_ids_to_be_deleted_in_persistence",
"_chat_ids_to_be_updated_in_persistence",
"_concurrent_updates",
"_concurrent_updates_sem",
"_conversation_handler_conversations",
"_initialized",
"_running",
"_user_data",
"_user_ids_to_be_deleted_in_persistence",
"_user_ids_to_be_updated_in_persistence",
"bot",
"bot_data",
"chat_data",
"context_types",
"error_handlers",
"handlers",
"job_queue",
"persistence",
"post_init",
"post_shutdown",
"update_queue",
"updater",
"user_data",
)
def __init__(
self: "Application[BT, CCT, UD, CD, BD, JQ]",
*,
bot: BT,
update_queue: asyncio.Queue,
updater: Optional[Updater],
job_queue: JQ,
concurrent_updates: Union[bool, int],
persistence: Optional[BasePersistence],
context_types: ContextTypes[CCT, UD, CD, BD],
post_init: Optional[
Callable[["Application[BT, CCT, UD, CD, BD, JQ]"], Coroutine[Any, Any, None]]
],
post_shutdown: Optional[
Callable[["Application[BT, CCT, UD, CD, BD, JQ]"], Coroutine[Any, Any, None]]
],
):
if not was_called_by(
inspect.currentframe(), Path(__file__).parent.resolve() / "_applicationbuilder.py"
):
warn(
"`Application` instances should be built via the `ApplicationBuilder`.",
stacklevel=2,
)
self.bot = bot
self.update_queue = update_queue
self.job_queue = job_queue
self.context_types = context_types
self.updater = updater
self.handlers: Dict[int, List[BaseHandler]] = {}
self.error_handlers: Dict[Callable, Union[bool, DefaultValue]] = {}
self.post_init = post_init
self.post_shutdown = post_shutdown
if isinstance(concurrent_updates, int) and concurrent_updates < 0:
raise ValueError("`concurrent_updates` must be a non-negative integer!")
if concurrent_updates is True:
concurrent_updates = 256
self._concurrent_updates_sem = asyncio.BoundedSemaphore(concurrent_updates or 1)
self._concurrent_updates: int = concurrent_updates or 0
self.bot_data = self.context_types.bot_data()
self._user_data: DefaultDict[int, UD] = defaultdict(self.context_types.user_data)
self._chat_data: DefaultDict[int, CD] = defaultdict(self.context_types.chat_data)
# Read only mapping
self.user_data: Mapping[int, UD] = MappingProxyType(self._user_data)
self.chat_data: Mapping[int, CD] = MappingProxyType(self._chat_data)
self.persistence: Optional[BasePersistence] = None
if persistence and not isinstance(persistence, BasePersistence):
raise TypeError("persistence must be based on telegram.ext.BasePersistence")
self.persistence = persistence
# Some bookkeeping for persistence logic
self._chat_ids_to_be_updated_in_persistence: Set[int] = set()
self._user_ids_to_be_updated_in_persistence: Set[int] = set()
self._chat_ids_to_be_deleted_in_persistence: Set[int] = set()
self._user_ids_to_be_deleted_in_persistence: Set[int] = set()
# This attribute will hold references to the conversation dicts of all conversation
# handlers so that we can extract the changed states during `update_persistence`
self._conversation_handler_conversations: Dict[
str, TrackingDict[ConversationKey, object]
] = {}
# A number of low-level helpers for the internal logic
self._initialized = False
self._running = False
self.__update_fetcher_task: Optional[asyncio.Task] = None
self.__update_persistence_task: Optional[asyncio.Task] = None
self.__update_persistence_event = asyncio.Event()
self.__update_persistence_lock = asyncio.Lock()
self.__create_task_tasks: Set[asyncio.Task] = set() # Used for awaiting tasks upon exit
def _check_initialized(self) -> None:
if not self._initialized:
raise RuntimeError(
"This Application was not initialized via `Application.initialize`!"
)
@property
def running(self) -> bool:
""":obj:`bool`: Indicates if this application is running.
.. seealso::
:meth:`start`, :meth:`stop`
"""
return self._running
@property
def concurrent_updates(self) -> int:
""":obj:`int`: The number of concurrent updates that will be processed in parallel. A
value of ``0`` indicates updates are *not* being processed concurrently.
"""
return self._concurrent_updates
async def initialize(self) -> None:
"""Initializes the Application by initializing:
* The :attr:`bot`, by calling :meth:`telegram.Bot.initialize`.
* The :attr:`updater`, by calling :meth:`telegram.ext.Updater.initialize`.
* The :attr:`persistence`, by loading persistent conversations and data.
Does *not* call :attr:`post_init` - that is only done by :meth:`run_polling` and
:meth:`run_webhook`.
.. seealso::
:meth:`shutdown`
"""
if self._initialized:
_logger.debug("This Application is already initialized.")
return
await self.bot.initialize()
if self.updater:
await self.updater.initialize()
if not self.persistence:
self._initialized = True
return
await self._initialize_persistence()
# Unfortunately due to circular imports this has to be here
# pylint: disable=import-outside-toplevel
from telegram.ext._conversationhandler import ConversationHandler
# Initialize the persistent conversation handlers with the stored states
for handler in itertools.chain.from_iterable(self.handlers.values()):
if isinstance(handler, ConversationHandler) and handler.persistent and handler.name:
await self._add_ch_to_persistence(handler)
self._initialized = True
async def _add_ch_to_persistence(self, handler: "ConversationHandler") -> None:
self._conversation_handler_conversations.update(
await handler._initialize_persistence(self) # pylint: disable=protected-access
)
async def shutdown(self) -> None:
"""Shuts down the Application by shutting down:
* :attr:`bot` by calling :meth:`telegram.Bot.shutdown`
* :attr:`updater` by calling :meth:`telegram.ext.Updater.shutdown`
* :attr:`persistence` by calling :meth:`update_persistence` and
:meth:`BasePersistence.flush`
Does *not* call :attr:`post_shutdown` - that is only done by :meth:`run_polling` and
:meth:`run_webhook`.
.. seealso::
:meth:`initialize`
Raises:
:exc:`RuntimeError`: If the application is still :attr:`running`.
"""
if self.running:
raise RuntimeError("This Application is still running!")
if not self._initialized:
_logger.debug("This Application is already shut down. Returning.")
return
await self.bot.shutdown()
if self.updater:
await self.updater.shutdown()
if self.persistence:
_logger.debug("Updating & flushing persistence before shutdown")
await self.update_persistence()
await self.persistence.flush()
_logger.debug("Updated and flushed persistence")
self._initialized = False
async def __aenter__(self: _AppType) -> _AppType:
"""Simple context manager which initializes the App."""
try:
await self.initialize()
return self
except Exception as exc:
await self.shutdown()
raise exc
async def __aexit__(
self,
exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> None:
"""Shutdown the App from the context manager."""
# Make sure not to return `True` so that exceptions are not suppressed
# https://docs.python.org/3/reference/datamodel.html?#object.__aexit__
await self.shutdown()
async def _initialize_persistence(self) -> None:
"""This method basically just loads all the data by awaiting the BP methods"""
if not self.persistence:
return
if self.persistence.store_data.user_data:
self._user_data.update(await self.persistence.get_user_data())
if self.persistence.store_data.chat_data:
self._chat_data.update(await self.persistence.get_chat_data())
if self.persistence.store_data.bot_data:
self.bot_data = await self.persistence.get_bot_data()
if not isinstance(self.bot_data, self.context_types.bot_data):
raise ValueError(
f"bot_data must be of type {self.context_types.bot_data.__name__}"
)
if self.persistence.store_data.callback_data:
persistent_data = await self.persistence.get_callback_data()
if persistent_data is not None:
if not isinstance(persistent_data, tuple) or len(persistent_data) != 2:
raise ValueError("callback_data must be a tuple of length 2")
# Mypy doesn't know that persistence.set_bot (see above) already checks that
# self.bot is an instance of ExtBot if callback_data should be stored ...
self.bot.callback_data_cache.load_persistence_data( # type: ignore[attr-defined]
persistent_data
)
@staticmethod
def builder() -> "InitApplicationBuilder":
"""Convenience method. Returns a new :class:`telegram.ext.ApplicationBuilder`.
.. versionadded:: 20.0
"""
# Unfortunately this needs to be here due to cyclical imports
from telegram.ext import ApplicationBuilder # pylint: disable=import-outside-toplevel
return ApplicationBuilder()
async def start(self) -> None:
"""Starts
* a background task that fetches updates from :attr:`update_queue` and processes them.
* :attr:`job_queue`, if set.
* a background task that calls :meth:`update_persistence` in regular intervals, if
:attr:`persistence` is set.
Note:
This does *not* start fetching updates from Telegram. To fetch updates, you need to
either start :attr:`updater` manually or use one of :meth:`run_polling` or
:meth:`run_webhook`.
.. seealso::
:meth:`stop`
Raises:
:exc:`RuntimeError`: If the application is already running or was not initialized.
"""
if self.running:
raise RuntimeError("This Application is already running!")
self._check_initialized()
self._running = True
self.__update_persistence_event.clear()
try:
if self.persistence:
self.__update_persistence_task = asyncio.create_task(
self._persistence_updater()
# TODO: Add this once we drop py3.7
# name=f'Application:{self.bot.id}:persistence_updater'
)
_logger.debug("Loop for updating persistence started")
if self.job_queue:
await self.job_queue.start() # type: ignore[union-attr]
_logger.debug("JobQueue started")
self.__update_fetcher_task = asyncio.create_task(
self._update_fetcher(),
# TODO: Add this once we drop py3.7
# name=f'Application:{self.bot.id}:update_fetcher'
)
_logger.info("Application started")
except Exception as exc:
self._running = False
raise exc
async def stop(self) -> None:
"""Stops the process after processing any pending updates or tasks created by
:meth:`create_task`. Also stops :attr:`job_queue`, if set.
Finally, calls :meth:`update_persistence` and :meth:`BasePersistence.flush` on
:attr:`persistence`, if set.
Warning:
Once this method is called, no more updates will be fetched from :attr:`update_queue`,
even if it's not empty.
.. seealso::
:meth:`start`
Note:
This does *not* stop :attr:`updater`. You need to either manually call
:meth:`telegram.ext.Updater.stop` or use one of :meth:`run_polling` or
:meth:`run_webhook`.
Raises:
:exc:`RuntimeError`: If the application is not running.
"""
if not self.running:
raise RuntimeError("This Application is not running!")
self._running = False
_logger.info("Application is stopping. This might take a moment.")
# Stop listening for new updates and handle all pending ones
await self.update_queue.put(_STOP_SIGNAL)
_logger.debug("Waiting for update_queue to join")
await self.update_queue.join()
if self.__update_fetcher_task:
await self.__update_fetcher_task
_logger.debug("Application stopped fetching of updates.")
if self.job_queue:
_logger.debug("Waiting for running jobs to finish")
await self.job_queue.stop(wait=True) # type: ignore[union-attr]
_logger.debug("JobQueue stopped")
_logger.debug("Waiting for `create_task` calls to be processed")
await asyncio.gather(*self.__create_task_tasks, return_exceptions=True)
# Make sure that this is the *last* step of stopping the application!
if self.persistence and self.__update_persistence_task:
_logger.debug("Waiting for persistence loop to finish")
self.__update_persistence_event.set()
await self.__update_persistence_task
self.__update_persistence_event.clear()
_logger.info("Application.stop() complete")
def run_polling(
self,
poll_interval: float = 0.0,
timeout: int = 10,
bootstrap_retries: int = -1,
read_timeout: float = 2,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
allowed_updates: List[str] = None,
drop_pending_updates: bool = None,
close_loop: bool = True,
stop_signals: ODVInput[Sequence[int]] = DEFAULT_NONE,
) -> None:
"""Convenience method that takes care of initializing and starting the app,
polling updates from Telegram using :meth:`telegram.ext.Updater.start_polling` and
a graceful shutdown of the app on exit.
The app will shut down when :exc:`KeyboardInterrupt` or :exc:`SystemExit` is raised.
On unix, the app will also shut down on receiving the signals specified by
:paramref:`stop_signals`.
If :attr:`post_init` is set, it will be called between :meth:`initialize` and
:meth:`telegram.ext.Updater.start_polling`.
If :attr:`post_shutdown` is set, it will be called after both :meth:`shutdown`
and :meth:`telegram.ext.Updater.shutdown`.
.. include:: inclusions/application_run_tip.rst
.. seealso::
:meth:`initialize`, :meth:`start`, :meth:`stop`, :meth:`shutdown`
:meth:`telegram.ext.Updater.start_polling`, :meth:`telegram.ext.Updater.stop`,
:meth:`run_webhook`
Args:
poll_interval (:obj:`float`, optional): Time to wait between polling updates from
Telegram in seconds. Default is ``0.0``.
timeout (:obj:`float`, optional): Passed to
:paramref:`telegram.Bot.get_updates.timeout`. Default is ``10`` seconds.
bootstrap_retries (:obj:`int`, optional): Whether the bootstrapping phase of the
:class:`telegram.ext.Updater` will retry on failures on the Telegram server.
* < 0 - retry indefinitely (default)
* 0 - no retries
* > 0 - retry up to X times
read_timeout (:obj:`float`, optional): Value to pass to
:paramref:`telegram.Bot.get_updates.read_timeout`. Defaults to ``2``.
write_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
:paramref:`telegram.Bot.get_updates.write_timeout`. Defaults to
:attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
connect_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
:paramref:`telegram.Bot.get_updates.connect_timeout`. Defaults to
:attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
pool_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
:paramref:`telegram.Bot.get_updates.pool_timeout`. Defaults to
:attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
drop_pending_updates (:obj:`bool`, optional): Whether to clean any pending updates on
Telegram servers before actually starting to poll. Default is :obj:`False`.
allowed_updates (List[:obj:`str`], optional): Passed to
:meth:`telegram.Bot.get_updates`.
close_loop (:obj:`bool`, optional): If :obj:`True`, the current event loop will be
closed upon shutdown.
.. seealso::
:meth:`asyncio.loop.close`
stop_signals (Sequence[:obj:`int`] | :obj:`None`, optional): Signals that will shut
down the app. Pass :obj:`None` to not use stop signals.
Defaults to :data:`signal.SIGINT`, :data:`signal.SIGTERM` and
:data:`signal.SIGABRT` on non Windows platforms.
Caution:
Not every :class:`asyncio.AbstractEventLoop` implements
:meth:`asyncio.loop.add_signal_handler`. Most notably, the standard event loop
on Windows, :class:`asyncio.ProactorEventLoop`, does not implement this method.
If this method is not available, stop signals can not be set.
Raises:
:exc:`RuntimeError`: If the Application does not have an :class:`telegram.ext.Updater`.
"""
if not self.updater:
raise RuntimeError(
"Application.run_polling is only available if the application has an Updater."
)
def error_callback(exc: TelegramError) -> None:
self.create_task(self.process_error(error=exc, update=None))
return self.__run(
updater_coroutine=self.updater.start_polling(
poll_interval=poll_interval,
timeout=timeout,
bootstrap_retries=bootstrap_retries,
read_timeout=read_timeout,
write_timeout=write_timeout,
connect_timeout=connect_timeout,
pool_timeout=pool_timeout,
allowed_updates=allowed_updates,
drop_pending_updates=drop_pending_updates,
error_callback=error_callback, # if there is an error in fetching updates
),
close_loop=close_loop,
stop_signals=stop_signals,
)
def run_webhook(
self,
listen: str = "127.0.0.1",
port: int = 80,
url_path: str = "",
cert: Union[str, Path] = None,
key: Union[str, Path] = None,
bootstrap_retries: int = 0,
webhook_url: str = None,
allowed_updates: List[str] = None,
drop_pending_updates: bool = None,
ip_address: str = None,
max_connections: int = 40,
close_loop: bool = True,
stop_signals: ODVInput[Sequence[int]] = DEFAULT_NONE,
secret_token: str = None,
) -> None:
"""Convenience method that takes care of initializing and starting the app,
listening for updates from Telegram using :meth:`telegram.ext.Updater.start_webhook` and
a graceful shutdown of the app on exit.
The app will shut down when :exc:`KeyboardInterrupt` or :exc:`SystemExit` is raised.
On unix, the app will also shut down on receiving the signals specified by
:paramref:`stop_signals`.
If :paramref:`cert`
and :paramref:`key` are not provided, the webhook will be started directly on
``http://listen:port/url_path``, so SSL can be handled by another
application. Else, the webhook will be started on
``https://listen:port/url_path``. Also calls :meth:`telegram.Bot.set_webhook` as
required.
If :attr:`post_init` is set, it will be called between :meth:`initialize` and
:meth:`telegram.ext.Updater.start_webhook`.
If :attr:`post_shutdown` is set, it will be called after both :meth:`shutdown`
and :meth:`telegram.ext.Updater.shutdown`.
.. include:: inclusions/application_run_tip.rst
.. seealso::
:meth:`initialize`, :meth:`start`, :meth:`stop`, :meth:`shutdown`
:meth:`telegram.ext.Updater.start_webhook`, :meth:`telegram.ext.Updater.stop`,
:meth:`run_polling`
Args:
listen (:obj:`str`, optional): IP-Address to listen on. Defaults to
`127.0.0.1 <https://en.wikipedia.org/wiki/Localhost>`_.
port (:obj:`int`, optional): Port the bot should be listening on. Must be one of
:attr:`telegram.constants.SUPPORTED_WEBHOOK_PORTS` unless the bot is running
behind a proxy. Defaults to ``80``.
url_path (:obj:`str`, optional): Path inside url. Defaults to `` '' ``
cert (:class:`pathlib.Path` | :obj:`str`, optional): Path to the SSL certificate file.
key (:class:`pathlib.Path` | :obj:`str`, optional): Path to the SSL key file.
bootstrap_retries (:obj:`int`, optional): Whether the bootstrapping phase of the
:class:`telegram.ext.Updater` will retry on failures on the Telegram server.
* < 0 - retry indefinitely
* 0 - no retries (default)
* > 0 - retry up to X times
webhook_url (:obj:`str`, optional): Explicitly specify the webhook url. Useful behind
NAT, reverse proxy, etc. Default is derived from :paramref:`listen`,
:paramref:`port`, :paramref:`url_path`, :paramref:`cert`, and :paramref:`key`.
allowed_updates (List[:obj:`str`], optional): Passed to
:meth:`telegram.Bot.set_webhook`.
drop_pending_updates (:obj:`bool`, optional): Whether to clean any pending updates on
Telegram servers before actually starting to poll. Default is :obj:`False`.
ip_address (:obj:`str`, optional): Passed to :meth:`telegram.Bot.set_webhook`.
max_connections (:obj:`int`, optional): Passed to
:meth:`telegram.Bot.set_webhook`. Defaults to ``40``.
close_loop (:obj:`bool`, optional): If :obj:`True`, the current event loop will be
closed upon shutdown. Defaults to :obj:`True`.
.. seealso::
:meth:`asyncio.loop.close`
stop_signals (Sequence[:obj:`int`] | :obj:`None`, optional): Signals that will shut
down the app. Pass :obj:`None` to not use stop signals.
Defaults to :data:`signal.SIGINT`, :data:`signal.SIGTERM` and
:data:`signal.SIGABRT`.
Caution:
Not every :class:`asyncio.AbstractEventLoop` implements
:meth:`asyncio.loop.add_signal_handler`. Most notably, the standard event loop
on Windows, :class:`asyncio.ProactorEventLoop`, does not implement this method.
If this method is not available, stop signals can not be set.
secret_token (:obj:`str`, optional): Secret token to ensure webhook requests originate
from Telegram. See :paramref:`telegram.Bot.set_webhook.secret_token` for more
details.
When added, the web server started by this call will expect the token to be set in
the ``X-Telegram-Bot-Api-Secret-Token`` header of an incoming request and will
raise a :class:`http.HTTPStatus.FORBIDDEN <http.HTTPStatus>` error if either the
header isn't set or it is set to a wrong token.
.. versionadded:: 20.0
"""
if not self.updater:
raise RuntimeError(
"Application.run_webhook is only available if the application has an Updater."
)
return self.__run(
updater_coroutine=self.updater.start_webhook(
listen=listen,
port=port,
url_path=url_path,
cert=cert,
key=key,
bootstrap_retries=bootstrap_retries,
drop_pending_updates=drop_pending_updates,
webhook_url=webhook_url,
allowed_updates=allowed_updates,
ip_address=ip_address,
max_connections=max_connections,
secret_token=secret_token,
),
close_loop=close_loop,
stop_signals=stop_signals,
)
@staticmethod
def _raise_system_exit() -> NoReturn:
raise SystemExit
def __run(
self,
updater_coroutine: Coroutine,
stop_signals: ODVInput[Sequence[int]],
close_loop: bool = True,
) -> None:
# Calling get_event_loop() should still be okay even in py3.10+ as long as there is a
# running event loop or we are in the main thread, which are the intended use cases.
# See the docs of get_event_loop() and get_running_loop() for more info
loop = asyncio.get_event_loop()
if stop_signals is DEFAULT_NONE and platform.system() != "Windows":
stop_signals = (signal.SIGINT, signal.SIGTERM, signal.SIGABRT)
try:
if not isinstance(stop_signals, DefaultValue):
for sig in stop_signals or []:
loop.add_signal_handler(sig, self._raise_system_exit)
except NotImplementedError as exc:
warn(
f"Could not add signal handlers for the stop signals {stop_signals} due to "
f"exception `{exc!r}`. If your event loop does not implement `add_signal_handler`,"
f" please pass `stop_signals=None`.",
stacklevel=3,
)
try:
loop.run_until_complete(self.initialize())
if self.post_init:
loop.run_until_complete(self.post_init(self))
loop.run_until_complete(updater_coroutine) # one of updater.start_webhook/polling
loop.run_until_complete(self.start())
loop.run_forever()
except (KeyboardInterrupt, SystemExit):
pass
except Exception as exc:
# In case the coroutine wasn't awaited, we don't need to bother the user with a warning
updater_coroutine.close()
raise exc
finally:
# We arrive here either by catching the exceptions above or if the loop gets stopped
try:
# Mypy doesn't know that we already check if updater is None
if self.updater.running: # type: ignore[union-attr]
loop.run_until_complete(self.updater.stop()) # type: ignore[union-attr]
if self.running:
loop.run_until_complete(self.stop())
loop.run_until_complete(self.shutdown())
if self.post_shutdown:
loop.run_until_complete(self.post_shutdown(self))
finally:
if close_loop:
loop.close()
def create_task(self, coroutine: Coroutine, update: object = None) -> asyncio.Task:
"""Thin wrapper around :func:`asyncio.create_task` that handles exceptions raised by
the :paramref:`coroutine` with :meth:`process_error`.
Note:
* If :paramref:`coroutine` raises an exception, it will be set on the task created by
this method even though it's handled by :meth:`process_error`.
* If the application is currently running, tasks created by this method will be
awaited with :meth:`stop`.
Args:
coroutine (:term:`coroutine function`): The coroutine to run as task.
update (:obj:`object`, optional): If set, will be passed to :meth:`process_error`
as additional information for the error handlers. Moreover, the corresponding
:attr:`chat_data` and :attr:`user_data` entries will be updated in the next run of
:meth:`update_persistence` after the :paramref:`coroutine` is finished.
Returns:
:class:`asyncio.Task`: The created task.
"""
return self.__create_task(coroutine=coroutine, update=update)
def __create_task(
self, coroutine: Coroutine, update: object = None, is_error_handler: bool = False
) -> asyncio.Task:
# Unfortunately, we can't know if `coroutine` runs one of the error handler functions
# but by passing `is_error_handler=True` from `process_error`, we can make sure that we
# get at most one recursion of the user calls `create_task` manually with an error handler
# function
task = asyncio.create_task(
self.__create_task_callback(
coroutine=coroutine, update=update, is_error_handler=is_error_handler
)
)
if self.running:
self.__create_task_tasks.add(task)
task.add_done_callback(self.__create_task_done_callback)
else:
warn(
"Tasks created via `Application.create_task` while the application is not "
"running won't be automatically awaited!",
stacklevel=3,
)
return task
def __create_task_done_callback(self, task: asyncio.Task) -> None:
self.__create_task_tasks.discard(task) # Discard from our set since we are done with it
# We just retrieve the eventual exception so that asyncio doesn't complain in case
# it's not retrieved somewhere else
try:
task.exception()
except (asyncio.CancelledError, asyncio.InvalidStateError):
pass
async def __create_task_callback(
self,
coroutine: Coroutine[Any, Any, _RT],
update: object = None,
is_error_handler: bool = False,
) -> _RT:
try:
return await coroutine
except asyncio.CancelledError as cancel:
# TODO: in py3.8+, CancelledError is a subclass of BaseException, so we can drop this
# clause when we drop py3.7
raise cancel
except Exception as exception:
if isinstance(exception, ApplicationHandlerStop):
warn(
"ApplicationHandlerStop is not supported with handlers "
"running non-blocking.",
stacklevel=1,
)
# Avoid infinite recursion of error handlers.
elif is_error_handler:
_logger.exception(
"An error was raised and an uncaught error was raised while "
"handling the error with an error_handler.",
exc_info=exception,
)
else:
# If we arrive here, an exception happened in the task and was neither
# ApplicationHandlerStop nor raised by an error handler.
# So we can and must handle it
await self.process_error(update=update, error=exception, coroutine=coroutine)
# Raise exception so that it can be set on the task and retrieved by task.exception()
raise exception
finally:
self._mark_for_persistence_update(update=update)
async def _update_fetcher(self) -> None:
# Continuously fetch updates from the queue. Exit only once the signal object is found.
while True:
update = await self.update_queue.get()
if update is _STOP_SIGNAL:
_logger.debug("Dropping pending updates")
while not self.update_queue.empty():
self.update_queue.task_done()
# For the _STOP_SIGNAL
self.update_queue.task_done()
return
_logger.debug("Processing update %s", update)
if self._concurrent_updates:
# We don't await the below because it has to be run concurrently
self.create_task(self.__process_update_wrapper(update), update=update)
else:
await self.__process_update_wrapper(update)
async def __process_update_wrapper(self, update: object) -> None:
async with self._concurrent_updates_sem:
await self.process_update(update)
self.update_queue.task_done()
async def process_update(self, update: object) -> None:
"""Processes a single update and marks the update to be updated by the persistence later.
Exceptions raised by handler callbacks will be processed by :meth:`process_update`.
.. versionchanged:: 20.0
Persistence is now updated in an interval set by
:attr:`telegram.ext.BasePersistence.update_interval`.
Args:
update (:class:`telegram.Update` | :obj:`object` | \
:class:`telegram.error.TelegramError`): The update to process.
Raises:
:exc:`RuntimeError`: If the application was not initialized.
"""
# Processing updates before initialize() is a problem e.g. if persistence is used
self._check_initialized()
context = None
any_blocking = False # Flag which is set to True if any handler specifies block=True
for handlers in self.handlers.values():
try:
for handler in handlers:
check = handler.check_update(update) # Should the handler handle this update?
if not (check is None or check is False): # if yes,
if not context: # build a context if not already built
context = self.context_types.context.from_update(update, self)