2016-07-15 01:30:54 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2023-01-01 21:31:29 +01:00
|
|
|
# Copyright (C) 2015-2023
|
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
|
2017-08-11 23:58:41 +02:00
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
2016-07-15 01:30:54 +02:00
|
|
|
# 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
|
2017-08-11 23:58:41 +02:00
|
|
|
# GNU Lesser Public License for more details.
|
2016-07-15 01:30:54 +02:00
|
|
|
#
|
2017-08-11 23:58:41 +02:00
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
2016-07-15 01:30:54 +02:00
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
2022-04-24 12:38:09 +02:00
|
|
|
"""Persistence of conversations is tested in test_basepersistence.py"""
|
|
|
|
import asyncio
|
2018-03-05 12:18:40 +01:00
|
|
|
import logging
|
2021-11-06 15:19:21 +01:00
|
|
|
from warnings import filterwarnings
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
import pytest
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
from telegram import (
|
2022-05-05 09:27:54 +02:00
|
|
|
CallbackQuery,
|
2020-10-09 17:22:07 +02:00
|
|
|
Chat,
|
2022-05-05 09:27:54 +02:00
|
|
|
ChosenInlineResult,
|
|
|
|
InlineQuery,
|
2022-04-24 12:38:09 +02:00
|
|
|
Message,
|
2020-10-09 17:22:07 +02:00
|
|
|
MessageEntity,
|
2022-04-24 12:38:09 +02:00
|
|
|
PreCheckoutQuery,
|
2022-05-05 09:27:54 +02:00
|
|
|
ShippingQuery,
|
|
|
|
Update,
|
|
|
|
User,
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
|
|
|
from telegram.ext import (
|
2022-05-05 09:27:54 +02:00
|
|
|
ApplicationBuilder,
|
2022-04-24 12:38:09 +02:00
|
|
|
ApplicationHandlerStop,
|
|
|
|
CallbackContext,
|
2020-10-09 17:22:07 +02:00
|
|
|
CallbackQueryHandler,
|
2022-04-24 12:38:09 +02:00
|
|
|
ChosenInlineResultHandler,
|
2022-05-05 09:27:54 +02:00
|
|
|
CommandHandler,
|
|
|
|
ConversationHandler,
|
2022-04-24 12:38:09 +02:00
|
|
|
Defaults,
|
2022-05-05 09:27:54 +02:00
|
|
|
InlineQueryHandler,
|
|
|
|
JobQueue,
|
|
|
|
MessageHandler,
|
|
|
|
PollAnswerHandler,
|
|
|
|
PollHandler,
|
|
|
|
PreCheckoutQueryHandler,
|
|
|
|
ShippingQueryHandler,
|
|
|
|
StringCommandHandler,
|
|
|
|
StringRegexHandler,
|
|
|
|
TypeHandler,
|
|
|
|
filters,
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2021-11-06 15:19:21 +01:00
|
|
|
from telegram.warnings import PTBUserWarning
|
2023-02-22 20:19:46 +01:00
|
|
|
from tests.auxil.build_messages import make_command_message
|
|
|
|
from tests.auxil.pytest_classes import PytestBot, make_bot
|
|
|
|
from tests.auxil.slots import mro_slots
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
@pytest.fixture(scope="class")
|
2017-08-11 23:58:41 +02:00
|
|
|
def user1():
|
2022-05-05 17:40:22 +02:00
|
|
|
return User(first_name="Misses Test", id=123, is_bot=False)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
@pytest.fixture(scope="class")
|
2017-08-11 23:58:41 +02:00
|
|
|
def user2():
|
2022-05-05 17:40:22 +02:00
|
|
|
return User(first_name="Mister Test", id=124, is_bot=False)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
def raise_ahs(func):
|
|
|
|
async def decorator(self, *args, **kwargs):
|
|
|
|
result = await func(self, *args, **kwargs)
|
|
|
|
if self.raise_app_handler_stop:
|
|
|
|
raise ApplicationHandlerStop(result)
|
2021-05-27 09:38:17 +02:00
|
|
|
return result
|
2020-08-21 23:20:28 +02:00
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2020-06-15 18:20:51 +02:00
|
|
|
class TestConversationHandler:
|
2022-04-24 12:38:09 +02:00
|
|
|
"""Persistence of conversations is tested in test_basepersistence.py"""
|
|
|
|
|
2016-07-15 01:30:54 +02:00
|
|
|
# State definitions
|
|
|
|
# At first we're thirsty. Then we brew coffee, we drink it
|
|
|
|
# and then we can start coding!
|
|
|
|
END, THIRSTY, BREWING, DRINKING, CODING = range(-1, 4)
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2019-10-11 21:59:36 +02:00
|
|
|
# Drinking state definitions (nested)
|
|
|
|
# At first we're holding the cup. Then we sip coffee, and last we swallow it
|
2022-05-05 17:40:22 +02:00
|
|
|
HOLDING, SIPPING, SWALLOWING, REPLENISHING, STOPPING = map(chr, range(ord("a"), ord("f")))
|
2019-10-11 21:59:36 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
current_state, entry_points, states, fallbacks = None, None, None, None
|
|
|
|
group = Chat(0, Chat.GROUP)
|
|
|
|
second_group = Chat(1, Chat.GROUP)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
raise_app_handler_stop = False
|
2020-08-21 23:20:28 +02:00
|
|
|
test_flag = False
|
|
|
|
|
2016-07-15 01:30:54 +02:00
|
|
|
# Test related
|
2017-08-11 23:58:41 +02:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def reset(self):
|
2022-04-24 12:38:09 +02:00
|
|
|
self.raise_app_handler_stop = False
|
2020-08-21 23:20:28 +02:00
|
|
|
self.test_flag = False
|
2021-04-05 13:25:27 +02:00
|
|
|
self.current_state = {}
|
2022-05-05 17:40:22 +02:00
|
|
|
self.entry_points = [CommandHandler("start", self.start)]
|
2016-10-12 22:56:57 +02:00
|
|
|
self.states = {
|
2022-05-05 17:40:22 +02:00
|
|
|
self.THIRSTY: [CommandHandler("brew", self.brew), CommandHandler("wait", self.start)],
|
|
|
|
self.BREWING: [CommandHandler("pourCoffee", self.drink)],
|
2020-10-09 17:22:07 +02:00
|
|
|
self.DRINKING: [
|
2022-05-05 17:40:22 +02:00
|
|
|
CommandHandler("startCoding", self.code),
|
|
|
|
CommandHandler("drinkMore", self.drink),
|
|
|
|
CommandHandler("end", self.end),
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
2016-10-12 22:56:57 +02:00
|
|
|
self.CODING: [
|
2022-05-05 17:40:22 +02:00
|
|
|
CommandHandler("keepCoding", self.code),
|
|
|
|
CommandHandler("gettingThirsty", self.start),
|
|
|
|
CommandHandler("drinkMore", self.drink),
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
2016-10-12 22:56:57 +02:00
|
|
|
}
|
2022-05-05 17:40:22 +02:00
|
|
|
self.fallbacks = [CommandHandler("eat", self.start)]
|
2018-10-04 08:58:40 +02:00
|
|
|
self.is_timeout = False
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2019-10-11 21:59:36 +02:00
|
|
|
# for nesting tests
|
|
|
|
self.nested_states = {
|
2022-05-05 17:40:22 +02:00
|
|
|
self.THIRSTY: [CommandHandler("brew", self.brew), CommandHandler("wait", self.start)],
|
|
|
|
self.BREWING: [CommandHandler("pourCoffee", self.drink)],
|
2019-10-11 21:59:36 +02:00
|
|
|
self.CODING: [
|
2022-05-05 17:40:22 +02:00
|
|
|
CommandHandler("keepCoding", self.code),
|
|
|
|
CommandHandler("gettingThirsty", self.start),
|
|
|
|
CommandHandler("drinkMore", self.drink),
|
2019-10-11 21:59:36 +02:00
|
|
|
],
|
|
|
|
}
|
2022-05-05 17:40:22 +02:00
|
|
|
self.drinking_entry_points = [CommandHandler("hold", self.hold)]
|
2019-10-11 21:59:36 +02:00
|
|
|
self.drinking_states = {
|
2022-05-05 17:40:22 +02:00
|
|
|
self.HOLDING: [CommandHandler("sip", self.sip)],
|
|
|
|
self.SIPPING: [CommandHandler("swallow", self.swallow)],
|
|
|
|
self.SWALLOWING: [CommandHandler("hold", self.hold)],
|
2019-10-11 21:59:36 +02:00
|
|
|
}
|
2020-10-09 17:22:07 +02:00
|
|
|
self.drinking_fallbacks = [
|
2022-05-05 17:40:22 +02:00
|
|
|
CommandHandler("replenish", self.replenish),
|
|
|
|
CommandHandler("stop", self.stop),
|
|
|
|
CommandHandler("end", self.end),
|
|
|
|
CommandHandler("startCoding", self.code),
|
|
|
|
CommandHandler("drinkMore", self.drink),
|
2020-10-09 17:22:07 +02:00
|
|
|
]
|
2019-10-11 21:59:36 +02:00
|
|
|
self.drinking_entry_points.extend(self.drinking_fallbacks)
|
|
|
|
|
|
|
|
# Map nested states to parent states:
|
|
|
|
self.drinking_map_to_parent = {
|
|
|
|
# Option 1 - Map a fictional internal state to an external parent state
|
|
|
|
self.REPLENISHING: self.BREWING,
|
|
|
|
# Option 2 - Map a fictional internal state to the END state on the parent
|
|
|
|
self.STOPPING: self.END,
|
|
|
|
# Option 3 - Map the internal END state to an external parent state
|
|
|
|
self.END: self.CODING,
|
|
|
|
# Option 4 - Map an external state to the same external parent state
|
|
|
|
self.CODING: self.CODING,
|
|
|
|
# Option 5 - Map an external state to the internal entry point
|
2020-10-09 17:22:07 +02:00
|
|
|
self.DRINKING: self.DRINKING,
|
2019-10-11 21:59:36 +02:00
|
|
|
}
|
|
|
|
|
2016-07-15 01:30:54 +02:00
|
|
|
# State handlers
|
|
|
|
def _set_state(self, update, state):
|
|
|
|
self.current_state[update.message.from_user.id] = state
|
|
|
|
return state
|
|
|
|
|
|
|
|
# Actions
|
2022-04-24 12:38:09 +02:00
|
|
|
@raise_ahs
|
|
|
|
async def start(self, update, context):
|
2020-01-11 19:51:31 +01:00
|
|
|
if isinstance(update, Update):
|
|
|
|
return self._set_state(update, self.THIRSTY)
|
2021-08-29 18:15:04 +02:00
|
|
|
return self._set_state(context.bot, self.THIRSTY)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
@raise_ahs
|
|
|
|
async def end(self, update, context):
|
2018-03-05 12:18:40 +01:00
|
|
|
return self._set_state(update, self.END)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
@raise_ahs
|
|
|
|
async def start_end(self, update, context):
|
2016-09-20 05:00:39 +02:00
|
|
|
return self._set_state(update, self.END)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
@raise_ahs
|
|
|
|
async def start_none(self, update, context):
|
2019-02-14 12:29:58 +01:00
|
|
|
return self._set_state(update, None)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
@raise_ahs
|
|
|
|
async def brew(self, update, context):
|
2020-01-11 19:51:31 +01:00
|
|
|
if isinstance(update, Update):
|
|
|
|
return self._set_state(update, self.BREWING)
|
2021-08-29 18:15:04 +02:00
|
|
|
return self._set_state(context.bot, self.BREWING)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
@raise_ahs
|
|
|
|
async def drink(self, update, context):
|
2016-07-15 01:30:54 +02:00
|
|
|
return self._set_state(update, self.DRINKING)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
@raise_ahs
|
|
|
|
async def code(self, update, context):
|
2016-07-15 01:30:54 +02:00
|
|
|
return self._set_state(update, self.CODING)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
@raise_ahs
|
|
|
|
async def passout(self, update, context):
|
2022-05-05 17:40:22 +02:00
|
|
|
assert update.message.text == "/brew"
|
2020-01-11 19:51:31 +01:00
|
|
|
assert isinstance(update, Update)
|
2018-10-04 08:58:40 +02:00
|
|
|
self.is_timeout = True
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
@raise_ahs
|
|
|
|
async def passout2(self, update, context):
|
2020-01-11 19:51:31 +01:00
|
|
|
assert isinstance(update, Update)
|
|
|
|
self.is_timeout = True
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
@raise_ahs
|
|
|
|
async def passout_context(self, update, context):
|
2022-05-05 17:40:22 +02:00
|
|
|
assert update.message.text == "/brew"
|
2020-01-11 19:51:31 +01:00
|
|
|
assert isinstance(context, CallbackContext)
|
|
|
|
self.is_timeout = True
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
@raise_ahs
|
|
|
|
async def passout2_context(self, update, context):
|
2020-01-11 19:51:31 +01:00
|
|
|
assert isinstance(context, CallbackContext)
|
2018-10-04 08:58:40 +02:00
|
|
|
self.is_timeout = True
|
|
|
|
|
2019-10-11 21:59:36 +02:00
|
|
|
# Drinking actions (nested)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
@raise_ahs
|
|
|
|
async def hold(self, update, context):
|
2019-10-11 21:59:36 +02:00
|
|
|
return self._set_state(update, self.HOLDING)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
@raise_ahs
|
|
|
|
async def sip(self, update, context):
|
2019-10-11 21:59:36 +02:00
|
|
|
return self._set_state(update, self.SIPPING)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
@raise_ahs
|
|
|
|
async def swallow(self, update, context):
|
2019-10-11 21:59:36 +02:00
|
|
|
return self._set_state(update, self.SWALLOWING)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
@raise_ahs
|
|
|
|
async def replenish(self, update, context):
|
2019-10-11 21:59:36 +02:00
|
|
|
return self._set_state(update, self.REPLENISHING)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
@raise_ahs
|
|
|
|
async def stop(self, update, context):
|
2019-10-11 21:59:36 +02:00
|
|
|
return self._set_state(update, self.STOPPING)
|
|
|
|
|
2023-02-22 20:19:46 +01:00
|
|
|
def test_slot_behaviour(self):
|
2022-04-24 12:38:09 +02:00
|
|
|
handler = ConversationHandler(entry_points=[], states={}, fallbacks=[])
|
|
|
|
for attr in handler.__slots__:
|
2022-05-05 17:40:22 +02:00
|
|
|
assert getattr(handler, attr, "err") != "err", f"got extra slot '{attr}'"
|
2022-04-24 12:38:09 +02:00
|
|
|
assert len(mro_slots(handler)) == len(set(mro_slots(handler))), "duplicate slot"
|
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
entry_points = []
|
|
|
|
states = {}
|
|
|
|
fallbacks = []
|
|
|
|
map_to_parent = {}
|
|
|
|
ch = ConversationHandler(
|
|
|
|
entry_points=entry_points,
|
|
|
|
states=states,
|
|
|
|
fallbacks=fallbacks,
|
2022-05-05 17:40:22 +02:00
|
|
|
per_chat="per_chat",
|
|
|
|
per_user="per_user",
|
|
|
|
per_message="per_message",
|
|
|
|
persistent="persistent",
|
|
|
|
name="name",
|
|
|
|
allow_reentry="allow_reentry",
|
2022-04-24 12:38:09 +02:00
|
|
|
conversation_timeout=42,
|
|
|
|
map_to_parent=map_to_parent,
|
|
|
|
)
|
|
|
|
assert ch.entry_points is entry_points
|
|
|
|
assert ch.states is states
|
|
|
|
assert ch.fallbacks is fallbacks
|
|
|
|
assert ch.map_to_parent is map_to_parent
|
2022-05-05 17:40:22 +02:00
|
|
|
assert ch.per_chat == "per_chat"
|
|
|
|
assert ch.per_user == "per_user"
|
|
|
|
assert ch.per_message == "per_message"
|
|
|
|
assert ch.persistent == "persistent"
|
|
|
|
assert ch.name == "name"
|
|
|
|
assert ch.allow_reentry == "allow_reentry"
|
2022-04-24 12:38:09 +02:00
|
|
|
|
|
|
|
def test_init_persistent_no_name(self):
|
|
|
|
with pytest.raises(ValueError, match="can't be persistent when handler is unnamed"):
|
|
|
|
ConversationHandler(
|
|
|
|
self.entry_points, states=self.states, fallbacks=[], persistent=True
|
|
|
|
)
|
|
|
|
|
|
|
|
async def test_check_update_returns_non(self, app, user1):
|
|
|
|
"""checks some cases where updates should not be handled"""
|
|
|
|
conv_handler = ConversationHandler([], {}, [], per_message=True, per_chat=True)
|
2022-05-05 17:40:22 +02:00
|
|
|
assert not conv_handler.check_update("not an Update")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert not conv_handler.check_update(Update(0))
|
|
|
|
assert not conv_handler.check_update(
|
2022-05-05 17:40:22 +02:00
|
|
|
Update(0, callback_query=CallbackQuery("1", from_user=user1, chat_instance="1"))
|
2022-04-24 12:38:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
async def test_handlers_generate_warning(self, recwarn):
|
|
|
|
"""this function tests all handler + per_* setting combinations."""
|
|
|
|
|
|
|
|
# the warning message action needs to be set to always,
|
|
|
|
# otherwise only the first occurrence will be issued
|
|
|
|
filterwarnings(action="always", category=PTBUserWarning)
|
|
|
|
|
|
|
|
# this class doesn't do anything, its just not the Update class
|
|
|
|
class NotUpdate:
|
|
|
|
pass
|
|
|
|
|
|
|
|
recwarn.clear()
|
|
|
|
|
|
|
|
# this conversation handler has the string, string_regex, Pollhandler and TypeHandler
|
|
|
|
# which should all generate a warning no matter the per_* setting. TypeHandler should
|
|
|
|
# not when the class is Update
|
|
|
|
ConversationHandler(
|
|
|
|
entry_points=[StringCommandHandler("code", self.code)],
|
|
|
|
states={
|
|
|
|
self.BREWING: [
|
|
|
|
StringRegexHandler("code", self.code),
|
|
|
|
PollHandler(self.code),
|
|
|
|
TypeHandler(NotUpdate, self.code),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
fallbacks=[TypeHandler(Update, self.code)],
|
|
|
|
)
|
|
|
|
|
|
|
|
# these handlers should all raise a warning when per_chat is True
|
|
|
|
ConversationHandler(
|
|
|
|
entry_points=[ShippingQueryHandler(self.code)],
|
|
|
|
states={
|
|
|
|
self.BREWING: [
|
|
|
|
InlineQueryHandler(self.code),
|
|
|
|
PreCheckoutQueryHandler(self.code),
|
|
|
|
PollAnswerHandler(self.code),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
fallbacks=[ChosenInlineResultHandler(self.code)],
|
|
|
|
per_chat=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
# the CallbackQueryHandler should *not* raise when per_message is True,
|
|
|
|
# but any other one should
|
|
|
|
ConversationHandler(
|
|
|
|
entry_points=[CallbackQueryHandler(self.code)],
|
|
|
|
states={
|
|
|
|
self.BREWING: [CommandHandler("code", self.code)],
|
|
|
|
},
|
|
|
|
fallbacks=[CallbackQueryHandler(self.code)],
|
|
|
|
per_message=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
# the CallbackQueryHandler should raise when per_message is False
|
|
|
|
ConversationHandler(
|
|
|
|
entry_points=[CommandHandler("code", self.code)],
|
|
|
|
states={
|
|
|
|
self.BREWING: [CommandHandler("code", self.code)],
|
|
|
|
},
|
|
|
|
fallbacks=[CallbackQueryHandler(self.code)],
|
|
|
|
per_message=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
# adding a nested conv to a conversation with timeout should warn
|
|
|
|
child = ConversationHandler(
|
|
|
|
entry_points=[CommandHandler("code", self.code)],
|
|
|
|
states={
|
|
|
|
self.BREWING: [CommandHandler("code", self.code)],
|
|
|
|
},
|
|
|
|
fallbacks=[CommandHandler("code", self.code)],
|
|
|
|
)
|
|
|
|
|
|
|
|
ConversationHandler(
|
|
|
|
entry_points=[CommandHandler("code", self.code)],
|
|
|
|
states={
|
|
|
|
self.BREWING: [child],
|
|
|
|
},
|
|
|
|
fallbacks=[CommandHandler("code", self.code)],
|
|
|
|
conversation_timeout=42,
|
|
|
|
)
|
|
|
|
|
|
|
|
# If per_message is True, per_chat should also be True, since msg ids are not unique
|
|
|
|
ConversationHandler(
|
|
|
|
entry_points=[CallbackQueryHandler(self.code, "code")],
|
|
|
|
states={
|
|
|
|
self.BREWING: [CallbackQueryHandler(self.code, "code")],
|
|
|
|
},
|
|
|
|
fallbacks=[CallbackQueryHandler(self.code, "code")],
|
|
|
|
per_message=True,
|
|
|
|
per_chat=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
# the overall number of handlers throwing a warning is 13
|
|
|
|
assert len(recwarn) == 13
|
|
|
|
# now we test the messages, they are raised in the order they are inserted
|
|
|
|
# into the conversation handler
|
|
|
|
assert str(recwarn[0].message) == (
|
|
|
|
"The `ConversationHandler` only handles updates of type `telegram.Update`. "
|
|
|
|
"StringCommandHandler handles updates of type `str`."
|
|
|
|
)
|
|
|
|
assert str(recwarn[1].message) == (
|
|
|
|
"The `ConversationHandler` only handles updates of type `telegram.Update`. "
|
|
|
|
"StringRegexHandler handles updates of type `str`."
|
|
|
|
)
|
|
|
|
assert str(recwarn[2].message) == (
|
|
|
|
"PollHandler will never trigger in a conversation since it has no information "
|
|
|
|
"about the chat or the user who voted in it. Do you mean the "
|
|
|
|
"`PollAnswerHandler`?"
|
|
|
|
)
|
|
|
|
assert str(recwarn[3].message) == (
|
|
|
|
"The `ConversationHandler` only handles updates of type `telegram.Update`. "
|
|
|
|
"The TypeHandler is set to handle NotUpdate."
|
|
|
|
)
|
|
|
|
|
|
|
|
per_faq_link = (
|
|
|
|
" Read this FAQ entry to learn more about the per_* settings: "
|
|
|
|
"https://github.com/python-telegram-bot/python-telegram-bot/wiki"
|
|
|
|
"/Frequently-Asked-Questions#what-do-the-per_-settings-in-conversationhandler-do."
|
|
|
|
)
|
|
|
|
|
|
|
|
assert str(recwarn[4].message) == (
|
|
|
|
"Updates handled by ShippingQueryHandler only have information about the user,"
|
|
|
|
" so this handler won't ever be triggered if `per_chat=True`." + per_faq_link
|
|
|
|
)
|
|
|
|
assert str(recwarn[5].message) == (
|
|
|
|
"Updates handled by ChosenInlineResultHandler only have information about the user,"
|
|
|
|
" so this handler won't ever be triggered if `per_chat=True`." + per_faq_link
|
|
|
|
)
|
|
|
|
assert str(recwarn[6].message) == (
|
|
|
|
"Updates handled by InlineQueryHandler only have information about the user,"
|
|
|
|
" so this handler won't ever be triggered if `per_chat=True`." + per_faq_link
|
|
|
|
)
|
|
|
|
assert str(recwarn[7].message) == (
|
|
|
|
"Updates handled by PreCheckoutQueryHandler only have information about the user,"
|
|
|
|
" so this handler won't ever be triggered if `per_chat=True`." + per_faq_link
|
|
|
|
)
|
|
|
|
assert str(recwarn[8].message) == (
|
|
|
|
"Updates handled by PollAnswerHandler only have information about the user,"
|
|
|
|
" so this handler won't ever be triggered if `per_chat=True`." + per_faq_link
|
|
|
|
)
|
|
|
|
assert str(recwarn[9].message) == (
|
|
|
|
"If 'per_message=True', all entry points, state handlers, and fallbacks must be "
|
|
|
|
"'CallbackQueryHandler', since no other handlers have a message context."
|
|
|
|
+ per_faq_link
|
|
|
|
)
|
|
|
|
assert str(recwarn[10].message) == (
|
|
|
|
"If 'per_message=False', 'CallbackQueryHandler' will not be tracked for "
|
|
|
|
"every message." + per_faq_link
|
|
|
|
)
|
|
|
|
assert str(recwarn[11].message) == (
|
|
|
|
"Using `conversation_timeout` with nested conversations is currently not "
|
|
|
|
"supported. You can still try to use it, but it will likely behave differently"
|
|
|
|
" from what you expect."
|
|
|
|
)
|
|
|
|
|
|
|
|
assert str(recwarn[12].message) == (
|
|
|
|
"If 'per_message=True' is used, 'per_chat=True' should also be used, "
|
|
|
|
"since message IDs are not globally unique."
|
|
|
|
)
|
|
|
|
|
|
|
|
# this for loop checks if the correct stacklevel is used when generating the warning
|
|
|
|
for warning in recwarn:
|
|
|
|
assert warning.filename == __file__, "incorrect stacklevel!"
|
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
@pytest.mark.parametrize(
|
2022-05-05 17:40:22 +02:00
|
|
|
"attr",
|
2020-10-09 17:22:07 +02:00
|
|
|
[
|
2022-05-05 17:40:22 +02:00
|
|
|
"entry_points",
|
|
|
|
"states",
|
|
|
|
"fallbacks",
|
|
|
|
"per_chat",
|
|
|
|
"per_user",
|
|
|
|
"per_message",
|
|
|
|
"name",
|
|
|
|
"persistent",
|
|
|
|
"allow_reentry",
|
|
|
|
"conversation_timeout",
|
|
|
|
"map_to_parent",
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
|
|
|
indirect=False,
|
|
|
|
)
|
2020-03-30 17:37:37 +02:00
|
|
|
def test_immutable(self, attr):
|
2022-04-24 12:38:09 +02:00
|
|
|
ch = ConversationHandler(entry_points=[], states={}, fallbacks=[])
|
2022-05-05 17:40:22 +02:00
|
|
|
with pytest.raises(AttributeError, match=f"You can not assign a new value to {attr}"):
|
2020-03-30 17:37:37 +02:00
|
|
|
setattr(ch, attr, True)
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
def test_per_all_false(self):
|
|
|
|
with pytest.raises(ValueError, match="can't all be 'False'"):
|
2020-10-09 17:22:07 +02:00
|
|
|
ConversationHandler(
|
2022-04-24 12:38:09 +02:00
|
|
|
entry_points=[],
|
|
|
|
states={},
|
|
|
|
fallbacks=[],
|
2020-10-09 17:22:07 +02:00
|
|
|
per_chat=False,
|
|
|
|
per_user=False,
|
|
|
|
per_message=False,
|
|
|
|
)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
@pytest.mark.parametrize("raise_ahs", [True, False])
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_basic_and_app_handler_stop(self, app, bot, user1, user2, raise_ahs):
|
2020-10-09 17:22:07 +02:00
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=self.entry_points, states=self.states, fallbacks=self.fallbacks
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
|
|
|
|
|
|
|
async def callback(_, __):
|
|
|
|
self.test_flag = True
|
|
|
|
|
|
|
|
app.add_handler(TypeHandler(object, callback), group=100)
|
|
|
|
self.raise_app_handler_stop = raise_ahs
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
# User one, starts the state machine.
|
2020-10-09 17:22:07 +02:00
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2020-10-09 17:22:07 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.THIRSTY
|
|
|
|
assert self.test_flag == (not raise_ahs)
|
|
|
|
|
|
|
|
# The user is thirsty and wants to brew coffee.
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/brew"
|
|
|
|
message.entities[0].length = len("/brew")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.BREWING
|
|
|
|
assert self.test_flag == (not raise_ahs)
|
|
|
|
|
|
|
|
# Lets see if an invalid command makes sure, no state is changed.
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/nothing"
|
|
|
|
message.entities[0].length = len("/nothing")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.BREWING
|
|
|
|
assert self.test_flag is True
|
|
|
|
self.test_flag = False
|
|
|
|
|
|
|
|
# Lets see if the state machine still works by pouring coffee.
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/pourCoffee"
|
|
|
|
message.entities[0].length = len("/pourCoffee")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.DRINKING
|
|
|
|
assert self.test_flag == (not raise_ahs)
|
|
|
|
|
|
|
|
# Let's now verify that for another user, who did not start yet,
|
|
|
|
# the state has not been changed.
|
|
|
|
message.from_user = user2
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
with pytest.raises(KeyError):
|
|
|
|
self.current_state[user2.id]
|
|
|
|
|
|
|
|
async def test_conversation_handler_end(self, caplog, app, bot, user1):
|
2020-10-09 17:22:07 +02:00
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=self.entry_points, states=self.states, fallbacks=self.fallbacks
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2018-03-05 12:18:40 +01:00
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2020-10-09 17:22:07 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2022-04-24 12:38:09 +02:00
|
|
|
|
|
|
|
async with app:
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/brew"
|
|
|
|
message.entities[0].length = len("/brew")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/pourCoffee"
|
|
|
|
message.entities[0].length = len("/pourCoffee")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/end"
|
|
|
|
message.entities[0].length = len("/end")
|
2022-04-24 12:38:09 +02:00
|
|
|
caplog.clear()
|
|
|
|
with caplog.at_level(logging.ERROR):
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert len(caplog.records) == 0
|
|
|
|
assert self.current_state[user1.id] == self.END
|
|
|
|
|
|
|
|
# make sure that the conversation has ended by checking that the start command is
|
|
|
|
# accepted again
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/start"
|
|
|
|
message.entities[0].length = len("/start")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(update_id=0, message=message))
|
|
|
|
|
|
|
|
async def test_conversation_handler_fallback(self, app, bot, user1, user2):
|
2020-10-09 17:22:07 +02:00
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=self.entry_points, states=self.states, fallbacks=self.fallbacks
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
# first check if fallback will not trigger start when not started
|
2020-10-09 17:22:07 +02:00
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/eat",
|
|
|
|
entities=[MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/eat"))],
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2022-04-24 12:38:09 +02:00
|
|
|
|
|
|
|
async with app:
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
with pytest.raises(KeyError):
|
|
|
|
self.current_state[user1.id]
|
|
|
|
|
|
|
|
# User starts the state machine.
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/start"
|
|
|
|
message.entities[0].length = len("/start")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.THIRSTY
|
|
|
|
|
|
|
|
# The user is thirsty and wants to brew coffee.
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/brew"
|
|
|
|
message.entities[0].length = len("/brew")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.BREWING
|
|
|
|
|
|
|
|
# Now a fallback command is issued
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/eat"
|
|
|
|
message.entities[0].length = len("/eat")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.THIRSTY
|
|
|
|
|
|
|
|
async def test_unknown_state_warning(self, app, bot, user1, recwarn):
|
|
|
|
def build_callback(state):
|
|
|
|
async def callback(_, __):
|
|
|
|
return state
|
|
|
|
|
|
|
|
return callback
|
|
|
|
|
2021-04-16 19:02:43 +02:00
|
|
|
handler = ConversationHandler(
|
2022-04-24 12:38:09 +02:00
|
|
|
entry_points=[CommandHandler("start", build_callback(1))],
|
2021-04-16 19:02:43 +02:00
|
|
|
states={
|
2022-04-24 12:38:09 +02:00
|
|
|
1: [TypeHandler(Update, build_callback(69))],
|
|
|
|
2: [TypeHandler(Update, build_callback(42))],
|
2021-04-16 19:02:43 +02:00
|
|
|
},
|
|
|
|
fallbacks=self.fallbacks,
|
|
|
|
name="xyz",
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2021-04-16 19:02:43 +02:00
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2021-04-16 19:02:43 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2021-04-16 19:02:43 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
try:
|
|
|
|
await app.process_update(Update(update_id=1, message=message))
|
|
|
|
except Exception as exc:
|
|
|
|
print(exc)
|
|
|
|
raise exc
|
|
|
|
assert len(recwarn) == 1
|
|
|
|
assert str(recwarn[0].message) == (
|
2022-09-16 22:13:11 +02:00
|
|
|
"'callback' returned state 69 which is unknown to the ConversationHandler xyz."
|
2022-04-24 12:38:09 +02:00
|
|
|
)
|
2021-04-16 19:02:43 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_conversation_handler_per_chat(self, app, bot, user1, user2):
|
2017-02-27 17:52:58 +01:00
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=self.entry_points,
|
|
|
|
states=self.states,
|
|
|
|
fallbacks=self.fallbacks,
|
2020-10-09 17:22:07 +02:00
|
|
|
per_user=False,
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2017-02-27 17:52:58 +01:00
|
|
|
|
|
|
|
# User one, starts the state machine.
|
2020-10-09 17:22:07 +02:00
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2020-10-09 17:22:07 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2017-02-27 17:52:58 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
|
|
|
|
# The user is thirsty and wants to brew coffee.
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/brew"
|
|
|
|
message.entities[0].length = len("/brew")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
|
|
|
|
# Let's now verify that for another user, who did not start yet,
|
|
|
|
# the state will be changed because they are in the same group.
|
|
|
|
message.from_user = user2
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/pourCoffee"
|
|
|
|
message.entities[0].length = len("/pourCoffee")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
|
|
|
|
# Check that we're in the DRINKING state by checking that the corresponding command
|
|
|
|
# is accepted
|
|
|
|
message.from_user = user1
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/startCoding"
|
|
|
|
message.entities[0].length = len("/startCoding")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(update_id=0, message=message))
|
|
|
|
message.from_user = user2
|
|
|
|
assert handler.check_update(Update(update_id=0, message=message))
|
|
|
|
|
|
|
|
async def test_conversation_handler_per_user(self, app, bot, user1):
|
2017-02-27 17:52:58 +01:00
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=self.entry_points,
|
|
|
|
states=self.states,
|
|
|
|
fallbacks=self.fallbacks,
|
2020-10-09 17:22:07 +02:00
|
|
|
per_chat=False,
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2017-02-27 17:52:58 +01:00
|
|
|
|
|
|
|
# User one, starts the state machine.
|
2020-10-09 17:22:07 +02:00
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2020-10-09 17:22:07 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2022-07-03 15:21:04 +02:00
|
|
|
|
|
|
|
# First check that updates without user won't be handled
|
|
|
|
message.from_user = None
|
|
|
|
assert not handler.check_update(Update(update_id=0, message=message))
|
|
|
|
|
|
|
|
message.from_user = user1
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
|
|
|
|
# The user is thirsty and wants to brew coffee.
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/brew"
|
|
|
|
message.entities[0].length = len("/brew")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
|
|
|
|
# Let's now verify that for the same user in a different group, the state will still be
|
|
|
|
# updated
|
|
|
|
message.chat = self.second_group
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/pourCoffee"
|
|
|
|
message.entities[0].length = len("/pourCoffee")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
|
|
|
|
# Check that we're in the DRINKING state by checking that the corresponding command
|
|
|
|
# is accepted
|
|
|
|
message.chat = self.group
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/startCoding"
|
|
|
|
message.entities[0].length = len("/startCoding")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(update_id=0, message=message))
|
|
|
|
message.chat = self.second_group
|
|
|
|
assert handler.check_update(Update(update_id=0, message=message))
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
@pytest.mark.parametrize("inline", [True, False])
|
2022-04-24 12:38:09 +02:00
|
|
|
@pytest.mark.filterwarnings("ignore: If 'per_message=True' is used, 'per_chat=True'")
|
|
|
|
async def test_conversation_handler_per_message(self, app, bot, user1, user2, inline):
|
|
|
|
async def entry(update, context):
|
2017-02-27 17:52:58 +01:00
|
|
|
return 1
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def one(update, context):
|
2017-02-27 17:52:58 +01:00
|
|
|
return 2
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def two(update, context):
|
2017-02-27 17:52:58 +01:00
|
|
|
return ConversationHandler.END
|
|
|
|
|
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=[CallbackQueryHandler(entry)],
|
2022-04-24 12:38:09 +02:00
|
|
|
states={
|
2022-05-05 17:40:22 +02:00
|
|
|
1: [CallbackQueryHandler(one, pattern="^1$")],
|
|
|
|
2: [CallbackQueryHandler(two, pattern="^2$")],
|
2022-04-24 12:38:09 +02:00
|
|
|
},
|
2017-02-27 17:52:58 +01:00
|
|
|
fallbacks=[],
|
2020-10-09 17:22:07 +02:00
|
|
|
per_message=True,
|
2022-04-24 12:38:09 +02:00
|
|
|
per_chat=not inline,
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2017-02-27 17:52:58 +01:00
|
|
|
|
|
|
|
# User one, starts the state machine.
|
2022-04-24 12:38:09 +02:00
|
|
|
message = (
|
2022-10-07 11:51:53 +02:00
|
|
|
Message(0, None, self.group, from_user=user1, text="msg w/ inlinekeyboard")
|
2022-04-24 12:38:09 +02:00
|
|
|
if not inline
|
|
|
|
else None
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
if message:
|
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
2022-05-05 17:40:22 +02:00
|
|
|
inline_message_id = "42" if inline else None
|
2022-04-24 12:38:09 +02:00
|
|
|
|
|
|
|
async with app:
|
|
|
|
cbq_1 = CallbackQuery(
|
|
|
|
0,
|
|
|
|
user1,
|
|
|
|
None,
|
|
|
|
message=message,
|
2022-05-05 17:40:22 +02:00
|
|
|
data="1",
|
2022-04-24 12:38:09 +02:00
|
|
|
inline_message_id=inline_message_id,
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
cbq_1.set_bot(bot)
|
2022-04-24 12:38:09 +02:00
|
|
|
cbq_2 = CallbackQuery(
|
|
|
|
0,
|
|
|
|
user1,
|
|
|
|
None,
|
|
|
|
message=message,
|
2022-05-05 17:40:22 +02:00
|
|
|
data="2",
|
2022-04-24 12:38:09 +02:00
|
|
|
inline_message_id=inline_message_id,
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
cbq_2.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
cbq_2._unfreeze()
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, callback_query=cbq_1))
|
2017-02-27 17:52:58 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
# Make sure that we're in the correct state
|
|
|
|
assert handler.check_update(Update(0, callback_query=cbq_1))
|
|
|
|
assert not handler.check_update(Update(0, callback_query=cbq_2))
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, callback_query=cbq_1))
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
# Make sure that we're in the correct state
|
|
|
|
assert not handler.check_update(Update(0, callback_query=cbq_1))
|
|
|
|
assert handler.check_update(Update(0, callback_query=cbq_2))
|
2017-02-27 17:52:58 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
# Let's now verify that for a different user in the same group, the state will not be
|
|
|
|
# updated
|
|
|
|
cbq_2.from_user = user2
|
|
|
|
await app.process_update(Update(update_id=0, callback_query=cbq_2))
|
2017-02-27 17:52:58 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
cbq_2.from_user = user1
|
|
|
|
assert not handler.check_update(Update(0, callback_query=cbq_1))
|
|
|
|
assert handler.check_update(Update(0, callback_query=cbq_2))
|
2016-09-20 05:00:39 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_end_on_first_message(self, app, bot, user1):
|
2016-09-20 05:00:39 +02:00
|
|
|
handler = ConversationHandler(
|
2022-05-05 17:40:22 +02:00
|
|
|
entry_points=[CommandHandler("start", self.start_end)], states={}, fallbacks=[]
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2016-09-20 05:00:39 +02:00
|
|
|
|
|
|
|
# User starts the state machine and immediately ends it.
|
2020-10-09 17:22:07 +02:00
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2020-10-09 17:22:07 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert handler.check_update(Update(update_id=0, message=message))
|
2016-09-20 05:00:39 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_end_on_first_message_non_blocking_handler(self, app, bot, user1):
|
2016-09-20 05:00:39 +02:00
|
|
|
handler = ConversationHandler(
|
2022-05-05 17:40:22 +02:00
|
|
|
entry_points=[CommandHandler("start", callback=self.start_end, block=False)],
|
2020-10-31 16:33:34 +01:00
|
|
|
states={},
|
|
|
|
fallbacks=[],
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2016-09-20 05:00:39 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
# User starts the state machine with a non-blocking function that immediately ends the
|
|
|
|
# conversation. non-blocking results are resolved when the users state is queried next
|
|
|
|
# time.
|
2020-10-09 17:22:07 +02:00
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2020-10-09 17:22:07 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
# give the task a chance to finish
|
|
|
|
await asyncio.sleep(0.1)
|
2016-09-20 05:00:39 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
# Let's check that processing the same update again is accepted. this confirms that
|
|
|
|
# a) the pending state is correctly resolved
|
|
|
|
# b) the conversation has ended
|
|
|
|
assert handler.check_update(Update(0, message=message))
|
2019-02-14 12:29:58 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_none_on_first_message(self, app, bot, user1):
|
2020-10-04 17:20:33 +02:00
|
|
|
handler = ConversationHandler(
|
2022-04-24 12:38:09 +02:00
|
|
|
entry_points=[MessageHandler(filters.ALL, self.start_none)], states={}, fallbacks=[]
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2019-02-14 12:29:58 +01:00
|
|
|
|
|
|
|
# User starts the state machine and a callback function returns None
|
2022-10-07 11:51:53 +02:00
|
|
|
message = Message(0, None, self.group, from_user=user1, text="/start")
|
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
# Check that the same message is accepted again, i.e. the conversation immediately
|
|
|
|
# ended
|
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
|
|
|
|
async def test_none_on_first_message_non_blocking_handler(self, app, bot, user1):
|
2020-10-04 17:20:33 +02:00
|
|
|
handler = ConversationHandler(
|
2022-05-05 17:40:22 +02:00
|
|
|
entry_points=[CommandHandler("start", self.start_none, block=False)],
|
2020-10-09 17:22:07 +02:00
|
|
|
states={},
|
|
|
|
fallbacks=[],
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2020-10-04 17:20:33 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
# User starts the state machine with a non-blocking handler that returns None
|
|
|
|
# non-blocking results are resolved when the users state is queried next time.
|
2020-10-09 17:22:07 +02:00
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2020-10-09 17:22:07 +02:00
|
|
|
from_user=user1,
|
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
# Give the task a chance to finish
|
|
|
|
await asyncio.sleep(0.1)
|
2019-02-14 12:29:58 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
# Let's check that processing the same update again is accepted. this confirms that
|
|
|
|
# a) the pending state is correctly resolved
|
|
|
|
# b) the conversation has ended
|
|
|
|
assert handler.check_update(Update(0, message=message))
|
2016-09-20 05:00:39 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_per_chat_message_without_chat(self, bot, user1):
|
2017-04-29 15:51:48 +02:00
|
|
|
handler = ConversationHandler(
|
2022-05-05 17:40:22 +02:00
|
|
|
entry_points=[CommandHandler("start", self.start_end)], states={}, fallbacks=[]
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
cbq = CallbackQuery(0, user1, None, None)
|
|
|
|
cbq.set_bot(bot)
|
2017-04-29 15:51:48 +02:00
|
|
|
update = Update(0, callback_query=cbq)
|
2017-08-11 23:58:41 +02:00
|
|
|
assert not handler.check_update(update)
|
2017-04-29 15:51:48 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_channel_message_without_chat(self, bot):
|
2020-10-09 17:22:07 +02:00
|
|
|
handler = ConversationHandler(
|
2021-11-20 11:36:18 +01:00
|
|
|
entry_points=[MessageHandler(filters.ALL, self.start_end)], states={}, fallbacks=[]
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message = Message(0, date=None, chat=Chat(0, Chat.CHANNEL, "Misses Test"))
|
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
2021-02-01 19:26:03 +01:00
|
|
|
|
|
|
|
update = Update(0, channel_post=message)
|
|
|
|
assert not handler.check_update(update)
|
|
|
|
|
|
|
|
update = Update(0, edited_channel_post=message)
|
2017-08-11 23:58:41 +02:00
|
|
|
assert not handler.check_update(update)
|
2018-02-12 15:09:51 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_all_update_types(self, app, bot, user1):
|
2020-10-09 17:22:07 +02:00
|
|
|
handler = ConversationHandler(
|
2022-05-05 17:40:22 +02:00
|
|
|
entry_points=[CommandHandler("start", self.start_end)], states={}, fallbacks=[]
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message = Message(0, None, self.group, from_user=user1, text="ignore")
|
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
2022-10-07 11:51:53 +02:00
|
|
|
callback_query = CallbackQuery(0, user1, None, message=message, data="data")
|
|
|
|
callback_query.set_bot(bot)
|
|
|
|
chosen_inline_result = ChosenInlineResult(0, user1, "query")
|
|
|
|
chosen_inline_result.set_bot(bot)
|
|
|
|
inline_query = InlineQuery(0, user1, "query", offset="")
|
|
|
|
inline_query.set_bot(bot)
|
|
|
|
pre_checkout_query = PreCheckoutQuery(0, user1, "USD", 100, [])
|
|
|
|
pre_checkout_query.set_bot(bot)
|
|
|
|
shipping_query = ShippingQuery(0, user1, [], None)
|
|
|
|
shipping_query.set_bot(bot)
|
2018-02-12 15:09:51 +01:00
|
|
|
assert not handler.check_update(Update(0, callback_query=callback_query))
|
|
|
|
assert not handler.check_update(Update(0, chosen_inline_result=chosen_inline_result))
|
|
|
|
assert not handler.check_update(Update(0, inline_query=inline_query))
|
|
|
|
assert not handler.check_update(Update(0, message=message))
|
|
|
|
assert not handler.check_update(Update(0, pre_checkout_query=pre_checkout_query))
|
|
|
|
assert not handler.check_update(Update(0, shipping_query=shipping_query))
|
2018-03-01 10:34:47 +01:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
@pytest.mark.parametrize("jq", [True, False])
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_no_running_job_queue_warning(self, app, bot, user1, recwarn, jq):
|
2021-04-30 10:10:46 +02:00
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=self.entry_points,
|
|
|
|
states=self.states,
|
|
|
|
fallbacks=self.fallbacks,
|
|
|
|
conversation_timeout=0.5,
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
if not jq:
|
2022-12-02 12:08:11 +01:00
|
|
|
app = ApplicationBuilder().token(bot.token).job_queue(None).build()
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2021-04-30 10:10:46 +02:00
|
|
|
|
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2021-04-30 10:10:46 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2021-04-30 10:10:46 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2021-04-30 10:10:46 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
await asyncio.sleep(0.5)
|
2022-12-02 12:08:11 +01:00
|
|
|
if jq:
|
|
|
|
assert len(recwarn) == 1
|
|
|
|
else:
|
|
|
|
assert len(recwarn) == 2
|
|
|
|
assert str(recwarn[0].message if jq else recwarn[1].message).startswith(
|
|
|
|
"Ignoring `conversation_timeout`"
|
|
|
|
)
|
|
|
|
assert ("is not running" if jq else "No `JobQueue` set up.") in str(recwarn[0].message)
|
2022-04-24 12:38:09 +02:00
|
|
|
# now set app.job_queue back to it's original value
|
2021-04-30 10:10:46 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_schedule_job_exception(self, app, bot, user1, monkeypatch, caplog):
|
2021-04-30 10:10:46 +02:00
|
|
|
def mocked_run_once(*a, **kw):
|
|
|
|
raise Exception("job error")
|
|
|
|
|
2021-08-19 22:01:10 +02:00
|
|
|
class DictJB(JobQueue):
|
|
|
|
pass
|
|
|
|
|
2022-12-02 12:08:11 +01:00
|
|
|
app = ApplicationBuilder().token(bot.token).job_queue(DictJB()).build()
|
2022-04-24 12:38:09 +02:00
|
|
|
monkeypatch.setattr(app.job_queue, "run_once", mocked_run_once)
|
2021-04-30 10:10:46 +02:00
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=self.entry_points,
|
|
|
|
states=self.states,
|
|
|
|
fallbacks=self.fallbacks,
|
|
|
|
conversation_timeout=100,
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2021-04-30 10:10:46 +02:00
|
|
|
|
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2021-04-30 10:10:46 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2021-04-30 10:10:46 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2021-04-30 10:10:46 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
await app.start()
|
2021-04-30 10:10:46 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
with caplog.at_level(logging.ERROR):
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
await asyncio.sleep(0.5)
|
|
|
|
|
|
|
|
assert len(caplog.records) == 1
|
|
|
|
assert caplog.records[0].message == "Failed to schedule timeout."
|
|
|
|
assert str(caplog.records[0].exc_info[1]) == "job error"
|
|
|
|
|
|
|
|
await app.stop()
|
|
|
|
|
2022-06-07 17:48:26 +02:00
|
|
|
@pytest.mark.parametrize(argnames="test_type", argvalues=["none", "exception"])
|
|
|
|
async def test_non_blocking_exception_or_none(self, app, bot, user1, caplog, test_type):
|
2022-04-24 12:38:09 +02:00
|
|
|
"""Here we make sure that when a non-blocking handler raises an
|
2022-06-07 17:48:26 +02:00
|
|
|
exception or returns None, the state isn't changed.
|
2021-04-30 10:10:46 +02:00
|
|
|
"""
|
2022-05-05 17:40:22 +02:00
|
|
|
error = Exception("task exception")
|
2021-04-30 10:10:46 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def conv_entry(*a, **kw):
|
2021-04-30 10:10:46 +02:00
|
|
|
return 1
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def raise_error(*a, **kw):
|
2022-06-07 17:48:26 +02:00
|
|
|
if test_type == "none":
|
|
|
|
return None
|
2022-04-24 12:38:09 +02:00
|
|
|
raise error
|
2021-04-30 10:10:46 +02:00
|
|
|
|
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=[CommandHandler("start", conv_entry)],
|
2022-05-05 17:40:22 +02:00
|
|
|
states={1: [MessageHandler(filters.Text(["error"]), raise_error)]},
|
2021-04-30 10:10:46 +02:00
|
|
|
fallbacks=self.fallbacks,
|
2022-04-24 12:38:09 +02:00
|
|
|
block=False,
|
2021-04-30 10:10:46 +02:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2021-04-30 10:10:46 +02:00
|
|
|
|
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2021-04-30 10:10:46 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2021-04-30 10:10:46 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2021-04-30 10:10:46 +02:00
|
|
|
# start the conversation
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
message.text = "error"
|
2022-06-07 17:48:26 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
caplog.clear()
|
|
|
|
with caplog.at_level(logging.ERROR):
|
|
|
|
# This also makes sure that we're still in the same state
|
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
if test_type == "exception":
|
|
|
|
assert len(caplog.records) == 1
|
|
|
|
assert (
|
|
|
|
caplog.records[0].message
|
|
|
|
== "Task function raised exception. Falling back to old state 1"
|
|
|
|
)
|
|
|
|
assert caplog.records[0].exc_info[1] is None
|
|
|
|
else:
|
|
|
|
assert len(caplog.records) == 0
|
|
|
|
|
|
|
|
async def test_non_blocking_entry_point_exception(self, app, bot, user1, caplog):
|
|
|
|
"""Here we make sure that when a non-blocking entry point raises an
|
|
|
|
exception, the state isn't changed.
|
|
|
|
"""
|
|
|
|
error = Exception("task exception")
|
|
|
|
|
|
|
|
async def raise_error(*a, **kw):
|
|
|
|
raise error
|
|
|
|
|
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=[CommandHandler("start", raise_error, block=False)],
|
|
|
|
states={},
|
|
|
|
fallbacks=self.fallbacks,
|
|
|
|
)
|
|
|
|
app.add_handler(handler)
|
|
|
|
|
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
|
|
|
text="/start",
|
|
|
|
entities=[
|
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2022-06-07 17:48:26 +02:00
|
|
|
# start the conversation
|
|
|
|
async with app:
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
caplog.clear()
|
|
|
|
with caplog.at_level(logging.ERROR):
|
|
|
|
# This also makes sure that we're still in the same state
|
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
assert len(caplog.records) == 1
|
|
|
|
assert (
|
|
|
|
caplog.records[0].message
|
2022-06-07 17:48:26 +02:00
|
|
|
== "Task function raised exception. Falling back to old state None"
|
2022-04-24 12:38:09 +02:00
|
|
|
)
|
2022-06-07 17:48:26 +02:00
|
|
|
assert caplog.records[0].exc_info[1] is None
|
2022-04-24 12:38:09 +02:00
|
|
|
|
|
|
|
async def test_conversation_timeout(self, app, bot, user1):
|
2020-10-09 17:22:07 +02:00
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=self.entry_points,
|
|
|
|
states=self.states,
|
|
|
|
fallbacks=self.fallbacks,
|
|
|
|
conversation_timeout=0.5,
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2018-03-01 10:34:47 +01:00
|
|
|
|
|
|
|
# Start state machine, then reach timeout
|
2022-04-24 12:38:09 +02:00
|
|
|
start_message = Message(
|
2020-10-09 17:22:07 +02:00
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2020-10-09 17:22:07 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
start_message.set_bot(bot)
|
2022-04-24 12:38:09 +02:00
|
|
|
brew_message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/brew",
|
2022-04-24 12:38:09 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/brew"))
|
2022-04-24 12:38:09 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
brew_message.set_bot(bot)
|
2022-04-24 12:38:09 +02:00
|
|
|
pour_coffee_message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/pourCoffee",
|
2022-04-24 12:38:09 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/pourCoffee"))
|
2022-04-24 12:38:09 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
pour_coffee_message.set_bot(bot)
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
await app.start()
|
|
|
|
|
|
|
|
await app.process_update(Update(update_id=0, message=start_message))
|
|
|
|
assert handler.check_update(Update(0, message=brew_message))
|
|
|
|
await asyncio.sleep(0.75)
|
|
|
|
assert handler.check_update(Update(0, message=start_message))
|
|
|
|
|
|
|
|
# Start state machine, do something, then reach timeout
|
|
|
|
await app.process_update(Update(update_id=1, message=start_message))
|
|
|
|
assert handler.check_update(Update(0, message=brew_message))
|
|
|
|
# assert handler.conversations.get((self.group.id, user1.id)) == self.THIRSTY
|
|
|
|
# start_message.text = '/brew'
|
|
|
|
# start_message.entities[0].length = len('/brew')
|
|
|
|
await app.process_update(Update(update_id=2, message=brew_message))
|
|
|
|
assert handler.check_update(Update(0, message=pour_coffee_message))
|
|
|
|
# assert handler.conversations.get((self.group.id, user1.id)) == self.BREWING
|
2023-02-11 10:45:17 +01:00
|
|
|
await asyncio.sleep(0.75)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(0, message=start_message))
|
|
|
|
# assert handler.conversations.get((self.group.id, user1.id)) is None
|
|
|
|
|
|
|
|
await app.stop()
|
|
|
|
|
|
|
|
async def test_timeout_not_triggered_on_conv_end_non_blocking(self, bot, app, user1):
|
2021-04-30 10:10:46 +02:00
|
|
|
def timeout(*a, **kw):
|
|
|
|
self.test_flag = True
|
|
|
|
|
|
|
|
self.states.update({ConversationHandler.TIMEOUT: [TypeHandler(Update, timeout)]})
|
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=self.entry_points,
|
|
|
|
states=self.states,
|
|
|
|
fallbacks=self.fallbacks,
|
|
|
|
conversation_timeout=0.5,
|
2022-04-24 12:38:09 +02:00
|
|
|
block=False,
|
2021-04-30 10:10:46 +02:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2021-04-30 10:10:46 +02:00
|
|
|
|
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2021-04-30 10:10:46 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2021-04-30 10:10:46 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
# start the conversation
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
await asyncio.sleep(0.1)
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/brew"
|
|
|
|
message.entities[0].length = len("/brew")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=1, message=message))
|
|
|
|
await asyncio.sleep(0.1)
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/pourCoffee"
|
|
|
|
message.entities[0].length = len("/pourCoffee")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=2, message=message))
|
|
|
|
await asyncio.sleep(0.1)
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/end"
|
|
|
|
message.entities[0].length = len("/end")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=3, message=message))
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
# assert timeout handler didn't get called
|
|
|
|
assert self.test_flag is False
|
|
|
|
|
|
|
|
async def test_conversation_timeout_application_handler_stop(self, app, bot, user1, recwarn):
|
2020-10-09 17:22:07 +02:00
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=self.entry_points,
|
|
|
|
states=self.states,
|
|
|
|
fallbacks=self.fallbacks,
|
|
|
|
conversation_timeout=0.5,
|
|
|
|
)
|
2020-08-21 23:20:28 +02:00
|
|
|
|
|
|
|
def timeout(*args, **kwargs):
|
2022-04-24 12:38:09 +02:00
|
|
|
raise ApplicationHandlerStop()
|
2020-08-21 23:20:28 +02:00
|
|
|
|
|
|
|
self.states.update({ConversationHandler.TIMEOUT: [TypeHandler(Update, timeout)]})
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2020-08-21 23:20:28 +02:00
|
|
|
|
|
|
|
# Start state machine, then reach timeout
|
2020-10-09 17:22:07 +02:00
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2020-10-09 17:22:07 +02:00
|
|
|
from_user=user1,
|
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2022-04-24 12:38:09 +02:00
|
|
|
brew_message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/brew",
|
2022-04-24 12:38:09 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/brew"))
|
2022-04-24 12:38:09 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
brew_message.set_bot(bot)
|
2022-04-24 12:38:09 +02:00
|
|
|
|
|
|
|
async with app:
|
|
|
|
await app.start()
|
2020-08-21 23:20:28 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
# Make sure that we're in the next state
|
|
|
|
assert handler.check_update(Update(0, message=brew_message))
|
|
|
|
await app.process_update(Update(0, message=brew_message))
|
|
|
|
await asyncio.sleep(0.9)
|
|
|
|
# Check that conversation has ended by checking that the start messages is accepted
|
|
|
|
# again
|
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
assert len(recwarn) == 1
|
2022-05-05 17:40:22 +02:00
|
|
|
assert str(recwarn[0].message).startswith("ApplicationHandlerStop in TIMEOUT")
|
2020-08-21 23:20:28 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.stop()
|
|
|
|
|
|
|
|
async def test_conversation_handler_timeout_update_and_context(self, app, bot, user1):
|
2020-04-10 13:18:43 +02:00
|
|
|
context = None
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def start_callback(u, c):
|
2020-04-10 13:18:43 +02:00
|
|
|
nonlocal context, self
|
|
|
|
context = c
|
2022-04-24 12:38:09 +02:00
|
|
|
return await self.start(u, c)
|
2020-04-10 13:18:43 +02:00
|
|
|
|
|
|
|
# Start state machine, then reach timeout
|
2020-10-09 17:22:07 +02:00
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2020-10-09 17:22:07 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2020-04-10 13:18:43 +02:00
|
|
|
update = Update(update_id=0, message=message)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def timeout_callback(u, c):
|
|
|
|
nonlocal update, context
|
2020-04-10 13:18:43 +02:00
|
|
|
assert u is update
|
|
|
|
assert c is context
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
self.is_timeout = (u is update) and (c is context)
|
2020-04-10 13:18:43 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
states = self.states
|
2022-05-05 17:40:22 +02:00
|
|
|
timeout_handler = CommandHandler("start", timeout_callback)
|
2022-04-24 12:38:09 +02:00
|
|
|
states.update({ConversationHandler.TIMEOUT: [timeout_handler]})
|
|
|
|
handler = ConversationHandler(
|
2022-05-05 17:40:22 +02:00
|
|
|
entry_points=[CommandHandler("start", start_callback)],
|
2022-04-24 12:38:09 +02:00
|
|
|
states=states,
|
|
|
|
fallbacks=self.fallbacks,
|
|
|
|
conversation_timeout=0.5,
|
|
|
|
)
|
|
|
|
app.add_handler(handler)
|
|
|
|
|
|
|
|
async with app:
|
|
|
|
await app.start()
|
|
|
|
|
|
|
|
await app.process_update(update)
|
|
|
|
await asyncio.sleep(0.9)
|
|
|
|
# check that the conversation has ended by checking that the start message is accepted
|
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
assert self.is_timeout
|
|
|
|
|
|
|
|
await app.stop()
|
2020-04-10 13:18:43 +02:00
|
|
|
|
2022-11-09 20:43:27 +01:00
|
|
|
@pytest.mark.flaky(3, 1)
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_conversation_timeout_keeps_extending(self, app, bot, user1):
|
2020-10-09 17:22:07 +02:00
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=self.entry_points,
|
|
|
|
states=self.states,
|
|
|
|
fallbacks=self.fallbacks,
|
|
|
|
conversation_timeout=0.5,
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2018-03-05 12:18:40 +01:00
|
|
|
|
|
|
|
# Start state machine, wait, do something, verify the timeout is extended.
|
|
|
|
# t=0 /start (timeout=.5)
|
2020-12-30 15:39:38 +01:00
|
|
|
# t=.35 /brew (timeout=.85)
|
2018-03-05 12:18:40 +01:00
|
|
|
# t=.5 original timeout
|
|
|
|
# t=.6 /pourCoffee (timeout=1.1)
|
2020-12-30 15:39:38 +01:00
|
|
|
# t=.85 second timeout
|
2018-03-05 12:18:40 +01:00
|
|
|
# t=1.1 actual timeout
|
2020-10-09 17:22:07 +02:00
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2020-10-09 17:22:07 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2022-04-24 12:38:09 +02:00
|
|
|
|
|
|
|
async with app:
|
|
|
|
await app.start()
|
|
|
|
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/brew"
|
|
|
|
message.entities[0].length = len("/brew")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
await asyncio.sleep(0.35) # t=.35
|
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/pourCoffee"
|
|
|
|
message.entities[0].length = len("/pourCoffee")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
await asyncio.sleep(0.25) # t=.6
|
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/startCoding"
|
|
|
|
message.entities[0].length = len("/startCoding")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
await asyncio.sleep(0.4) # t=1.0
|
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
await asyncio.sleep(0.3) # t=1.3
|
|
|
|
assert not handler.check_update(Update(0, message=message))
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/start"
|
|
|
|
message.entities[0].length = len("/start")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
|
|
|
|
await app.stop()
|
|
|
|
|
|
|
|
async def test_conversation_timeout_two_users(self, app, bot, user1, user2):
|
2020-10-09 17:22:07 +02:00
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=self.entry_points,
|
|
|
|
states=self.states,
|
|
|
|
fallbacks=self.fallbacks,
|
|
|
|
conversation_timeout=0.5,
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2018-03-01 10:34:47 +01:00
|
|
|
|
|
|
|
# Start state machine, do something as second user, then reach timeout
|
2020-10-09 17:22:07 +02:00
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2020-10-09 17:22:07 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2022-04-24 12:38:09 +02:00
|
|
|
|
|
|
|
async with app:
|
|
|
|
await app.start()
|
|
|
|
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/brew"
|
|
|
|
message.entities[0].length = len("/brew")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
message.from_user = user2
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/start"
|
|
|
|
message.entities[0].length = len("/start")
|
2022-04-24 12:38:09 +02:00
|
|
|
# Make sure that user2s conversation has not yet started
|
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/brew"
|
|
|
|
message.entities[0].length = len("/brew")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
await asyncio.sleep(0.7)
|
|
|
|
# check that both conversations have ended by checking that the start message is
|
|
|
|
# accepted again
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/start"
|
|
|
|
message.entities[0].length = len("/start")
|
2022-04-24 12:38:09 +02:00
|
|
|
message.from_user = user1
|
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
message.from_user = user2
|
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
|
|
|
|
await app.stop()
|
|
|
|
|
|
|
|
async def test_conversation_handler_timeout_state(self, app, bot, user1):
|
2018-10-04 08:58:40 +02:00
|
|
|
states = self.states
|
2020-10-09 17:22:07 +02:00
|
|
|
states.update(
|
|
|
|
{
|
|
|
|
ConversationHandler.TIMEOUT: [
|
2022-05-05 17:40:22 +02:00
|
|
|
CommandHandler("brew", self.passout),
|
|
|
|
MessageHandler(~filters.Regex("oding"), self.passout2),
|
2020-10-09 17:22:07 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=self.entry_points,
|
|
|
|
states=states,
|
|
|
|
fallbacks=self.fallbacks,
|
|
|
|
conversation_timeout=0.5,
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2019-02-14 11:52:31 +01:00
|
|
|
|
2018-10-04 08:58:40 +02:00
|
|
|
# CommandHandler timeout
|
2020-10-09 17:22:07 +02:00
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2020-10-09 17:22:07 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2018-10-04 08:58:40 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
await app.start()
|
|
|
|
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/brew"
|
|
|
|
message.entities[0].length = len("/brew")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
await asyncio.sleep(0.7)
|
|
|
|
# check that conversation has ended by checking that start cmd is accepted again
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/start"
|
|
|
|
message.entities[0].length = len("/start")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
assert self.is_timeout
|
|
|
|
|
|
|
|
# MessageHandler timeout
|
|
|
|
self.is_timeout = False
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/start"
|
|
|
|
message.entities[0].length = len("/start")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=1, message=message))
|
|
|
|
await asyncio.sleep(0.7)
|
|
|
|
# check that conversation has ended by checking that start cmd is accepted again
|
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
assert self.is_timeout
|
|
|
|
|
|
|
|
# Timeout but no valid handler
|
|
|
|
self.is_timeout = False
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/brew"
|
|
|
|
message.entities[0].length = len("/brew")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/startCoding"
|
|
|
|
message.entities[0].length = len("/startCoding")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
await asyncio.sleep(0.7)
|
|
|
|
# check that conversation has ended by checking that start cmd is accepted again
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/start"
|
|
|
|
message.entities[0].length = len("/start")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
assert not self.is_timeout
|
|
|
|
|
|
|
|
await app.stop()
|
|
|
|
|
|
|
|
async def test_conversation_handler_timeout_state_context(self, app, bot, user1):
|
2020-01-11 19:51:31 +01:00
|
|
|
states = self.states
|
2020-10-09 17:22:07 +02:00
|
|
|
states.update(
|
|
|
|
{
|
|
|
|
ConversationHandler.TIMEOUT: [
|
2022-05-05 17:40:22 +02:00
|
|
|
CommandHandler("brew", self.passout_context),
|
|
|
|
MessageHandler(~filters.Regex("oding"), self.passout2_context),
|
2020-10-09 17:22:07 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=self.entry_points,
|
|
|
|
states=states,
|
|
|
|
fallbacks=self.fallbacks,
|
|
|
|
conversation_timeout=0.5,
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2020-01-11 19:51:31 +01:00
|
|
|
|
|
|
|
# CommandHandler timeout
|
2020-10-09 17:22:07 +02:00
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2020-10-09 17:22:07 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
await app.start()
|
|
|
|
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/brew"
|
|
|
|
message.entities[0].length = len("/brew")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
await asyncio.sleep(0.7)
|
|
|
|
# check that conversation has ended by checking that start cmd is accepted again
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/start"
|
|
|
|
message.entities[0].length = len("/start")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
assert self.is_timeout
|
|
|
|
|
|
|
|
# MessageHandler timeout
|
|
|
|
self.is_timeout = False
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/start"
|
|
|
|
message.entities[0].length = len("/start")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=1, message=message))
|
|
|
|
await asyncio.sleep(0.7)
|
|
|
|
# check that conversation has ended by checking that start cmd is accepted again
|
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
assert self.is_timeout
|
|
|
|
|
|
|
|
# Timeout but no valid handler
|
|
|
|
self.is_timeout = False
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/brew"
|
|
|
|
message.entities[0].length = len("/brew")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/startCoding"
|
|
|
|
message.entities[0].length = len("/startCoding")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
await asyncio.sleep(0.7)
|
|
|
|
# check that conversation has ended by checking that start cmd is accepted again
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/start"
|
|
|
|
message.entities[0].length = len("/start")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
assert not self.is_timeout
|
|
|
|
|
|
|
|
await app.stop()
|
|
|
|
|
|
|
|
async def test_conversation_timeout_cancel_conflict(self, app, bot, user1):
|
2019-10-17 00:03:53 +02:00
|
|
|
# Start state machine, wait half the timeout,
|
|
|
|
# then call a callback that takes more than the timeout
|
|
|
|
# t=0 /start (timeout=.5)
|
|
|
|
# t=.25 /slowbrew (sleep .5)
|
|
|
|
# | t=.5 original timeout (should not execute)
|
|
|
|
# | t=.75 /slowbrew returns (timeout=1.25)
|
|
|
|
# t=1.25 timeout
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def slowbrew(_update, context):
|
|
|
|
await asyncio.sleep(0.25)
|
2019-10-17 00:03:53 +02:00
|
|
|
# Let's give to the original timeout a chance to execute
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.25)
|
2019-10-17 00:03:53 +02:00
|
|
|
# By returning None we do not override the conversation state so
|
|
|
|
# we can see if the timeout has been executed
|
|
|
|
|
|
|
|
states = self.states
|
2022-05-05 17:40:22 +02:00
|
|
|
states[self.THIRSTY].append(CommandHandler("slowbrew", slowbrew))
|
2020-10-09 17:22:07 +02:00
|
|
|
states.update({ConversationHandler.TIMEOUT: [MessageHandler(None, self.passout2)]})
|
2019-10-17 00:03:53 +02:00
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=self.entry_points,
|
|
|
|
states=states,
|
|
|
|
fallbacks=self.fallbacks,
|
|
|
|
conversation_timeout=0.5,
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2019-10-17 00:03:53 +02:00
|
|
|
|
|
|
|
# CommandHandler timeout
|
2020-10-09 17:22:07 +02:00
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2020-10-09 17:22:07 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2021-11-06 15:19:21 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
await app.start()
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
await asyncio.sleep(0.25)
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/slowbrew"
|
|
|
|
message.entities[0].length = len("/slowbrew")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
# Check that conversation has not ended by checking that start cmd is not accepted
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/start"
|
|
|
|
message.entities[0].length = len("/start")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert not handler.check_update(Update(0, message=message))
|
|
|
|
assert not self.is_timeout
|
|
|
|
|
|
|
|
await asyncio.sleep(0.7)
|
|
|
|
# Check that conversation has ended by checking that start cmd is accepted again
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/start"
|
|
|
|
message.entities[0].length = len("/start")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
assert self.is_timeout
|
|
|
|
|
|
|
|
await app.stop()
|
|
|
|
|
|
|
|
async def test_nested_conversation_handler(self, app, bot, user1, user2):
|
2020-10-09 17:22:07 +02:00
|
|
|
self.nested_states[self.DRINKING] = [
|
|
|
|
ConversationHandler(
|
|
|
|
entry_points=self.drinking_entry_points,
|
|
|
|
states=self.drinking_states,
|
|
|
|
fallbacks=self.drinking_fallbacks,
|
|
|
|
map_to_parent=self.drinking_map_to_parent,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=self.entry_points, states=self.nested_states, fallbacks=self.fallbacks
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2019-10-11 21:59:36 +02:00
|
|
|
|
|
|
|
# User one, starts the state machine.
|
2020-10-09 17:22:07 +02:00
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
|
|
|
from_user=user1,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2020-10-09 17:22:07 +02:00
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.THIRSTY
|
|
|
|
|
|
|
|
# The user is thirsty and wants to brew coffee.
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/brew"
|
|
|
|
message.entities[0].length = len("/brew")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.BREWING
|
|
|
|
|
|
|
|
# Lets pour some coffee.
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/pourCoffee"
|
|
|
|
message.entities[0].length = len("/pourCoffee")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.DRINKING
|
|
|
|
|
|
|
|
# The user is holding the cup
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/hold"
|
|
|
|
message.entities[0].length = len("/hold")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.HOLDING
|
|
|
|
|
|
|
|
# The user is sipping coffee
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/sip"
|
|
|
|
message.entities[0].length = len("/sip")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.SIPPING
|
|
|
|
|
|
|
|
# The user is swallowing
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/swallow"
|
|
|
|
message.entities[0].length = len("/swallow")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.SWALLOWING
|
|
|
|
|
|
|
|
# The user is holding the cup again
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/hold"
|
|
|
|
message.entities[0].length = len("/hold")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.HOLDING
|
|
|
|
|
|
|
|
# The user wants to replenish the coffee supply
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/replenish"
|
|
|
|
message.entities[0].length = len("/replenish")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.REPLENISHING
|
|
|
|
# check that we're in the right state now by checking that the update is accepted
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/pourCoffee"
|
|
|
|
message.entities[0].length = len("/pourCoffee")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
|
|
|
|
# The user wants to drink their coffee again)
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.DRINKING
|
|
|
|
|
|
|
|
# The user is now ready to start coding
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/startCoding"
|
|
|
|
message.entities[0].length = len("/startCoding")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.CODING
|
|
|
|
|
|
|
|
# The user decides it's time to drink again
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/drinkMore"
|
|
|
|
message.entities[0].length = len("/drinkMore")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.DRINKING
|
|
|
|
|
|
|
|
# The user is holding their cup
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/hold"
|
|
|
|
message.entities[0].length = len("/hold")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.HOLDING
|
|
|
|
|
|
|
|
# The user wants to end with the drinking and go back to coding
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/end"
|
|
|
|
message.entities[0].length = len("/end")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.END
|
|
|
|
# check that we're in the right state now by checking that the update is accepted
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/drinkMore"
|
|
|
|
message.entities[0].length = len("/drinkMore")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
|
|
|
|
# The user wants to drink once more
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.DRINKING
|
|
|
|
|
|
|
|
# The user wants to stop altogether
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/stop"
|
|
|
|
message.entities[0].length = len("/stop")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.STOPPING
|
|
|
|
# check that the conversation has ended by checking that the start cmd is accepted
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/start"
|
|
|
|
message.entities[0].length = len("/start")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
|
|
|
|
async def test_nested_conversation_application_handler_stop(self, app, bot, user1, user2):
|
2020-10-09 17:22:07 +02:00
|
|
|
self.nested_states[self.DRINKING] = [
|
|
|
|
ConversationHandler(
|
|
|
|
entry_points=self.drinking_entry_points,
|
|
|
|
states=self.drinking_states,
|
|
|
|
fallbacks=self.drinking_fallbacks,
|
|
|
|
map_to_parent=self.drinking_map_to_parent,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
handler = ConversationHandler(
|
|
|
|
entry_points=self.entry_points, states=self.nested_states, fallbacks=self.fallbacks
|
|
|
|
)
|
2020-08-21 23:20:28 +02:00
|
|
|
|
|
|
|
def test_callback(u, c):
|
|
|
|
self.test_flag = True
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
|
|
|
app.add_handler(TypeHandler(Update, test_callback), group=1)
|
|
|
|
self.raise_app_handler_stop = True
|
2020-08-21 23:20:28 +02:00
|
|
|
|
|
|
|
# User one, starts the state machine.
|
2020-10-09 17:22:07 +02:00
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2020-10-09 17:22:07 +02:00
|
|
|
from_user=user1,
|
|
|
|
entities=[
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len("/start"))
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
|
|
|
message.entities[0]._unfreeze()
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.THIRSTY
|
|
|
|
assert not self.test_flag
|
|
|
|
|
|
|
|
# The user is thirsty and wants to brew coffee.
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/brew"
|
|
|
|
message.entities[0].length = len("/brew")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.BREWING
|
|
|
|
assert not self.test_flag
|
|
|
|
|
|
|
|
# Lets pour some coffee.
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/pourCoffee"
|
|
|
|
message.entities[0].length = len("/pourCoffee")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.DRINKING
|
|
|
|
assert not self.test_flag
|
|
|
|
|
|
|
|
# The user is holding the cup
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/hold"
|
|
|
|
message.entities[0].length = len("/hold")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.HOLDING
|
|
|
|
assert not self.test_flag
|
|
|
|
|
|
|
|
# The user is sipping coffee
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/sip"
|
|
|
|
message.entities[0].length = len("/sip")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.SIPPING
|
|
|
|
assert not self.test_flag
|
|
|
|
|
|
|
|
# The user is swallowing
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/swallow"
|
|
|
|
message.entities[0].length = len("/swallow")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.SWALLOWING
|
|
|
|
assert not self.test_flag
|
|
|
|
|
|
|
|
# The user is holding the cup again
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/hold"
|
|
|
|
message.entities[0].length = len("/hold")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.HOLDING
|
|
|
|
assert not self.test_flag
|
|
|
|
|
|
|
|
# The user wants to replenish the coffee supply
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/replenish"
|
|
|
|
message.entities[0].length = len("/replenish")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.REPLENISHING
|
|
|
|
# check that we're in the right state now by checking that the update is accepted
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/pourCoffee"
|
|
|
|
message.entities[0].length = len("/pourCoffee")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
assert not self.test_flag
|
|
|
|
|
|
|
|
# The user wants to drink their coffee again
|
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.DRINKING
|
|
|
|
assert not self.test_flag
|
|
|
|
|
|
|
|
# The user is now ready to start coding
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/startCoding"
|
|
|
|
message.entities[0].length = len("/startCoding")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.CODING
|
|
|
|
assert not self.test_flag
|
|
|
|
|
|
|
|
# The user decides it's time to drink again
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/drinkMore"
|
|
|
|
message.entities[0].length = len("/drinkMore")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.DRINKING
|
|
|
|
assert not self.test_flag
|
|
|
|
|
|
|
|
# The user is holding their cup
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/hold"
|
|
|
|
message.entities[0].length = len("/hold")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.HOLDING
|
|
|
|
assert not self.test_flag
|
|
|
|
|
|
|
|
# The user wants to end with the drinking and go back to coding
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/end"
|
|
|
|
message.entities[0].length = len("/end")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.END
|
|
|
|
# check that we're in the right state now by checking that the update is accepted
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/drinkMore"
|
|
|
|
message.entities[0].length = len("/drinkMore")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
assert not self.test_flag
|
|
|
|
|
|
|
|
# The user wants to drink once more
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/drinkMore"
|
|
|
|
message.entities[0].length = len("/drinkMore")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.DRINKING
|
|
|
|
assert not self.test_flag
|
|
|
|
|
|
|
|
# The user wants to stop altogether
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/stop"
|
|
|
|
message.entities[0].length = len("/stop")
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(update_id=0, message=message))
|
|
|
|
assert self.current_state[user1.id] == self.STOPPING
|
|
|
|
# check that the conv has ended by checking that the start cmd is accepted
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "/start"
|
|
|
|
message.entities[0].length = len("/start")
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(Update(0, message=message))
|
|
|
|
assert not self.test_flag
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
@pytest.mark.parametrize("callback_raises", [True, False])
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_timeout_non_block(self, app, user1, callback_raises):
|
|
|
|
event = asyncio.Event()
|
|
|
|
|
|
|
|
async def callback(_, __):
|
|
|
|
await event.wait()
|
|
|
|
if callback_raises:
|
|
|
|
raise RuntimeError
|
|
|
|
return 1
|
|
|
|
|
|
|
|
conv_handler = ConversationHandler(
|
|
|
|
entry_points=[MessageHandler(filters.ALL, callback=callback, block=False)],
|
|
|
|
states={ConversationHandler.TIMEOUT: [TypeHandler(Update, self.passout2)]},
|
|
|
|
fallbacks=[],
|
|
|
|
conversation_timeout=0.5,
|
|
|
|
)
|
|
|
|
app.add_handler(conv_handler)
|
|
|
|
|
|
|
|
async with app:
|
|
|
|
await app.start()
|
|
|
|
|
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2022-04-24 12:38:09 +02:00
|
|
|
from_user=user1,
|
|
|
|
)
|
|
|
|
assert conv_handler.check_update(Update(0, message=message))
|
|
|
|
await app.process_update(Update(0, message=message))
|
|
|
|
await asyncio.sleep(0.7)
|
|
|
|
assert not self.is_timeout
|
|
|
|
event.set()
|
|
|
|
await asyncio.sleep(0.7)
|
|
|
|
assert self.is_timeout == (not callback_raises)
|
|
|
|
|
|
|
|
await app.stop()
|
|
|
|
|
|
|
|
async def test_no_timeout_on_end(self, app, user1):
|
|
|
|
conv_handler = ConversationHandler(
|
|
|
|
entry_points=[MessageHandler(filters.ALL, callback=self.start_end)],
|
|
|
|
states={ConversationHandler.TIMEOUT: [TypeHandler(Update, self.passout2)]},
|
|
|
|
fallbacks=[],
|
|
|
|
conversation_timeout=0.5,
|
|
|
|
)
|
|
|
|
app.add_handler(conv_handler)
|
|
|
|
|
|
|
|
async with app:
|
|
|
|
await app.start()
|
|
|
|
|
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2022-04-24 12:38:09 +02:00
|
|
|
from_user=user1,
|
|
|
|
)
|
|
|
|
assert conv_handler.check_update(Update(0, message=message))
|
|
|
|
await app.process_update(Update(0, message=message))
|
|
|
|
await asyncio.sleep(0.7)
|
|
|
|
assert not self.is_timeout
|
|
|
|
|
|
|
|
await app.stop()
|
|
|
|
|
|
|
|
async def test_conversation_handler_block_dont_override(self, app):
|
|
|
|
"""This just makes sure that we don't change any attributes of the handlers of the conv"""
|
2021-01-09 20:05:58 +01:00
|
|
|
conv_handler = ConversationHandler(
|
|
|
|
entry_points=self.entry_points,
|
|
|
|
states=self.states,
|
|
|
|
fallbacks=self.fallbacks,
|
2022-04-24 12:38:09 +02:00
|
|
|
block=False,
|
2021-01-09 20:05:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
all_handlers = conv_handler.entry_points + conv_handler.fallbacks
|
|
|
|
for state_handlers in conv_handler.states.values():
|
|
|
|
all_handlers += state_handlers
|
|
|
|
|
|
|
|
for handler in all_handlers:
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.block
|
2021-01-09 20:05:58 +01:00
|
|
|
|
|
|
|
conv_handler = ConversationHandler(
|
2022-05-05 17:40:22 +02:00
|
|
|
entry_points=[CommandHandler("start", self.start_end, block=False)],
|
|
|
|
states={1: [CommandHandler("start", self.start_end, block=False)]},
|
|
|
|
fallbacks=[CommandHandler("start", self.start_end, block=False)],
|
2022-04-24 12:38:09 +02:00
|
|
|
block=True,
|
2021-01-09 20:05:58 +01:00
|
|
|
)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
all_handlers = conv_handler.entry_points + conv_handler.fallbacks
|
2021-01-09 20:05:58 +01:00
|
|
|
for state_handlers in conv_handler.states.values():
|
|
|
|
all_handlers += state_handlers
|
|
|
|
|
|
|
|
for handler in all_handlers:
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.block is False
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
@pytest.mark.parametrize("default_block", [True, False, None])
|
|
|
|
@pytest.mark.parametrize("ch_block", [True, False, None])
|
|
|
|
@pytest.mark.parametrize("handler_block", [True, False, None])
|
|
|
|
@pytest.mark.parametrize("ext_bot", [True, False], ids=["ExtBot", "Bot"])
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_blocking_resolution_order(
|
2023-02-11 10:45:17 +01:00
|
|
|
self, bot_info, default_block, ch_block, handler_block, ext_bot
|
2022-04-24 12:38:09 +02:00
|
|
|
):
|
|
|
|
event = asyncio.Event()
|
|
|
|
|
|
|
|
async def callback(_, __):
|
|
|
|
await event.wait()
|
|
|
|
event.clear()
|
|
|
|
self.test_flag = True
|
|
|
|
return 1
|
|
|
|
|
|
|
|
if handler_block is not None:
|
2022-05-05 17:40:22 +02:00
|
|
|
handler = CommandHandler("start", callback=callback, block=handler_block)
|
2022-04-24 12:38:09 +02:00
|
|
|
fallback = MessageHandler(filters.ALL, callback, block=handler_block)
|
|
|
|
else:
|
2022-05-05 17:40:22 +02:00
|
|
|
handler = CommandHandler("start", callback=callback)
|
2022-04-24 12:38:09 +02:00
|
|
|
fallback = MessageHandler(filters.ALL, callback, block=handler_block)
|
|
|
|
|
|
|
|
if default_block is not None:
|
|
|
|
defaults = Defaults(block=default_block)
|
|
|
|
else:
|
|
|
|
defaults = None
|
|
|
|
|
|
|
|
if ch_block is not None:
|
|
|
|
conv_handler = ConversationHandler(
|
|
|
|
entry_points=[handler],
|
|
|
|
states={1: [handler]},
|
|
|
|
fallbacks=[fallback],
|
|
|
|
block=ch_block,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
conv_handler = ConversationHandler(
|
|
|
|
entry_points=[handler],
|
|
|
|
states={1: [handler]},
|
|
|
|
fallbacks=[fallback],
|
|
|
|
)
|
|
|
|
|
2023-02-22 20:19:46 +01:00
|
|
|
bot = make_bot(bot_info, defaults=defaults) if ext_bot else PytestBot(bot_info["token"])
|
2022-04-24 12:38:09 +02:00
|
|
|
app = ApplicationBuilder().bot(bot).build()
|
|
|
|
app.add_handler(conv_handler)
|
|
|
|
|
|
|
|
async with app:
|
2022-10-07 11:51:53 +02:00
|
|
|
start_message = make_command_message("/start")
|
|
|
|
start_message.set_bot(bot)
|
|
|
|
fallback_message = make_command_message("/fallback")
|
|
|
|
fallback_message.set_bot(bot)
|
2022-04-24 12:38:09 +02:00
|
|
|
|
|
|
|
# This loop makes sure that we test all of entry points, states handler & fallbacks
|
2023-02-11 10:45:17 +01:00
|
|
|
for message in [start_message, fallback_message]:
|
2022-04-24 12:38:09 +02:00
|
|
|
process_update_task = asyncio.create_task(
|
|
|
|
app.process_update(Update(0, message=message))
|
|
|
|
)
|
|
|
|
if (
|
|
|
|
# resolution order is handler_block -> ch_block -> default_block
|
|
|
|
# setting block=True/False on a lower priority setting may only have an effect
|
|
|
|
# if it wasn't set for the higher priority settings
|
|
|
|
(handler_block is False)
|
|
|
|
or ((handler_block is None) and (ch_block is False))
|
|
|
|
or (
|
|
|
|
(handler_block is None)
|
|
|
|
and (ch_block is None)
|
|
|
|
and ext_bot
|
|
|
|
and (default_block is False)
|
|
|
|
)
|
|
|
|
):
|
|
|
|
# check that the handler was called non-blocking by checking that
|
|
|
|
# `process_update` has finished
|
|
|
|
await asyncio.sleep(0.01)
|
|
|
|
assert process_update_task.done()
|
|
|
|
else:
|
|
|
|
# the opposite
|
|
|
|
assert not process_update_task.done()
|
|
|
|
|
|
|
|
# In any case, the callback must not have finished
|
|
|
|
assert not self.test_flag
|
|
|
|
|
|
|
|
# After setting the event, the callback must have finished and in the blocking
|
|
|
|
# case this leads to `process_update` finishing.
|
|
|
|
event.set()
|
|
|
|
await asyncio.sleep(0.01)
|
|
|
|
assert process_update_task.done()
|
|
|
|
assert self.test_flag
|
|
|
|
self.test_flag = False
|
|
|
|
|
|
|
|
async def test_waiting_state(self, app, user1):
|
|
|
|
event = asyncio.Event()
|
|
|
|
|
|
|
|
async def callback_1(_, __):
|
|
|
|
self.test_flag = 1
|
|
|
|
|
|
|
|
async def callback_2(_, __):
|
|
|
|
self.test_flag = 2
|
|
|
|
|
|
|
|
async def callback_3(_, __):
|
|
|
|
self.test_flag = 3
|
|
|
|
|
|
|
|
async def blocking(_, __):
|
|
|
|
await event.wait()
|
|
|
|
return 1
|
|
|
|
|
|
|
|
conv_handler = ConversationHandler(
|
|
|
|
entry_points=[MessageHandler(filters.ALL, callback=blocking, block=False)],
|
|
|
|
states={
|
|
|
|
ConversationHandler.WAITING: [
|
2022-05-05 17:40:22 +02:00
|
|
|
MessageHandler(filters.Regex("1"), callback_1),
|
|
|
|
MessageHandler(filters.Regex("2"), callback_2),
|
2022-04-24 12:38:09 +02:00
|
|
|
],
|
2022-05-05 17:40:22 +02:00
|
|
|
1: [MessageHandler(filters.Regex("2"), callback_3)],
|
2022-04-24 12:38:09 +02:00
|
|
|
},
|
|
|
|
fallbacks=[],
|
|
|
|
)
|
|
|
|
app.add_handler(conv_handler)
|
|
|
|
|
|
|
|
message = Message(
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
self.group,
|
2022-05-05 17:40:22 +02:00
|
|
|
text="/start",
|
2022-04-24 12:38:09 +02:00
|
|
|
from_user=user1,
|
|
|
|
)
|
2022-12-15 15:00:36 +01:00
|
|
|
message._unfreeze()
|
2022-04-24 12:38:09 +02:00
|
|
|
|
|
|
|
async with app:
|
|
|
|
await app.process_update(Update(0, message=message))
|
|
|
|
assert not self.test_flag
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "1"
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(0, message=message))
|
|
|
|
assert self.test_flag == 1
|
2022-05-05 17:40:22 +02:00
|
|
|
message.text = "2"
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.process_update(Update(0, message=message))
|
|
|
|
assert self.test_flag == 2
|
|
|
|
event.set()
|
|
|
|
await asyncio.sleep(0.05)
|
|
|
|
self.test_flag = None
|
|
|
|
await app.process_update(Update(0, message=message))
|
|
|
|
assert self.test_flag == 3
|