Retrieving data from Airtable (GET)

Bots are often used to show data from external sources to your user. An easy way to manage this data is by using Airtable. Airtable is a tool that allows you to create a spreadsheet that you can talk to with an API.

In this tutorial we will set up an integration with Airtable. Because of all the code it looks quite technical, but in fact it's pretty easy.

If you're new to using variables in Chatlayer.ai, follow this tutorial first.

As an example we're going to be building a bot that shows information about chatbot meetups.

  • The first thing we have to do is create some meetup data. In this example, we will be using this Airtable. Feel free to reuse it!

  • Start by building a short flow that states the purpose of the bot and that asks about which month the user wants to know meetup info.

  • For the month question, make sure you're using an input validation that saves the response as themonth variable and that continues to the next step: an action bot dialog.

  • Configure the action bot dialog that is triggered after the input validation with a code plug-in. Add the "month" variable as an argument, as shown in the screenshot:

  • Next, add this code snippet:

const { month } = args;
const { records = [] } = await fetch(
"https://api.airtable.com/v0/(yourAppId)/(yourTableName)",
{
method: "GET",
headers: {
Authorization: "Bearer (insert token here)",
},
}
).then((res) => res.json());
const found = records.filter((rec) => rec.fields.Month === month.toLowerCase());
const builder = ChatlayerResponseBuilder();
if (found.length) {
builder.setSessionVariable("meetups", found);
} else {
builder.setSessionVariable("nomeetups", found);
}
builder.send();

Remember to get the right app id, table name and bearer token for your Airtable. You can find it here.

  • This code block searches the table for a meetup in the month that the user said, and if it finds one, returns that information to Chatlayer.ai. If there is no meetup found, the nomeetups variable is saved to the session of the user.

  • If you follow the flow to this point, it will look something like this:

  • And the following data is saved on the session:

  • Now all we need to do is show that data! Add a Go To at the end of the Action bot dialog where you have added the code block.

  • Configure this Go To as following:

  • This way, the user will get a different response if there are no meetups in the month that they've asked about.

  • Finally, configure the messages in "show meetup info" to show the retrieved info from the Airtable sheet.

  • All done! You can now test the full flow.