-
Notifications
You must be signed in to change notification settings - Fork 221
Tweets
Documentation for Tweetinvi before 0.9.9.x
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
- Extended Tweets
- Publish Tweets
- Publish Tweets with media
- Publish in reply to
- Tweet Length
- Publish a Retweet
- Delete Tweets
- Favorite Tweets
- Oembeded Tweets
To help the developers to work with the new generation of Tweets, we have created a dedicated page for Extended Tweets.
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
).
Lets start by publishing a simple tweet...
var firstTweet = Tweet.PublishTweet("I love Tweetinvi!");
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(latitude, longitude);
var tweet = Tweet.PublishTweet("Some love from France!", new PublishTweetOptionalParameters
{
Coordinates = France
});
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
});
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);
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.UploadBinary(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
IMPORTANT : In September 2015, Twitter changed the requirements for publishing a tweet in reply to another one. You now have to add "@screenName" (screen name of the author of the tweet you are replying to) to the text of the tweet you want to publish.
var tweetToReplyTo = Tweet.GetTweet(tweetIdtoReplyTo);
// We must add @screenName of the author of the tweet we want to reply to
var textToPublish = string.Format("@{0} {1}",tweetToReplyTo.CreatedBy.ScreenName, text);
var tweet = Tweet.PublishTweetInReplyTo(textToPublish, tweetIdtoReplyTo);
Console.WriteLine("Publish success? {0}", tweet != null);
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();
var retweet = Tweet.PublishRetweet(<tweet_identifier>);
var success = Tweet.DestroyTweet(<tweet_identifier>);
var success = Tweet.FavoriteTweet(<tweet_identifier>);
var oembedTweet = Tweet.GenerateOEmbedTweet(<tweet_identifier>);