DEV Community

Cover image for Parse Airtable with SMS for Climate Non-Profits with JavaScript and Twilio Serverless
Lizzie Siegle for Twilio

Posted on • Edited on

Parse Airtable with SMS for Climate Non-Profits with JavaScript and Twilio Serverless

This blog post was written for Twilio and originally published on the Twilio blog.

April is Earth Month and has Earth Day coming up! My teammate Michelle Glauser and I were talking about Earth-related organizations and how hard it is to find some to support or volunteer with. To fix this problem, read on to learn how to build a serverless JavaScript app to parse an Airtable of eco-friendly non-profits, and see what they are working on via SMS.
example SMS

Prerequisites

  • A Twilio account - sign up for a free one here and receive an extra $10 if you upgrade through this link
  • Node.js installed - download it here
  • A Twilio number with SMS capabilities (this quickstart will help you get going)
  • An Airtable account You’re going to need your Airtable API key and a base ID to access the base from your app. After logging in to Airtable, get the API key from your account page–keep this a secret! While logged in, make a copy of this project's Airtable base to add it to your Airtable account. ### Setup your Airtable You can then get the base ID from the auto-generated API documentation, which will now show the Airtable x Twilio blog base). You can retrieve the base ID from either the URL or the introduction section. docs In the copied Airtable base, select the climate orgs tab. It contains an org column containing non-profit organization names you could and should support. It also includes their corresponding overview, website, and category columns. The possible categories include climate, environment, water, food+agriculture, and transportation. ### Get Started with the Twilio Serverless Toolkit The Serverless Toolkit is CLI tooling that helps you develop locally and deploy to Twilio Runtime. The best way to work with the Serverless Toolkit is through the Twilio CLI. If you don't have the Twilio CLI installed yet, run the following commands on the command line to install it and the Serverless Toolkit:
npm install twilio-cli -g
twilio login
twilio plugins:install @twilio-labs/plugin-serverless
Enter fullscreen mode Exit fullscreen mode

Create your new project and install our lone requirement airtable, an Airtable client library for Node.js, by running:

twilio serverless:init ecofriendly-orgs-airtable-sms
cd ecofriendly-orgs-airtable-sms
npm install airtable
Enter fullscreen mode Exit fullscreen mode

Open the .env file and add an environment variable for your Airtable API key from the Airtable page you kept open with your key as well as your Airtable Base ID! In this blog post, the API key is called AIRTABLE_API_KEY and the Base ID is called AIRTABLE_BASE_ID.

AIRTABLE_API_KEY=xxxx...
AIRTABLE_BASE_ID=xxxx...
Enter fullscreen mode Exit fullscreen mode

Make a Twilio Function with JavaScript

cd into the /functions directory and make a new file called sms.js containing the following code:

const airtable = require("airtable");
exports.handler = function (context, event, callback) {
  const base = new airtable({
   apiKey: context.AIRTABLE_API_KEY,
 }).base(context.AIRTABLE_BASE_ID);
 const twiml = new Twilio.twiml.MessagingResponse();
 const category = event.Body.toLowerCase().trim();
 let randArr = [];
 let randRecord;
 return base("climate orgs")
 .select()
 .all()
 .then((records) => {
   records.forEach((record) => {
     if (category.includes(String(record.get("category")))) { 
       randArr.push(record);
      } //if
    }); //records.forEach
    if(randArr.length > 0) {
      let randNum = Math.random()*randArr.length;
      randRecord = randArr[Math.floor(randNum)];
      twiml.message(`A random ${category} org. is ${randRecord.get("org")}. \n\n${randRecord.get("overview")} More at ${randRecord.get("website")}. \n\nThese are the categories you can text📲 to discover non-profits about: energy⚡️, transportation🚴🏻‍♀️, water💧, food+agriculture🐷.`);
      callback(null, twiml);
    }
    twiml.message(`Send one of these categories : energy, environment, climate, transportation, water, food+agriculture.`);
    callback(null, twiml);
   });
};
Enter fullscreen mode Exit fullscreen mode

This code imports the Airtable API, makes a Twilio Messaging Response object, creates a new Airtable object with our API key and base ID, loops through the records in our base under climate orgs, checks that the inbound message is in the Airtable base's categories column, and if it is, returns a random record corresponding to that category so the user can learn about a non-profit related to it.

You can view the complete app on GitHub here.

Configure the Function with a Twilio Phone Number

To open up our app to the web with a public-facing URL, go back to the ecofriendly-orgs-airtable-sms root directory and run twilio serverless:deploy. Once you see a Function URL ending in /sms, go to the phone numbers section of your Twilio Console and select the Twilio number you purchased and scroll down to the Messaging section. Under A MESSAGE COMES IN, change Webhook to Function and then under Service select ecofriendly-orgs-airtable-sms. For Environment select dev-environment, and then for Function Path select /sms. Click the Save button below and tada! You can now text your Twilio number a category and receive an eco-related non-profit organization relating to that category.
sms example

What's Next for the Planet and Twilio Serverless?

How are you celebrating Earth Month this April? How are you fighting climate change or making water clean or helping the planet? I plan on walking, taking public transit, and trying to do my part not just this month, but every month.

Twilio's Serverless Toolkit makes it possible to deploy web apps quickly, Twilio Runtime seamlessly handles servers for you, and Airtable makes storing and accessing data easy.

Let me know online what you're building with Serverless and/or Airtable!

Top comments (0)