In this tutorial, you will learn how to build a simple xApp that allows users to collect their signatures. The xApp enables users to draw their signature in an HTML-based interface and submit it to a Flow for further processing. We will cover the necessary code snippets and steps to create this functionality.
Prerequisites
- Create an empty Flow.
Explore the Code
- Open the code in the Code Sandbox by clicking the following link:
- The code contains a submit button that, when clicked, displays an alert with the JSON data containing the base64-encoded image in PNG format. The data is represented in a JSON format with a single field called
signaturePng
.
submitButton.addEventListener("click", (event) => {
if (signaturePad.isEmpty()) {
alert("Please provide a signature first")
} else {
const data = { signaturePng: signaturePad.toDataURL() }
SDK.submit(data)
}
}) - To include the required SDK in your xApp, use the SDK xApp page URL and the SDK's submit function to send the JSON data.
- In the
app-page-sdk.js
file, you will find an xApp page SDK mock file that creates the window.SDK object. The provided mock file defines the submit function to display an alert with the submitted data. In the actual shell page of the xApp, the data will be sent to the Cognigy flow for further processing.
window.SDK = {
submit: (data) => alert(JSON.stringify(data))
}
Build the xApp
- Open a new Flow.
- In the Flow editor, add an xApp: Init Session Node. The agent session needs to be initialized to communicate with the Cognigy backend.
The xApp: Init Session Node is used to start the session, which provides the token and the xApp URL in theagent.session
object. - Below xApp: Init Session Node, add an xApp: Show HTML Node. Copy the code from Code Sandbox and paste it to the HTML Document field with selected Full HTML Document content.
- Click Save Node.
- Below the xApp: Show HTML Node, add a Question Node.
- In the Node, select the xApp question type.
- Select Text with Buttons output type.
- In the text field, enter
Please draw your signature in the xApp
. - Click Add Button. Fill in the following fields:
- Button xApp — enter the text:
Open xApp
. - Select Button Type — select URL.
- URL — add xApp Session URL.
- URL Target — select Open URL in a new tab.
- Button xApp — enter the text:
- Click Save.
- Click Save Node.
- Below the Question Node, add a Say Node.
- In the Say Node, select the Gallery Output Type.
- Click Add Card.
- In the Title field, specify
Received signature
. - Click Add image.
- In the Image URL field, specify
{{ input.data._cognigy._app.payload.signaturePng }}
. - To save changes, click Done.
- Click Save Node.
Test your Flow via the Interaction Panel.
Once the node is added, you should always start with the xApp: Init Session node. After that, when you try again, your input object will contain the token and the URL for the xApp Framework. Take this URL and create a signature.
Now you can deploy your virtual agent via any of the supported channels, such as Webchat Widget.
Comments
1 comment