use of twitter4j.DirectMessage in project twitter4j by yusuke.
the class SendDirectMessage method main.
/**
* Usage: java twitter4j.examples.directMessage.DirectMessage [recipient screen name] [message]
*
* @param args String[]
*/
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Usage: java twitter4j.examples.directmessage.SendDirectMessage [recipient screen name] [message]");
System.exit(-1);
}
Twitter twitter = new TwitterFactory().getInstance();
try {
DirectMessage message = twitter.sendDirectMessage(args[0], args[1]);
System.out.println("Direct message successfully sent to " + message.getRecipientScreenName());
System.exit(0);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to send a direct message: " + te.getMessage());
System.exit(-1);
}
}
use of twitter4j.DirectMessage in project opennms by OpenNMS.
the class MicroblogDMNotificationStrategy method send.
/**
* {@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;
}
use of twitter4j.DirectMessage in project tutorials by eugenp.
the class Application method sendDirectMessage.
public static String sendDirectMessage(String recipientName, String msg) throws TwitterException {
Twitter twitter = getTwitterinstance();
DirectMessage message = twitter.sendDirectMessage(recipientName, msg);
return message.getText();
}
use of twitter4j.DirectMessage in project twitter4j by yusuke.
the class GetDirectMessages method main.
/**
* Usage: java twitter4j.examples.directmessage.GetDirectMessages
*
* @param args String[]
*/
public static void main(String[] args) {
Twitter twitter = new TwitterFactory().getInstance();
try {
String cursor = null;
int count = 20;
DirectMessageList messages;
do {
System.out.println("* cursor:" + cursor);
messages = cursor == null ? twitter.getDirectMessages(count) : twitter.getDirectMessages(count, cursor);
for (DirectMessage message : messages) {
System.out.println("From: " + message.getSenderId() + " id:" + message.getId() + " [" + message.getCreatedAt() + "]" + " - " + message.getText());
System.out.println("raw[" + message + "]");
}
cursor = messages.getNextCursor();
} while (messages.size() > 0 && cursor != null);
System.out.println("done.");
System.exit(0);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to get messages: " + te.getMessage());
System.exit(-1);
}
}
use of twitter4j.DirectMessage in project openhab1-addons by openhab.
the class Twitter method sendDirectMessage.
/**
* Sends a direct message via Twitter
*
* @param recipientId the receiver of this direct message
* @param messageTxt the direct message to send
*
* @return <code>true</code>, if sending the direct message has been successful and
* <code>false</code> in all other cases.
*/
@ActionDoc(text = "Sends a direct message via Twitter", returns = "<code>true</code>, if sending the direct message has been successful and <code>false</code> in all other cases.")
public static boolean sendDirectMessage(@ParamDoc(name = "recipientId", text = "the receiver of this direct message") String recipientId, @ParamDoc(name = "messageTxt", text = "the direct message to send") String messageTxt) {
if (!isEnabled) {
logger.debug("Twitter client is disabled > execution aborted!");
return false;
}
try {
// abbreviate the Tweet to meet the 140 character limit ...
messageTxt = StringUtils.abbreviate(messageTxt, CHARACTER_LIMIT);
// send the direct message
DirectMessage message = client.sendDirectMessage(recipientId, messageTxt);
logger.debug("Successfully sent direct message '{}' to @", message.getText(), message.getRecipientScreenName());
return true;
} catch (TwitterException e) {
logger.error("Failed to send Tweet '" + messageTxt + "' because of: " + e.getLocalizedMessage());
return false;
}
}
Aggregations