Skip to content

Commit

Permalink
Add condition asserting str is in Message.text (#335)
Browse files Browse the repository at this point in the history
# 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 <[email protected]>
  • Loading branch information
ZergLev and RLKRo authored Mar 15, 2024
1 parent a76884d commit 9e62a84
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions dff/script/conditions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .std_conditions import (
exact_match,
has_text,
regexp,
check_cond_seq,
aggregate,
Expand Down
16 changes: 16 additions & 0 deletions dff/script/conditions/std_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
"""
Expand Down
5 changes: 5 additions & 0 deletions tests/script/conditions/test_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9e62a84

Please sign in to comment.