Building an IM Bot for the NY Senate OpenLeg API

The NY Senate made an exciting announcement today – a host of new open source software and services are being deployed to enhance transparency in what the Senate does.

This is big news, and a major tip of the hat goes to the NY Senate CIO, Andrew Hoppin, and his staff. They are doing amazing work in advancing transparency in the Senate – an organization that has traditionally been as closed as closed can get.

The announcement by the NY Senate has many different components, but the one I find most exciting right out of the gate is the release of an API to query the Senate bill tracking system. I’ve had an idea for a while now to do an Instant Messaging (IM) application that would let someone query the status of a bill, and get a bill summary, using an IM client.

Because the bill tracking system is now exposed via an API, building this kind of application is easy — so I went ahead and did it. Alltogether it took about 2 hours to build, test and deploy.

The code for my sample application can be downloaded here.

You can test this application out by adding opensenate@bot.im to your Jabber IM client (Google chat will work just fine). If you send an IM with a Senate Bill number (e.g., S2111) you will get a response with the bill sponsor, summary and the last action taken on the bill.

Here is what I used to build this application:

  • The NY Senate Open Leg Service API (note – at the time of this writing, the API documentation appears to be offline. Hope this is restored soon.)
  • PHP (however, since we’re justing hitting a REST API using HTTP, there are a host of different languages that will do).
  • The IMified platform – more on IMified here.
  • A simple PHP class for building IM applications – more on this class, and a primer on building IM apps here.

That’s it! Its admittedly simple, and there is no real error handling in the app just yet, but if you give it a live bill number it works just fine. I’ll probably do some more work to build this example app out as the Open Leg Service API matures.

Now you can get the status of a bill and summary information quickly and easily using an IM client. This is just a quick example of what can be done when an organization like the NY Senate has forward thinking IT leadership, that embraces open standards, open source and open information.


UPDATE: Twitter support added to NY Senate Bill Status Bot.

Because of some awesome new functionality added to the IMified platform, I’ve added Twitter support for the NY Senate bot. Now you can get the bill sponsor and bill status by sending the bill ID (e.g., S2111) to @opensenate.

@opensenate S2111

I’ve also updated the code sample provided above to reflect the changes to the IMified API. This significantly enhances the usefulness of the application, and allows you to turn inquiry into advocacy (can you say Retweet!). I’ll be writing more about this shortly but for now I am working on some minor tweaks to the sample bot.

Enjoy!

UPDATE II: New GitHub Repo

The latest code is now up on GitHub.

9 comments

  1. Mark Headd · June 18, 2009

    Hmmm. Looks like the API might be down.

    If you run into any problems testing this app, bear in mind that the API is still in Beta.

  2. Nathan Freitas · June 18, 2009

    The API is back up now… apologies for the outages here and there. Still getting our production server issues ironed out.

    Thanks for building this AWESOME bot. You are officially the first external API user. That deserves some sort of prize!

  3. Mark Headd · June 18, 2009

    The work you guys at the NY Senate are doing is important, and deserves the attention of the developer community (which I have no doubt you’ll get).

    I’d like to work on a voice-enabled telephone app in the near future, and perhaps develop more formal collections of class libraries for interacting with the API (PHP and C#).

    I’m inspired by what you guys are doing — can’t wait to see what you guys have planned next!

  4. Dave Hoff · June 18, 2009

    Very cool Mark! Thanks for choosing IMified to demo the API integration and for sharing your code.

  5. Nathan Freitas · June 24, 2009

    API docs are posted here, fyi: http://wiki.github.com/nysenatecio/OpenLeg

    Nice work on the Twitter support. I am working on something along those lines, but perhaps you can add it… i want to post all bill actions out to a Twitter account as they happen (or as we get them each hour). Can IMified handle a “push” type of feed as opposed to a query?

  6. Nathan Freitas · June 24, 2009

    Also, when the bot tweets back the info on the bill, you should also include the bill permalink in the response:

    http://open.nysenate.gov/openleg/api/html/bill/Sxxxx etc

  7. Mark Headd · June 24, 2009

    Nathan:

    I think a push type of interaction might best be handled by the Twitter API itself – http://apiwiki.twitter.com/Twitter-API-Documentation. Probably no need for IMified for this part.

    As you get the bill status changes, they can be added to the @opensenate Twitter account (or the account of your choice) via a simple status update API call. Anyone following @opensenate would see the updated bill status. Additionally, you could add #hashtags to make it easier for followers and others to pick out the ones of importance to them (e.g., #s21 status changed to xxx).

    I had thought about adding a permalink to the bill in the @opensenate Tweets, and plan on doing so shortly. I will probably want to make a call to a URL shortening service, to reduce the number of characters to the bill permalink, before adding it to the Tweet. I’ve used the cli.gs API (http://cli.gs/#api) for this in the past. Probably won’t be able to do it until this weekend though – we’re at the end of a sprint at my day job and we’re closing out the last few items before sprint review.

    I’ve done both of the above (invoking the Twitter API to update status, and shortening a URL for posting in a Tweet) on previous projects. You may notice that the code sample I include in the post has a base class for REST-based API invocations.

    This base class can be easily extended to do both of the above. Makes it really easy to add new functionality to the NY Senate bot!

    I’ll post back here and hit you up with a @reply when I’ve had a chance to add the permalink to the @opensenate Tweets.

    What are your thoughts on just using the Twitter API to crank out status updates when the status of the bills change?

  8. Dave Hoff · June 29, 2009

    Nathan, Mark,

    Our API will support pushing messages to users in the form of an @reply or DM currently, but we do intend on adding a broadcast type push that will update your bots status as your looking to do. We’ll let you know when that’s in place.

  9. Mark Headd · June 29, 2009

    Sweet! Thanks, Dave!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s