vADC Docs

TrafficScript can Tweet Too

by on ‎03-15-2013 11:26 AM - edited on ‎06-02-2015 12:08 PM by PaulWallace (1,666 Views)

Not content with getting your Traffic Manager to tweet through the event handing system (Traffic Managers can Tweet Too), this article explains how you can tweet directly from TrafficScript.

 

This is more than just a trivial example; sending tweets from the event handling system requires an 'action program', and the Stingray Virtual Appliance does not include the necessary third-party libraries to conduct the OAuth-authenticated transaction.  This example also illustrates how you can construct and perform OAuth authentication from TrafficScript.

 

Getting started - Create your Twitter 'Application'

 

Tp get started, you'll need to create a twitter 'Application' that will receive and process your twitter status updates.  Follow the instructions in the Traffic Managers can Tweet Too article, up to the point where you have an application with the two public/secret key pairs:

 

twitteroauth.png

The TrafficScript Rule

 

The following TrafficScript rule posts status updates using your application's and user's OAuth credentials.  The document Authorizing a request | Twitter Developers describes the process for authorizing a request using OAuth to the twitter service.

 

The rule is for test and demonstration purposes.  If you assign it to an HTTP virtual server (as a request rule), it will intercept all requests and look for a query-string parameter called 'status'.  If that parameter exists, it will post the value of status to twitter using the credentials in the rule.  Make sure to update the rule with the correct parameters from your twitter application.

 

The rule uses the TrafficScript HMAC library described in the document HowTo: Calculating HMAC hashes in TrafficScript.

 

# TrafficScript Twitter Client


import libHMAC.rts as hmac;


# Key client parameters

# https://dev.twitter.com/apps/<app-id>/oauth


$oauth_consumer_key = "Pplp3z4ogRW4wKP3YOlAA";

$oauth_token        = "1267105740-xhMWKdsNqoKAof7wptTZ5PmNrodBJcQm1tQ5ssR";


$consumer_secret    = "jbWlWYOSgzC9WXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

$oauth_token_secret = "p8GCJUZLXk1AeXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";


$method = "POST";

$url = "https://api.twitter.com/1/statuses/update.json";



#-------


$status = http.getFormParam( "status" );

if( ! $status ) {

   http.sendResponse( 200, "text/plain", "Please provide a status in the querystring (?status=...)", "" );

}


$oauth = [

   "oauth_consumer_key"     => $oauth_consumer_key,

   "oauth_nonce"            => string.base64encode( string.randomBytes( 32 )),

   "oauth_signature_method" => "HMAC-SHA1",

   "oauth_timestamp"        => sys.time(),

   "oauth_token"            => $oauth_token,

   "oauth_version"          => "1.0"

];


$pstring = "";

foreach( $k in array.sort( hash.keys( $oauth ))) {

   $pstring .= string_escapeStrict( $k ) . "=" . string_escapeStrict( $oauth[ $k ] ) . "&";

}

$pstring .= "status=". string_escapeStrict( $status );


$sbstring = string.uppercase( $method ) ."&" . string_escapeStrict( $url ) . "&" . string_escapeStrict( $pstring );

$skey     = string_escapeStrict( $consumer_secret ) . "&" . string_escapeStrict( $oauth_token_secret );


$oauth["oauth_signature"] = string.base64encode( hmac.SHA1( $skey, $sbstring ) );


$body = "status=". string_escapeStrict( $status );


$oauthheader = "OAuth ";

foreach( $k in hash.keys( $oauth )) {

   $oauthheader .= string_escapeStrict( $k ) . "=\"" . string_escapeStrict( $oauth[ $k ] ) . "\", ";

}

$oauthheader = string.drop( $oauthheader, 2 );


$r = http.request.post( $url, $body, "Authorization: ".$oauthheader, 10 );



$response = "Response code: ".$1." (".$4.")\n" .

   "Content Type: ".$2."\n" .

   "\n" .

   $r . "\n\n" .

   "I sent: POST " . $url ."\n" .

   $body . "\n\n" .

   "   pstring:   ".$pstring."\n".

   "   sbstring:  ".$sbstring."\n".

   "   skey:      ".$skey."\n".

   "   signature: ".$oauth["oauth_signature"]."\n\n".

   $oauthheader ."\n";


http.sendResponse( 200, "text/plain", $response, "" );



# %-encoding to the strict standards of RFC 3986

sub string_escapeStrict( $a ) {

   $r = "";

   while( string.length( $a ) ) {

      $c = string.left( $a, 1 ); $a = string.skip( $a, 1 );

      

      $i = ord( $c );

      if( ($i >= 0x30 && $i <= 0x39 ) 

            || ( $i >= 0x41 && $i <= 0x5A ) 

            || ( $i >= 0x61 && $i <= 0x7A )

            || $i == 0x2D || $i == 0x2E || $i == 0x5F || $i == 0x7E ) {

         $r .= $c;

      } else {

         $h = ($i & 0xF0 ) >> 4;

         $l = ($i & 0x0F );

         $r .= "%" . 

            string.substring( "0123456789ABCDEF", $h, $h ) . 

            string.substring( "0123456789ABCDEF", $l, $l );

      }

   }

   return $r;

}

 

Submit a request to this virtual server, and if all is well you should get a 200 status code response:

 

dotweet.png... along with your first 'Hello, World' from TrafficScript:

 

tweethello.png

 

One thing to note - twitter does rate-limit and de-dupe tweets from the same source, so repeatedly submitting the same URL without changing the query string each time is not going to work so well.

 

Good luck!

Contributors