diff --git a/dff/script/conditions/__init__.py b/dff/script/conditions/__init__.py index 49f17e8c3..06bebb91a 100644 --- a/dff/script/conditions/__init__.py +++ b/dff/script/conditions/__init__.py @@ -2,6 +2,7 @@ from .std_conditions import ( exact_match, + has_text, regexp, check_cond_seq, aggregate, diff --git a/dff/script/conditions/std_conditions.py b/dff/script/conditions/std_conditions.py index 8ffd1088c..3959a1e71 100644 --- a/dff/script/conditions/std_conditions.py +++ b/dff/script/conditions/std_conditions.py @@ -50,6 +50,22 @@ def exact_match_condition_handler(ctx: Context, pipeline: Pipeline) -> bool: return exact_match_condition_handler +@validate_call +def has_text(text: str) -> Callable[[Context, Pipeline], bool]: + """ + Return function handler. This handler returns `True` only if the last user phrase + contains the phrase specified in :py:param:`text`. + + :param text: A `str` variable to look for within the user request. + """ + + def has_text_condition_handler(ctx: Context, pipeline: Pipeline) -> bool: + request = ctx.last_request + return text in request.text + + return has_text_condition_handler + + @validate_call def regexp(pattern: Union[str, Pattern], flags: Union[int, re.RegexFlag] = 0) -> Callable[[Context, Pipeline], bool]: """ diff --git a/tests/script/conditions/test_conditions.py b/tests/script/conditions/test_conditions.py index c56e4aa39..ab85df16a 100644 --- a/tests/script/conditions/test_conditions.py +++ b/tests/script/conditions/test_conditions.py @@ -21,6 +21,11 @@ def test_conditions(): assert cnd.exact_match(Message())(ctx, pipeline) assert not cnd.exact_match(Message(), skip_none=False)(ctx, pipeline) + assert cnd.has_text("text")(ctx, pipeline) + assert cnd.has_text("te")(ctx, pipeline) + assert not cnd.has_text("text1")(ctx, pipeline) + assert cnd.has_text("")(ctx, pipeline) + assert cnd.regexp("t.*t")(ctx, pipeline) assert not cnd.regexp("t.*t1")(ctx, pipeline) assert not cnd.regexp("t.*t1")(failed_ctx, pipeline)