Skip to main content

Apply API to Chatbot

WOZTELL 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 WOZTELL, 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:

  1. WOZTELL has a specific syntax for actions. You must return a new promise and resolve it at last.
  2. 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
  3. Fetch data from JSON placeholder. WOZTELL is using node-fetch npm package in here.
  4. Assign user data to this.member with this.lodash.set
  5. Update this.member through the use of resolve
return new Promise((resolve) => {
const userId = this.messageEvent.data.text

const userInfo = fetch(`https:\/\/jsonplaceholder.typicode.com/users/${userId}`)
.then(res => res.json())

this.lodash.set(this.member, "botMeta.tempData.userInfo", userInfo)

resolve({
member: this.member
})
})

For later usage, you can access the to-dos data with this.lodash.get(this.member, "botMeta.tempData.userInfo", null).