Airtable as a database for your chatbot

Airtable as a database for your chatbot

Use Airtable as a database for your chatbot

Apps Jun 18, 2020

Building a chatbot often rhymes with creating conversations and it usually boils down to understanding the user intent, getting the chatbot to ask a question, and basically going down a decision tree to eventually get some information about the user, but where do we store these gathered data now?

The obvious answer would be to get a Mysql or MongoDB database on a server somewhere and interact with it. This idea sure is the way to go when building a big project that needs to scale to millions of rows but more often than not we just need a few thousand rows at most, to store some user data (answers to a survey for instance).

That’s when Airtable comes into play!

Airtable allows you to quickly create spreadsheets that can be used as databases for your chatbots.

How to use Airtable with CSML?

The CSML studio offers some plug-and-play apps. Let’s have a look and see how that works.

Step 1: Create a database on Airtable

First, sign in or create an account on Airtable if you don’t already have one, there is a free plan that’s perfect! Create a new database. For the sake of this exercise, we will create a table to manage subscriptions to an event. So here are the 3 fields of our database’s only table: first_name, last_name, and email.

Database on Airtable
Database on Airtable

Step 2: Get your API key, database id, and table name

To get your API key, go to https://airtable.com/account, copy the API key and keep it safe!

Then go to https://airtable.com/api, click on your database, and then on the table you want to use (in the sidebar), the database id and table name are in the URL as follow: https://airtable.com/{DATABASE_ID}/api/docs#curl/table:{TABLE_NAME}

Step 3: Create your chatbot

Let’s build a small chatbot that we’ll use to register people for an event.

To do so, sign in or create a free account on the CSML Studio. Create a new chatbot and paste the code below.

start:
  say "Hello stranger, I'm here to help you register to an awesome event 🥳"
  goto name

name:
say Typing(2000)
  say "My name is Eventobot, what's yours ?"
  hold
  remember firstname = event
  say "Nice to meet you {{firstname}} 😎"
  say Typing(2000)
  say "I'd also need your last name just in case there is more than one {{firstname}} registered 😉."
  hold
  remember lastname = event
  say "Perfect, now let me send you a confirmation for the event."
  goto email

email:
say Typing(2000)
  say "what's your email address ?"
  hold
  if (!Find("@", in=event)) {
    say "It looks like your email is not valid 🤨"
    goto email
  }
  remember email = event
  do fields = { "firstname": firstname, "lastname": lastname, "email": email } 
  do newRecord = Fn("airtable", method="createRecord", spreadsheet_id="mydatabaseid", table_name="my_table_name", fields=fields)
  say "I got everything I need, see you on tuesday for the awesome event!"
  say "Bye! 👋"
  goto end

This 30 lines chatbot will:

  • ask the user his/her first name, last name, email
  • check the email address contains a “@”
  • save the user on Airtable

Make sure you replace the spreadsheet_id (also called database id) and the table name.

Build the chatbot by clicking on the “Build” button on the top right of the window.

Step 4: Install the Airtable App

Click on the “Functions” link in the sidebar, then on “Apps directory” in the topbar, and on the Airtable box. Go down to the bottom of the modal, enter the API key in the input box and then install the app.

Airtable App
Airtable App

Step 5: Test and deploy

That’s it! Now you can come back to the editor, click on “Build and run” to make sure everything works fine, and as you finish the conversation with the chatbot, you’ll see the information being sent to Airtable.

If you want to go a step further and deploy your chatbot, click on “Channels”, we got a Webapp that is available with a single click install.

If anything goes wrong, you can join our Slack to get support from our team and the CSML community made up of hundreds of chatbot developers.

Tags