Search in sources :

Example 1 with DirectMessage

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);
    }
}
Also used : DirectMessage(twitter4j.DirectMessage) Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory) TwitterException(twitter4j.TwitterException)

Example 2 with DirectMessage

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;
}
Also used : DirectMessage(twitter4j.DirectMessage) Twitter(twitter4j.Twitter) TwitterException(twitter4j.TwitterException)

Example 3 with DirectMessage

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();
}
Also used : DirectMessage(twitter4j.DirectMessage) Twitter(twitter4j.Twitter)

Example 4 with DirectMessage

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);
    }
}
Also used : DirectMessage(twitter4j.DirectMessage) DirectMessageList(twitter4j.DirectMessageList) Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory) TwitterException(twitter4j.TwitterException)

Example 5 with DirectMessage

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;
    }
}
Also used : DirectMessage(twitter4j.DirectMessage) TwitterException(twitter4j.TwitterException) ActionDoc(org.openhab.core.scriptengine.action.ActionDoc)

Aggregations

DirectMessage (twitter4j.DirectMessage)8 TwitterException (twitter4j.TwitterException)7 Twitter (twitter4j.Twitter)6 TwitterFactory (twitter4j.TwitterFactory)3 Intent (android.content.Intent)2 DMDataSource (com.klinker.android.twitter.data.sq_lite.DMDataSource)2 Paging (twitter4j.Paging)2 User (twitter4j.User)2 AlarmManager (android.app.AlarmManager)1 PendingIntent (android.app.PendingIntent)1 Context (android.content.Context)1 AppSettings (com.klinker.android.twitter.settings.AppSettings)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 ActionDoc (org.openhab.core.scriptengine.action.ActionDoc)1 DirectMessageList (twitter4j.DirectMessageList)1