Example usage for twitter4j Twitter getMentionsTimeline

List of usage examples for twitter4j Twitter getMentionsTimeline

Introduction

In this page you can find the example usage for twitter4j Twitter getMentionsTimeline.

Prototype

ResponseList<Status> getMentionsTimeline() throws TwitterException;

Source Link

Document

Returns the 20 most recent mentions (tweets containing a users's @screen_name) for the authenticating user.<br> The timeline returned is the equivalent of the one seen when you view your mentions on twitter.com.<br> This method can only return up to 800 tweets.<br> See <a href="https://dev.twitter.com/docs/working-with-timelines">Working with Timelines</a> for instructions on traversing timelines.

Usage

From source file:com.ikungolf.java.javatwitter.GetMentions.java

License:Apache License

/**
 * Usage: java twitter4j.examples.timeline.GetMentions
 *
 * @param args String[]/*  w  ww  . j  a v a 2  s  . c om*/
 */
public static void main(String[] args) {
    // gets Twitter instance with default credentials
    Twitter twitter = new TwitterFactory().getInstance();
    try {
        User user = twitter.verifyCredentials();
        List<Status> statuses = twitter.getMentionsTimeline();
        System.out.println("Showing @" + user.getScreenName() + "'s mentions.");
        for (Status status : statuses) {
            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
        }
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get timeline: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:com.ikungolf.java.javatwitter.twitterCmd.java

public Status getLastestMessage() {
    String msg = new String();
    Status s = null;//from  w  w  w  .j  ava2  s.c o  m
    // gets Twitter instance with default credentials
    Twitter twitter = new TwitterFactory().getInstance();
    try {
        User user = twitter.verifyCredentials();
        List<Status> statuses = twitter.getMentionsTimeline();

        s = statuses.get(0);
        msg = s.getText();
        System.out.println("Message: " + s.getText());
        //            for (Status status : statuses) {
        //                System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
        //            }
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get timeline: " + te.getMessage());
        System.exit(-1);
    }

    return s;
}

From source file:kerguelenpetrel.RespondServlet.java

License:Apache License

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    StringBuilder builder = new StringBuilder();
    resp.setContentType("text/plain; charset=UTF-8");

    try {//from w  w w  .  j a v a 2  s  .  co  m
        //Get the Twitter object
        Twitter twit = TwitterFactory.getSingleton();
        ResponseList<Status> mentions = twit.getMentionsTimeline();

        lastPostIdEntity = datastore.get(KeyFactory.createKey("lastPostIDEntity", "ID"));
        lastPostId = Long.parseLong(lastPostIdEntity.getProperty("lastPostID").toString());

        if (mentions.size() == 0) {
            resp.getWriter().println("No mentions so far...\n");
            return;
        }

        for (Status mention : mentions) {
            if (lastPostId < mention.getId()) {
                if (mention.getUser().getId() == twit.getId())
                    ; //don't respond to myself

                else if (mention.isRetweeted())
                    mention = twit.createFavorite(mention.getId()); //mark the retweet as a favourite
                else if (mention.getText().toLowerCase().contains("bye")) {
                    builder.setLength(0);
                    builder.append("@");
                    builder.append(mention.getUser().getScreenName());
                    builder.append(" Bye");
                } else {
                    builder.setLength(0);
                    //Add the screen name of the person we are responding to
                    builder.append("@");
                    builder.append(mention.getUser().getScreenName() + " ");

                    //Get feed title as content
                    builder.append(getFeedTitle(resp));

                    //Get a Wordnik trend
                    builder.append(getWordnikTrend(resp));

                    /* Tweets are maximum 280 characters */
                    if (builder.length() > 280) {
                        builder.setLength(builder.lastIndexOf(" ", 270));
                        builder.append(end[(r.nextInt(end.length))]);
                    }
                }
                //Set the status
                StatusUpdate status = new StatusUpdate(builder.toString());

                //Post the status
                twit.updateStatus(status);
                resp.getWriter().println("Tweet posted: " + status.getStatus());
            }
        }
        //Save last post ID
        lastPostIdEntity.setProperty("lastPostID", (Long.toString(mentions.get(0).getId())));
        datastore.put(lastPostIdEntity);
    } catch (EntityNotFoundException e) {
        // Make new ResponseIDentity
        lastPostIdEntity = new Entity("lastPostIDEntity", "ID");
        lastPostIdEntity.setProperty("lastPostID", "0");
        datastore.put(lastPostIdEntity);
        resp.getWriter()
                .println("Made new lastPostId " + lastPostIdEntity.getProperty("lastPostID").toString());
    } catch (TwitterException e) {
        resp.getWriter().println("Problem with Twitter \n");
        e.printStackTrace(resp.getWriter());
    }
}

From source file:twitfeedbot.RespondServlet.java

License:Open Source License

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    ResponseList<Status> mentions;
    Twitter twit;
    StringBuilder builder = new StringBuilder();
    long lastPostId = 0;

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Entity lastPostIdEntity;/*  ww  w. j ava 2s.  c o  m*/

    resp.setContentType("text/html");
    resp.getWriter().println("<html>");
    resp.getWriter().println("<body>");
    try {
        ConfigurationBuilder twitterConfigBuilder = new ConfigurationBuilder();
        twitterConfigBuilder.setDebugEnabled(false);

        System.setProperty("WORDNIK_API_KEY", WORDNIK_KEY);
        twitterConfigBuilder.setOAuthConsumerKey(CONSUMER_KEY);
        twitterConfigBuilder.setOAuthConsumerSecret(CONSUMER_SECRET);
        twitterConfigBuilder.setOAuthAccessToken(ACCESS_TOKEN);
        twitterConfigBuilder.setOAuthAccessTokenSecret(ACCESS_SECRET);

        twit = new TwitterFactory(twitterConfigBuilder.build()).getInstance();
        mentions = twit.getMentionsTimeline();

        lastPostIdEntity = datastore.get(KeyFactory.createKey("lastPostIDEntity", "ID"));
        lastPostId = Long.parseLong(lastPostIdEntity.getProperty("lastPostID").toString());

        if (mentions.size() == 0 || mentions.get(0).getId() == lastPostId)
            resp.getWriter().println("No mentions so far...<br>");
        else {
            resp.getWriter().println("Responding to mentions...<br>");
            for (Status mention : mentions) {
                builder.setLength(0); // Clear the String Builder
                if (lastPostId < mention.getId()) {
                    //Figure out how to like a reply with certain words (best fuck etc)
                    if (mention.getUser().getId() == twit.getId())
                        ;//don't respond to myself
                    else if (mention.isRetweeted())
                        ; //don't respond to retweet
                    else if (mention.getText().toLowerCase().contains("bye")) // Say goodbye
                    {
                        builder.setLength(0); // Clear the String Builder 
                        builder.append("@").append(mention.getUser().getScreenName()).append(" Ok. Bye");
                    } else {
                        builder.setLength(0); // Clear the String Builder
                        builder.append("@").append(mention.getUser().getScreenName());
                        builder.append(" ");

                        //Append Wordnik example sentence
                        builder.append(WordApi.topExample(WordsApi.randomWord().getWord()).getText());
                        if (builder.length() > 140) {
                            if (builder.lastIndexOf(";", 110) > 0)
                                builder.setLength(builder.lastIndexOf(";", 110));
                            else if (builder.lastIndexOf(":", 110) > 0)
                                builder.setLength(builder.lastIndexOf(":", 110));
                            else if (builder.lastIndexOf(",", 110) > 0)
                                builder.setLength(builder.lastIndexOf(",", 110));
                            else
                                builder.setLength(110);
                        }
                    }
                    twit.updateStatus(builder.toString());
                    resp.getWriter().println("Reply posted: " + builder.toString() + "<br>");
                    builder.delete(0, builder.length()); //Clear the builder
                }
            }
            //Save last post ID
            lastPostIdEntity.setProperty("lastPostID", (Long.toString(mentions.get(0).getId())));
            datastore.put(lastPostIdEntity);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
        resp.getWriter().println("Input file(s) not found<br>");
        resp.getWriter().println("<pre>");
        e.printStackTrace(resp.getWriter());
        resp.getWriter().println("</pre>");
    } catch (EntityNotFoundException e) {
        resp.getWriter().println("lastPostID not found. Creating...<br>");
        lastPostIdEntity = new Entity("lastPostIDEntity", "ID");
        //lastPostIdEntity.setProperty("lastPostID", 0);
        lastPostIdEntity.setProperty("lastPostID", "883352596160946176");
        datastore.put(lastPostIdEntity);

    } catch (TwitterException e) {
        resp.getWriter().println("Problem with Twitter <br>");
        resp.getWriter().println("<pre>");
        e.printStackTrace(resp.getWriter());
        resp.getWriter().println("</pre>");
    } catch (KnickerException e) {
        e.printStackTrace(System.err);
        resp.getWriter().println("Problem with Wordnik <br>");
        resp.getWriter().println("<pre>");
        e.printStackTrace(resp.getWriter());
        resp.getWriter().println("</pre>");
    } catch (Exception e) {
        e.printStackTrace(System.err);
        resp.getWriter().println("<pre>");
        e.printStackTrace(resp.getWriter());
        resp.getWriter().println("</pre>");
    }
}