Execute SWML

View as Markdown

Execute a remote SWML document and return to the current document. Use %{return_value.['object_field']} to reference the return values from the external SWML.

Output Node Connectors

Condition
string

The condition that is evaluated to determine which path to take. Additional conditions can be added by clicking the Add condition button. Additional conditions will act as JavaScript else-if statements.

Else
string

The path to take if none of the conditions are met.

Node Settings

URL
string

The URL of the SWML document to execute. The URL must return swml in valid JSON or YAML format.

Params
object

The parameters to pass to the SWML document.

Meta
object

The metadata to pass to the SWML document. A JSON object to serialize.

Conditions
string

The conditions that are evaluated to determine which path to take. Additional conditions can be added by clicking the Add condition button. Additional conditions will act as JavaScript else-if statements.

Example

In this example, we will execute a SWML script that is hosted on a remote server. When making a request to the server, we will pass the User and Token parameters.

The SWML document will return a JSON object with a play method field. This play field will be used for TTS (Text-to-Speech) in the current document and will welcome the user with the User parameter and say the Token parameter.

Execute SWML node example that executes a remote SWML document.

Call Flow using the Execute SWML node.

Execute SWML Node Settings

  • URL: Self-hosted ngrok URL.
  • Params: { "Content-Type": "application/json", "User": "user_1", "Token": "123" }
  • Meta: None
  • Condition: %{return_value.return_value} === 1

SWML Document

Below is the SWML document that will be executed.

andJSON
1sections:
2 main:
3 - play: 'say: Hello <USER_NAME>, welcome to the SWML demo! Your token is <TOKEN>!'

Server Code

Below is the server code that will return the SWML document.

Pre-requisites

  • ExpressJs installed in your environment.
  • ngrok installed in your environment.
1const express = require('express');
2const ngrok = require('ngrok');
3const app = express();
4
5// Body parser middleware to handle JSON payloads
6app.use(express.json());
7
8// Define the route
9app.post('/swml', (req, res) => {
10
11 let reqBody = req.body;
12 console.log(reqBody);
13
14 let user = reqBody.params.User;
15 let token = reqBody.params.Token;
16
17
18 const swml = {
19 "sections": {
20 "main": [
21 {
22 "play": `say: Hello ${user}, welcome to the SWML demo! Your token is ${token}!`
23 }
24 ]
25 }
26 };
27
28 res.json(swml);
29});
30
31
32// Start the server and use ngrok to expose it
33const port = 5000;
34app.listen(port, async () => {
35 const url = await ngrok.connect(port);
36 console.log(`Server running on ${url}`);
37});