Create SMS Business Cards with Twilio and Node.js

December 17, 2018
Written by
Brandon Sherman
Contributor
Opinions expressed by Twilio contributors are their own

Exchanging phone numbers

After I gave a talk at Shakacon this year, I was pleased that people came up to me with additional questions—pleased, that is, until someone asked me for a business card and I realized I didn’t have any with me. Whoops.

In this quick post, I’ll describe how I solved that problem by creating an app that sends business cards over SMS and I’ll show how you can do it too.

Why Build an SMS Business Card

I wanted to create something quickly that would let me share information selectively; not everyone should have my cell phone number. It had to be flexible and extensible. It had to solve the problem with the resources I had on-hand. That meant my laptop, a power adapter, and a Twilio account were my raw materials.

This app really showcases the power of Twilio and what’s possible with our APIs.  I thought through the best way to solve my problem, and quickly decided to build an app that would listen for keywords sent in via SMS then respond with my contact information.  

For example, if you text me the name of the conference where we’ve met, I can send back the appropriate information for you tailored to that conference. The app gives me complete control over which information I send to different people. The keyword and phone number also fit nicely on a slide so people can easily see how to contact me (without, say, having to take a picture of the contact info in my presentation).

Build Your Own SMS Business Card with Node.js

To make this work you’ll need a Twilio account and a Twilio phone number, so you should go get those first. I’ll wait.

The Node.js code that makes this work is below.

When my function gets called by an inbound SMS handler, it first creates a TwiML MessagingResponse object. The case statement then creates the appropriate response based on the keyword received. This approach makes it simple to add new keywords and corresponding responses. Finally, the MessagingResponse is returned and finds its way to the recipient.

/*
   After you have deployed your Function, head to your phone number and configure the inbound SMS handler to point at this Function
*/
exports.handler = function(context, event, callback) {
    let twiml = new Twilio.twiml.MessagingResponse();
    let msg = "Brandon Sherman\nTech Lead, Cloud Security Engineer @ Twilio\nbsherman@example.com\n";
    const body = event.Body ? event.Body.toLowerCase() : null;
    switch (body) {
        case 'help':
            twiml.message("You can text the conference name to get my contact info");
            break;
        case 'shakacon':
            twiml.message(msg+"shakacon@example.com");
            break;
        case 'reinvent':
        case 're:invent':
            twiml.message(msg+'reinvent@example.com');
            break;
        case 'derbycon':
            twiml.message(msg+'derbycon@example.com')
            break;
        case 'blog':
            twiml.message(msg);
            break;
        default:
            twiml.message("Sorry, I don't understand you.  Try texting the conference name as input.");
            break;
    }

    callback(null, twiml);
};

For more examples of SMS responses, check out TwiML™ for Programmable SMS on the Twilio Docs.  You can build a very complicated app with our APIs— this app hardly scratches the surface of our capabilities.

Expanding the SMS Business Card App

I was able to extend this app very quickly at DerbyCon. It was easy to use, gave people a way to get in touch with me, and also let me track who reached out. As a result, I have had several interesting conversations I might not have had with people who used the app to contact me after the conference. 

It turned out to be a stroke of luck that I forgot my business cards.  Now I have an SMS Business Card app that is like having a custom business card for every conference I attend!

Imagine the possibilities once you get comfortable with the different ways Twilio lets you send SMS responses: you can build your own smart response bot, create a custom group messaging service, and find other ways to be more productive. I would love to see what you build.

Have your own business card app? Want to show it to me? Get my card by texting blog to 1.916.473.2524!

Brandon has been working with AWS infrastructure for four years and is a Senior Cloud Infrastructure engineer at Twilio, where the challenge of real-time cloud communications requires thinking about security in new and exciting ways. He wants to replace himself with microservices & APIs but until he manages to do that, you'll find him teaching anyone who will listen that they can be a "security person" too.