My Last Tweet...Powered by Kynetx


Kynetx Logo

I've had a box on the right-hand side of this blog showing my lastest tweet. Today I realized that it would be better powered by Kynetx. I took a few minutes and wrote one up. You can see it on the right-hand side of this page.

To do this, I used the Twitter API to grab my tweets and then replaced an empty div on my page with it. Since Kynetx rules run each time the page is loaded, it's constantly updated.

The first step was to declare a datasource to read my tweets:

global {
 datasource tweets <-
   "http://<username>:<password>@twitter.com/statuses/user_timeline.json"
}

I've redacted my username and password for obvious reasons. If you substitute yours there, this rule will work for you (including getting your profile image).

The rule is pretty simple too:

  rule publish_tweets is active {
    select using ".*" setting ()

    pre {
      tweets = datasource:tweets("?a");
      res = tweets.pick("$.[0]..text");
      img = tweets.pick("$.[0]..profile_image_url");
      twit_res = <<
  <div style="width:165px;background-color:#39F;...
  <div style="height:47px;margin:3px;padding:3px;...    
        <a href="http://twtter.com/windley">
\t<img align="left" border="0" width="40px" src="#{img}"> 
        Phil on Twitter</a> </div>
   <div style="background-color:#039;margin:3px;...
    #{res}
   <div>
   <div style="text-align:right">
       <a style="text-size:7px;color:#AAA" 
          href="http://www.kynetx.com">
       Powered by Kynetx</a></div>
  </div>
  >>
    }
    replace_html("#tweets", twit_res);
  }

This grabs the latest tweets from the datasource that was declared, manipulates the JSON usingthee pick operator and JSONPath, the sticks it into an HTML template (everything between the << and the >> is the template). Finally, the action replaces an element with the ID tweets on my blog with the instantiated template.

One improvement to make to this rule would be to get the name and twitter URL from the tweetstream so that it's completely independent of me. Another would be to process the text to make the URLs live. More work for Jessie.

You might be thinking, "so how many people are going to go in and write this rule just to get a tweetbox on their blog?" That misses the point. The idea is that you could build an application like TwitStamp on top of Kynetx. We have an API for building rules so that your code could just generate the rule for someone and then give them the tags to put on their blog given a little information (like the Twitter name and password). Kynetx is a platform for building context automation applications.

If you'd like to play around with Kynetx, signup for a developer account...it's free. Contact me with any questions.