Example usage for twitter4j DirectMessage getText

List of usage examples for twitter4j DirectMessage getText

Introduction

In this page you can find the example usage for twitter4j DirectMessage getText.

Prototype

String getText();

Source Link

Usage

From source file:br.com.porcelli.hornetq.integration.twitter.support.TweetMessageConverterSupport.java

License:Apache License

public static ServerMessage buildMessage(final String queueName, final DirectMessage dm) {

    final ServerMessage msg = new ServerMessageImpl(dm.getId(),
            InternalTwitterConstants.INITIAL_MESSAGE_BUFFER_SIZE);
    msg.setAddress(new SimpleString(queueName));
    msg.setDurable(true);/*from   www .  java2 s .co  m*/

    msg.putStringProperty(TwitterConstants.KEY_MSG_TYPE, MessageType.DM.toString());

    msg.putStringProperty(TwitterConstants.KEY_ID, read(dm.getId()));
    msg.putStringProperty(TwitterConstants.KEY_TEXT, read(dm.getText()));
    msg.putStringProperty(TwitterConstants.KEY_CREATED_AT, read(dm.getCreatedAt()));

    if (dm.getSender() != null) {
        buildUserData(InternalTwitterConstants.KEY_USER_SENDER_PREFIX, dm.getSender(), msg);
    }

    if (dm.getRecipient() != null) {
        buildUserData(InternalTwitterConstants.KEY_USER_RECIPIENT_PREFIX, dm.getRecipient(), msg);
    }

    msg.putStringProperty(TwitterConstants.KEY_RAW_JSON, dm.toString());

    return msg;
}

From source file:cd.examentwitter.Metodos.java

/**
 * Metodos para mandar un tuit privado a otro usuario de la red
 *//*  ww  w  . j av a 2 s . c o m*/
public void mp() {

    try {
        String name = Metodos.recipientId();
        String mp = Metodos.tuit();

        DirectMessage message = twitter.sendDirectMessage(name, mp);

        System.out.println("Sent: " + message.getText() + " to @" + message.getRecipientScreenName());

    } catch (TwitterException ex) {
        Logger.getLogger(Metodos.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.daiv.android.twitter.utils.TweetLinkUtils.java

License:Apache License

public static String[] getLinksInStatus(DirectMessage status) {
    UserMentionEntity[] users = status.getUserMentionEntities();
    String mUsers = "";

    for (UserMentionEntity name : users) {
        String n = name.getScreenName();
        if (n.length() > 1) {
            mUsers += n + "  ";
        }/* ww  w. java2 s .  co m*/
    }

    HashtagEntity[] hashtags = status.getHashtagEntities();
    String mHashtags = "";

    for (HashtagEntity hashtagEntity : hashtags) {
        String text = hashtagEntity.getText();
        if (text.length() > 1) {
            mHashtags += text + "  ";
        }
    }

    URLEntity[] urls = status.getURLEntities();
    String expandedUrls = "";
    String compressedUrls = "";

    for (URLEntity entity : urls) {
        String url = entity.getExpandedURL();
        if (url.length() > 1) {
            expandedUrls += url + "  ";
            compressedUrls += entity.getURL() + "  ";
        }
    }

    MediaEntity[] medias = status.getMediaEntities();
    String mediaExp = "";
    String mediaComp = "";
    String mediaDisplay = "";

    for (MediaEntity e : medias) {
        String url = e.getURL();
        if (url.length() > 1) {
            mediaComp += url + "  ";
            mediaExp += e.getExpandedURL() + "  ";
            mediaDisplay += e.getDisplayURL() + "  ";
        }
    }

    String[] sExpandedUrls;
    String[] sCompressedUrls;
    String[] sMediaExp;
    String[] sMediaComp;
    String[] sMediaDisply;

    try {
        sCompressedUrls = compressedUrls.split("  ");
    } catch (Exception e) {
        sCompressedUrls = new String[0];
    }

    try {
        sExpandedUrls = expandedUrls.split("  ");
    } catch (Exception e) {
        sExpandedUrls = new String[0];
    }

    try {
        sMediaComp = mediaComp.split("  ");
    } catch (Exception e) {
        sMediaComp = new String[0];
    }

    try {
        sMediaExp = mediaExp.split("  ");
    } catch (Exception e) {
        sMediaExp = new String[0];
    }

    try {
        sMediaDisply = mediaDisplay.split("  ");
    } catch (Exception e) {
        sMediaDisply = new String[0];
    }

    String tweetTexts = status.getText();

    String imageUrl = "";
    String otherUrl = "";

    for (int i = 0; i < sCompressedUrls.length; i++) {
        String comp = sCompressedUrls[i];
        String exp = sExpandedUrls[i];

        if (comp.length() > 1 && exp.length() > 1) {
            String str = exp.toLowerCase();

            tweetTexts = tweetTexts.replace(comp,
                    exp.replace("http://", "").replace("https://", "").replace("www.", ""));

            if (str.contains("instag") && !str.contains("blog.instag")) {
                imageUrl = exp + "media/?size=m";
                otherUrl += exp + "  ";
            } else if (str.contains("youtub") && !(str.contains("channel") || str.contains("user"))) { // normal youtube link
                // first get the youtube video code
                int start = exp.indexOf("v=") + 2;
                int end = exp.length();
                if (exp.substring(start).contains("&")) {
                    end = exp.indexOf("&");
                } else if (exp.substring(start).contains("?")) {
                    end = exp.indexOf("?");
                }
                imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, end) + "/hqdefault.jpg";
                otherUrl += exp + "  ";
            } else if (str.contains("youtu.be")) { // shortened youtube link
                // first get the youtube video code
                int start = exp.indexOf(".be/") + 4;
                int end = exp.length();
                if (exp.substring(start).contains("&")) {
                    end = exp.indexOf("&");
                } else if (exp.substring(start).contains("?")) {
                    end = exp.indexOf("?");
                }
                imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, end) + "/hqdefault.jpg";
                otherUrl += exp + "  ";
            } else if (str.contains("twitpic")) {
                int start = exp.indexOf(".com/") + 5;
                imageUrl = "http://twitpic.com/show/full/" + exp.substring(start).replace("/", "");
                otherUrl += exp + "  ";
            } else if (str.contains("imgur") && !str.contains("/a/")) {
                int start = exp.indexOf(".com/") + 6;
                imageUrl = "http://i.imgur.com/" + exp.substring(start) + "l.jpg";
                imageUrl = imageUrl.replace("gallery/", "").replace("a/", "");
                otherUrl += exp + "  ";
            } else if (str.contains("pbs.twimg.com")) {
                imageUrl = exp;
                otherUrl += exp + "  ";
            } else if (str.contains("ow.ly/i/")) {
                Log.v("Test_owly", exp);
                imageUrl = "http://static.ow.ly/photos/original/"
                        + exp.substring(exp.lastIndexOf("/")).replaceAll("/", "") + ".jpg";
                otherUrl += exp + "  ";
            } else if (str.contains(".jpg") || str.contains(".png")) {
                imageUrl = exp;
                otherUrl += exp + "  ";
            } else if (str.contains("img.ly")) {
                imageUrl = exp.replace("https", "http").replace("http://img.ly/", "http://img.ly/show/large/");
                otherUrl += exp + "  ";
            } else {
                otherUrl += exp + "  ";
            }
        }
    }

    for (int i = 0; i < sMediaComp.length; i++) {
        String comp = sMediaComp[i];
        String exp = sMediaExp[i];

        if (comp.length() > 1 && exp.length() > 1) {
            tweetTexts = tweetTexts.replace(comp, sMediaDisply[i]);
            imageUrl = status.getMediaEntities()[0].getMediaURL();
            otherUrl += sMediaDisply[i];
        }
    }

    return new String[] { tweetTexts, imageUrl, otherUrl, mHashtags, mUsers };
}

From source file:com.dwdesign.tweetings.model.ParcelableDirectMessage.java

License:Open Source License

public ParcelableDirectMessage(final DirectMessage message, final long account_id, final boolean is_gap) {
    this.account_id = account_id;
    this.is_gap = is_gap;
    final User sender = message.getSender(), recipient = message.getRecipient();
    message_id = message.getId();//from  ww w  .  j av a  2  s  .c  o m
    message_timestamp = getTime(message.getCreatedAt());
    sender_id = sender != null ? sender.getId() : -1;
    recipient_id = recipient != null ? recipient.getId() : -1;
    text = message.getText();
    sender_name = sender != null ? sender.getName() : null;
    recipient_name = recipient != null ? recipient.getName() : null;
    sender_screen_name = sender != null ? sender.getScreenName() : null;
    recipient_screen_name = recipient != null ? recipient.getScreenName() : null;
    sender_profile_image_url = sender != null ? sender.getProfileImageURL() : null;
    recipient_profile_image_url = recipient != null ? recipient.getProfileImageURL() : null;
}

From source file:com.dwdesign.tweetings.util.Utils.java

License:Open Source License

public static ContentValues makeDirectMessageContentValues(final DirectMessage message, final long account_id) {
    if (message == null || message.getId() <= 0)
        return null;
    final ContentValues values = new ContentValues();
    final User sender = message.getSender(), recipient = message.getRecipient();
    if (sender == null || recipient == null)
        return null;
    values.put(DirectMessages.ACCOUNT_ID, account_id);
    values.put(DirectMessages.MESSAGE_ID, message.getId());
    values.put(DirectMessages.MESSAGE_TIMESTAMP, message.getCreatedAt().getTime());
    values.put(DirectMessages.SENDER_ID, sender.getId());
    values.put(DirectMessages.RECIPIENT_ID, recipient.getId());
    values.put(DirectMessages.TEXT, message.getText());
    values.put(DirectMessages.SENDER_NAME, sender.getName());
    values.put(DirectMessages.SENDER_SCREEN_NAME, sender.getScreenName());
    values.put(DirectMessages.RECIPIENT_NAME, recipient.getName());
    values.put(DirectMessages.RECIPIENT_SCREEN_NAME, recipient.getScreenName());
    final URL sender_profile_image_url = sender.getProfileImageURL();
    final URL recipient_profile_image_url = recipient.getProfileImageURL();
    if (sender_profile_image_url != null) {
        values.put(DirectMessages.SENDER_PROFILE_IMAGE_URL, sender_profile_image_url.toString());
    }/*w  w w  .  j av  a 2  s  .  c  om*/
    if (recipient_profile_image_url != null) {
        values.put(DirectMessages.RECIPIENT_PROFILE_IMAGE_URL, recipient_profile_image_url.toString());
    }
    return values;
}

From source file:com.google.appinventor.components.runtime.Twitter.java

License:Open Source License

/**
 * Gets the most recent messages sent directly to you.
 *//*  w ww .j  av  a  2  s  .c o  m*/
@SimpleFunction(description = "Requests the " + MAX_MENTIONS_RETURNED + " most "
        + "recent direct messages sent to the logged-in user.  When the "
        + "messages have been retrieved, the system will raise the "
        + "<code>DirectMessagesReceived</code> event and set the "
        + "<code>DirectMessages</code> property to the list of messages."
        + "<p><u>Requirements</u>: This should only be called after the "
        + "<code>IsAuthorized</code> event has been raised, indicating that the "
        + "user has successfully logged in to Twitter.</p>")
public void RequestDirectMessages() {
    if (twitter == null || userName.length() == 0) {
        form.dispatchErrorOccurredEvent(this, "RequestDirectMessages",
                ErrorMessages.ERROR_TWITTER_REQUEST_DIRECT_MESSAGES_FAILED, "Need to login?");
        return;
    }
    AsynchUtil.runAsynchronously(new Runnable() {
        List<DirectMessage> messages = Collections.emptyList();

        @Override
        public void run() {
            try {
                messages = twitter.getDirectMessages();
            } catch (TwitterException e) {
                form.dispatchErrorOccurredEvent(Twitter.this, "RequestDirectMessages",
                        ErrorMessages.ERROR_TWITTER_REQUEST_DIRECT_MESSAGES_FAILED, e.getMessage());
            } finally {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        directMessages.clear();
                        for (DirectMessage message : messages) {
                            directMessages.add(message.getSenderScreenName() + " " + message.getText());
                        }
                        DirectMessagesReceived(directMessages);
                    }
                });
            }
        }

    });
}

From source file:com.ikungolf.java.javatwitter.directmessage.GetDirectMessages.java

License:Apache License

private String getLastestMessage() {
    Twitter twitter = new TwitterFactory().getInstance();
    String msg = new String();
    try {//from  w  w  w  .  ja v  a2s. c o  m
        Paging paging = new Paging(1);
        List<DirectMessage> messages;

        messages = twitter.getDirectMessages(paging);
        System.out.println(messages.size());
        DirectMessage dm = messages.get(0);

        System.out.println("Message: " + dm.getText());
        messageId = dm.getId();
        if (messageId != tempMsgId) {
            tempMsgId = messageId;
        }
        msg = dm.getText();

    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get messages: " + te.getMessage());
        System.exit(-1);
    }

    return msg;
}

From source file:com.ikungolf.java.javatwitter.directmessage.GetSentDirectMessages.java

License:Apache License

/**
 * Usage: java twitter4j.examples.directmessages.GetSentDirectMessages
 *
 * @param args message/*from  w ww.ja va2  s  . c om*/
 */
public static void main(String[] args) {
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        Paging page = new Paging(1);
        List<DirectMessage> directMessages;
        do {
            directMessages = twitter.getSentDirectMessages(page);
            for (DirectMessage message : directMessages) {
                System.out.println("To: @" + message.getRecipientScreenName() + " id:" + message.getId() + " - "
                        + message.getText());
            }
            page.setPage(page.getPage() + 1);
        } while (directMessages.size() > 0 && page.getPage() < 10);
        System.out.println("done.");
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get sent messages: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:com.ikungolf.java.javatwitter.directmessage.ShowDirectMessage.java

License:Apache License

/**
 * Usage: java twitter4j.examples.directmessage.ShowDirectMessage [message id]
 *
 * @param args String[]//from w  w  w  . ja va 2 s.  co  m
 */
public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.directmessage.ShowDirectMessage [message id]");
        System.exit(-1);
    }
    Twitter twitter = new TwitterFactory().getInstance();
    try {
        DirectMessage message = twitter.showDirectMessage(Long.parseLong(args[0]));
        System.out.println("From: @" + message.getSenderScreenName() + " id:" + message.getId() + " - "
                + message.getText());
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get message: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:com.klinker.android.twitter.utils.TweetLinkUtils.java

License:Apache License

public static String[] getLinksInStatus(DirectMessage status) {
    UserMentionEntity[] users = status.getUserMentionEntities();
    String mUsers = "";

    for (UserMentionEntity name : users) {
        String n = name.getScreenName();
        if (n.length() > 1) {
            mUsers += n + "  ";
        }/*from w  ww. j  a  va  2s  .c o m*/
    }

    HashtagEntity[] hashtags = status.getHashtagEntities();
    String mHashtags = "";

    for (HashtagEntity hashtagEntity : hashtags) {
        String text = hashtagEntity.getText();
        if (text.length() > 1) {
            mHashtags += text + "  ";
        }
    }

    URLEntity[] urls = status.getURLEntities();
    String expandedUrls = "";
    String compressedUrls = "";

    for (URLEntity entity : urls) {
        String url = entity.getExpandedURL();
        if (url.length() > 1) {
            expandedUrls += url + "  ";
            compressedUrls += entity.getURL() + "  ";
        }
    }

    MediaEntity[] medias = status.getMediaEntities();
    String mediaExp = "";
    String mediaComp = "";
    String mediaDisplay = "";

    for (MediaEntity e : medias) {
        String url = e.getURL();
        if (url.length() > 1) {
            mediaComp += url + "  ";
            mediaExp += e.getExpandedURL() + "  ";
            mediaDisplay += e.getDisplayURL() + "  ";
        }
    }

    String[] sExpandedUrls;
    String[] sCompressedUrls;
    String[] sMediaExp;
    String[] sMediaComp;
    String[] sMediaDisply;

    try {
        sCompressedUrls = compressedUrls.split("  ");
    } catch (Exception e) {
        sCompressedUrls = new String[0];
    }

    try {
        sExpandedUrls = expandedUrls.split("  ");
    } catch (Exception e) {
        sExpandedUrls = new String[0];
    }

    try {
        sMediaComp = mediaComp.split("  ");
    } catch (Exception e) {
        sMediaComp = new String[0];
    }

    try {
        sMediaExp = mediaExp.split("  ");
    } catch (Exception e) {
        sMediaExp = new String[0];
    }

    try {
        sMediaDisply = mediaDisplay.split("  ");
    } catch (Exception e) {
        sMediaDisply = new String[0];
    }

    String tweetTexts = status.getText();

    String imageUrl = "";
    String otherUrl = "";

    for (int i = 0; i < sCompressedUrls.length; i++) {
        String comp = sCompressedUrls[i];
        String exp = sExpandedUrls[i];

        if (comp.length() > 1 && exp.length() > 1) {
            String str = exp.toLowerCase();

            tweetTexts = tweetTexts.replace(comp,
                    exp.replace("http://", "").replace("https://", "").replace("www.", ""));

            if (str.contains("instag") && !str.contains("blog.instag")) {
                imageUrl = exp + "media/?size=m";
                otherUrl += exp + "  ";
            } else if (str.contains("youtub") && !(str.contains("channel") || str.contains("user"))) { // normal youtube link
                // first get the youtube video code
                int start = exp.indexOf("v=") + 2;
                int end = exp.length();
                if (exp.substring(start).contains("&")) {
                    end = exp.indexOf("&");
                } else if (exp.substring(start).contains("?")) {
                    end = exp.indexOf("?");
                }
                imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, end) + "/hqdefault.jpg";
                otherUrl += exp + "  ";
            } else if (str.contains("youtu.be")) { // shortened youtube link
                // first get the youtube video code
                int start = exp.indexOf(".be/") + 4;
                int end = exp.length();
                if (exp.substring(start).contains("&")) {
                    end = exp.indexOf("&");
                } else if (exp.substring(start).contains("?")) {
                    end = exp.indexOf("?");
                }
                imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, end) + "/hqdefault.jpg";
                otherUrl += exp + "  ";
            } else if (str.contains("twitpic")) {
                int start = exp.indexOf(".com/") + 5;
                imageUrl = "http://twitpic.com/show/full/" + exp.substring(start).replace("/", "");
                otherUrl += exp + "  ";
            } else if (str.contains("imgur") && !str.contains("/a/")) {
                int start = exp.indexOf(".com/") + 6;
                imageUrl = "http://i.imgur.com/" + exp.substring(start) + "m.jpg";
                imageUrl = imageUrl.replace("gallery/", "").replace("a/", "");
                otherUrl += exp + "  ";
            } else if (str.contains("pbs.twimg.com")) {
                imageUrl = exp;
                otherUrl += exp + "  ";
            } else if (str.contains("ow.ly/i")) {
                Log.v("talon_owly", exp);
                imageUrl = "http://static.ow.ly/photos/original/"
                        + exp.substring(exp.lastIndexOf("/")).replaceAll("/", "") + ".jpg";
                otherUrl += exp + "  ";
            } else if (str.contains(".jpg") || str.contains(".png")) {
                imageUrl = exp;
                otherUrl += exp + "  ";
            } else {
                otherUrl += exp + "  ";
            }
        }
    }

    for (int i = 0; i < sMediaComp.length; i++) {
        String comp = sMediaComp[i];
        String exp = sMediaExp[i];

        if (comp.length() > 1 && exp.length() > 1) {
            tweetTexts = tweetTexts.replace(comp, sMediaDisply[i]);
            /*try {
            tweetTexts = tweetTexts.replace(comp, exp.replace("http://", "").replace("https://", "").replace("www.", "").substring(0, 30) + "...");
            } catch (Exception e) {
            tweetTexts = tweetTexts.replace(comp, exp.replace("http://", "").replace("https://", "").replace("www.", ""));
            }*/
            imageUrl = status.getMediaEntities()[0].getMediaURL();
            otherUrl += sMediaDisply[i];
        }
    }

    return new String[] { tweetTexts, imageUrl, otherUrl, mHashtags, mUsers };
}