Exemplo n.º 1
0
        protected virtual void OnActivitiesReceived(object sender, IList <Activity> activities)
        {
            foreach (Activity activity in activities)
            {
                string messageId = activity.ReplyToId;

                if (!string.IsNullOrEmpty(messageId))
                {
                    MessageIdAndTimestamp messageIdAndTimestamp = new MessageIdAndTimestamp(messageId);

                    TwitterUserIdentifier twitterUserIdentifier =
                        _messageAndUserIdCache.GetTwitterUserWaitingForReply(messageIdAndTimestamp);

                    if (twitterUserIdentifier != null)
                    {
                        ReplyInTwitter(activity, twitterUserIdentifier);
                    }
                    else
                    {
                        // Looks like we received the reply activity before we got back
                        // the response from sending the original message to the bot
                        _messageAndUserIdCache.AddPendingReplyFromBotToTwitterUser(messageIdAndTimestamp, activity);

                        System.Diagnostics.Debug.WriteLine($"Stored activity with message ID '{messageId}'");
                    }
                }
            }
        }
Exemplo n.º 2
0
        public Activity GetPendingReplyFromBotToTwitterUser(MessageIdAndTimestamp messageIdAndTimestamp)
        {
            if (messageIdAndTimestamp != null &&
                _pendingRepliesFromBotToTwitterUser.ContainsKey(messageIdAndTimestamp))
            {
                return(_pendingRepliesFromBotToTwitterUser[messageIdAndTimestamp]);
            }

            return(null);
        }
Exemplo n.º 3
0
        public TwitterUserIdentifier GetTwitterUserWaitingForReply(MessageIdAndTimestamp messageIdAndTimestamp)
        {
            if (messageIdAndTimestamp != null &&
                _twitterUsersWaitingForReply.ContainsKey(messageIdAndTimestamp))
            {
                return(_twitterUsersWaitingForReply[messageIdAndTimestamp]);
            }

            return(null);
        }
Exemplo n.º 4
0
        public bool AddTwitterUserWaitingForReply(
            MessageIdAndTimestamp messageIdAndTimestamp, TwitterUserIdentifier twitterUserIdentifier)
        {
            if (messageIdAndTimestamp != null &&
                twitterUserIdentifier != null &&
                !_twitterUsersWaitingForReply.ContainsKey(messageIdAndTimestamp))
            {
                _twitterUsersWaitingForReply.Add(messageIdAndTimestamp, twitterUserIdentifier);
                return(true);
            }

            return(false);
        }
Exemplo n.º 5
0
        public bool AddPendingReplyFromBotToTwitterUser(
            MessageIdAndTimestamp messageIdAndTimestamp, Activity pendingReplyActivity)
        {
            if (messageIdAndTimestamp != null &&
                !string.IsNullOrEmpty(messageIdAndTimestamp.DirectLineMessageId) &&
                pendingReplyActivity != null &&
                !_pendingRepliesFromBotToTwitterUser.ContainsKey(messageIdAndTimestamp))
            {
                _pendingRepliesFromBotToTwitterUser.Add(messageIdAndTimestamp, pendingReplyActivity);
                return(true);
            }

            return(false);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Sends the message in the received tweet to the bot via Direct Line.
        /// If we get a valid response indicating that the message was received by the bot,
        /// we will store the Twitter user identifiers (to be able to reply back).
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="messageEventArgs">Contains the Twitter message details.</param>
        protected virtual async void OnTweetReceivedAsync(object sender, Tweetinvi.Events.MessageEventArgs messageEventArgs)
        {
            string messageId = await _directLineManager.SendMessageAsync(
                messageEventArgs.Message.Text,
                messageEventArgs.Message.SenderId.ToString(),
                messageEventArgs.Message.SenderScreenName);

            if (string.IsNullOrEmpty(messageId))
            {
                System.Diagnostics.Debug.WriteLine(
                    $"Failed to send the message from user '{messageEventArgs.Message.SenderScreenName}' to the bot - message text was '{messageEventArgs.Message.Text}'");
            }
            else
            {
                System.Diagnostics.Debug.WriteLine(
                    $"Message from user '{messageEventArgs.Message.SenderScreenName}' successfully sent to the bot - message ID is '{messageId}'");

                MessageIdAndTimestamp messageIdAndTimestamp = new MessageIdAndTimestamp(messageId);

                TwitterUserIdentifier twitterUserIdentifier = new TwitterUserIdentifier()
                {
                    TwitterUserId = messageEventArgs.Message.SenderId,
                    ScreenName    = messageEventArgs.Message.SenderScreenName
                };

                // Store the Twitter user details so that we know who to reply to
                _messageAndUserIdCache.AddTwitterUserWaitingForReply(messageIdAndTimestamp, twitterUserIdentifier);

                _directLineManager.StartPolling();
            }

            // Check for pending activities
            foreach (ActivityForTwitterUserBundle pendingMessage
                     in _messageAndUserIdCache.GetPendingRepliesToTwitterUsers())
            {
                ReplyInTwitter(
                    pendingMessage.ActivityForTwitterUser,
                    pendingMessage.TwitterUserIdentifier);
            }
        }
Exemplo n.º 7
0
 public bool RemoveTwitterUserWaitingForReply(MessageIdAndTimestamp messageIdAndTimestamp)
 {
     return(_twitterUsersWaitingForReply.Remove(messageIdAndTimestamp));
 }
Exemplo n.º 8
0
 public bool RemovePendingReplyFromBotToTwitterUser(MessageIdAndTimestamp messageIdAndTimestamp)
 {
     return(_pendingRepliesFromBotToTwitterUser.Remove(messageIdAndTimestamp));
 }
Exemplo n.º 9
0
        public void LazyCleanupTest()
        {
            int minCacheExpiryInSeconds     = 5;
            int numberOfDualRecordsToCreate = 4;

            Assert.AreEqual(true, minCacheExpiryInSeconds > numberOfDualRecordsToCreate); // Sanity check

            IMessageAndUserIdCache cache = new InMemoryMessageAndUserIdCache(minCacheExpiryInSeconds);

            DateTime recordCreationStartedTime = DateTime.Now;

            for (int i = 0; i < numberOfDualRecordsToCreate; ++i)
            {
                DateTime dateTimeStart = DateTime.Now;

                MessageIdAndTimestamp messageIdAndTimestamp = new MessageIdAndTimestamp(i.ToString());

                bool recordAddedSuccessfully = cache.AddPendingReplyFromBotToTwitterUser(
                    messageIdAndTimestamp,
                    new Activity("message", $"{i}", dateTimeStart, dateTimeStart, $"serviceUrl{i}", $"channelId{i}"));

                Assert.AreEqual(true, recordAddedSuccessfully);

                recordAddedSuccessfully = cache.AddTwitterUserWaitingForReply(
                    messageIdAndTimestamp,
                    new TwitterUserIdentifier()
                {
                    ScreenName = $"screenName{i}", TwitterUserId = i
                });

                Assert.AreEqual(true, recordAddedSuccessfully);

                System.Diagnostics.Debug.WriteLine($"Total of {(i + 1) * 2} records created");

                DateTime dateTimeEnd        = DateTime.Now;
                TimeSpan timeElapsed        = dateTimeEnd - dateTimeStart;
                long     millisecondsPassed = timeElapsed.Milliseconds;

                if (millisecondsPassed < 1000)
                {
                    Thread.Sleep(1000 - (int)millisecondsPassed);
                }
            }

            // (numberOfDualRecordsToCreate) seconds should have passed now

            int numberOfDualRecords = cache.GetPendingRepliesToTwitterUsers().Count;

            Assert.AreEqual(numberOfDualRecordsToCreate, numberOfDualRecords);

            bool timeToEnd = false;
            int  millisecondsExpectedUntilAllRecordsExpired =
                minCacheExpiryInSeconds * 1000 + numberOfDualRecordsToCreate * 1000 + 1000;

            while (!timeToEnd)
            {
                cache.GetPendingRepliesToTwitterUsers(); // This should execute lazy purge

                Assert.AreEqual(true, cache.GetPendingRepliesToTwitterUsers().Count <= numberOfDualRecords);
                numberOfDualRecords = cache.GetPendingRepliesToTwitterUsers().Count;

                System.Diagnostics.Debug.WriteLine($"Number of dual records is now {numberOfDualRecords}");

                Thread.Sleep(900);
                timeToEnd = ((DateTime.Now - recordCreationStartedTime).TotalMilliseconds
                             > millisecondsExpectedUntilAllRecordsExpired);
            }

            Assert.AreEqual(0, numberOfDualRecords);
        }