-
Notifications
You must be signed in to change notification settings - Fork 241
Direct messaging
Ramtin Jokar edited this page Feb 14, 2019
·
17 revisions
You can find all direct messaging functions in IInstaApi.MessagingProcessor class.
var inbox = await InstaApi.MessagingProcessor
.GetDirectInboxAsync(PaginationParameters.MaxPagesToLoad(1));
var firstThread = inbox.Value.Inbox.Threads.FirstOrDefault();
var requestedThreadId = firstThread.ThreadId;
var threads = await InstaApi.MessagingProcessor
.GetDirectInboxThreadAsync(requestedThreadId, PaginationParameters.MaxPagesToLoad(1));
var desireUsername = "rmt4006";
var desireThread = inbox.Value.Inbox.Threads
.Find(u => u.Users.FirstOrDefault().UserName.ToLower() == desireUsername);
var requestedThreadId = desireThread.ThreadId;
var threads = await InstaApi.MessagingProcessor
.GetDirectInboxThreadAsync(requestedThreadId);
var rankedRecipients = await InstaApi.MessagingProcessor.GetRankedRecipientsAsync();
var recentRecipients = await InstaApi.MessagingProcessor
.GetRecentRecipientsAsync();
var pendingDirect = await InstaApi.MessagingProcessor
.GetPendingDirectAsync();
var inbox = await InstaApi.MessagingProcessor.GetDirectInboxAsync();
var firstThread = inbox.Value.Inbox.Threads.FirstOrDefault();
var requestedThreadId = firstThread.ThreadId;
var directText = await InstaApi.MessagingProcessor
.SendDirectTextAsync(null, requestedThreadId, "Hello Ramtin,\r\nHow are you today?");
var desireUsername = "rmt4006";
var user = await InstaApi.UserProcessor.GetUserAsync(desireUsername);
var userId = user.Value.Pk.ToString();
var directText = await InstaApi.MessagingProcessor
.SendDirectTextAsync(userId, null, "Hello Ramtin,\r\nHow are you today?");
var userList = new List<string>();
// get user id(pk) for user 1
var user1 = await InstaApi.UserProcessor.GetUserAsync("rmt4006");
// add user id(pk) 1 to userList
userList.Add(user1.Value.Pk.ToString());
// get user id(pk) for user 2
var user2 = await InstaApi.UserProcessor.GetUserAsync("alingame");
// add user id(pk) 2 to userList
userList.Add(user2.Value.Pk.ToString());
// user ids must be splitted with comma character ,
var recipients = string.Join(",", userList);
// it should be something like this: userId1,userId2,userId3,userId3,....
// for current sample: 5318277344,719072853
var directText = await InstaApi.MessagingProcessor
.SendDirectTextAsync(recipients, null, "Hello Ramtin,\r\nHow are you today?");
var image = new InstaImage
{
// some photo path
Uri = "C:\\Files\\image1.jpg"
};
var directPhoto = await InstaApi.MessagingProcessor
.SendDirectPhotoAsync(image, requestedThreadId);
Note: only JPEG or JPG file will accept by instagram.
var directPhoto = await InstaApi.MessagingProcessor
.SendDirectPhotoToRecipientsAsync(image, "userId1", "userId2", "userId3", "....");
var directPhoto = await InstaApi.MessagingProcessor
.SendDirectDisappearingPhotoAsync(image, InstaViewMode.Replayable, requestedThreadId);
var video = new InstaVideo
{
// some video path
Uri = "C:\\Files\\video1.mp4"
};
var videoToUpload = new InstaVideoUpload
{
Video = video
};
var directVideo = await InstaApi.MessagingProcessor
.SendDirectVideoAsync(videoToUpload, requestedThreadId);
Note: only MP4 or MOV file will accept by instagram.
var directVideo = await InstaApi.MessagingProcessor
.SendDirectVideoToRecipientsAsync(videoToUpload, "userId1", "userId2", "userId3", "....");
var directVideo = await InstaApi.MessagingProcessor
.SendDirectDisappearingVideoAsync(videoToUpload, InstaViewMode.Replayable, requestedThreadId);
var link = "https://github.com/ramtinak/InstagramApiSharp";
var text = $"Hi, check this awesome instagram library for .net:\r\n{link}\r\nDon't forget to report issues!";
var directLink = await InstaApi.MessagingProcessor
.SendDirectLinkAsync(text, link, requestedThreadId);
// location information for Iran/Fars/Kazerun
double latitude = 29.61949;
double longitude = 51.65415;
var cityName = "Kazerun";
var locations = await InstaApi.LocationProcessor
.SearchLocationAsync(latitude, longitude, cityName);
var cityLocation = locations.Value.FirstOrDefault();
var cityExternalId = cityLocation.ExternalId;
var directLocation = await InstaApi.MessagingProcessor
.SendDirectLocationAsync(cityExternalId, requestedThreadId);
var user = await InstaApi.UserProcessor.GetUserAsync("rmt4006");
var userId = user.Value.Pk;
var directProfile = await InstaApi
.MessagingProcessor.SendDirectProfileAsync(userId, requestedThreadId);
A message can have different types, InstagramApiSharp supports 17 different type.
note: first item(message) of a thread:
var firstItem = firstThread.Items.FirstOrDefault();
- if
firstItem.ItemType
wasText
then you should getfirstItem.Text
property. - if
firstItem.ItemType
wasLike
then you should getfirstItem.Text
property. - if
firstItem.ItemType
wasMediaShare
then you should getfirstItem.MediaShare
property. - if
firstItem.ItemType
wasLink
then you should getfirstItem.LinkMedia
property. - if
firstItem.ItemType
wasMedia
then you should getfirstItem.Media
property. - if
firstItem.ItemType
wasStoryShare
then you should getfirstItem.StoryShare
property. - if
firstItem.ItemType
wasRavenMedia
then you should getfirstItem.VisualMedia
property. Note: if you are using API version lower than 61, you must getfirstItem.RavenMedia
property instead. - if
firstItem.ItemType
wasActionLog
then you should getfirstItem.ActionLog
property. - if
firstItem.ItemType
wasProfile
then you should getfirstItem.ProfileMedia
property. - if
firstItem.ItemType
wasPlaceholder
then you should getfirstItem.Placeholder
property. - if
firstItem.ItemType
wasLocation
then you should getfirstItem.LocationMedia
property. - if
firstItem.ItemType
wasFelixShare
then you should getfirstItem.FelixShareMedia
property. (this is for instagram tv shared video) (api version v61 or newer is required) - if
firstItem.ItemType
wasReelShare
then you should getfirstItem.ReelShareMedia
property. - if
firstItem.ItemType
wasVoiceMedia
then you should getfirstItem.VoiceMedia
property. (api version v74 or newer is required) - if
firstItem.ItemType
wasAnimatedMedia
then you should getfirstItem.AnimatedMedia
property. (api version v74 or newer is required) - if
firstItem.ItemType
wasHashtag
then you should getfirstItem.HashtagMedia
property. - if
firstItem.ItemType
wasLiveViewerInvite
then you should getfirstItem.LiveViewerInvite
property. (shared live broadcast)
You can check this quick sample: Direct messaging example class
Iranian Developers (c) 2021 | Bahar 1400