Example #1
0
        private void NormarlizeAttachmentType(FbMessageAttachment attachment)
        {
            if (!string.IsNullOrEmpty(attachment.MimeType))
            {
                if (attachment.MimeType.StartsWith("image/", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (attachment.Type != MessageAttachmentType.AnimatedImage)
                    {
                        attachment.Type = MessageAttachmentType.Image;
                    }
                }
                else if (attachment.MimeType.StartsWith("video/", StringComparison.InvariantCultureIgnoreCase))
                {
                    attachment.Type = MessageAttachmentType.Video;
                }

                else if (attachment.MimeType.StartsWith("audio/", StringComparison.InvariantCultureIgnoreCase))
                {
                    attachment.Type = MessageAttachmentType.Audio;
                }
                else
                {
                    attachment.Type = MessageAttachmentType.File;
                }
            }
        }
Example #2
0
        public async Task <FbPagingData <FbConversation> > GetConversationsMessages(string pageId, string token)
        {
            Checker.NotNullOrWhiteSpace(token, nameof(token));
            Checker.NotNullOrWhiteSpace(token, nameof(pageId));
            FacebookClient client = new FacebookClient(token);

            long since = DateTime.UtcNow.AddHours(-1).ToUnixTimeSeconds();

            string sharesFields   = $"shares{{link,name,id}}";
            string messagesFields = $"messages.since({since}){{from,to,message,id,created_time,attachments,{sharesFields}}}";
            string url            = $"/{pageId}/conversations?fields=id,updated_time,{messagesFields}";

            FbPagingData <FbConversation> PagingConversation = new FbPagingData <FbConversation>();

            List <FbConversation> Conversations = new List <FbConversation>();
            dynamic fbConversations             = await client.GetTaskAsync(url);

            FbCursors FbCursors = new FbCursors
            {
                before = fbConversations.paging != null ? fbConversations.paging.cursors.before : null,
                after  = fbConversations.paging != null ? fbConversations.paging.cursors.after : null
            };
            FbPaging FbPaging = new FbPaging
            {
                cursors  = FbCursors,
                next     = fbConversations.paging != null ? fbConversations.paging.next : null,
                previous = fbConversations.paging != null ? fbConversations.paging.previous : null
            };

            PagingConversation.paging = FbPaging;
            foreach (var conversation in fbConversations.data)
            {
                List <FbMessage> FbMessages = new List <FbMessage>();
                foreach (var fbMessage in conversation.messages.data)
                {
                    dynamic receiver = fbMessage.to.data[0];
                    var     message  = new FbMessage
                    {
                        Id            = fbMessage.id,
                        SendTime      = Convert.ToDateTime(fbMessage.created_time).ToUniversalTime(),
                        SenderId      = fbMessage.from.id,
                        SenderEmail   = fbMessage.from.email,
                        ReceiverId    = receiver.id,
                        ReceiverEmail = receiver.email,
                        Content       = fbMessage.message
                    };

                    if (fbMessage.attachments != null)
                    {
                        foreach (dynamic attachmnent in fbMessage.attachments.data)
                        {
                            var messageAttachment = new FbMessageAttachment
                            {
                                Id       = attachmnent.id,
                                MimeType = attachmnent.mime_type,
                                Name     = attachmnent.name
                            };

                            if (attachmnent.size != null)
                            {
                                messageAttachment.Size = attachmnent.size;
                            }

                            if (attachmnent.file_url != null)
                            {
                                messageAttachment.Type = MessageAttachmentType.File;
                                messageAttachment.Url  = attachmnent.file_url;
                            }

                            if (attachmnent.image_data != null)
                            {
                                messageAttachment.Type       = MessageAttachmentType.Image;
                                messageAttachment.Url        = attachmnent.image_data.url;
                                messageAttachment.PreviewUrl = attachmnent.image_data.preview_url;
                            }

                            if (attachmnent.video_data != null)
                            {
                                messageAttachment.Type       = MessageAttachmentType.Video;
                                messageAttachment.Url        = attachmnent.video_data.url;
                                messageAttachment.PreviewUrl = attachmnent.video_data.preview_url;
                            }
                            NormarlizeAttachmentType(messageAttachment);
                            message.Attachments.Add(messageAttachment);
                        }
                    }

                    if (fbMessage.shares != null)
                    {
                        foreach (dynamic share in fbMessage.shares.data)
                        {
                            var messageShare = new FbMessageAttachment
                            {
                                Name = share.name,
                                Id   = share.id,
                                Url  = share.link,
                            };

                            if (!string.IsNullOrWhiteSpace(messageShare.Url) && string.IsNullOrWhiteSpace(messageShare.MimeType))
                            {
                                Uri uri = new Uri(messageShare.Url);
                                messageShare.MimeType = uri.GetMimeType();
                                if (messageShare.MimeType.Contains("image"))
                                {
                                    messageShare.Type = MessageAttachmentType.Image;
                                }
                            }

                            if (string.IsNullOrWhiteSpace(messageShare.Url) && !string.IsNullOrWhiteSpace(messageShare.Name) && string.IsNullOrWhiteSpace(message.Content))
                            {
                                message.Content = messageShare.Name;
                            }

                            if (!string.IsNullOrWhiteSpace(messageShare.Url))
                            {
                                NormarlizeAttachmentType(messageShare);
                                message.Attachments.Add(messageShare);
                            }
                        }
                    }

                    FbMessages.Add(message);
                }
                FbCursors messageFbCursors = new FbCursors
                {
                    before = conversation.messages.paging.cursors.before,
                    after  = conversation.messages.paging.cursors.after
                };
                FbPaging messageFbPaging = new FbPaging
                {
                    cursors  = messageFbCursors,
                    next     = conversation.messages.paging.next,
                    previous = conversation.messages.paging.previous
                };
                FbPagingData <FbMessage> PagingFbMessage = new FbPagingData <FbMessage>
                {
                    data   = FbMessages,
                    paging = messageFbPaging
                };
                FbConversation FbConversation = new FbConversation
                {
                    Id         = conversation.id,
                    UpdateTime = Convert.ToDateTime(conversation.updated_time).ToUniversalTime(),
                    Messages   = PagingFbMessage
                };
                Conversations.Add(FbConversation);
            }
            PagingConversation.data = Conversations;

            return(PagingConversation);
        }
Example #3
0
        public async Task <IList <FbMessage> > GetMessagesFromConversationId(string token, string fbConversationId, int limit)
        {
            List <FbMessage> messages = new List <FbMessage>();

            Checker.NotNullOrWhiteSpace(token, nameof(token));
            Checker.NotNullOrWhiteSpace(fbConversationId, nameof(fbConversationId));
            FacebookClient client     = new FacebookClient(token);
            string         url        = $"/{fbConversationId}/messages?fields=from,to,message,id,created_time,attachments,shares{{link,name,id}}&limit={limit}";
            dynamic        fbMessages = await client.GetTaskAsync(url);

            foreach (var fbMessage in fbMessages.data)
            {
                dynamic receiver = fbMessage.to.data[0];
                var     message  = new FbMessage
                {
                    Id            = fbMessage.id,
                    SendTime      = Convert.ToDateTime(fbMessage.created_time).ToUniversalTime(),
                    SenderId      = fbMessage.from.id,
                    SenderEmail   = fbMessage.from.email,
                    ReceiverId    = receiver.id,
                    ReceiverEmail = receiver.email,
                    Content       = fbMessage.message
                };

                if (fbMessage.attachments != null)
                {
                    foreach (dynamic attachmnent in fbMessage.attachments.data)
                    {
                        var messageAttachment = new FbMessageAttachment
                        {
                            Id       = attachmnent.id,
                            MimeType = attachmnent.mime_type,
                            Name     = attachmnent.name
                        };

                        if (attachmnent.size != null)
                        {
                            messageAttachment.Size = attachmnent.size;
                        }

                        if (attachmnent.file_url != null)
                        {
                            messageAttachment.Type = MessageAttachmentType.File;
                            messageAttachment.Url  = attachmnent.file_url;
                        }

                        if (attachmnent.image_data != null)
                        {
                            messageAttachment.Type       = MessageAttachmentType.Image;
                            messageAttachment.Url        = attachmnent.image_data.url;
                            messageAttachment.PreviewUrl = attachmnent.image_data.preview_url;
                        }

                        if (attachmnent.video_data != null)
                        {
                            messageAttachment.Type       = MessageAttachmentType.Video;
                            messageAttachment.Url        = attachmnent.video_data.url;
                            messageAttachment.PreviewUrl = attachmnent.video_data.preview_url;
                        }

                        message.Attachments.Add(messageAttachment);
                    }
                }

                if (fbMessage.shares != null)
                {
                    foreach (dynamic share in fbMessage.shares.data)
                    {
                        var messageShare = new FbMessageAttachment
                        {
                            Name = share.name,
                            Id   = share.id,
                            Url  = share.link,
                        };

                        if (!string.IsNullOrWhiteSpace(messageShare.Url) && string.IsNullOrWhiteSpace(messageShare.MimeType))
                        {
                            Uri uri = new Uri(messageShare.Url);
                            messageShare.MimeType = uri.GetMimeType();
                            if (messageShare.MimeType.Contains("image"))
                            {
                                messageShare.Type = MessageAttachmentType.Image;
                            }
                        }

                        if (string.IsNullOrWhiteSpace(messageShare.Url) && !string.IsNullOrWhiteSpace(messageShare.Name) && string.IsNullOrWhiteSpace(message.Content))
                        {
                            message.Content = messageShare.Name;
                        }

                        if (!string.IsNullOrWhiteSpace(messageShare.Url))
                        {
                            message.Attachments.Add(messageShare);
                        }
                    }
                }

                messages.Add(message);
            }

            return(messages);
        }