A Phing task to update your Twitter status
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
to the folder where your fing tasks are stored (on my system it is
. Please note you might have to create the
folder within
. 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
and open it for editing with your favorite editor. We’ll use gedit :
The code for this file is as shown below:
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
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:
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:
Secondly, let’s create a custom target that will send a tweet with the message we want.
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:
This will post the message to twitter, replacing the
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.