Ejemplo n.º 1
0
        public Message ReplyTwitterDirectMessage(int conversationId, string message, bool isCloseConversation = false)
        {
            var twitterService = DependencyResolver.Resolve <ITwitterService>();

            Conversation conversation = _conversationService.CheckIfExists(conversationId);

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

            _conversationService.CheckIfCanReopenWhenReply(conversation);

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

            if (twitterAccount == null)
            {
                throw SocialExceptions.BadRequest($"Twitter account has been deleted.");
            }

            var customer = messages.Where(t => t.IsDeleted == false && t.Sender.Type == SocialUserType.Customer)
                           .OrderByDescending(t => t.SendTime).Select(t => t.Sender).FirstOrDefault();

            if (customer == null)
            {
                throw new NotSupportedException($"Cant't find last message from conversation. Conversation Id={conversationId}.");
            }

            IUser prviousTwitterUser = twitterService.GetUser(twitterAccount, long.Parse(customer.OriginalId));

            if (prviousTwitterUser == null)
            {
                throw new NotSupportedException($"Cant't find twitter user. Conversation Id={conversationId}.");
            }

            // publish twitter direct message
            IMessage twitterDirectMessage = twitterService.PublishMessage(twitterAccount, prviousTwitterUser, message);

            // create message to db
            Message directMessage = TwitterConverter.ConvertToMessage(twitterDirectMessage);

            directMessage.ConversationId = conversation.Id;
            directMessage.SenderId       = twitterAccount.Id;
            directMessage.SendAgentId    = UserContext.UserId;
            directMessage.ReceiverId     = customer.Id;

            Save(directMessage, isCloseConversation);

            return(this.Find(directMessage.Id));
        }
Ejemplo n.º 2
0
        public Message GetTweetMessage(SocialAccount account, long tweetId)
        {
            _twitterClient.SetUserCredentials(account.Token, account.TokenSecret);
            var tweet = _twitterClient.GetTweet(tweetId);

            if (tweet == null)
            {
                return(null);
            }

            var message = TwitterConverter.ConvertToMessage(tweet);

            message.Sender = new SocialUser
            {
                Name         = tweet.CreatedBy.Name,
                Avatar       = tweet.CreatedBy.ProfileImageUrl,
                ScreenName   = tweet.CreatedBy.ScreenName,
                OriginalId   = tweet.CreatedBy.IdStr,
                OriginalLink = TwitterHelper.GetUserUrl(tweet.CreatedBy.ScreenName)
            };
            return(message);
        }
Ejemplo n.º 3
0
        public Message ReplyTwitterTweetMessage(int conversationId, int twitterAccountId, string content, bool isCloseConversation = false)
        {
            var          twitterService = DependencyResolver.Resolve <ITwitterService>();
            Conversation conversation   = _conversationService.CheckIfExists(conversationId);

            if (conversation.Source != ConversationSource.TwitterTweet)
            {
                throw SocialExceptions.BadRequest("Conversation source must be twitter tweet.");
            }

            SocialAccount twitterAccount = _socialAccountService.FindAllWithDeleted().FirstOrDefault(t => t.Id == twitterAccountId);

            if (twitterAccount != null && twitterAccount.IsDeleted)
            {
                twitterAccount = _socialAccountService.FindAll().FirstOrDefault(t => t.SocialUser.OriginalId == twitterAccount.SocialUser.OriginalId);
            }
            if (twitterAccount == null)
            {
                throw SocialExceptions.BadRequest("Twitter account has been deleted.");
            }

            var     messages         = Repository.FindAll().Include(t => t.Sender).Include(t => t.Receiver).Where(t => t.ConversationId == conversation.Id).ToList();
            var     previousMessages = messages.Where(t => t.SenderId != twitterAccountId).OrderByDescending(t => t.SendTime);
            Message replyMessage     = null;

            foreach (var previousMessage in previousMessages)
            {
                if (previousMessage.IsDeleted)
                {
                    continue;
                }
                //if (previousMessage.Sender.Type == SocialUserType.IntegrationAccount)
                //{
                //    continue;
                //}

                ITweet previousTweet = twitterService.GetTweet(twitterAccount, long.Parse(previousMessage.OriginalId));
                if (previousTweet != null && !previousTweet.IsTweetDestroyed)
                {
                    // publish tweet
                    if (!content.Contains("@" + previousTweet.CreatedBy.ScreenName))
                    {
                        content = "@" + previousTweet.CreatedBy.ScreenName + " " + content;
                    }
                    var replyTweet = twitterService.ReplyTweet(twitterAccount, previousTweet, content);
                    if (replyTweet == null)
                    {
                        continue;
                    }

                    // add message
                    replyMessage = TwitterConverter.ConvertToMessage(replyTweet);
                    replyMessage.ConversationId = conversation.Id;
                    replyMessage.SenderId       = twitterAccount.Id;
                    replyMessage.SendAgentId    = UserContext.UserId;
                    replyMessage.ReceiverId     = previousMessage.Sender.Id;

                    Save(replyMessage, isCloseConversation);

                    break;
                }
            }

            if (replyMessage == null)
            {
                throw SocialExceptions.OriginalPostOrTweetHasBeenDeleted();
            }

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