Using Async API to Access Third-Party Application
In this guidance, you will be able to implement a bot that uses an action type Async API. Async API is used to make the response API send via command without waiting for the API results to move to the next state (a more seamless API call process).
Introduction
For using an API to access third-party applications from your chatbot, you will need to create an action API. Also, a state must be created before creating an action
How to Use
In this guidance, you will learn how to integrate NLU in your chatbot to recognize specific end-users input. Before you start, you will need to create at least one entity and it is already trained.
- Go to your project, then click Flow.
- Create a state, for example a
apiExample
state. Click + icon in bottom right to create new state - Click + in the action box to create a new action.
- When all actions available are shown, then choose API
- Click tab Async API
Each field explanation:
Field Name |
Description |
---|---|
Action name | Name of your action. Max. 30 characters with alphanumeric. |
Condition | Which condition can trigger this action |
URI | Your API URL |
Method | Choose POST or GET |
Body | Input API body by creating key and value. |
Headers | Input API headers by creating key and value. |
Headers | Input API headers by creating key and value. |
Query | - |
Form Data | - |
Result Path | - |
Command | The API result will be sent to command. |
- Click the Create Action button to create action.
- Click the Create to finalize state creation.
How to Collect Async API Result
If you want to collect API results, you just need to create an Intent to receive results from Command. For example:
To access the value, the API result can be stored in payload, then you can access it by writing payload
. For example:
todoList:
active: true
volatile: true
intents:
todoListIntent:
type: text
initial: true
fallback: false
condition: 'content === "#todoList"'
freeTextIntent:
type: text
initial: false
fallback: false
apiResultIntent:
type: command
initial: true
fallback: false
condition: 'content === "apiResult"'
states:
init:
transitions:
inputTask:
condition: 'intent === "todoListIntent"'
apiResultState:
condition: 'intent === "apiResultIntent"'
initial: true
saveTask:
transitions: {}
action: addTodoList
end: true
inputTask:
transitions:
saveTask:
condition: 'intent === "freeTextIntent"'
mapping:
payload.task: content
action: inputTask
apiResultState:
transitions: {}
action: apiResult
end: true
actions:
addTodoList:
type: asyncApi
options:
uri: 'http://www.example.com/api/v1/todo'
method: POST
body:
- key: task
value: $(payload.task)
headers: []
command: apiResult
inputTask:
type: text
options:
text: 'Enter a new task'
apiResult:
type: text
options:
text: $(payload.status)
methods: {}
Figure 3 Example of Async API use in YAML codes
This is the end of the guidance. You can contact support@kata.ai if you have any difficulties when implementing this.