-
Notifications
You must be signed in to change notification settings - Fork 217
Tweet 0.9.9.x
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.
- Publish Tweets
- Optional Parameters
- Publish Tweets with media
- Tweet Length
- Publish a Retweet
- Delete Tweets
- Favorite Tweets
- Oembeded Tweets
Please note that instead of a <tweet_identifier> you can use either a (long) tweet_id, an ITweet or an ITweetDTO.
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);
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. -
Other parameters
: sensitive and trim_user.
In the sections below you will be provided with examples
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
});
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.
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 }
});
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.
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();
var retweet = Tweet.PublishRetweet(<tweet_identifier>);
var success = Tweet.DestroyTweet(<tweet_identifier>);
var success = Tweet.FavoriteTweet(<tweet_identifier>);
var oembedTweet = Tweet.GenerateOEmbedTweet(<tweet_identifier>);