I’m working on a Twilio Autopilot bot that needs to fetch information from an Airtable database. The flow works like this: first I use a Collect action to get a date from the user (like Feb 15, 2019). Then I want to search my Airtable base using that date to find the matching record. Finally I need to display the information back to the user in a readable format (something like “on Feb 15 the Downtown Cinema is showing this film, the Mall Theater is showing…”).
I have the basic Airtable retrieval working but I’m stuck on how to extract the returned data into variables that I can use in my response. Here’s my current function:
exports.handler = function(context, event, callback) {
var AirtableAPI = require('airtable');
var database = new AirtableAPI({apiKey:
'myapikey'}).base('mydatabase');
database('Events').find('rec123ABC456DEF789', function(error, result) {
if (error) { console.error(error); return; }
console.log(result);
});
}
The response from Airtable looks like this:
{
"id": "rec123ABC456DEF789",
"fields": {
"Date": "2019-03-15",
"Downtown Cinema": "Spider-Man",
"Mall Theater": "Iron Man"
},
"createdTime": "2019-03-15T18:30:22.000Z"
}
How can I access the fields data and pass it back to the user through Autopilot?
You’re using find() when you need select() to filter by date. Your current code’s looking for a specific record ID, not matching dates.
Here’s what I use for similar stuff:
exports.handler = function(context, event, callback) {
var AirtableAPI = require('airtable');
var database = new AirtableAPI({apiKey: 'myapikey'}).base('mydatabase');
const userDate = event.Field_date_value; // from your Collect action
database('Events').select({
filterByFormula: `{Date} = '${userDate}'`
}).firstPage(function(error, records) {
if (error) {
return callback(null, {
actions: [{
say: "Sorry, I couldn't find that information."
}]
});
}
if (records.length === 0) {
return callback(null, {
actions: [{
say: "No events found for that date."
}]
});
}
const record = records[0];
const downtownMovie = record.fields['Downtown Cinema'];
const mallMovie = record.fields['Mall Theater'];
const message = `On ${userDate}, Downtown Cinema is showing ${downtownMovie} and Mall Theater is showing ${mallMovie}.`;
callback(null, {
actions: [{
say: message
}]
});
});
};
filterByFormula searches by your date field instead of hardcoded record IDs. Way better for dynamic queries.
You’re just logging the result instead of sending the data back to Autopilot. That’s the main problem. After you get the Airtable response, parse the fields and structure it for Autopilot’s format. I hit this same issue last month. Autopilot needs a specific response structure with an actions array. Once you get the Airtable result, extract what you need like const cinemaMovie = result.fields['Downtown Cinema'] and build your response message. The crucial part is calling callback(null, responseObject) where responseObject has the actions Autopilot should run. Also watch out for date format mismatches - what the user sends vs. how it’s stored in Airtable. I had to convert the date format before querying to get consistent results.
you’re missing the key piece - actually pulling data based on what the user enters. right now you’ve got a hardcoded record ID, which won’t work when users input different dates. you need to grab the date from event.Field_date_value (or whatever you called your collect field) and use that in your Airtable query. also, double-check that your date formats match exactly between user input and what’s stored in Airtable. that’s usually where things break.
Your code isn’t handling the callback correctly or returning data to Autopilot. You need to pass the extracted data back through the callback parameter. Here’s what worked for me in a similar setup: grab the Airtable result, extract fields using result.fields, and build your response string. Then call callback(null, responseObject) where responseObject has the actions you want Autopilot to run. For your case, you’d access cinema data with result.fields['Downtown Cinema'] and result.fields['Mall Theater'], build a message like “On [date] the Downtown Cinema is showing [movie]…”, then return it in a Say action. Don’t forget to handle errors by calling callback(error) if the Airtable request fails. Autopilot needs a specific JSON structure in the callback response to know what to do next.