@Converter public static String toString(DirectMessage dm) throws ParseException { return new StringBuilder() .append(dm.getCreatedAt()).append(" (").append(dm.getSenderScreenName()).append(") ") .append(dm.getText()) .toString(); }
private long getOtherId(final DirectMessage directMessageEvent) { return sessionManager.isCurrentUser(directMessageEvent.getSenderId()) ? directMessageEvent.getRecipientId() : directMessageEvent.getSenderId(); }
static List<DirectMessage> constructDirectMessages(Response res, Twitter twitter) throws TwitterException { Document doc = res.asDocument(); if (isRootNodeNilClasses(doc)) { return new ArrayList<DirectMessage>(0); } else { try { ensureRootNodeNameIs("direct-messages", doc); NodeList list = doc.getDocumentElement().getElementsByTagName( "direct_message"); int size = list.getLength(); List<DirectMessage> messages = new ArrayList<DirectMessage>(size); for (int i = 0; i < size; i++) { Element status = (Element) list.item(i); messages.add(new DirectMessage(res, status, twitter)); } return messages; } catch (TwitterException te) { if (isRootNodeNilClasses(doc)) { return new ArrayList<DirectMessage>(0); } else { throw te; } } } }
/** * Usage: java twitter4j.examples.GetDirectMessages ID Password * @param args String[] */ public static void main(String[] args) { if (args.length < 2) { System.out.println("No TwitterID/Password specified."); System.out.println( "Usage: java twitter4j.examples.GetDirectMessages ID Password"); System.exit( -1); } Twitter twitter = new Twitter(args[0], args[1]); try { List<DirectMessage> messages = twitter.getDirectMessages(); for (DirectMessage message : messages) { System.out.println("Sender:" + message.getSenderScreenName()); System.out.println("Text:" + message.getText() + "\n"); } System.exit(0); } catch (TwitterException te) { System.out.println("Failed to get messages: " + te.getMessage()); System.exit( -1); } } }
public void onDirectMessage(long forUser, DirectMessage directMessage) { log.debug("DirectMessage for the user:" + forUser + " text:" + directMessage.getText()); }
private DirectMessageList removeDMsNotSentByMe(DirectMessageList list) throws TwitterException { if(myId == -1) { myId = verifyCredentials().getId(); } // filter direct messages not sent by me for (int i = list.size() - 1; i >= 0; i--) { if (list.get(i).getSenderId() != myId) { list.remove(i); } } return list; }
@Override public List<Exchange> pollConsume() throws TwitterException { List<DirectMessage> directMessages = getTwitter().getDirectMessages(getLastIdPaging()); for (int i = 0; i < directMessages.size(); i++) { setLastIdIfGreater(directMessages.get(i).getId()); } return TwitterEventType.DIRECT_MESSAGE.createExchangeList(endpoint, directMessages); }
/** * Returns a list of the direct messages sent to the authenticating user. * <br>This method calls http://twitter.com/direct_messages * * @param since narrows the resulting list of direct messages to just those sent after the specified HTTP-formatted date * @return list of direct messages * @throws TwitterException when Twitter service or network is unavailable * @deprecated Use getDirectMessages(Paging paging) instead * @see <a href="http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-direct_messages">Twitter API Wiki / Twitter REST API Method: direct_messages</a> */ public List<DirectMessage> getDirectMessages(Date since) throws TwitterException { return DirectMessage.constructDirectMessages(get(getBaseURL() + "direct_messages.xml", "since", format.format(since), true), this); }
private DirectMessageList removeDMsNotSentToMe(DirectMessageList list) throws TwitterException { if(myId == -1) { myId = verifyCredentials().getId(); } // filter direct messages not sent by me for (int i = list.size() - 1; i >= 0; i--) { if (list.get(i).getRecipientId() != myId) { list.remove(i); } } return list; }
/** * Destroys the direct message specified in the required ID parameter. The authenticating user must be the recipient of the specified direct message. * <br>This method calls http://twitter.com/direct_messages/destroy * * @param id the ID of the direct message to destroy * @return the deleted direct message * @throws TwitterException when Twitter service or network is unavailable * @see <a href="http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-direct_messages%C2%A0destroy">Twitter API Wiki / Twitter REST API Method: direct_messages destroy</a> * @since Twitter4J 2.0.1 */ public DirectMessage destroyDirectMessage(int id) throws TwitterException { return new DirectMessage(http.post(getBaseURL() + "direct_messages/destroy/" + id + ".xml", new PostParameter[0], true), this); }
List<DirectMessage> messages = twitter.getDirectMessages(); for (DirectMessage message : messages) { System.out.println(message.getText()); }
private void profilePictures() { JavaFxObservable.valuesOf(currentMessage).forEach(messageEvent -> { final boolean isSentByMe = sessionManager.isCurrentUser(messageEvent.getSenderId()); final User sender = cachedTwitterInfoService.getUser(messageEvent.getSenderId()); if (isSentByMe) { container.setAlignment(Pos.TOP_RIGHT); ppSetupSender(currentUserPpBox, sender); ppSetupReceiver(otherPpBox); } else { container.setAlignment(Pos.TOP_LEFT); ppSetupSender(otherPpBox, sender); ppSetupReceiver(currentUserPpBox); } }); }
/** {@inheritDoc} */ @Override public int send(List<Argument> arguments) { Twitter svc = buildUblogService(arguments); String destUser = findDestName(arguments); DirectMessage response; if (destUser == null || "".equals(destUser)) { LOG.error("Cannot send a microblog DM notice to a user with no microblog username set. Either set a microblog username for this OpenNMS user or use the MicroblogUpdateNotificationStrategy instead."); return 1; } // In case the user tried to be helpful, avoid a double @@ if (destUser.startsWith("@")) destUser = destUser.substring(1); String fullMessage = buildMessageBody(arguments); LOG.debug("Dispatching microblog DM notification at base URL '{}' with destination user '{}' and message '{}'", svc.getConfiguration().getClientURL(), destUser, fullMessage); try { response = svc.sendDirectMessage(destUser, fullMessage); } catch (TwitterException e) { LOG.error("Microblog notification failed at service URL '{}' to destination user '{}'", svc.getConfiguration().getClientURL(), destUser, e); return 1; } LOG.info("Microblog DM notification succeeded: DM sent with ID {}", response.getId()); return 0; } }
/** * Returns a list of the direct messages sent to the authenticating user. * <br>This method calls http://twitter.com/direct_messages * * @return List * @throws TwitterException when Twitter service or network is unavailable * @see <a href="http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-direct_messages">Twitter API Wiki / Twitter REST API Method: direct_messages</a> */ public List<DirectMessage> getDirectMessages() throws TwitterException { return DirectMessage.constructDirectMessages(get(getBaseURL() + "direct_messages.xml", true), this); }
/** * Sends a new direct message to the specified user from the authenticating user. Requires both the user and text parameters below. * The text will be trimed if the length of the text is exceeding 140 characters. * <br>This method calls http://twitter.com/direct_messages/new * * @param id the ID or screen name of the user to whom send the direct message * @param text String * @return DirectMessage * @throws TwitterException when Twitter service or network is unavailable @see <a href="http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-direct_messages%C2%A0new">Twitter API Wiki / Twitter REST API Method: direct_messages new</a> */ public DirectMessage sendDirectMessage(String id, String text) throws TwitterException { return new DirectMessage(http.post(getBaseURL() + "direct_messages/new.xml", new PostParameter[]{new PostParameter("user", id), new PostParameter("text", text)}, true), this); }
public void onDirectMessage(DirectMessage directMessage) { log.debug("onDirectMessage text:" + directMessage.getText()); }
/** * Returns a list of the direct messages sent by the authenticating user. * <br>This method calls http://twitter.com/direct_messages/sent * * @param since narrows the resulting list of direct messages to just those sent after the specified HTTP-formatted date * @return List * @throws TwitterException when Twitter service or network is unavailable * @deprecated using long sinceId is suggested. * @see <a href="http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-direct_messages%C2%A0sent">Twitter API Wiki / Twitter REST API Method: direct_messages sent</a> */ public List<DirectMessage> getSentDirectMessages(Date since) throws TwitterException { return DirectMessage.constructDirectMessages(get(getBaseURL() + "direct_messages/sent.xml", "since", format.format(since), true), this); }
/** * Returns a list of the direct messages sent by the authenticating user. * <br>This method calls http://twitter.com/direct_messages/sent * * @return List * @throws TwitterException when Twitter service or network is unavailable * @see <a href="http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-direct_messages%C2%A0sent">Twitter API Wiki / Twitter REST API Method: direct_messages sent</a> */ public List<DirectMessage> getSentDirectMessages() throws TwitterException { return DirectMessage.constructDirectMessages(get(getBaseURL() + "direct_messages/sent.xml", new PostParameter[0], true), this); }
/** * Returns a list of the direct messages sent to the authenticating user. * <br>This method calls http://twitter.com/direct_messages * * @param paging controls pagination * @return List * @throws TwitterException when Twitter service or network is unavailable * @see <a href="http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-direct_messages">Twitter API Wiki / Twitter REST API Method: direct_messages</a> */ public List<DirectMessage> getDirectMessages(Paging paging) throws TwitterException { return DirectMessage.constructDirectMessages(get(getBaseURL() + "direct_messages.xml", null, paging, true), this); }
/** * Returns a list of the direct messages sent by the authenticating user. * <br>This method calls http://twitter.com/direct_messages/sent * * @param paging controls pagination * @return List * @throws TwitterException when Twitter service or network is unavailable * @since Twitter4J 2.0.1 * @see <a href="http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-direct_messages%C2%A0sent">Twitter API Wiki / Twitter REST API Method: direct_messages sent</a> */ public List<DirectMessage> getSentDirectMessages(Paging paging) throws TwitterException { return DirectMessage.constructDirectMessages(get(getBaseURL() + "direct_messages/sent.xml", new PostParameter[0],paging, true), this); }