Skip to content
linvi edited this page Jun 6, 2015 · 25 revisions

Overview

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

PLEASE NOTE THAT TWEET PUBLICATION WILL BE IMPROVED IN VERSION 0.9.9.0

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 firstTweet = Tweet.PublishTweet("I love Tweetinvi!");

// *** In 2 steps ***
var firstTweet = Tweet.CreateTweet("I love Tweetinvi!");
// If the Publish method succeed the tweet is hydrated with the data return by Twitter.
var success = firstTweet.Publish();

Now let's publish a reply to the first tweet.

var reply = Tweet.CreateTweet("Tweetinvi loves you back!");

// If the operation is a success, the reply object is updated
var success = firstTweet.PublishReply(reply);

If you want to add some geo localization information use the appropriate method.

var tweet = Tweet.CreateTweet(text);

var coordinates = new Coordinates(longitude, latitude);
var success = tweet.PublishWithGeo(coordinates); 

Publish a tweet with media

Twitter allows developer to send images and videos within Tweets. Tweets have an AddMedia method that allow developers to simply add a byte[] as a media of the tweet.

byte[] file1 = File.ReadAllBytes(filePath);
byte[] file2 = File.ReadAllBytes(filePath);

var tweet = Tweet.CreateTweet("hello");

tweet.AddMedia(file1);
tweet.AddMedia(file2);

var success = tweet.Publish();

Tweet Length

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...

To simplify your life, Tweetinvi has implemented an extension method to the String class as well as a Length property to the ITweet interface and you can

// From a Tweet
var tweet = Tweet.CreateTweet("I love https://github.com/linvi/tweetinvi");
var twitterLength = tweet.Length;

// 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