Example usage for twitter4j Twitter sendDirectMessage

List of usage examples for twitter4j Twitter sendDirectMessage

Introduction

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

Prototype

DirectMessage sendDirectMessage(long userId, String text, QuickReply... quickReplies) throws TwitterException;

Source Link

Document

Sends a new direct message to the specified user from the authenticating user.

Usage

From source file:de.vanita5.twittnuker.service.BackgroundOperationService.java

License:Open Source License

private SingleResponse<ParcelableDirectMessage> sendDirectMessage(final NotificationCompat.Builder builder,
        final long accountId, final long recipientId, final String text, final String imageUri) {
    final Twitter twitter = getTwitterInstance(this, accountId, true, true);
    try {//w w  w  .j  a va2s  .  c o  m
        final ParcelableDirectMessage directMessage;
        if (imageUri != null) {
            final String path = getImagePathFromUri(this, Uri.parse(imageUri));
            if (path == null)
                throw new FileNotFoundException();
            final BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, o);
            final File file = new File(path);
            Utils.downscaleImageIfNeeded(file, 100);
            final ContentLengthInputStream is = new ContentLengthInputStream(file);
            is.setReadListener(new MessageMediaUploadListener(this, mNotificationManager, builder, text));
            final MediaUploadResponse uploadResp = twitter.uploadMedia(file.getName(), is, o.outMimeType);
            directMessage = new ParcelableDirectMessage(
                    twitter.sendDirectMessage(recipientId, text, uploadResp.getId()), accountId, true);
            file.delete();
        } else {
            directMessage = new ParcelableDirectMessage(twitter.sendDirectMessage(recipientId, text), accountId,
                    true);
        }
        return SingleResponse.getInstance(directMessage);
    } catch (final IOException e) {
        return SingleResponse.getInstance(e);
    } catch (final TwitterException e) {
        return SingleResponse.getInstance(e);
    }
}

From source file:org.getlantern.firetweet.service.BackgroundOperationService.java

License:Open Source License

private SingleResponse<ParcelableDirectMessage> sendDirectMessage(final NotificationCompat.Builder builder,
        final long accountId, final long recipientId, final String text, final String imageUri) {
    final Twitter twitter = getTwitterInstance(this, accountId, true, true);
    if (twitter == null)
        return SingleResponse.getInstance();
    try {/*from w  w w . j a va2  s . c o  m*/
        final ParcelableDirectMessage directMessage;
        if (imageUri != null) {
            final String path = getImagePathFromUri(this, Uri.parse(imageUri));
            if (path == null)
                throw new FileNotFoundException();
            final BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, o);
            final File file = new File(path);
            BitmapUtils.downscaleImageIfNeeded(file, 100);
            final ContentLengthInputStream is = new ContentLengthInputStream(file);
            is.setReadListener(new MessageMediaUploadListener(this, mNotificationManager, builder, text));
            final MediaUploadResponse uploadResp = twitter.uploadMedia(file.getName(), is, o.outMimeType);
            directMessage = new ParcelableDirectMessage(
                    twitter.sendDirectMessage(recipientId, text, uploadResp.getId()), accountId, true);
            if (!file.delete()) {
                Log.d(LOGTAG, String.format("unable to delete %s", path));
            }
        } else {
            directMessage = new ParcelableDirectMessage(twitter.sendDirectMessage(recipientId, text), accountId,
                    true);
        }
        Utils.setLastSeen(this, recipientId, System.currentTimeMillis());

        return SingleResponse.getInstance(directMessage);
    } catch (final IOException e) {
        Crashlytics.logException(e);
        return SingleResponse.getInstance(e);
    } catch (final TwitterException e) {
        Crashlytics.logException(e);
        return SingleResponse.getInstance(e);
    }
}

From source file:org.mariotaku.twidere.service.BackgroundOperationService.java

License:Open Source License

private SingleResponse<ParcelableDirectMessage> sendDirectMessage(final NotificationCompat.Builder builder,
        final long accountId, final long recipientId, final String text, final String imageUri) {
    final Twitter twitter = getTwitterInstance(this, accountId, true, true);
    if (twitter == null)
        return SingleResponse.getInstance();
    try {//from w  w  w  .j a  v  a 2s  .com
        final ParcelableDirectMessage directMessage;
        if (imageUri != null) {
            final String path = getImagePathFromUri(this, Uri.parse(imageUri));
            if (path == null)
                throw new FileNotFoundException();
            final BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, o);
            final File file = new File(path);
            BitmapUtils.downscaleImageIfNeeded(file, 100);
            final ContentLengthInputStream is = new ContentLengthInputStream(file);
            is.setReadListener(new MessageMediaUploadListener(this, mNotificationManager, builder, text));
            final MediaUploadResponse uploadResp = twitter.uploadMedia(file.getName(), is, o.outMimeType);
            directMessage = new ParcelableDirectMessage(
                    twitter.sendDirectMessage(recipientId, text, uploadResp.getId()), accountId, true);
            if (!file.delete()) {
                Log.d(LOGTAG, String.format("unable to delete %s", path));
            }
        } else {
            directMessage = new ParcelableDirectMessage(twitter.sendDirectMessage(recipientId, text), accountId,
                    true);
        }
        Utils.setLastSeen(this, recipientId, System.currentTimeMillis());

        return SingleResponse.getInstance(directMessage);
    } catch (final IOException e) {
        return SingleResponse.getInstance(e);
    } catch (final TwitterException e) {
        return SingleResponse.getInstance(e);
    }
}