How To Create GIF Polls With Wedgies and Twilio MMS

October 08, 2014
Written by

Wedgies Logo

twilio-mms
Wedgies delivers polls wherever you need them. That could mean a scoreboard poll at the NCAA finals, a reader poll on USAToday.com, or a nice little embedded poll on Tumblr. The folks at Wedgies have been there and done that. Now they’re bringing their gif-laden polls to the small screen – your phone.

In this post, Wedgies all-star/co-founder Jimmy Jacobson will show you how to use Twilio MMS create your own poll that you can share and vote on, all with a simple text.

Check out Jimmy’s tutorial below and fork it here.

 

Creating a GIF Poll using Twilio MMS and Wedgies

Store your Wedgies API key in an environment variable and reference in a global variable.
 

var WEDGIES_API_KEY = process.env.WEDGIES_API_KEY;

 

This is the express route to handle the incoming Twilio SMS
 

app.post('/', function(req, res, next) {

 

This function is defined below, and returns a Wedgies Poll from the Wedgies API or an error object that can be passed to the Express function next.
 

getWedgiePoll(function(error, poll) {
        if (error) {
            return next(error);
        }

 

Make sure your Twilio credentials are stored in environment variables.
 

var client = new twilio.RestClient(process.env.TWILIO_ACCOUNT_SID,
        process.env.TWILIO_AUTH_TOKEN);

 

Prepare the MMS message with a to, body and from fields.
 

// Now, get the parameters POSTed from our form:
        var twilioMessage = {
            to: req.body.From,
            body: poll.text + " " + poll.urls.page,
            from: process.env.TWILIO_NUMBER
        };

 

If the Wedgies poll has an image, make it an MMS!

if (poll.images && poll.images.questionImage) {
            twilioMessage.mediaUrl = poll.images.questionImage.url;
        }

 

Send the MMS using Twilio’s awesome NodeJS client library

// Now let's send the message! This is going back to Twilio, not to your user
        client.sendMessage(twilioMessage, function(error, messageData) {
            if (error) {
                // Remember, errors are not strings
                var err = new Error(error);
                err.status = 500;
                return next(err);
            } else {
                res.status = 201;
                res.send('Message sent! SID: ' + messageData.sid);
            }
        });
    });
});

 

This is the function that hits the Wedgies API and returns a poll
 

function getWedgiePoll(callback) {

 

Here is actual API call using Request

var requestOptions = {
        url: "https://api.wedgies.com/question?limit=1",
        headers: {
            "wedgies-api-key": WEDGIES_API_KEY
        },
        json: true
    };

    request.get(requestOptions, function(error, response, body) {

 

This is a pet peeve of mine. Use objects for errors, not strings. Here we handle the case where the request flat out fails. This could be due to a bad URL or similar.

if (error) {
            // An error is not a string, let's use an error object
            var err = new Error(error);
            err.status = 500;
            return callback(err);
        }

 

Here we handle the case where the Wedgies API call is successful, but the response code is something other than 200

if (response.statusCode != 200) {
            var err = new Error("Not 200, Not OKAY");
            err.status = response.statusCode;
            return callback(err);
        }

 

Finally, everything looks good, return the poll.

return callback(null, body);
    });
}

 

That’s it! Take a look a deeper dive into Wedgies’ platform here