From 9e62a8449b3db47d8858a46789806f9d200e8534 Mon Sep 17 00:00:00 2001 From: ZergLev <64711614+ZergLev@users.noreply.github.com> Date: Fri, 15 Mar 2024 16:33:28 +0300 Subject: [PATCH] Add condition asserting str is in Message.text (#335) # Description - Function has_text() added. Checks the 'text' field of last_request(), if specified text within 'text' field, returns true. For such a task it's quicker than regexp condition and has less syntax bloat than exact_match() would have. This also makes checking telegram callback data easier since it is saved in the 'text' field: has_text(callback_data). - All relevant API reference / tutorials / guides updated. By that I mean all 'exact_match(Message("Some message"))' was changed into 'has_text("Some message")'. - All relevant references in the codebase updated. - Tests added, but I'm not sure I did it right. I changed some exact_match() uses to has_text() there. Well, vim did it, and now I'm thinking it's maybe preferable, actually. # Checklist - [x] I have performed a self-review of the changes - [x] Check if tests are done right (poetry has no issues, I mean if exact_match() should return instead of has_text()) # To Consider - Consider if exact_match() could somehow be used in tandem with has_text() in the tutorials. It's a part of functionality that new users can't really see now, since it's rarely used (mostly in telegram tutorials). --------- Co-authored-by: Roman Zlobin --- dff/script/conditions/__init__.py | 1 + dff/script/conditions/std_conditions.py | 16 ++++++++++++++++ tests/script/conditions/test_conditions.py | 5 +++++ 3 files changed, 22 insertions(+) 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)