Skip to content
linvi edited this page Aug 2, 2015 · 25 revisions

Documentation for Tweetinvi before 0.9.9.x

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. Tweet.PublishTweet can be invoked with a string as well as optional parameters (PublishTweetOptionalParameters).

Publish a simple Tweet

Lets start by publishing a simple tweet...

var firstTweet = Tweet.PublishTweet("I love Tweetinvi!");

Tweet with additional Parameters

As mentioned Tweets can be published with optional parameters like mentions, hashtags, medias, localization... All these values can be set up in the PublishTweetOptionalParameters.

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

var reply = Tweet.PublishTweet("Tweetinvi loves you back", new PublishTweetOptionalParameters
{
    InReplyToTweet = firstTweet
});

Lets take another example with geo localization.

var France = new Coordinates(longitude, latitude);
var tweet = Tweet.PublishTweet("Some love from France!", new PublishTweetOptionalParameters
{
    Coordinates = France
});

Create a Tweet Publish configuration before publishing

In some cases you will want to pass through a Tweet configuration instead of publishing it directly. You can do this with the PublishTweetParameters class.

var publishConfig = new PublishTweetParameters("hello", new PublishTweetOptionalParameters 
{ 
    InReplyToTweet = firstTweet
});

Most common publication

Tweetinvi added a set of methods for the most common publication types. For example, replying to a Tweet can be done as followed:

var tweet = Tweet.PublishTweetInReplyTo("Tweetinvi loves you back", firstTweet);

Publish a tweet with media

Twitter allows developer to send images and videos within Tweets. Note that you can send up to 4 images or 1 video in a single Tweet.

The advised solution is to use the Upload methods to add a media to Twitter and use the returned media to add it to your publication.

byte[] file1 = File.ReadAllBytes(filePath);
var media = Upload.UploadImage(file1);

var tweet = Tweet.PublishTweet("This is my awesome photo!", new PublishTweetOptionalParameters
{
    Medias = new List<IMedia> { media }
});

You can add medias using ids, IMedia objects or binaries using the following properties :

  • MediaIds
  • Medias
  • MediaBinaries

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.

// From a Tweet configuration
var publishParameter = new PublishTweetParameters("hello tweetinvi", new PublishTweetOptionalParameters
{
    QuotedTweet = existingTweet
});

var publicationLength = Tweet.Length(publishParameter);

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

Favourite a Tweet

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

OEmbed Tweet

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