Tweeting With Webscript

Thanks to Webscript's built-in support for OAuth, it's pretty easy to interact with the Twitter API from a script.

To demonstrate interacting with Twitter, I built an example that ties in with The Flatterist, which you may remember from our blog post over the weekend in which we integrated The Flatterist with Twilio.

The new example lets you send a compliment from The Flatterist to anyone on Twitter.

To call Twitter's API, you need OAuth credentials. Those typically come from authenticating a user with Twitter. In this case, I only need credentials for a single user (@flatterist), so I'm able to obtain those directly from dev.twitter.com by creating an application and then clicking "Create my access token."

In Webscript, you can use OAuth credentials in any HTTP call by constructing the right kind of Lua table. Here's a basic script that sends a tweet:

local TWITTER_CREDS = {
        oauth={
                consumertoken='<TWITTER CONSUMER KEY>',
                consumersecret='<TWITTER CONSUMER SECRET>',
                accesstoken='<TWITTER ACCESS TOKEN>',
                tokensecret='<TWITTER ACCESS TOKEN SECRET>'
        }
}

local tweet = 'Hello, World!'

local response = http.request {
        url = 'https://api.twitter.com/1.1/statuses/update.json',
        data = { status = tweet },
        auth = TWITTER_CREDS
}

We want to tweet something more interesting, so we'll accept a few parameters sent to us by the form at the bottom of www.flatterist.com: username, from, and id. The first two are just Twitter usernames (who's receiving the tweet and who caused it to be sent). The id refers to one of the compliments in The Flatterist's list. The following code finds the right compliment and puts together a full tweet:

local underscore = require 'underscore'

local username = request.form.username
local id = request.form.id
local from = request.form.from

-- fetch JSON-encoded compliments
local data = json.parse(http.request {
        url = 'http://www.flatterist.com/compliments.json'
}.content)

-- find the matching compliment
-- JSON for a compliment looks like ["<id>", "<compliment text>"]
local compliment = underscore.detect(data.compliments,
        function(compliment) return compliment[1] == id end)

local tweet = string.format(
        '@%s %s http://www.flatterist.com/#%s',
        username, compliment[2], id)
if from ~= nil then tweet = tweet .. ' (from @'..from..')' end

The last thing our script will do is redirect the user to the new tweet. Redirecting a user is just a matter of returning an HTTP 302 status code with an empty body and an appropriate Location header:

local response = http.request {
        url = 'https://api.twitter.com/1.1/statuses/update.json',
        data = { status = tweet },
        auth = TWITTER_CREDS
}

return 302, '', {
        Location = 'https://twitter.com/flatterist/status/'
        .. json.parse(response.content).id_str
}

You can find the full script on our examples page, and you can try it out by visiting The Flatterist.