Geeky Nuggets

A Phing task to update your Twitter status

June 29, 2011 | 8 Minute Read

Phing and Twitter

I recently starting working with phing to automate the build and deployement process for the web apps I’m building. I use it to compile my LESS files to CSS and minimize then, to compress and concatenate the javascript files, to optimize images using smushit, etc…

I like to be able to comunicate exactly what I’m doing, and thankfully Phing has a built in task to email a message to a list of recipients, which is good, but slightly old school. I wanted something that would be able to integrate readily with twitter, i.e. update my status when a build was completed. I saw this post, which unfortunately uses Basic Auth. As you doubtless know, Twitter deprecated this form of authentication, and now asks all users to authenticate using OAuth.

Download twitterOAuth library

That meant I had to roll my own phing task… However, thanks to great work by cleverer people, doing it was quite simple. Using Abraham Williams’ twitteroauth library means that the coding I’d have to do was greatly reduced. Awesome, because I aim to be lazy.

The first step is to download the twitteroauth library from github. Extract it to a temporary folder and copy the subfolder

twitteroauth

to the folder where your fing tasks are stored (on my system it is

/usr/share/php/phing/tasks/my/

. Please note you might have to create the

my/

folder within

/usr/share/php/phing/tasks/

. You will need root privileges for these steps.

Register application with Twitter

Secondly, you need to register an application with twitter, as you will need a consumer key and consumer secret later on. Please do so now, and come back once you have your app data available.

Create the custom phing task

Next up is creating the actual task PHP class. Create an empty file named TwitterUpdateTask.php

sudo touch /usr/share/php/phing/tasks/my/TwitterUpdateTask.php

and open it for editing with your favorite editor. We’ll use gedit :

gksu gedit /usr/share/php/phing/tasks/my/TwitterUpdateTask.php

The code for this file is as shown below:

    <?php
    require_once ("phing/Task.php");
    require_once ("phing/tasks/my/twitteroauth/twitteroauth.php");
    /* get the lines below from http://twittertokens.6px.eu/ */
    define('CONSUMER_KEY', 'Your Consumer Key Here');
    define('CONSUMER_SECRET', 'Your Consumer Secret Here');
    define('OAUTH_TOKEN', 'Your OAuth Token Here');
    define('OAUTH_TOKEN_SECRET', 'Your Oauth Token Secret Here');
    class TwitterUpdateTask extends Task {
        private $message;
        /*
         * The setter for the status message
         */
        public function setMessage($str){
            $this->message=$str;
        }
        public function main(){
            /* Connect to twitter */
            $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET);
            /* Pass the status message as a parameter */
            $parameters = array('status' => $this->message);
            /* Post the data to the API endpoint */
            $status = $connection->post('statuses/update', $parameters);
            if (isset($status->error)){
                /* if there is an error, fail the build */
                throw new BuildException($status->error);
            }else{
                /* if there is no error, show a success message */
                $this->log ("Status posted to twitter");
            }
        }
    }

Get the OAuth token and secret

Don’t close this file, we still need to add the authentication data to it. You can already place your consumer key and consumer secret in the apropriate

define()

calls (the first two lines), but if not, we’ll do it now.

Navigate to http://twittertokens.6px.eu, put your consumer data and consumer secret in the apropriate fields and click on “Sign in with Twitter”. A twitter page will open asking for your confirmation. Click “Sign in” and you are redirected to http://twittertokens.6px.eu/. You should see 4 lines of code appear, that look like this:

    define('CONSUMER_KEY', 'V1UsnZJrgfhgKJFoqsQ');
    define('CONSUMER_SECRET', 'UUazcBfXrWW1jcpiSU564hg654t1EMki8gzptQU');
    define('OAUTH_TOKEN', '325454656-EuCudghg8tTYwKf9yjt5nhqr14i5egHJPeVRGVxQv');
    define('OAUTH_TOKEN_SECRET', 'gh6854hg6tyGEvGEiEi15XmUtOmDpaONM');

Copy this code to the corresponding spot in the TwitterUpdateTask.php file, overwriting what’s already there.

Please note though that I’m not making any claims as to how secure this is or whatever. I don’t store any of your data anywhere, but if sending your application consumer token and secret worries you, find another way to get the Oauth tokens.

Create the build file

First, we define the custom task:

<taskdef name="twitterupdate" classname="phing.tasks.my.TwitterUpdateTask" />

Secondly, let’s create a custom target that will send a tweet with the message we want.

    <target name="tweet">
        <twitterupdate message="${twitter.status}" />
    </target>

Now we can call this task from another one, but we need to make that the twitter.status is set. Let’s we have a “staging” target. Part of it could look like this:

    <!-- Set the timestamp to be used in the twitter update -->
    <tstamp>
        <format property="build.time" pattern="%Y-%m-%d %H:%I" />
    </tstamp>
    <property name="twitter.status" value="Staging build completed at ${build.time}" />
    <phingcall target="tweet" />

This will post the message to twitter, replacing the

${build.time}

token by the actual build and time.

Do you use Phing for your webapp build and deployment? If so, please share any custom tasks you might have created.