Skip to content

Tweet 0.9.9.x

linvi edited this page Jul 9, 2015 · 9 revisions

Overview

IF YOU ARE USING TWEETINVI <= 0.9.8.x please read this documentation

Tweets are 140 characters text messages that can contain various information entities like HashTags, Locations, Images, Videos, URLs.

Let's code

Please note that instead of a <tweet_identifier> you can use either a (long) tweet_id, an ITweet or an ITweetDTO.

Publish Tweets

Lets start by publishing a simple tweet...

// *** In a single call ***
var tweet = Tweet.PublishTweet("I love Tweetinvi!");

// *** Create a variable that you can pass through your functions ***
var publishParameter = new PublishTweetParameters("I love tweetinvi!", new PublishTweetOptionalParameters());
var tweet = Tweet.PublishTweet(publishParameter);

Optional Parameters

When publishing a tweet you can set up various optional parameters. The PublishTweetOptionalParameters will let you configure these parameters.

In the sections below you will be provided with examples

Publish in reply to another tweet

InReplyToTweet/InReplyToTweetId specify which existing tweet you want to reply to.

var tweetToReplyTo = Tweet.PublishTweet("Tweetinvi loves its geeky developers!");
var tweet = Tweet.PublishTweet("We love you back!", new PublishTweetOptionalParameters
{
    InReplyToTweet = tweetToReplyTo,
    // OR
    InReplyToTweetId = tweetToReplyTo.Id
});

Publish with geo information

PlaceId specifies a place from where you've been publishing the tweet.

var sanFranciscoPlaceId = "5a110d312052166f";
var tweet = Tweet.PublishTweet("Hello San Fransisco!" + Guid.NewGuid(), new PublishTweetOptionalParameters
{
    PlaceId = sanFranciscoPlaceId
});

// When the tweet has been published you can get the place information as followed
var placeInformation = tweet.Place;

Coordinates specifies a more precise location from where the Tweet has been published.

var sanFranciscoCoordinates = new Coordinates(-122.40062, 37.78217);
var tweet = Tweet.PublishTweet("Hello San Fransisco!" + Guid.NewGuid(), new PublishTweetOptionalParameters
{
    Coordinates = sanFranciscoCoordinates
});

// When the tweet has been published you can get the tweet coordinates as followed
var coordinates = tweet.Coordinates;

DisplayExactCoordinates will let followers see a pin for the exact location where the tweet has been published.

Publish with media

Twitter allows developer to send images and videos within Tweets. In a single Tweet you can publish up to 4 images OR a single video.

Tweetinvi gives you 3 different ways to publish Tweets with media; using an upload id, a binary or an IMedia object. Please note that videos can only be added to a tweet using an upload id OR an IMedia object.

I would personally recommend uploading the media before performing the query. You will have more control over the process.

// Publish already uploaded media from their id
var tweet = Tweet.PublishTweet("Publishing my uploaded media!", new PublishTweetOptionalParameters()
{
	MediaIds = new List<long> { 42 	}
});
// Publish images from their binaries
var image1Binary = File.ReadAllBytes(imagePath);
var image2Binary = File.ReadAllBytes(imagePath2);
var tweet = Tweet.PublishTweet("Publishing some images...", new PublishTweetOptionalParameters()
{
    MediaBinaries = new List<byte[]>
    {
        image1Binary,
        image2Binary
    }
});
// Publish video from a Media - Note that the media can either be a new media or an already uploaded media
var video = new Media
{
    ContentType = "video/mp4",
    Data = videoBinary
};

var tweet = Tweet.PublishTweet("Publishing my amazing video!", new PublishTweetOptionalParameters()
{
    Medias = new List<IMedia> { video }
});

Other Parameters

Sensitive specifies whether media published with the tweet could be sensitive for other users. TrimUser specifies whether you will receive all the metadata of the tweet creator as part of the Twitter response. If set to false, the Tweet.Creator property will only contain the user id.

Check if I can publish a tweet

Before publishing a Tweet you should ensure that the size of the Tweet contains less than 140 characters. Twitter has a very specific mechanism to calculate the size of a Tweet based on the hashtags, urls, medias...

The static Tweet class contains 2 methods to help you. Tweet.Length() and Tweet.CanBePublished().

// Length of a publication
var length = Tweet.Length("Test me if you #can!", new PublishTweetOptionalParameters
{
    MediaIds = new List<long> {42}
});

// Check if the tweet can be published
var canBePublished = TweetinviConsts.MAX_TWEET_SIZE <= length;

// OR
var canBePublished = Tweet.CanBePublished("Test me if you #can!", new PublishTweetOptionalParameters
{
    MediaIds = new List<long> {42}
});

To simplify your life, Tweetinvi has implemented an extension method to the string class. This will let you know the size of a text using the Twitter algorithm. Please note that contrary to the previous example, this will not take into consideration the media.

// From a string (the extension namespace is 'Tweetinvi.Core.Extensions')
var twitterLength = "I love https://github.com/linvi/tweetinvi".TweetLength();

Publish a Retweet

var retweet = Tweet.PublishRetweet(<tweet_identifier>);

Delete a Tweet

var success = Tweet.DestroyTweet(<tweet_identifier>);

Favorite a Tweet

var success = Tweet.FavoriteTweet(<tweet_identifier>);

OEmbed Tweet

var oembedTweet = Tweet.GenerateOEmbedTweet(<tweet_identifier>);
Clone this wiki locally