Apply API to Chatbot
Stella is a highly customizable bot building tool and provides the flexibility to build a personalized chatbot flow for any business use case. You can make use of actions
in Stella, which includes both Pre-actions
and Post-actions
.
Please refer to the sample code below on fetching data from JSON placeholder.
The sample code logic is as follows:
- Stella has a specific syntax for actions. You must return a new promise and resolve it at last.
- Assume the chatbot has asked the customers their user ID, the chatbot will then get the questions answer(user's ID) from customer and save it as a constant in Javascript
- Fetch data from JSON placeholder. Stella is using node-fetch npm package in here.
- Assign user data to
this.member
withthis.lodash.set
- Update
this.member
through the use of resolve
return new Promise(async (resolve, reject) => {
try {
const userId = this.messageEvent.data.text
const userInfo = await this.fetch(`https:\/\/jsonplaceholder.typicode.com/users/${userId}`)
.then(res => res.json())
this.lodash.set(this.member, "botMeta.tempData.userInfo", userInfo)
resolve({
member: this.member
})
} catch (error) {
reject(error)
}
})
For later usage, you can access the to-dos data with this.lodash.get(this.member, "botMeta.tempData.userInfo", null)
.