Example #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}'");
                    }
                }
            }
        }
Example #2
0
        public bool AddTwitterUserWaitingForReply(
            MessageIdAndTimestamp messageIdAndTimestamp, TwitterUserIdentifier twitterUserIdentifier)
        {
            if (messageIdAndTimestamp != null &&
                twitterUserIdentifier != null &&
                !_twitterUsersWaitingForReply.ContainsKey(messageIdAndTimestamp))
            {
                _twitterUsersWaitingForReply.Add(messageIdAndTimestamp, twitterUserIdentifier);
                return(true);
            }

            return(false);
        }
Example #3
0
        protected virtual void ReplyInTwitter(Activity activity, TwitterUserIdentifier twitterUserIdentifier)
        {
            if (activity == null || twitterUserIdentifier == null)
            {
                throw new ArgumentNullException("Either the activity or the Twitter user identifier is null");
            }

            string messageId = activity.ReplyToId;

            if (string.IsNullOrEmpty(messageId))
            {
                throw new ArgumentNullException("The activity is missing the 'reply to ID'");
            }

            System.Diagnostics.Debug.WriteLine(
                $"Replying to user '{twitterUserIdentifier.ScreenName}' using message in activity with message ID '{messageId}'");

            _twitterManager.SendMessage(activity.Text, twitterUserIdentifier.TwitterUserId, twitterUserIdentifier.ScreenName);
        }
Example #4
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);
            }
        }