Quantcast
Channel: Tweetinvi a friendly Twitter C# library
Viewing all articles
Browse latest Browse all 4126

New Post: Show messages (DM) in conversation view

$
0
0
Hi,

The issue with messages is that they are pretty limited in term of number. Twitter allow the developers to access up to 800 messages but by default Tweetinvi only allows up to 200 because it using a simple query.

If 200 is enough you can just do the following:
var messages = await MessageAsync.GetLatestMessagesReceived(200);
Dictionary<IUser, IEnumerable<IMessage>> messagesPerUser =  messages.GroupBy(x => x.SenderId).ToDictionary(g => g.First().Sender, g => g.Select(m => m));
If it is not you can create a custom query but nothing has yet been implement for Messages.
// Get the first result set
IEnumerable<IMessageDTO> messagesDTO = await TwitterAccessorAsync.ExecuteGETQuery<IEnumerable<IMessageDTO>>("https://api.twitter.com/1.1/direct_messages.json?count=200");
var dtoResults = messagesDTO.ToList();

// Get the minimum id (get the older message)var minId = dtoResults.Min(x => x.Id) - 1;

while (messagesDTO != null&& messagesDTO.Count() == 200)
{
    // Set the min message as being the max so that no younger message can be receivedvar query = String.Format("https://api.twitter.com/1.1/direct_messages.json?max_id={0}&count=200", minId);

    // Get the new list of result
    messagesDTO = await TwitterAccessorAsync.ExecuteGETQuery<IEnumerable<IMessageDTO>>(query);
    dtoResults.AddRange(messagesDTO);
    minId = messagesDTO.Min(x => x.Id) - 1;
}

var messages = Message.MessageFactory.GenerateMessagesFromMessagesDTO(messagesDTO);

// Get the list of messages per user
Dictionary<IUser, IEnumerable<IMessage>> messagesPerUser =  messages.GroupBy(x => x.SenderId).ToDictionary(g => g.First().Sender, g => g.Select(m => m));
Hope it helps.
Linvi

Viewing all articles
Browse latest Browse all 4126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>