-
Notifications
You must be signed in to change notification settings - Fork 245
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.ItemTypewasTextthen you should getfirstItem.Textproperty. - if
firstItem.ItemTypewasLikethen you should getfirstItem.Textproperty. - if
firstItem.ItemTypewasMediaSharethen you should getfirstItem.MediaShareproperty. - if
firstItem.ItemTypewasLinkthen you should getfirstItem.LinkMediaproperty. - if
firstItem.ItemTypewasMediathen you should getfirstItem.Mediaproperty. - if
firstItem.ItemTypewasStorySharethen you should getfirstItem.StoryShareproperty. - if
firstItem.ItemTypewasRavenMediathen you should getfirstItem.VisualMediaproperty. Note: if you are using API version lower than 61, you must getfirstItem.RavenMediaproperty instead. - if
firstItem.ItemTypewasActionLogthen you should getfirstItem.ActionLogproperty. - if
firstItem.ItemTypewasProfilethen you should getfirstItem.ProfileMediaproperty. - if
firstItem.ItemTypewasPlaceholderthen you should getfirstItem.Placeholderproperty. - if
firstItem.ItemTypewasLocationthen you should getfirstItem.LocationMediaproperty. - if
firstItem.ItemTypewasFelixSharethen you should getfirstItem.FelixShareMediaproperty. (this is for instagram tv shared video) (api version v61 or newer is required) - if
firstItem.ItemTypewasReelSharethen you should getfirstItem.ReelShareMediaproperty. - if
firstItem.ItemTypewasVoiceMediathen you should getfirstItem.VoiceMediaproperty. (api version v74 or newer is required) - if
firstItem.ItemTypewasAnimatedMediathen you should getfirstItem.AnimatedMediaproperty. (api version v74 or newer is required) - if
firstItem.ItemTypewasHashtagthen you should getfirstItem.HashtagMediaproperty. - if
firstItem.ItemTypewasLiveViewerInvitethen you should getfirstItem.LiveViewerInviteproperty. (shared live broadcast)
You can check this quick sample: Direct messaging example class
Iranian Developers (c) 2021 | Bahar 1400