I’m working on a Twilio Autopilot bot that needs to get information from an Airtable database. First, I use a Collect action to get a date from the user (like Feb 15, 2020). After that, I want to search my Airtable base using that date to find the matching record.
The goal is to tell the user what’s happening on that date (“on Feb 15 the Cinema A is showing this film, Cinema B is showing that film”).
I have this code for getting data from Airtable, but I’m stuck on how to use the returned information as variables in my response:
exports.handler = function(context, event, callback) {
var AirtableAPI = require('airtable');
var database = new AirtableAPI({apiKey:
'my_api_key'}).base('my_base_id');
database('Schedule').find('rec123ABC456DEF', function(error, result) {
if (error) { console.error(error); return; }
console.log(result);
});
}
Set your Twilio function timeout higher - my autopilot died mid-conversation when Airtable was slow to respond. Also test how it handles weird date inputs like “tomorrow” or “next Friday” instead of real dates.
You’re using find() which needs a specific record ID, but you want to search by date. Switch to select() with filtering instead.
Here’s what worked for me:
exports.handler = function(context, event, callback) {
var AirtableAPI = require('airtable');
var database = new AirtableAPI({apiKey: 'your_key'}).base('your_base');
// Get the date from Autopilot memory
var userDate = event.Field_date_value; // or however you're storing it
database('Schedule').select({
filterByFormula: `{Date} = '${userDate}'`
}).firstPage(function(error, records) {
if (error) {
callback(null, { say: 'Sorry, I had trouble finding that information.' });
return;
}
if (records.length === 0) {
callback(null, { say: 'No movies scheduled for that date.' });
return;
}
var record = records[0];
var message = `On ${record.fields.Date}, Cinema A is showing ${record.fields['Cinema A']} and Cinema B is showing ${record.fields['Cinema B']}.`;
callback(null, { say: message });
});
};
Use select() with filterByFormula to actually search your data instead of looking for a hardcoded record ID. Just make sure your date format matches exactly what’s in Airtable.
Check your date format conversion between user input and what Airtable expects. I hit this exact problem - users said “Feb 15, 2020” but Airtable stored “2020-02-15”. Parse and convert the date before filtering. What if multiple records match your date? firstPage() works, but add validation to grab the right record. I added extra filter criteria since our database had multiple entries per date for different locations. Test your error handling hard. Airtable’s API gets flaky and you don’t want your bot hanging when requests fail. Always have a fallback response ready.
ur not using the callback function right to send data back to autopilot. Extract the fields and return them in the callback response like this: callback(null, { say: 'on ' + result.fields.Date + ' Cinema A shows ' + result.fields['Cinema A'] }). also handle the error case or it’ll break.
Hit this exact problem building a restaurant booking bot last year. You’re mixing up two different operations.
Your current code uses find() with a hardcoded record ID, but you need dynamic filtering based on user input.
Here’s what works:
exports.handler = function(context, event, callback) {
var AirtableAPI = require('airtable');
var database = new AirtableAPI({apiKey: context.AIRTABLE_API_KEY}).base(context.AIRTABLE_BASE_ID);
// Extract the collected date from Autopilot
var collectedDate = event.Memory.twilio.collected_data.your_collect_task_name.answers.date.answer;
database('Schedule').select({
filterByFormula: `{Date} = '${collectedDate}'`,
maxRecords: 1
}).eachPage(function(records, fetchNextPage) {
if (records.length > 0) {
var record = records[0];
var response = `On ${record.fields.Date}, Cinema A is showing ${record.fields['Cinema A']} and Cinema B is showing ${record.fields['Cinema B']}.`;
callback(null, { say: response });
} else {
callback(null, { say: 'Sorry, no movies scheduled for that date.' });
}
}, function(error) {
if (error) {
callback(null, { say: 'I had trouble checking the schedule. Please try again.' });
}
});
};
Replace your_collect_task_name with whatever you named your Collect action.
Biggest gotcha is date format matching. If your Collect action returns “Feb 15, 2020” but Airtable expects “2020-02-15”, you’ll need to parse and reformat the date first.
Store your API credentials in Twilio Functions environment variables instead of hardcoding them. Way cleaner and more secure.
I keep hitting timeouts with this setup. Your approach works, but you need timeout handling so the function doesn’t hang when Airtable’s API crawls. Wrap your database call in a timeout check and bail early if it drags on too long. Also watch out for pagination - if your Schedule table gets big, the select might miss records or just timeout completely. I’d add a view in Airtable that filters by date range to speed things up. For that date parsing problem people mentioned, write a helper function that handles different input formats before you hit the API. Store the parsed date in a variable and log it so you can debug easier. One more thing - test with dates that aren’t in your database to make sure your fallback message actually helps instead of being useless.
don’t forget to handle the async stuff properly - your callback might timeout if airtable’s being slow. also, check if the date collection actually worked before querying. autopilot sometimes fails silently on date parsing and you’ll end up with undefined values.
You’re hardcoding the record ID instead of using the date the user actually entered. First, grab the collected date from Autopilot’s memory - it’ll be in event.Memory.twilio.collected_data.collect_date.answers.date.answer (adjust for your task name). Parse that date into whatever format Airtable wants, then query with it. Wrap your Airtable calls in try-catch blocks too - I’ve seen bots crash from API timeouts that weren’t handled. And test with different date formats because users never input dates the way you expect them to.
Had the same issue with a concert venue bot. You’re hardcoding the record search instead of grabbing the date from your Collect action. Your function needs to pull the user’s date input and structure the callback properly. You’re missing the part where you access Autopilot’s collected data from memory, then return it correctly. Try var userDate = event.Memory.twilio.collected_data.collect_date_task.answers.date.answer to grab the date, build your response from the Airtable fields, and return it with callback(null, { say: your_message }). Watch out for date format mismatches too - users input vs. how Airtable stores dates. I added date parsing because users would say “February 15th” but my database wanted “2020-02-15”. Test different date inputs so your parsing handles the weird cases.