Sunday, November 28, 2010

OWB and Twitter

--

Integrating with Twitter may or may not be a need for a organization.
In this post we will look at  how to Tweet from OWB.
Tweeting using a simple TCL/JAVA script from OMB*Plus activity or a Java program using a JAVA activity in process flow. In short , tweets can be send from OMB*Plus prompt. Yes, it can be send. Well thats the fun part.

Twitter exposes API's to manipulate data.
Twitter also enforced OAuth authentication and recommends all client applications to use it.
How the OAuth works is described here and all the related terminology associated with OAuth can be found here.

If you have a Twitter Account , this is what you need to do..

1) Register a Twitter Application-

Signin into http://dev.twitter.com/ and choose option 2 - Register an App

Make sure you choose Application Type - "Client" as shown below.






































After registering , twitter will generate Consumer Key and Consumer Secret.













































2) Registering Twitter Application from OWB -

Current version of Java (1.6) does not have inbuild libraries to support OAuth authentication. Either end users have to write there own java libraries to get around OAuth and integrate with twitter api's or use open source libraries like jTwitter , scribe , twitter4j , etc.

In this post, I will be using twitter4j api's (twitter4j_latestversion.jar) which can be downloaded from here. Add the jar file to Oracle_Home/owb/lib/ext.

I have created an OWB expert , to register any twitter application for any twitter account. This will generate the Access Token and Token Secret required for integrating with twitter API's.

The expert can be downloaded from here.

Import the expert and you should see it under Global Navigator -> Public experts.






Execute the expert.

This will popup a window. Enter the cosumer key , cosumer secret and output file (to store the generated Access Token and Token Secret) and hit enter. There are other optional proxy parameters which can be set as per your environment, if needed.







































This will popup another window with an authentication URL.






















Copy the URL. Use any browser and grant the Twitter application access by logging with your twitter account.


























This will generate a PINCODE which is needed to generate Access Token and Token Secret.






















Goback to the executing Expert Window and Enter pincode and test mesaage (optional).




















Success Message will be displayed on successful generation of access token and token secret.

















The output file has the Access Token and Token Secret.

















In this case , a test message was entered. Signing back into Twitter displays the message.



































Note: This process of generating Access Token and Token Secret is needed only once. If the consumer key and consumer token are regenerated for twitter application, new access token and token secret needs to  be regenerated


3) Tweeting from Process flow -

In OWB11gR2 , external calls like executing a Java Class using Java Activity or TCL scripts using OMB*Plus activity can be performed within process flow.

In this post i will be demonstrating using OMB*Plus activity operator to execute a TCL/JAVA script to tweet messages.

You can also create a Java program and call it from Java activity in process flow.

A process flow with an OMB*Plus activity.















OMB*Plus Activity with variables defined and values set for all the variables - Consumer Key, Consumer Secret , Access Token , Token Secret , Message.





























In the Script parameter , use the following code

package require java

# send tweets using twitter4j api's

# set the fields using processflow variables
# required fields 
set consumerKey ${CONSUMER_KEY} 
set consumerSecret ${CONSUMER_SECRET} 
set accessToken ${ACCESS_TOKEN} 
set accessTokenSecret ${TOKEN_SECRET} 
set msg ${MESSAGE} 

#optional fields 
set proxyHost "" 
# if not using proxyHost default the value of proxyPort to zero 
set proxyPort 0
set proxyUsername "" 
set proxyPassword ""  

java::import twitter4j.Status 
java::import twitter4j.Twitter 
java::import twitter4j.TwitterFactory 
java::import twitter4j.conf.ConfigurationBuilder  

set cb [ java::new ConfigurationBuilder ] 
$cb setOAuthConsumerKey $consumerKey  
$cb setOAuthConsumerSecret $consumerSecret 
$cb setOAuthAccessToken $accessToken 
$cb setOAuthAccessTokenSecret $accessTokenSecret  

if { $proxyHost != "" } {   
       $cb setHttpProxyHost $proxyHost   
       $cb setHttpProxyPort $proxyPort 
    } 

if { $proxyUsername != "" }  {   
      $cb setHttpProxyUser $proxyUsername   
      $cb setHttpProxyPassword $proxyPassword 
   }   

set tf [ java::new TwitterFactory [ $cb build ] ] 
set twitter [ $tf getInstance ] 
set status [ $twitter updateStatus $msg ]  



If you want to put some pretty icon (your own or some free icon ) it can be done using Icon Set and OMB command. Thank you David Allan for Icon Set use.

OMBALTER PROCESS_FLOW 'YOUR_PROCESSFLOW' MODIFY ACTIVITY 'YOUR_OMBACTIVITY' SET REF ICONSET 'YOUR_ICONS'















Script can be modified further to do other things like getting new tweets , searching tweets , etc..but off course its limited to api's provided by twitter4j.
In my opinion its a complete library.


Now the fun part.

Tweeting from OMB*Plus prompt -

Use the script below and save it to some file say OWBTweet.tcl

# Send Tweets from OMBPlus using twitter4j api's
#
# Usage:
#   OMBCC '/YOUR_PROJECT'
#   source 
#   Example:
#   source "c:\\temp\\OWBTweet.tcl"
#
#   Syntax:
#   OMBTWEET  MSG
#   Example
#   OMBTWEET "HELLO TWITTER"
#
#   Set the ConumerKey , ConsumerSecret , TokenAccess and Token Secret in the code below.
#   Proxy Host , Proxy Port , Proxy Username and Proxy Password are optional
#
#
#
#
proc OMBTWEET {message} {

# required fields - set it with your values
set consumerKey yourconsumerkeyvalue
set consumerSecret yourconsumersecretvalue
set accessToken youraccesstokenvalue
set accessTokenSecret youraccesstokensecretvalue

#optional fields
set proxyHost ""
# if not using proxyHost default the value of proxyPort to zero
set proxyPort 0
set proxyUsername ""
set proxyPassword ""

java::import twitter4j.Status
java::import twitter4j.Twitter
java::import twitter4j.TwitterFactory
java::import twitter4j.conf.ConfigurationBuilder

set cb [ java::new ConfigurationBuilder ]
$cb setOAuthConsumerKey $consumerKey 
$cb setOAuthConsumerSecret $consumerSecret
$cb setOAuthAccessToken $accessToken
$cb setOAuthAccessTokenSecret $accessTokenSecret

if { $proxyHost != "" } { 
  $cb setHttpProxyHost $proxyHost
  $cb setHttpProxyPort $proxyPort
}

if { $proxyUsername != "" } { 
  $cb setHttpProxyUser $proxyUsername
  $cb setHttpProxyPassword $proxyPassword
}

set tf [ java::new TwitterFactory [ $cb build ] ]
set twitter [ $tf getInstance ]
set msg $message
set status [ $twitter updateStatus $msg ]
puts "Message Tweeted Successfully."
}


Change it as per your environment. Source the script and execute the command.












If you want to source the srcipt as command its worth looking at Oleg's blogpost over here.

--

1 comment: