How to post tweet pragmatically without touching the tweet button


#1

The android kit official documentation provides a guide to build a tweet via TweetComposer and after building the tweet, we touch the tweet button and tweet is posted. But, I want to post the tweet without touching the tweet button. I mean, I need a code like below sample code…

Twitter.postToMyTwitterAccount(“Hi!.. I am a tweet…”);

and this code will post to the logged in account via my android application. How can I do this via android kit?


#2

Hi,

We provide a StatusesService that you can use to post a tweet. To get the StatusesService,

StatusesService statusesService = Twitter.getApiClient(session).getStatusesService();
statusesService.update(...)

Here’s javadoc for the method:

    /**
 * Updates the authenticating user's current status, also known as tweeting.
 * <p>
 * For each update attempt, the update text is compared with the authenticating user's recent
 * tweets. Any attempt that would result in duplication will be blocked, resulting in a 403
 * error. Therefore, a user cannot submit the same status twice in a row.
 * <p>
 * While not rate limited by the API a user is limited in the number of tweets they can create
 * at a time. If the number of updates posted by the user reaches the current allowed limit this
 * method will return an HTTP 403 error.
 *
 * @param status (required) The text of your status update, typically up to 140 characters. URL
 *               encode as necessary. [node:840,title="t.co link wrapping"] may effect character
 *               counts. There are some special commands in this field to be aware of. For
 *               instance, preceding a message with "D " or "M " and following it with a screen
 *               name can create a direct message to that user if the relationship allows for
 *               it.
 * @param inReplyToStatusId (optional) The ID of an existing status that the update is in reply
 *                          to. Note:: This parameter will be ignored unless the author of the
 *                          tweet this parameter references is mentioned within the status text.
 *                          Therefore, you must include @username, where username is the author
 *                          of the referenced tweet, within the update.
 * @param possiblySensitive (optional) If you upload Tweet media that might be considered
 *                          sensitive content such as nudity, violence, or medical procedures,
 *                          you should set this value to true. See Media setting and best
 *                          practices for more context. Defaults to false.
 * @param latitude (optional) The latitude of the location this tweet refers to. This parameter
 *                 will be ignored unless it is inside the range -90.0 to +90.0 (North is
 *                 positive) inclusive. It will also be ignored if there isn't a corresponding
 *                 long parameter.
 * @param longitude (optional) The longitude of the location this tweet refers to. The valid
 *                  ranges for longitude is -180.0 to +180.0 (East is positive) inclusive. This
 *                  parameter will be ignored if outside that range, if it is not a number, if
 *                  geo_enabled is disabled, or if there not a corresponding lat parameter.
 * @param placeId (optional) A place in the world. These IDs can be retrieved from [node:29].
 * @param displayCoordinates (optional) Whether or not to put a pin on the exact coordinates a
 *                           tweet has been sent from.
 * @param trimUser (optional) When set to either true, t or 1, each tweet returned in a timeline
 *                 will include a user object including only the status authors numerical ID.
 *                 Omit this parameter to receive the complete user object.
 * @param cb The callback to invoke when the request completes.
 */
@FormUrlEncoded
@POST("/1.1/statuses/update.json")
void update(@Field("status") String status,
       @Field("in_reply_to_status_id") Long inReplyToStatusId,
       @Field("possibly_sensitive") Boolean possiblySensitive,
       @Field("lat") Double latitude,
       @Field("long") Double longitude,
       @Field("place_id") String placeId,
       @Field("display_cooridnates") Boolean displayCoordinates,
       @Field("trim_user") Boolean trimUser,
       Callback<Tweet> cb);

#3

lientm! thanks for replying. I will try the code you provided.
How can I attached photo with tweet via this code or is there any other way?

===== update =====
I tried your code and it works well. Now I want to attach photo with tweet…
How can I attach photo?


#4

Hi,

At this time, there is no way to post a tweet with a photo through the API.


#5

ok. do you plan in future to provide tweet with photo through API? When ?


#6

Hi, Could you please say if it was provided tweet with photo through API? Or it is still no way to way.


#7

One thing to note is that the Developer Policy (section 3) specifies that users should be made aware of any actions that an app is making on their behalf, so using the Tweet Composer is a good way of doing that in an explicit way, to ensure that a user is aware of what the app is doing.


#8

Still they did’t provide the photo uploading with tweet. We have to wait…


#9

You can use the media/upload endpoint to upload images for use in Tweets, via the REST API.


#10

But does this builder provide a callback that the Tweet has been updated?


#11

No, this is not the same as the TweetDelegate provided in Twitter Kit today - that’s a benefit of using the Tweet Composer.


#12

Hi,

The easiest way to share image on twitter is by using the tweet-composer.

   TweetComposer.Builder builder = new TweetComposer.Builder(this)
        .text("just setting up my Fabric.")
        .image(myImageUri);
   builder.show();

The image Uri should be a file Uri (i.e. :code:file://<absolute_path> scheme) to a local file. For example,

File myImageFile = new File("/path/to/image");
Uri myImageUri = Uri.fromFile(myImageFile);

If you want to know when the tweet has been updated, instead of calling builder.show(), you can call builder.createIntent() and then call startActivityForResult.

        Intent intent = new TweetComposer.Builder(this)
                .text("Tweet from Fabric!")
                .url(new URL("http://www.twitter.com"))
                .createIntent();
        startActivityForResult(intent, TWEET_COMPOSER_RESULT);

#13

Hello,

I’m an android programmer, I was just wondering if there are reference docs for the twitter kit SDK for android? I know that IOS references doc exists but can you tell me if android ones also exist?


#14

Javadocs live alongside the libraries in Maven - see here for the Twitter Kit 1.3.3 docs for example.


#15

Isn’t that what you would tell your user when they login the first time in your app? If you have a log in?


#16

Hi @lientm
I have tried the StatusesService for post tweet , as you refer , but i got below error ,

StatusesService statusesService = Twitter.getApiClient(session).getStatusesService();

can you please give direction to implement.

Or I need to implement the Login authentication first and then I post the tweet ,

java.lang.IllegalStateException: Must have valid session. Did you authenticate with Twitter?
            at com.twitter.sdk.android.core.TwitterCore.getApiClient(TwitterCore.java:258)
            at com.twitter.sdk.android.Twitter.getApiClient(Twitter.java:142)
            at com.example.test.fragment.OffersAllFragment$2$1.onClick(OffersAllFragment.java:290)
```

#17

You need the user to have logged in first - it is not possible to post a Tweet without an authenticated User context.


#18

Hi, Do you have a sample project with how to share image using tweet-composer?


#19

A post was split to a new topic: How do I attach an image URL when tweeting through Fabric?


#20

Hi @lientm

Can I get the tweet id from startActivityForResult and them do tweet with statusesService.update?

or with other method?