Skip to content

Tweet 0.9.9.x

linvi edited this page Jul 9, 2015 · 9 revisions

Overview

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.

  • Reply specifies which tweet you want to reply to.
  • Geo specifies geographic information for the tweet.
  • Media specifies which media you want to add to the tweet.
  • Sensitive specifies whether media published with the tweet could be sensitive for other users.
  • Other 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
});

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
});
  • DisplayExactCoordinates will let followers see a pin for the exact location where the tweet has been published.

Publish with media

  • 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