AgentsDataMap

expression

View as MarkdownOpen in Claude

Add a pattern-based response that does not require an API call. Expressions are evaluated in order; the first matching pattern wins.

Parameters

test_value
strRequired

Template string to test (e.g., "${args.command}").

pattern
str | re.PatternRequired

Regex pattern to match against the test value.

output
FunctionResultRequired

FunctionResult to return when the pattern matches.

nomatch_output
FunctionResult

Optional FunctionResult to return when the pattern does not match.

Returns

DataMap — Self for method chaining.

Example

1from signalwire import AgentBase, DataMap
2from signalwire import FunctionResult
3
4volume_control = (
5 DataMap("set_volume")
6 .purpose("Control audio volume")
7 .parameter("level", "string", "Volume level", required=True)
8 .expression(
9 "${args.level}", r"high|loud|up",
10 FunctionResult("Volume increased")
11 )
12 .expression(
13 "${args.level}", r"low|quiet|down",
14 FunctionResult("Volume decreased")
15 )
16 .expression(
17 "${args.level}", r"mute|off",
18 FunctionResult("Audio muted")
19 )
20)
21
22agent = AgentBase(name="media-agent")
23agent.set_prompt_text("You are a helpful assistant.")
24agent.register_swaig_function(volume_control.to_swaig_function())
25
26if __name__ == "__main__":
27 agent.run()