Ejemplo n.º 1
0
        public Message ReplyFacebookMessage(int conversationId, string content, bool isCloseConversation = false)
        {
            var conversation = _conversationService.CheckIfExists(conversationId);

            if (conversation.Source != ConversationSource.FacebookMessage)
            {
                throw SocialExceptions.BadRequest("Conversation source must be facebook message.");
            }

            _conversationService.CheckIfCanReopenWhenReply(conversation);

            var           messages      = Repository.FindAll().Include(t => t.Sender).Include(t => t.Receiver).Where(t => t.ConversationId == conversation.Id).ToList();
            SocialAccount socialAccount = GetSocialAccountFromMessages(messages);

            if (socialAccount == null)
            {
                throw SocialExceptions.BadRequest("Facebook integration account can't be found from conversation.");
            }

            SocialUser recipient = messages.Where(t => t.Sender.Type == SocialUserType.Customer)
                                   .OrderByDescending(t => t.SendTime)
                                   .Select(t => t.Sender).FirstOrDefault();

            // publish message to facebook
            string fbMessageId = _fbClient.PublishMessage(socialAccount.Token, conversation.OriginalId, content);

            if (string.IsNullOrWhiteSpace(fbMessageId))
            {
                return(null);
            }
            // create message
            var message = new Message
            {
                ConversationId = conversation.Id,
                Content        = content,
                SenderId       = socialAccount.Id,
                ReceiverId     = recipient != null ? recipient.Id : socialAccount.Id,
                SendAgentId    = UserContext.UserId,
                SendTime       = DateTime.UtcNow,
                OriginalId     = fbMessageId,
                Source         = MessageSource.FacebookMessage
            };

            Save(message, isCloseConversation);

            return(this.Find(message.Id));
        }