2016-07-15 01:30:54 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2021-01-03 06:10:24 +01:00
|
|
|
# Copyright (C) 2015-2021
|
2016-07-15 01:30:54 +02:00
|
|
|
# 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/].
|
2020-10-31 16:33:34 +01:00
|
|
|
# pylint: disable=R0201
|
2017-09-01 08:43:08 +02:00
|
|
|
"""This module contains the ConversationHandler."""
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
import logging
|
2019-02-13 23:28:23 +01:00
|
|
|
import warnings
|
2019-10-17 00:03:53 +02:00
|
|
|
from threading import Lock
|
2021-01-30 11:38:54 +01:00
|
|
|
from typing import TYPE_CHECKING, Dict, List, NoReturn, Optional, Tuple, cast, ClassVar
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
from telegram import Update
|
2020-10-09 17:22:07 +02:00
|
|
|
from telegram.ext import (
|
2020-10-31 16:33:34 +01:00
|
|
|
BasePersistence,
|
|
|
|
CallbackContext,
|
2020-10-09 17:22:07 +02:00
|
|
|
CallbackQueryHandler,
|
|
|
|
ChosenInlineResultHandler,
|
|
|
|
DispatcherHandlerStop,
|
2020-10-31 16:33:34 +01:00
|
|
|
Handler,
|
|
|
|
InlineQueryHandler,
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2021-01-30 14:15:39 +01:00
|
|
|
from telegram.ext.utils.promise import Promise
|
2020-12-16 17:34:57 +01:00
|
|
|
from telegram.utils.types import ConversationDict
|
2020-10-06 19:28:40 +02:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from telegram.ext import Dispatcher, Job
|
|
|
|
CheckUpdateType = Optional[Tuple[Tuple[int, ...], Handler, object]]
|
|
|
|
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2020-06-15 18:20:51 +02:00
|
|
|
class _ConversationTimeoutContext:
|
2020-10-09 17:22:07 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
conversation_key: Tuple[int, ...],
|
|
|
|
update: Update,
|
|
|
|
dispatcher: 'Dispatcher',
|
|
|
|
callback_context: Optional[CallbackContext],
|
|
|
|
):
|
2018-10-04 08:58:40 +02:00
|
|
|
self.conversation_key = conversation_key
|
|
|
|
self.update = update
|
|
|
|
self.dispatcher = dispatcher
|
2020-04-10 13:18:43 +02:00
|
|
|
self.callback_context = callback_context
|
2018-10-04 08:58:40 +02:00
|
|
|
|
|
|
|
|
2020-12-16 17:34:57 +01:00
|
|
|
class ConversationHandler(Handler[Update]):
|
2016-07-15 01:30:54 +02:00
|
|
|
"""
|
2016-12-25 21:36:01 +01:00
|
|
|
A handler to hold a conversation with a single user by managing four collections of other
|
2019-10-27 00:04:48 +02:00
|
|
|
handlers.
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2017-07-23 22:33:08 +02:00
|
|
|
The first collection, a ``list`` named :attr:`entry_points`, is used to initiate the
|
|
|
|
conversation, for example with a :class:`telegram.ext.CommandHandler` or
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
:class:`telegram.ext.MessageHandler`.
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2017-07-23 22:33:08 +02:00
|
|
|
The second collection, a ``dict`` named :attr:`states`, contains the different conversation
|
|
|
|
steps and one or more associated handlers that should be used if the user sends a message when
|
2018-10-04 08:58:40 +02:00
|
|
|
the conversation with them is currently in that state. Here you can also define a state for
|
2019-02-14 11:00:21 +01:00
|
|
|
:attr:`TIMEOUT` to define the behavior when :attr:`conversation_timeout` is exceeded, and a
|
|
|
|
state for :attr:`WAITING` to define behavior when a new update is received while the previous
|
|
|
|
``@run_async`` decorated handler is not finished.
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2017-07-23 22:33:08 +02:00
|
|
|
The third collection, a ``list`` named :attr:`fallbacks`, is used if the user is currently in a
|
2016-07-15 01:30:54 +02:00
|
|
|
conversation but the state has either no associated handler or the handler that is associated
|
|
|
|
to the state is inappropriate for the update, for example if the update contains a command, but
|
|
|
|
a regular text message is expected. You could use this for a ``/cancel`` command or to let the
|
|
|
|
user know their message was not recognized.
|
|
|
|
|
|
|
|
To change the state of conversation, the callback function of a handler must return the new
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
state after responding to the user. If it does not return anything (returning :obj:`None` by
|
|
|
|
default), the state will not change. If an entry point callback function returns :obj:`None`,
|
2019-02-14 12:29:58 +01:00
|
|
|
the conversation ends immediately after the execution of this callback function.
|
|
|
|
To end the conversation, the callback function must return :attr:`END` or ``-1``. To
|
|
|
|
handle the conversation timeout, use handler :attr:`TIMEOUT` or ``-2``.
|
2020-11-29 16:25:47 +01:00
|
|
|
Finally, :class:`telegram.ext.DispatcherHandlerStop` can be used in conversations as described
|
|
|
|
in the corresponding documentation.
|
2017-07-23 22:33:08 +02:00
|
|
|
|
2019-10-11 21:59:36 +02:00
|
|
|
Note:
|
|
|
|
In each of the described collections of handlers, a handler may in turn be a
|
|
|
|
:class:`ConversationHandler`. In that case, the nested :class:`ConversationHandler` should
|
|
|
|
have the attribute :attr:`map_to_parent` which allows to return to the parent conversation
|
|
|
|
at specified states within the nested conversation.
|
|
|
|
|
|
|
|
Note that the keys in :attr:`map_to_parent` must not appear as keys in :attr:`states`
|
|
|
|
attribute or else the latter will be ignored. You may map :attr:`END` to one of the parents
|
|
|
|
states to continue the parent conversation after this has ended or even map a state to
|
|
|
|
:attr:`END` to end the *parent* conversation from within the nested one. For an example on
|
|
|
|
nested :class:`ConversationHandler` s, see our `examples`_.
|
|
|
|
|
|
|
|
.. _`examples`: https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples
|
|
|
|
|
2016-07-15 01:30:54 +02:00
|
|
|
Args:
|
2017-07-23 22:33:08 +02:00
|
|
|
entry_points (List[:class:`telegram.ext.Handler`]): A list of ``Handler`` objects that can
|
|
|
|
trigger the start of the conversation. The first handler which :attr:`check_update`
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
method returns :obj:`True` will be used. If all return :obj:`False`, the update is not
|
2017-07-23 22:33:08 +02:00
|
|
|
handled.
|
|
|
|
states (Dict[:obj:`object`, List[:class:`telegram.ext.Handler`]]): A :obj:`dict` that
|
|
|
|
defines the different states of conversation a user can be in and one or more
|
|
|
|
associated ``Handler`` objects that should be used in that state. The first handler
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
which :attr:`check_update` method returns :obj:`True` will be used.
|
2017-07-23 22:33:08 +02:00
|
|
|
fallbacks (List[:class:`telegram.ext.Handler`]): A list of handlers that might be used if
|
|
|
|
the user is in a conversation, but every handler for their current state returned
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
:obj:`False` on :attr:`check_update`. The first handler which :attr:`check_update`
|
|
|
|
method returns :obj:`True` will be used. If all return :obj:`False`, the update is not
|
|
|
|
handled.
|
|
|
|
allow_reentry (:obj:`bool`, optional): If set to :obj:`True`, a user that is currently in a
|
2016-07-15 01:30:54 +02:00
|
|
|
conversation can restart the conversation by triggering one of the entry points.
|
2017-07-23 22:33:08 +02:00
|
|
|
per_chat (:obj:`bool`, optional): If the conversationkey should contain the Chat's ID.
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
Default is :obj:`True`.
|
2017-07-23 22:33:08 +02:00
|
|
|
per_user (:obj:`bool`, optional): If the conversationkey should contain the User's ID.
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
Default is :obj:`True`.
|
2017-07-23 22:33:08 +02:00
|
|
|
per_message (:obj:`bool`, optional): If the conversationkey should contain the Message's
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
ID. Default is :obj:`False`.
|
2018-10-04 08:58:40 +02:00
|
|
|
conversation_timeout (:obj:`float` | :obj:`datetime.timedelta`, optional): When this
|
|
|
|
handler is inactive more than this timeout (in seconds), it will be automatically
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
ended. If this value is 0 or :obj:`None` (default), there will be no timeout. The last
|
2020-04-10 13:18:43 +02:00
|
|
|
received update and the corresponding ``context`` will be handled by ALL the handler's
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
who's :attr:`check_update` method returns :obj:`True` that are in the state
|
2020-04-10 13:18:43 +02:00
|
|
|
:attr:`ConversationHandler.TIMEOUT`.
|
2018-09-20 22:50:40 +02:00
|
|
|
name (:obj:`str`, optional): The name for this conversationhandler. Required for
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
persistence.
|
2018-09-20 22:50:40 +02:00
|
|
|
persistent (:obj:`bool`, optional): If the conversations dict for this handler should be
|
|
|
|
saved. Name is required and persistence has to be set in :class:`telegram.ext.Updater`
|
2019-10-11 21:59:36 +02:00
|
|
|
map_to_parent (Dict[:obj:`object`, :obj:`object`], optional): A :obj:`dict` that can be
|
|
|
|
used to instruct a nested conversationhandler to transition into a mapped state on
|
|
|
|
its parent conversationhandler in place of a specified nested state.
|
2021-01-09 20:05:58 +01:00
|
|
|
run_async (:obj:`bool`, optional): Pass :obj:`True` to *override* the
|
|
|
|
:attr:`Handler.run_async` setting of all handlers (in :attr:`entry_points`,
|
|
|
|
:attr:`states` and :attr:`fallbacks`).
|
|
|
|
|
|
|
|
Note:
|
|
|
|
If set to :obj:`True`, you should not pass a handler instance, that needs to be
|
|
|
|
run synchronously in another context.
|
|
|
|
|
|
|
|
.. versionadded:: 13.2
|
2017-07-23 22:33:08 +02:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2020-12-30 15:59:50 +01:00
|
|
|
Attributes:
|
|
|
|
entry_points (List[:class:`telegram.ext.Handler`]): A list of ``Handler`` objects that can
|
|
|
|
trigger the start of the conversation.
|
|
|
|
states (Dict[:obj:`object`, List[:class:`telegram.ext.Handler`]]): A :obj:`dict` that
|
|
|
|
defines the different states of conversation a user can be in and one or more
|
|
|
|
associated ``Handler`` objects that should be used in that state.
|
|
|
|
fallbacks (List[:class:`telegram.ext.Handler`]): A list of handlers that might be used if
|
|
|
|
the user is in a conversation, but every handler for their current state returned
|
|
|
|
:obj:`False` on :attr:`check_update`.
|
|
|
|
allow_reentry (:obj:`bool`): Determines if a user can restart a conversation with
|
|
|
|
an entry point.
|
|
|
|
per_chat (:obj:`bool`): If the conversationkey should contain the Chat's ID.
|
|
|
|
per_user (:obj:`bool`): If the conversationkey should contain the User's ID.
|
|
|
|
per_message (:obj:`bool`): If the conversationkey should contain the Message's
|
|
|
|
ID.
|
|
|
|
conversation_timeout (:obj:`float` | :obj:`datetime.timedelta`): Optional. When this
|
|
|
|
handler is inactive more than this timeout (in seconds), it will be automatically
|
|
|
|
ended. If this value is 0 (default), there will be no timeout. When it's triggered, the
|
|
|
|
last received update and the corresponding ``context`` will be handled by ALL the
|
|
|
|
handler's who's :attr:`check_update` method returns :obj:`True` that are in the state
|
|
|
|
:attr:`ConversationHandler.TIMEOUT`.
|
|
|
|
name (:obj:`str`): Optional. The name for this conversationhandler. Required for
|
|
|
|
persistence
|
|
|
|
persistent (:obj:`bool`): Optional. If the conversations dict for this handler should be
|
|
|
|
saved. Name is required and persistence has to be set in :class:`telegram.ext.Updater`
|
|
|
|
map_to_parent (Dict[:obj:`object`, :obj:`object`]): Optional. A :obj:`dict` that can be
|
|
|
|
used to instruct a nested conversationhandler to transition into a mapped state on
|
|
|
|
its parent conversationhandler in place of a specified nested state.
|
2021-01-09 20:05:58 +01:00
|
|
|
run_async (:obj:`bool`): If :obj:`True`, will override the
|
|
|
|
:attr:`Handler.run_async` setting of all internal handlers on initialization.
|
|
|
|
|
|
|
|
.. versionadded:: 13.2
|
2020-12-30 15:59:50 +01:00
|
|
|
|
2017-09-01 08:43:08 +02:00
|
|
|
"""
|
2020-10-09 17:22:07 +02:00
|
|
|
|
2020-10-15 18:50:47 +02:00
|
|
|
END: ClassVar[int] = -1
|
2017-07-23 22:33:08 +02:00
|
|
|
""":obj:`int`: Used as a constant to return when a conversation is ended."""
|
2020-10-15 18:50:47 +02:00
|
|
|
TIMEOUT: ClassVar[int] = -2
|
2018-10-04 08:58:40 +02:00
|
|
|
""":obj:`int`: Used as a constant to handle state when a conversation is timed out."""
|
2020-10-15 18:50:47 +02:00
|
|
|
WAITING: ClassVar[int] = -3
|
2019-02-14 11:00:21 +01:00
|
|
|
""":obj:`int`: Used as a constant to handle state when a conversation is still waiting on the
|
|
|
|
previous ``@run_sync`` decorated running handler to finish."""
|
2020-10-31 16:33:34 +01:00
|
|
|
# pylint: disable=W0231
|
2020-10-09 17:22:07 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
entry_points: List[Handler],
|
|
|
|
states: Dict[object, List[Handler]],
|
|
|
|
fallbacks: List[Handler],
|
|
|
|
allow_reentry: bool = False,
|
|
|
|
per_chat: bool = True,
|
|
|
|
per_user: bool = True,
|
|
|
|
per_message: bool = False,
|
|
|
|
conversation_timeout: int = None,
|
|
|
|
name: str = None,
|
|
|
|
persistent: bool = False,
|
|
|
|
map_to_parent: Dict[object, object] = None,
|
2021-01-09 20:05:58 +01:00
|
|
|
run_async: bool = False,
|
2020-10-09 17:22:07 +02:00
|
|
|
):
|
2021-01-09 20:05:58 +01:00
|
|
|
self.run_async = run_async
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2020-03-30 17:37:37 +02:00
|
|
|
self._entry_points = entry_points
|
|
|
|
self._states = states
|
|
|
|
self._fallbacks = fallbacks
|
|
|
|
|
|
|
|
self._allow_reentry = allow_reentry
|
|
|
|
self._per_user = per_user
|
|
|
|
self._per_chat = per_chat
|
|
|
|
self._per_message = per_message
|
|
|
|
self._conversation_timeout = conversation_timeout
|
|
|
|
self._name = name
|
2018-09-20 22:50:40 +02:00
|
|
|
if persistent and not self.name:
|
|
|
|
raise ValueError("Conversations can't be persistent when handler is unnamed.")
|
2020-10-06 19:28:40 +02:00
|
|
|
self.persistent: bool = persistent
|
|
|
|
self._persistence: Optional[BasePersistence] = None
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
""":obj:`telegram.ext.BasePersistence`: The persistence used to store conversations.
|
2018-09-20 22:50:40 +02:00
|
|
|
Set by dispatcher"""
|
2020-03-30 17:37:37 +02:00
|
|
|
self._map_to_parent = map_to_parent
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
self.timeout_jobs: Dict[Tuple[int, ...], 'Job'] = dict()
|
2019-10-17 00:03:53 +02:00
|
|
|
self._timeout_jobs_lock = Lock()
|
2020-10-06 19:28:40 +02:00
|
|
|
self._conversations: ConversationDict = dict()
|
2019-10-17 00:03:53 +02:00
|
|
|
self._conversations_lock = Lock()
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
self.logger = logging.getLogger(__name__)
|
|
|
|
|
2017-02-27 17:52:58 +01:00
|
|
|
if not any((self.per_user, self.per_chat, self.per_message)):
|
|
|
|
raise ValueError("'per_user', 'per_chat' and 'per_message' can't all be 'False'")
|
|
|
|
|
2017-04-29 14:15:17 +02:00
|
|
|
if self.per_message and not self.per_chat:
|
2020-10-09 17:22:07 +02:00
|
|
|
warnings.warn(
|
|
|
|
"If 'per_message=True' is used, 'per_chat=True' should also be used, "
|
|
|
|
"since message IDs are not globally unique."
|
|
|
|
)
|
2017-04-29 14:15:17 +02:00
|
|
|
|
2021-01-09 20:05:58 +01:00
|
|
|
all_handlers: List[Handler] = list()
|
2017-02-27 17:52:58 +01:00
|
|
|
all_handlers.extend(entry_points)
|
|
|
|
all_handlers.extend(fallbacks)
|
|
|
|
|
|
|
|
for state_handlers in states.values():
|
|
|
|
all_handlers.extend(state_handlers)
|
|
|
|
|
|
|
|
if self.per_message:
|
|
|
|
for handler in all_handlers:
|
|
|
|
if not isinstance(handler, CallbackQueryHandler):
|
2020-10-09 17:22:07 +02:00
|
|
|
warnings.warn(
|
|
|
|
"If 'per_message=True', all entry points and state handlers"
|
|
|
|
" must be 'CallbackQueryHandler', since no other handlers "
|
|
|
|
"have a message context."
|
|
|
|
)
|
2019-02-13 22:08:49 +01:00
|
|
|
break
|
2017-02-27 17:52:58 +01:00
|
|
|
else:
|
|
|
|
for handler in all_handlers:
|
|
|
|
if isinstance(handler, CallbackQueryHandler):
|
2020-10-09 17:22:07 +02:00
|
|
|
warnings.warn(
|
|
|
|
"If 'per_message=False', 'CallbackQueryHandler' will not be "
|
|
|
|
"tracked for every message."
|
|
|
|
)
|
2019-02-13 22:08:49 +01:00
|
|
|
break
|
2017-02-27 17:52:58 +01:00
|
|
|
|
|
|
|
if self.per_chat:
|
|
|
|
for handler in all_handlers:
|
|
|
|
if isinstance(handler, (InlineQueryHandler, ChosenInlineResultHandler)):
|
2020-10-09 17:22:07 +02:00
|
|
|
warnings.warn(
|
|
|
|
"If 'per_chat=True', 'InlineQueryHandler' can not be used, "
|
|
|
|
"since inline queries have no chat context."
|
|
|
|
)
|
2019-02-13 22:08:49 +01:00
|
|
|
break
|
2017-02-27 17:52:58 +01:00
|
|
|
|
2021-01-09 20:05:58 +01:00
|
|
|
if self.run_async:
|
|
|
|
for handler in all_handlers:
|
|
|
|
handler.run_async = True
|
|
|
|
|
2020-03-30 17:37:37 +02:00
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def entry_points(self) -> List[Handler]:
|
2020-03-30 17:37:37 +02:00
|
|
|
return self._entry_points
|
|
|
|
|
|
|
|
@entry_points.setter
|
2021-01-30 11:38:54 +01:00
|
|
|
def entry_points(self, value: object) -> NoReturn:
|
2020-03-30 17:37:37 +02:00
|
|
|
raise ValueError('You can not assign a new value to entry_points after initialization.')
|
|
|
|
|
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def states(self) -> Dict[object, List[Handler]]:
|
2020-03-30 17:37:37 +02:00
|
|
|
return self._states
|
|
|
|
|
|
|
|
@states.setter
|
2021-01-30 11:38:54 +01:00
|
|
|
def states(self, value: object) -> NoReturn:
|
2020-03-30 17:37:37 +02:00
|
|
|
raise ValueError('You can not assign a new value to states after initialization.')
|
|
|
|
|
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def fallbacks(self) -> List[Handler]:
|
2020-03-30 17:37:37 +02:00
|
|
|
return self._fallbacks
|
|
|
|
|
|
|
|
@fallbacks.setter
|
2021-01-30 11:38:54 +01:00
|
|
|
def fallbacks(self, value: object) -> NoReturn:
|
2020-03-30 17:37:37 +02:00
|
|
|
raise ValueError('You can not assign a new value to fallbacks after initialization.')
|
|
|
|
|
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def allow_reentry(self) -> bool:
|
2020-03-30 17:37:37 +02:00
|
|
|
return self._allow_reentry
|
|
|
|
|
|
|
|
@allow_reentry.setter
|
2021-01-30 11:38:54 +01:00
|
|
|
def allow_reentry(self, value: object) -> NoReturn:
|
2020-03-30 17:37:37 +02:00
|
|
|
raise ValueError('You can not assign a new value to allow_reentry after initialization.')
|
|
|
|
|
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def per_user(self) -> bool:
|
2020-03-30 17:37:37 +02:00
|
|
|
return self._per_user
|
|
|
|
|
|
|
|
@per_user.setter
|
2021-01-30 11:38:54 +01:00
|
|
|
def per_user(self, value: object) -> NoReturn:
|
2020-03-30 17:37:37 +02:00
|
|
|
raise ValueError('You can not assign a new value to per_user after initialization.')
|
|
|
|
|
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def per_chat(self) -> bool:
|
2020-03-30 17:37:37 +02:00
|
|
|
return self._per_chat
|
|
|
|
|
|
|
|
@per_chat.setter
|
2021-01-30 11:38:54 +01:00
|
|
|
def per_chat(self, value: object) -> NoReturn:
|
2020-03-30 17:37:37 +02:00
|
|
|
raise ValueError('You can not assign a new value to per_chat after initialization.')
|
|
|
|
|
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def per_message(self) -> bool:
|
2020-03-30 17:37:37 +02:00
|
|
|
return self._per_message
|
|
|
|
|
|
|
|
@per_message.setter
|
2021-01-30 11:38:54 +01:00
|
|
|
def per_message(self, value: object) -> NoReturn:
|
2020-03-30 17:37:37 +02:00
|
|
|
raise ValueError('You can not assign a new value to per_message after initialization.')
|
|
|
|
|
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def conversation_timeout(self) -> Optional[int]:
|
2020-03-30 17:37:37 +02:00
|
|
|
return self._conversation_timeout
|
|
|
|
|
|
|
|
@conversation_timeout.setter
|
2021-01-30 11:38:54 +01:00
|
|
|
def conversation_timeout(self, value: object) -> NoReturn:
|
2020-10-09 17:22:07 +02:00
|
|
|
raise ValueError(
|
|
|
|
'You can not assign a new value to conversation_timeout after ' 'initialization.'
|
|
|
|
)
|
2020-03-30 17:37:37 +02:00
|
|
|
|
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def name(self) -> Optional[str]:
|
2020-03-30 17:37:37 +02:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@name.setter
|
2021-01-30 11:38:54 +01:00
|
|
|
def name(self, value: object) -> NoReturn:
|
2020-03-30 17:37:37 +02:00
|
|
|
raise ValueError('You can not assign a new value to name after initialization.')
|
|
|
|
|
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def map_to_parent(self) -> Optional[Dict[object, object]]:
|
2020-03-30 17:37:37 +02:00
|
|
|
return self._map_to_parent
|
|
|
|
|
|
|
|
@map_to_parent.setter
|
2021-01-30 11:38:54 +01:00
|
|
|
def map_to_parent(self, value: object) -> NoReturn:
|
2020-03-30 17:37:37 +02:00
|
|
|
raise ValueError('You can not assign a new value to map_to_parent after initialization.')
|
|
|
|
|
2020-02-02 22:31:56 +01:00
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def persistence(self) -> Optional[BasePersistence]:
|
2020-02-02 22:31:56 +01:00
|
|
|
return self._persistence
|
|
|
|
|
|
|
|
@persistence.setter
|
2020-10-06 19:28:40 +02:00
|
|
|
def persistence(self, persistence: BasePersistence) -> None:
|
2020-02-02 22:31:56 +01:00
|
|
|
self._persistence = persistence
|
|
|
|
# Set persistence for nested conversations
|
|
|
|
for handlers in self.states.values():
|
|
|
|
for handler in handlers:
|
|
|
|
if isinstance(handler, ConversationHandler):
|
|
|
|
handler.persistence = self.persistence
|
|
|
|
|
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def conversations(self) -> ConversationDict:
|
2020-02-02 22:31:56 +01:00
|
|
|
return self._conversations
|
|
|
|
|
|
|
|
@conversations.setter
|
2020-10-06 19:28:40 +02:00
|
|
|
def conversations(self, value: ConversationDict) -> None:
|
2020-02-02 22:31:56 +01:00
|
|
|
self._conversations = value
|
|
|
|
# Set conversations for nested conversations
|
|
|
|
for handlers in self.states.values():
|
|
|
|
for handler in handlers:
|
2020-10-06 19:28:40 +02:00
|
|
|
if isinstance(handler, ConversationHandler) and self.persistence and handler.name:
|
2020-02-02 22:31:56 +01:00
|
|
|
handler.conversations = self.persistence.get_conversations(handler.name)
|
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def _get_key(self, update: Update) -> Tuple[int, ...]:
|
2017-03-26 14:36:34 +02:00
|
|
|
chat = update.effective_chat
|
|
|
|
user = update.effective_user
|
|
|
|
|
2017-02-27 17:52:58 +01:00
|
|
|
key = list()
|
|
|
|
|
|
|
|
if self.per_chat:
|
2020-10-06 19:28:40 +02:00
|
|
|
key.append(chat.id) # type: ignore[union-attr]
|
2017-02-27 17:52:58 +01:00
|
|
|
|
2017-07-23 21:29:52 +02:00
|
|
|
if self.per_user and user is not None:
|
2017-02-27 17:52:58 +01:00
|
|
|
key.append(user.id)
|
|
|
|
|
|
|
|
if self.per_message:
|
2020-10-09 17:22:07 +02:00
|
|
|
key.append(
|
|
|
|
update.callback_query.inline_message_id # type: ignore[union-attr]
|
|
|
|
or update.callback_query.message.message_id # type: ignore[union-attr]
|
|
|
|
)
|
2017-02-27 17:52:58 +01:00
|
|
|
|
|
|
|
return tuple(key)
|
|
|
|
|
2021-01-30 11:38:54 +01:00
|
|
|
def check_update(self, update: object) -> CheckUpdateType: # pylint: disable=R0911
|
2017-07-23 22:33:08 +02:00
|
|
|
"""
|
|
|
|
Determines whether an update should be handled by this conversationhandler, and if so in
|
|
|
|
which state the conversation currently is.
|
|
|
|
|
|
|
|
Args:
|
2020-12-16 17:34:57 +01:00
|
|
|
update (:class:`telegram.Update` | :obj:`object`): Incoming update.
|
2017-07-23 22:33:08 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`bool`
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2017-09-01 08:43:08 +02:00
|
|
|
"""
|
2020-10-31 16:33:34 +01:00
|
|
|
if not isinstance(update, Update):
|
|
|
|
return None
|
2016-12-25 21:36:01 +01:00
|
|
|
# Ignore messages in channels
|
2020-10-31 16:33:34 +01:00
|
|
|
if update.channel_post:
|
|
|
|
return None
|
|
|
|
if self.per_chat and not update.effective_chat:
|
|
|
|
return None
|
|
|
|
if self.per_message and not update.callback_query:
|
|
|
|
return None
|
|
|
|
if update.callback_query and self.per_chat and not update.callback_query.message:
|
2018-09-21 08:57:01 +02:00
|
|
|
return None
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2017-02-27 17:52:58 +01:00
|
|
|
key = self._get_key(update)
|
2019-10-17 00:03:53 +02:00
|
|
|
with self._conversations_lock:
|
|
|
|
state = self.conversations.get(key)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
# Resolve promises
|
2019-01-30 20:20:35 +01:00
|
|
|
if isinstance(state, tuple) and len(state) == 2 and isinstance(state[1], Promise):
|
2016-07-15 01:30:54 +02:00
|
|
|
self.logger.debug('waiting for promise...')
|
|
|
|
|
|
|
|
old_state, new_state = state
|
2019-02-14 11:00:21 +01:00
|
|
|
if new_state.done.wait(0):
|
2018-09-10 20:05:45 +02:00
|
|
|
try:
|
2019-02-14 11:00:21 +01:00
|
|
|
res = new_state.result(0)
|
2018-09-10 20:05:45 +02:00
|
|
|
res = res if res is not None else old_state
|
|
|
|
except Exception as exc:
|
|
|
|
self.logger.exception("Promise function raised exception")
|
2020-10-31 16:33:34 +01:00
|
|
|
self.logger.exception("%s", exc)
|
2018-09-10 20:05:45 +02:00
|
|
|
res = old_state
|
|
|
|
finally:
|
2019-02-14 12:29:58 +01:00
|
|
|
if res is None and old_state is None:
|
|
|
|
res = self.END
|
2018-09-10 20:05:45 +02:00
|
|
|
self.update_state(res, key)
|
2019-10-17 00:03:53 +02:00
|
|
|
with self._conversations_lock:
|
|
|
|
state = self.conversations.get(key)
|
2016-07-15 01:30:54 +02:00
|
|
|
else:
|
2020-10-06 19:28:40 +02:00
|
|
|
hdlrs = self.states.get(self.WAITING, [])
|
|
|
|
for hdlr in hdlrs:
|
|
|
|
check = hdlr.check_update(update)
|
2018-09-21 08:57:01 +02:00
|
|
|
if check is not None and check is not False:
|
2020-10-06 19:28:40 +02:00
|
|
|
return key, hdlr, check
|
2019-02-13 11:37:13 +01:00
|
|
|
return None
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
self.logger.debug('selecting conversation %s with state %s', str(key), str(state))
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
handler = None
|
|
|
|
|
|
|
|
# Search entry points for a match
|
|
|
|
if state is None or self.allow_reentry:
|
|
|
|
for entry_point in self.entry_points:
|
2018-09-21 08:57:01 +02:00
|
|
|
check = entry_point.check_update(update)
|
|
|
|
if check is not None and check is not False:
|
2016-07-15 01:30:54 +02:00
|
|
|
handler = entry_point
|
|
|
|
break
|
|
|
|
|
|
|
|
else:
|
|
|
|
if state is None:
|
2018-09-21 08:57:01 +02:00
|
|
|
return None
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
# Get the handler list for current state, if we didn't find one yet and we're still here
|
|
|
|
if state is not None and not handler:
|
|
|
|
handlers = self.states.get(state)
|
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
for candidate in handlers or []:
|
2018-09-21 08:57:01 +02:00
|
|
|
check = candidate.check_update(update)
|
|
|
|
if check is not None and check is not False:
|
2016-07-15 01:30:54 +02:00
|
|
|
handler = candidate
|
|
|
|
break
|
|
|
|
|
|
|
|
# Find a fallback handler if all other handlers fail
|
|
|
|
else:
|
|
|
|
for fallback in self.fallbacks:
|
2018-09-21 08:57:01 +02:00
|
|
|
check = fallback.check_update(update)
|
|
|
|
if check is not None and check is not False:
|
2016-07-15 01:30:54 +02:00
|
|
|
handler = fallback
|
|
|
|
break
|
|
|
|
|
|
|
|
else:
|
2018-09-21 08:57:01 +02:00
|
|
|
return None
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
return key, handler, check # type: ignore[return-value]
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
def handle_update( # type: ignore[override]
|
|
|
|
self,
|
2020-12-16 17:34:57 +01:00
|
|
|
update: Update,
|
2020-10-09 17:22:07 +02:00
|
|
|
dispatcher: 'Dispatcher',
|
|
|
|
check_result: CheckUpdateType,
|
|
|
|
context: CallbackContext = None,
|
|
|
|
) -> Optional[object]:
|
2017-09-01 08:43:08 +02:00
|
|
|
"""Send the update to the callback for the current state and Handler
|
2017-07-23 22:33:08 +02:00
|
|
|
|
|
|
|
Args:
|
2018-09-21 08:57:01 +02:00
|
|
|
check_result: The result from check_update. For this handler it's a tuple of key,
|
|
|
|
handler, and the handler's check result.
|
2017-07-23 22:33:08 +02:00
|
|
|
update (:class:`telegram.Update`): Incoming telegram update.
|
|
|
|
dispatcher (:class:`telegram.ext.Dispatcher`): Dispatcher that originated the Update.
|
2020-10-06 19:28:40 +02:00
|
|
|
context (:class:`telegram.ext.CallbackContext`, optional): The context as provided by
|
|
|
|
the dispatcher.
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2017-09-01 08:43:08 +02:00
|
|
|
"""
|
2020-10-06 19:28:40 +02:00
|
|
|
update = cast(Update, update) # for mypy
|
|
|
|
conversation_key, handler, check_result = check_result # type: ignore[assignment,misc]
|
2020-08-21 23:20:28 +02:00
|
|
|
raise_dp_handler_stop = False
|
2019-10-17 00:03:53 +02:00
|
|
|
|
|
|
|
with self._timeout_jobs_lock:
|
|
|
|
# Remove the old timeout job (if present)
|
|
|
|
timeout_job = self.timeout_jobs.pop(conversation_key, None)
|
|
|
|
|
|
|
|
if timeout_job is not None:
|
|
|
|
timeout_job.schedule_removal()
|
2020-08-21 23:20:28 +02:00
|
|
|
try:
|
|
|
|
new_state = handler.handle_update(update, dispatcher, check_result, context)
|
2020-10-31 16:33:34 +01:00
|
|
|
except DispatcherHandlerStop as exception:
|
|
|
|
new_state = exception.state
|
2020-08-21 23:20:28 +02:00
|
|
|
raise_dp_handler_stop = True
|
2019-10-17 00:03:53 +02:00
|
|
|
with self._timeout_jobs_lock:
|
2020-10-06 19:28:40 +02:00
|
|
|
if self.conversation_timeout and new_state != self.END and dispatcher.job_queue:
|
2019-10-17 00:03:53 +02:00
|
|
|
# Add the new timeout job
|
|
|
|
self.timeout_jobs[conversation_key] = dispatcher.job_queue.run_once(
|
2020-10-09 17:22:07 +02:00
|
|
|
self._trigger_timeout, # type: ignore[arg-type]
|
|
|
|
self.conversation_timeout,
|
|
|
|
context=_ConversationTimeoutContext(
|
|
|
|
conversation_key, update, dispatcher, context
|
|
|
|
),
|
|
|
|
)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2019-10-11 21:59:36 +02:00
|
|
|
if isinstance(self.map_to_parent, dict) and new_state in self.map_to_parent:
|
|
|
|
self.update_state(self.END, conversation_key)
|
2020-08-21 23:20:28 +02:00
|
|
|
if raise_dp_handler_stop:
|
|
|
|
raise DispatcherHandlerStop(self.map_to_parent.get(new_state))
|
2020-10-31 16:33:34 +01:00
|
|
|
return self.map_to_parent.get(new_state)
|
|
|
|
|
|
|
|
self.update_state(new_state, conversation_key)
|
|
|
|
if raise_dp_handler_stop:
|
|
|
|
# Don't pass the new state here. If we're in a nested conversation, the parent is
|
|
|
|
# expecting None as return value.
|
|
|
|
raise DispatcherHandlerStop()
|
2020-10-06 19:28:40 +02:00
|
|
|
return None
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
def update_state(self, new_state: object, key: Tuple[int, ...]) -> None:
|
2016-07-15 01:30:54 +02:00
|
|
|
if new_state == self.END:
|
2019-10-17 00:03:53 +02:00
|
|
|
with self._conversations_lock:
|
|
|
|
if key in self.conversations:
|
|
|
|
# If there is no key in conversations, nothing is done.
|
|
|
|
del self.conversations[key]
|
2020-10-06 19:28:40 +02:00
|
|
|
if self.persistent and self.persistence and self.name:
|
2019-10-17 00:03:53 +02:00
|
|
|
self.persistence.update_conversation(self.name, key, None)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
elif isinstance(new_state, Promise):
|
2019-10-17 00:03:53 +02:00
|
|
|
with self._conversations_lock:
|
|
|
|
self.conversations[key] = (self.conversations.get(key), new_state)
|
2020-10-06 19:28:40 +02:00
|
|
|
if self.persistent and self.persistence and self.name:
|
2020-10-09 17:22:07 +02:00
|
|
|
self.persistence.update_conversation(
|
|
|
|
self.name, key, (self.conversations.get(key), new_state)
|
|
|
|
)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
elif new_state is not None:
|
2019-10-17 00:03:53 +02:00
|
|
|
with self._conversations_lock:
|
|
|
|
self.conversations[key] = new_state
|
2020-10-06 19:28:40 +02:00
|
|
|
if self.persistent and self.persistence and self.name:
|
2019-10-17 00:03:53 +02:00
|
|
|
self.persistence.update_conversation(self.name, key, new_state)
|
2018-03-01 10:34:47 +01:00
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
def _trigger_timeout(self, context: _ConversationTimeoutContext, job: 'Job' = None) -> None:
|
2018-10-04 08:58:40 +02:00
|
|
|
self.logger.debug('conversation timeout was triggered!')
|
2019-04-05 20:03:44 +02:00
|
|
|
|
2019-04-05 12:59:32 +02:00
|
|
|
# Backward compatibility with bots that do not use CallbackContext
|
2020-01-11 19:51:31 +01:00
|
|
|
callback_context = None
|
2019-04-05 12:59:32 +02:00
|
|
|
if isinstance(context, CallbackContext):
|
2019-10-17 00:03:53 +02:00
|
|
|
job = context.job
|
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
context = job.context # type:ignore[union-attr,assignment]
|
2020-04-10 13:18:43 +02:00
|
|
|
callback_context = context.callback_context
|
2019-10-17 00:03:53 +02:00
|
|
|
|
|
|
|
with self._timeout_jobs_lock:
|
|
|
|
found_job = self.timeout_jobs[context.conversation_key]
|
|
|
|
if found_job is not job:
|
|
|
|
# The timeout has been canceled in handle_update
|
|
|
|
return
|
|
|
|
del self.timeout_jobs[context.conversation_key]
|
2019-04-05 20:03:44 +02:00
|
|
|
|
2018-10-04 08:58:40 +02:00
|
|
|
handlers = self.states.get(self.TIMEOUT, [])
|
|
|
|
for handler in handlers:
|
2019-04-05 12:59:32 +02:00
|
|
|
check = handler.check_update(context.update)
|
2018-10-04 08:58:40 +02:00
|
|
|
if check is not None and check is not False:
|
2020-08-21 23:20:28 +02:00
|
|
|
try:
|
2020-10-09 17:22:07 +02:00
|
|
|
handler.handle_update(
|
|
|
|
context.update, context.dispatcher, check, callback_context
|
|
|
|
)
|
2020-08-21 23:20:28 +02:00
|
|
|
except DispatcherHandlerStop:
|
2020-10-09 17:22:07 +02:00
|
|
|
self.logger.warning(
|
|
|
|
'DispatcherHandlerStop in TIMEOUT state of '
|
|
|
|
'ConversationHandler has no effect. Ignoring.'
|
|
|
|
)
|
2019-04-05 12:59:32 +02:00
|
|
|
self.update_state(self.END, context.conversation_key)
|