The data_map object is a crucial component of
SWAIG Functions in the
ai SWML method.
Data Maps are used to request, process, and transform incoming data, and trigger specific actions or responses, directly within the serverless context of your SWML Script.
Data Maps enable some of the most powerful serverless capabilities of SWML Scripts.
A data_map transforms the static JSON document in your SWML Script into a highly capable, dynamic application,
removing the need to host a separate script to integrate with REST APIs to query, transform, and post data.
In particular, the data_map object facilitates a full range of input processing, interfacing with external APIs, and state management.
The capabilities and use cases described in this guide include:
This guide also explains how to utilize stored variables in the context of Data Maps.
Expressions
in data_map are used to match patterns in incoming data. They help identify specific
pieces of data passed from the user while interacting with the AI agent and trigger actions or
responses based on the expression match.
Expressions are particularly useful in scenarios where user inputs need to be dynamically evaluated and processed locally.
By utilizing Perl Compatible Regular Expressions (PCRE), data_map can effectively parse and handle a wide
variety of inputs, enabling flexible and powerful data processing within your SWML scripts.
Multiple expressions can be stated inside the data_map.expressions field.
These expressions are evaluated in the order they are stated until one of the expressions has a matching pattern.
Key points to note about expressions:
Pattern Matching: The pattern field contains a Perl Compatible Regular Expressions (PCRE) to match specific data in the input.
\w+ pattern matches any word characters, and the
.* pattern matches any character.
\w+ pattern is used to match a specific target value, while the .* pattern is a catch-all for any input.String Field: The string field allows you to dynamically or statically set values based on the matched pattern.
In the example, ${meta_data.table.${lc:args.target}} fetches the target from the SWAIG.functions.<function>.meta_data field.
It parses the table object and retrieves the target based on the args.target value. The lc:
function is used to convert the target to lowercase for case-insensitive matching.
In this scenario, a valid target can be either support or sales.
If the target is found in meta_data.table, the call is connected to the respective number.
If the target is not found in meta_data.table, the string field returns empty and does not match the first expression object.
This leads to matching the second expression, which is a catch-all for any string that returns any valid character.
Since an empty string is a valid character, this causes a successful match.
Output: Based on the match, specific actions or responses can be triggered. This allows performing conditional tasks depending on the user’s input. Each expression object contains its own output.
Webhooks extend the capabilities of your SWML scripts by enabling communication with external systems. This can be essential for integrating with APIs, fetching real-time data, or pushing updates to external services.
Key points to note about webhooks:
url field can include dynamic values, making it possible to construct
requests based on the current state or input data.method field allows you to define the HTTP method (GET, POST, etc.)
to be used for the request, providing flexibility in how data is sent or received.output section specifies how the response from the webhook should be processed,
including actions to be taken and responses to be fed to the AI agent.URL Construction: The url field specifies the endpoint to which the HTTP request should be made.
https://icanhazdadjoke.com/search?term={lc:args.type} constructs the URL with a query parameter term based on the args.type value.
https://icanhazdadjoke.com/search is used to fetch jokes based on the query parameter term.args.target is a SWAIG.function argument that contains the target value passed by the user when interacting with the AI agent.lc: function is used to convert the args.type value to lowercase for consistency.HTTP Method: The method field defines the HTTP method to be used for the request.
GET is used to fetch data from the specified URL.Webhook Response Handling: The output section specifies how the response from the webhook should be processed and if any actions should be taken.
icanhazdadjoke API, the response
contains an array of jokes that we can access and play back to the user.output section, the response field is used to tell the AI agent to relay the joke to the user.
array[0].results[0].joke syntax is used to access the first joke in the results array.Example Response:
Outputs define the responses and actions that should be taken when an expression matches or a webhook request completes. This can include sending messages, triggering functions, or modifying variables.
Outputs are the mechanisms through which your SWML script communicates and reacts to data. They enable the script to provide feedback to the user, execute actions, and modify the script’s state based on input data and external interactions.
Outputs are most often used within Expressions or Webhooks. However, they can also be used as a top level object within a Data Map.
Example:
When passing a SWML object that includes a connect (or other method that transfers the call) in output.action[], your script must be JSON, and "transfer": true, must be included before the SWML in the same action object. Refer to the example above.
Responses in data_map are used to provide feedback to the AI agent based on the user’s input or external data.
They can include messages, prompts, or other forms of communication to guide the user through the interaction.
Key points to note about responses:
Example:
response field contains the message to be relayed to AI agent. This message
can be instructive, informative, or conversational, depending on the context.
I'm sorry, I was unable to transfer your call to ${args.target} due to being an invalid destination.
is the message that will be sent to the AI agent if the args.target is not found in the meta_data.table object.
This will cause the AI to inform the user that the transfer destination is invalid.${args.target} placeholder is used to dynamically insert the target value provided by the user into the response message.Actions in data_map define what should happen when the swaig.function executes and
a specific expression matches or a webhook request completes.
Actions can include triggering functions, modifying variables, controlling the flow of the current SWML script or even starting a new SWML script.
Key points to note about actions:
Example:
action field specifies the actions to be taken based on the webhook response.
transfer and get_joke functions based on the response from the webhook.
transfer function is activated (active: true) to enable the user to be transferred to the target department.get_joke function is deactivated (active: false) to prevent further joke requests after the first one has been fulfilled.When working with data_map, you have access to several special variable scopes that are only available in SWAIG/AI contexts:
args - Function arguments passed to the SWAIG functionmeta_data - Function-level metadata that persists across function callsglobal_data - Application-wide data accessible across all functionsprompt_vars - Built-in template variables for call and AI session informationFor complete technical specifications, types, and properties, see the SWAIG Variables Reference.
For general SWML variable scopes like call, params, and vars, see the Variables Reference.
When working with data_map, there are several ways to utilize stored values, such as function.argument (args),
function.meta_data, and SWML variables (vars).
SWAIG function arguments are processed as
args and can be accessed within the data_map section. These arguments are defined in the
functions section of the SWAIG object. Initially, the value of the argument is empty, but
as the user interacts with the AI, the value is updated based on the user’s input.
To access the value of a specific argument, use ${args.<argument_name>}. For example, if
the argument is named target, you can access its value using ${args.target}.
Both global_data and function.meta_data are versatile environmental objects that can
store arbitrary data. This data can be set initially in the SWML script or dynamically
through the SWML set_global_data
or set_meta_data actions.
meta_data: Stores data specific to a particular function.global_data: Accessible across all functions within the SWML script.To access the value of a specific meta_data key, use ${meta_data.<key>}. For example,
if the key is table, you can access its value with ${meta_data.table}.
For global_data, access the value of a specific key using ${global_data.<key>}.
For example, if the key is table, you can access its value with ${global_data.table}.
If your meta_data or global_data is an object, you can access a specific property using
${meta_data.<key>.<property>} or ${global_data.<key>.<property>}. For example, if
the key is table and the property is support, you can access its value with ${meta_data.table.support}
or ${global_data.table.support}.
SWML variables (vars) store data created by a SWML request
or set method.
To access the value of a specific SWML variable, use ${vars.<variable_name>}. For example,
if the variable is joke, you can access its value with ${vars.joke}.
This structure helps efficiently manage and retrieve data within your SWML scripts, providing a clear and consistent way to handle environmental data and variables.
Below is a comprehensive example that incorporates all aspects of data_map and is ready to be used in a SWML script.
Replace the following placeholders with actual values for this SWML script to work properly:
+1XXXXXXXXXX - Phone number you want to transfer to for support.+1YYYYYYYYYY - Phone number you want to transfer to for sales.https://example.site.com/summarize - URL where the call summary will be sent.args, meta_data, and SWML variables can be accessed within the data_map section.