Show me the code! – By Davanum Srinivas

August 27, 2008

Porting Twitter script for Ubiquity to BlueTwit (internal twitter clone)

Filed under: Uncategorized — Davanum Srinivas @ 11:01 am

Turned out to be a piece of cake! (Note: replace xyz@us.ibm.com with your internal ibm id). Just open the command editor (chrome://ubiquity/content/editor.html), cut and paste this code below and run the bluetwit command.

const BLUETWIT_STATUS_MAXLEN = 160;

CmdUtils.CreateCommand({
  name: "bluetwit",
  takes: {status: noun_arb_text},
  
  preview: function(previewBlock, statusText) {
    var previewTemplate = "Updates your BlueTwit status to: <br/>" +       
                          "<b>${status}</b><br /><br />" + 
                          "Characters remaining: <b>${chars}</b>";
    var truncateTemplate = "<br />The last <b>${truncate}</b> " + 
                           "characters will be truncated!";
    var previewData = {
      status: statusText.text,
      chars: BLUETWIT_STATUS_MAXLEN - statusText.text.length
    };
      
    var previewHTML = CmdUtils.renderTemplate(previewTemplate,
                                                    previewData);
    
    if(previewData.chars < 0) {
      var truncateData = {
        truncate: 0 - previewData.chars
      };
      
      previewHTML += CmdUtils.renderTemplate(truncateTemplate,
                                                   truncateData);
    }
    
    previewBlock.innerHTML = previewHTML;
  },
  
  execute: function(statusText) {
    if(statusText.text.length < 1) {
      displayMessage("BlueTwit requires a status to be entered");
      return;
    }
    
    var updateUrl = "https://bluetwit.hursley.ibm.com/BlueTwit2/xyz@us.ibm.com";
    
    jQuery.ajax({
      type: "POST",
      url: updateUrl,
      data: "comment=" + statusText.text,
      dataType: "xml",
      error: function() {
        displayMessage("BlueTwit error - status not updated");
      },
      success: function() {
        displayMessage("BlueTwit status updated");
      }
    });
  }
});

4 Comments »

  1. I seem to get an error message but post is successful.

    Comment by ragtag — September 2, 2008 @ 5:06 pm

  2. [...] Embraces Social Media, BusinessWeek by Stephen Baker Social Networking: The Twitterverse Debates Porting Twitter Script for Ubiquity to BlueTwit Twitter Behind the Firewall (Photos on Flickr) [...]

    Pingback by Enterprise Microsharing Apps: Read All About Em | Pistachio — November 3, 2008 @ 3:07 am

  3. Works for me…the ‘success’ messages don’t work for me but it posts so what the heck. Thanks very much for the code.

    Comment by emilyobyrne — January 6, 2009 @ 7:48 am

  4. Was able to eliminate the error seen by changing jQuery.ajax call data return type to “text” and not “xml” since the return is not xml. :)

    Comment by John — February 3, 2009 @ 11:38 am


RSS feed for comments on this post. TrackBack URI

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 )

Connecting to %s

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 44 other followers