Please disable your adblock and script blockers to view this page

Search this blog

Thursday, 9 March 2017

Send Direct Message to followers using Twiter4J API in Oracle ADF and Java


My previous post was about tweeting using Twitter4J API from Oracle ADF Application . Twitter4J is a Java API that simplifies accessting twitter features in our application easily

In this I am going to show how to send DM (Direct Message) to anyone using their twitter handle and for this we need to use same consumer key, secret and access token, secret (How to access all these is described in previous post)



So Now we have all the keys and secrets now I have added one more input text in same application to input user's twitter handle and a button to send message


and believe me this is really simple , see the code in managed bean to send DM

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import oracle.adf.view.rich.component.rich.input.RichInputText;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;

import twitter4j.auth.AccessToken;

public class TweetUsingJavaAPIBean {

    //Component Binding of input text
    private RichInputText tweetTextBind;
    //Component Binding of twitter handle input text
    private RichInputText twitterHandleBind;

    public void setTweetTextBind(RichInputText tweetTextBind) {
        this.tweetTextBind = tweetTextBind;
    }

    public RichInputText getTweetTextBind() {
        return tweetTextBind;
    }

    public TweetUsingJavaAPIBean() {
    }

    //Put your app consumer key, secret and access token , secret here
    private static final String consumerKey = "K*******************X";
    private static final String consumerSecret = "u**************************************nRrD";
    private static final String token = "4*********************************************************7Vlg";
    private static final String tokenSecret = "H**************************************dy9";

 

    /**Send direct message to your followers using their twitter handle
     * @param actionEvent
     */
    public void sendDMTwitterAction(ActionEvent actionEvent) {
        if (twitterHandleBind.getValue() != null && tweetTextBind.getValue() != null) {
            Twitter twitter = new TwitterFactory().getInstance();
            twitter.setOAuthConsumer(consumerKey, consumerSecret);
            AccessToken accessToken = new AccessToken(token, tokenSecret);
            twitter.setOAuthAccessToken(accessToken);
            try {
                twitter.sendDirectMessage(twitterHandleBind.getValue().toString(), tweetTextBind.getValue().toString());
            } catch (TwitterException e) {
                e.printStackTrace();
            }
        }else {
            FacesMessage errMsg = new FacesMessage("Please Enter Twitter Handle and Text Message");
            errMsg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, errMsg);
        }
    }

    public void setTwitterHandleBind(RichInputText twitterHandleBind) {
        this.twitterHandleBind = twitterHandleBind;
    }

    public RichInputText getTwitterHandleBind() {
        return twitterHandleBind;
    }
}

Now just run and check application


and in Twitter DM Box we can check it



Cheers :) Happy Learning 

  By the way My Twitter Handle is - ;)

No comments :

Post a Comment