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

Sharing is Caring

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

In this post, 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 the previous post). I suggest you go through earlier posts on Twitter4J API before reading this.

So Now we have all the keys and secrets now I have added one more input text in the 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 Direct Messages using Twitter4J API

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


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);
try {
twitter.sendDirectMessage(twitterHandleBind.getValue().toString(), tweetTextBind.getValue().toString());
} catch (TwitterException e) {
}else {
FacesMessage errMsg = new FacesMessage("Please Enter Twitter Handle and Text Message");
FacesContext.getCurrentInstance().addMessage(null, errMsg);

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

public RichInputText getTwitterHandleBind() {
return twitterHandleBind;

Now just run and check application

Twiter4J API

and in Twitter Direct Messages Box we can check it

Twitter4J API

Cheers 🙂 Happy Learning 

By the way, My Twitter Handle is – 😉 Follow @ashish__awasthi

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *