Skip to content

Direct messaging

Ramtin Jokar edited this page Feb 14, 2019 · 17 revisions

You can find all direct messaging functions in IInstaApi.MessagingProcessor class.

How can I get inbox?

var inbox = await InstaApi.MessagingProcessor
    .GetDirectInboxAsync(PaginationParameters.MaxPagesToLoad(1));

How can I get a thread?

var firstThread = inbox.Value.Inbox.Threads.FirstOrDefault();
var requestedThreadId = firstThread.ThreadId;
var threads = await InstaApi.MessagingProcessor
    .GetDirectInboxThreadAsync(requestedThreadId, PaginationParameters.MaxPagesToLoad(1));

How can I get a thread by username?

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);

How can I get ranked recipients?

var rankedRecipients = await InstaApi.MessagingProcessor.GetRankedRecipientsAsync();

How can I get recent recipients?

var recentRecipients = await InstaApi.MessagingProcessor
    .GetRecentRecipientsAsync();

How can I get pending direct(requests)?

var pendingDirect = await InstaApi.MessagingProcessor
    .GetPendingDirectAsync();

How can I send text by thread id?

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?");

How can I send text by username?

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?");

How can I send text to multiple username?

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?");

How can I send photo as direct message?

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.

How can I send photo to multiple user ids as direct message?

var directPhoto = await InstaApi.MessagingProcessor
    .SendDirectPhotoToRecipientsAsync(image, "userId1", "userId2", "userId3", "....");

How can I send disappearing photo as direct message?

var directPhoto = await InstaApi.MessagingProcessor
    .SendDirectDisappearingPhotoAsync(image, InstaViewMode.Replayable, requestedThreadId);

How can I send video as direct message?

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.

How can I send video to multiple user ids as direct message?

var directVideo = await InstaApi.MessagingProcessor
    .SendDirectVideoToRecipientsAsync(videoToUpload, "userId1", "userId2", "userId3", "....");

How can I send disappearing video as direct message?

var directVideo = await InstaApi.MessagingProcessor
    .SendDirectDisappearingVideoAsync(videoToUpload, InstaViewMode.Replayable, requestedThreadId);

How can I send link as direct message?

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);

How can I send location as direct message?

// 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);

How can I send profile as direct message?

var user = await InstaApi.UserProcessor.GetUserAsync("rmt4006");
var userId = user.Value.Pk;

var directProfile = await InstaApi
    .MessagingProcessor.SendDirectProfileAsync(userId, requestedThreadId);

Other notes

For a single item of a thread:

A message can have different types, InstagramApiSharp supports 17 different type.

note: first item(message) of a thread:

var firstItem = firstThread.Items.FirstOrDefault();
  1. if firstItem.ItemType was Text then you should get firstItem.Text property.
  2. if firstItem.ItemType was Like then you should get firstItem.Text property.
  3. if firstItem.ItemType was MediaShare then you should get firstItem.MediaShare property.
  4. if firstItem.ItemType was Link then you should get firstItem.LinkMedia property.
  5. if firstItem.ItemType was Media then you should get firstItem.Media property.
  6. if firstItem.ItemType was StoryShare then you should get firstItem.StoryShare property.
  7. if firstItem.ItemType was RavenMedia then you should get firstItem.VisualMedia property. Note: if you are using API version lower than 61, you must get firstItem.RavenMedia property instead.
  8. if firstItem.ItemType was ActionLog then you should get firstItem.ActionLog property.
  9. if firstItem.ItemType was Profile then you should get firstItem.ProfileMedia property.
  10. if firstItem.ItemType was Placeholder then you should get firstItem.Placeholder property.
  11. if firstItem.ItemType was Location then you should get firstItem.LocationMedia property.
  12. if firstItem.ItemType was FelixShare then you should get firstItem.FelixShareMedia property. (this is for instagram tv shared video) (api version v61 or newer is required)
  13. if firstItem.ItemType was ReelShare then you should get firstItem.ReelShareMedia property.
  14. if firstItem.ItemType was VoiceMedia then you should get firstItem.VoiceMedia property. (api version v74 or newer is required)
  15. if firstItem.ItemType was AnimatedMedia then you should get firstItem.AnimatedMedia property. (api version v74 or newer is required)
  16. if firstItem.ItemType was Hashtag then you should get firstItem.HashtagMedia property.
  17. if firstItem.ItemType was LiveViewerInvite then you should get firstItem.LiveViewerInvite property. (shared live broadcast)

You can check this quick sample: Direct messaging example class