Skip to main content

Connect to Datasource

Now you have your first WhatsApp chatbot working properly and people can make a booking on your bot, but how to check the booking record?

In this section, we are going to teach you how to save user's data into Datasource so you can have a copy of the record.

Expected Outcome

You can check booking record in Datasource on WOZTELL.

Check booking record in Datasource
Check booking record in Datasource

Save Name

Create Pre-action

  1. To save the user input, we must perform the action in the node of the following level. For example, if we want to save the user's name, we have to save it in the node of "Ask email address" because the bot can only receive the user's name input at the node comes after.

  2. Click on "Ask email address" node, and click "+ New Action" under Pre-actions.

Add pre-actions
Add pre-actions
  1. Name this pre-action and select "Advance".
Create pre-actions
Create pre-actions
  1. Paste the following code into the action. It allows you to save the entered name in user's tempData and categorize it under name of booking.
return new Promise((resolve, reject) => {
this.lodash.set(this.member, "botMeta.tempData.booking.name", this.messageEvent.data.text)
resolve({
member: this.member,
})
})
Create pre-actions
Create pre-actions
  1. Save the pre-action.
Created save name action
Created save name action

Save Email Address

Create Pre-action

  1. Similarly, goes to "Booking confirmation" node, and click "+ New Action" under Pre-actions.
Add pre-actions
Add pre-actions
  1. Name the pre-action, and select "Advanced". Paste the following code, it allows you to save the email address in user's tempData and categorize it under email of booking.
return new Promise((resolve, reject) => {
this.lodash.set(this.member, "botMeta.tempData.booking.email", this.messageEvent.data.text)
resolve({
member: this.member,
})
})
Create pre-actions
Create pre-actions
  1. Save the post-action and the node.
Save email address
Save email address

Create Data Source

  1. Now goes to Data Source at the top blue menu.

  2. Click "+ New Data Source" to create a mini database to store your booking confirmation.

Datasource
Datasource
  1. Name your Data Source: Booking Confirmation and click "+ Create" to save it.
Name your Datasource
Name your Datasource
  1. You have created your Data Source now. You can see it on the Datasource list and click "View" to see the Data Source details.
Datasource list
Datasource list
  1. You can see that there is no data now for your Datasource but don't worry, we will add the booking record in this database later. Now copy the Data Source ID in your notes and save it for later use.
Datasource details
Datasource details

Save Record in Data Source

Create Post-action

  1. Now you can go back to your workspace and goes to "Booking confirmation" node. Click "+ New Action" under Post-actions.
Add post-action
Add post-action
  1. Click "+ New Action". With the following code, you can now retrieve the user's tempData and save it back in the Datasource.
return new Promise(async (resolve, reject) => {
try {
await this.upsertDataToDataSource({
dataSourceId: "YOUR_DATASOURCE_ID",
data: {
"Name": this.lodash.get(this.member, "botMeta.tempData.booking.name"),
"Email": this.lodash.get(this.member, "botMeta.tempData.booking.email"),
}
})
} catch (e) {
console.log("cannot run upsertDataToDataSource, ", e.message)
}
resolve({
member: this.member
})
})
Save record into Datasource
Save record into Datasource
tip
  • Remember to input the copied Data Source ID in the script.
  • The pairs inside data represents the column head ("Name") and the value (this.member.botMeta.tempData.booking.name) filled in the field per record entry.
  1. Save the action and save the node.
Save Action and Node
Save Action and Node
  1. Now, you can try going through your chatbot as a user. The information typed by the users should be saved to the Data Source.