Show messages (DM) in conversation view

Mar 31, 2014 at 7:01 PM
Hi!
I'm trying to group DM by sender and then show them in a conversation view, how can I do this on Windows Phone?

Thank you for your great work,
Alessandro
Mar 31, 2014 at 11:25 PM
Edited Mar 31, 2014 at 11:30 PM
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 received
    var 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
Marked as answer by linvi on 4/4/2014 at 4:45 PM
Apr 1, 2014 at 5:33 PM
Thank you!
I've another question, I'm using the streaming API but I haven't found how to stream the mentions. How can I do?

Thanks again!
Apr 2, 2014 at 12:21 AM
Hi,

I am not sure what you mean by streaming mentions.

If you want to get all the tweets with a specific mention I think you want to use the filteredstream:
var fs = Stream.CreateFilteredStream();
fs.AddTrack("@MENTION_USER_NAME");
fs.MatchingTweetReceived += (sender, args) =>
{
    // Mention detected!
};
fs.StartStreamMatchingAllConditions();
If you want to receive all the tweets related with your account (friends/followers...) you can use the UserStream:
var us = Stream.CreateUserStream();
us.TweetCreatedByAnyone += (sender, args) =>
{
    var mention = args.Tweet.UserMentions;
    if (mention != null)
    {
        if (mention.Any(x => x.ScreenName == "MENTION_USER_NAME"))
        {
            // The username has been detected within this tweet!
        }
    }
};
us.StartStream();
Is this of any help?
Linvi