Integrating Airtable data into Twilio Autopilot responses

I’m working on a Twilio Autopilot project where I need to fetch info from Airtable based on a date the user gives. The bot asks for a date, then I want to look up that date in Airtable and tell the user what movies are playing in different theaters.

I’ve got the Airtable part working, but I’m stuck on how to use the data in my bot’s response. Here’s what I have so far:

function handleRequest(context, event, callback) {
  const Airtable = require('airtable');
  const base = new Airtable({apiKey: 'mykey'}).base('mybase');

  base('Shows').find('someRecordId', (error, record) => {
    if (error) {
      console.error(error);
      return;
    }
    console.log(record);
  });
}

This gives me data like:

{
  "id": "someRecordId",
  "fields": {
    "ShowDate": "2023-05-15",
    "BigScreen": "Guardians of the Galaxy",
    "SmallScreen": "The Super Mario Bros. Movie"
  },
  "createdTime": "2023-05-15T10:00:00.000Z"
}

How can I take this info and use it in my bot’s response to the user? Any tips would be great!

hey, i’ve dealt with similar stuff before. u could try something like this:

let response = Movies on ${record.fields.ShowDate}: Big Screen: ${record.fields.BigScreen} Small Screen: ${record.fields.SmallScreen};

callback(null, response);

that should give u a neat response to send back to the user. hope it helps!

I’ve implemented something similar in one of my projects. Here’s a practical approach:

Instead of hardcoding the record ID, you can query based on the date the user provides. This makes your bot more dynamic:

function handleRequest(context, event, callback) {
  const Airtable = require('airtable');
  const base = new Airtable({apiKey: 'mykey'}).base('mybase');
  const userDate = event.Field_user_date_value; // Assuming you're getting the date from the user

  base('Shows').select({
    filterByFormula: `{ShowDate} = '${userDate}'`
  }).firstPage((err, records) => {
    if (err) {
      callback(err);
      return;
    }
    
    if (records.length > 0) {
      const show = records[0].fields;
      const response = `On ${show.ShowDate}, we're showing ${show.BigScreen} on the big screen and ${show.SmallScreen} on the small screen. Enjoy the movies!`;
      callback(null, response);
    } else {
      callback(null, 'Sorry, no shows found for that date.');
    }
  });
}

This approach allows for more flexibility and better user interaction. Remember to sanitize user input to prevent injection attacks.

To incorporate the Airtable data into your Twilio Autopilot responses, you’ll want to modify your handleRequest function. Instead of just logging the record, you can use the data to construct a response string. Here’s an approach:

function handleRequest(context, event, callback) {
  const Airtable = require('airtable');
  const base = new Airtable({apiKey: 'mykey'}).base('mybase');

  base('Shows').find('someRecordId', (error, record) => {
    if (error) {
      callback(error);
      return;
    }
    
    const response = `On ${record.fields.ShowDate}, the Big Screen is showing ${record.fields.BigScreen} and the Small Screen is showing ${record.fields.SmallScreen}.`;
    
    callback(null, response);
  });
}

This constructs a string using the Airtable data and passes it back through the callback. You can then use this response in your Autopilot flow to send back to the user. Remember to handle potential errors or missing data to ensure a smooth user experience.