/// <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.MatchedTweetReceivedEventArgs messageEventArgs) { var tweet = messageEventArgs.Tweet; var conversationId = string.IsNullOrEmpty(tweet.InReplyToStatusIdStr) ? null : _conversationCache.GetConversationOfTweet(new IdAndTimestamp(tweet.InReplyToStatusIdStr)); var sendResult = await _directLineManager.SendMessageAsync( conversationId, tweet.Text, tweet.CreatedBy.UserIdentifier.IdStr, tweet.CreatedBy.UserIdentifier.ScreenName); if (sendResult == null) { Debug.WriteLine( $"Failed to send the message from user '{tweet.CreatedBy.UserIdentifier.ScreenName}' to the bot - message text was '{tweet.Text}'"); } else { Debug.WriteLine( $"Message from user '{tweet.CreatedBy.UserIdentifier.ScreenName}' successfully sent to the bot - message ID is '{sendResult.MessageId}'"); _conversationCache.PutLatestTweetOfConversation(new IdAndTimestamp(sendResult.Conversation.ConversationId), tweet); _directLineManager.StartPolling(); } }
/// <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); } }