private async Task HandleNoQuestion(IDialogContext context, Microsoft.Bot.Connector.Activity activity, QuestionModel questionModel, string channelId) { var messageId = questionModel.MessageId; // We stripped the @mention and html tags. If nothing remains then this means the user forgot to tag the bot in the original question and tagged it in a reply, so we need to handle it // Get the ID of the parent message (which should be the root) var thisTeamsMessage = await GetMessage(questionModel.GroupId, channelId, messageId); // check for null below if (thisTeamsMessage == null) { // Get root message var rootTeamsMessage = await GetRootMessage(questionModel.GroupId, channelId, messageId, questionModel.ConversationId); var question = MicrosoftTeamsChannelHelper.StripHtmlTags(rootTeamsMessage.Body.Content); messageId = rootTeamsMessage.Id; var conversationId = channelId + @";messageid=" + messageId; questionModel.QuestionText = question; questionModel.MessageId = messageId; questionModel.ConversationId = conversationId; // Get original poster var connector = new ConnectorClient(new Uri(activity.ServiceUrl)); IList <ChannelAccount> members = await connector.Conversations.GetConversationMembersAsync(questionModel.TeamId); var teamsMembers = members.AsTeamsChannelAccounts(); var originalPosterAsTeamsChannelAccount = teamsMembers.Where(x => x.ObjectId == rootTeamsMessage.From.User.Id).FirstOrDefault(); var userUpn = originalPosterAsTeamsChannelAccount.UserPrincipalName; var user = SQLService.GetUser(userUpn); // Handle if the bot gets tagged again in the same set of replies if (SQLService.DoesConversationIdExist(conversationId)) { // WE DO NOTHING! } else { // get courseID var courseID = SQLService.GetCourseIDByName(questionModel.TeamName.Trim()); var course = SQLService.GetCourse(courseID); questionModel.CourseID = courseID; await HandleQuestionWorkflow(context, activity, course, questionModel); } } else { // Empty was the root, which means the user simply forgot to ask a question. await context.PostAsync($"If you have a question, please include it as part of your message."); } }
private async Task HandleChannelConversation(IDialogContext context, Microsoft.Bot.Connector.Activity activity) { var connector = new ConnectorClient(new Uri(activity.ServiceUrl)); // Channel conversation // Get the question text, and team and channel details var question = activity.Text; var messageId = activity.Id; var conversationId = activity.Conversation.Id; var channelData = activity.GetChannelData <TeamsChannelData>(); string tenantId = channelData.Tenant.Id; string teamId = channelData.Team.Id; string channelId = channelData.Channel.Id; TeamDetails teamDetails = connector.GetTeamsConnectorClient().Teams.FetchTeamDetails(teamId); var teamName = teamDetails.Name; var groupId = teamDetails.AadGroupId; ConversationList channelList = connector.GetTeamsConnectorClient().Teams.FetchChannelList(teamId); var channel = channelList.Conversations.Where(x => x.Id == channelId).FirstOrDefault(); var channelName = channel?.Name; var topic = channelName ?? "General"; IList <ChannelAccount> members = await connector.Conversations.GetConversationMembersAsync(teamId); var teamsMembers = members.AsTeamsChannelAccounts(); // Get original poster var originalPosterAsTeamsChannelAccount = teamsMembers.Where(x => x.ObjectId == activity.From.Properties["aadObjectId"].ToString()).FirstOrDefault(); var userUpn = originalPosterAsTeamsChannelAccount.UserPrincipalName; var user = SQLService.GetUser(userUpn); // strip @bot mention for teams if (activity.ChannelId == "msteams") { activity = MicrosoftTeamsChannelHelper.StripAtMentionText(activity); } else { activity.Text = activity.Text.Trim(); } // strip the html tags question = MicrosoftTeamsChannelHelper.StripHtmlTags(activity.Text); var questionModel = new QuestionModel() { ID = 0, TenantId = tenantId, GroupId = groupId, TeamId = teamId, TeamName = teamName, ConversationId = conversationId, MessageId = messageId, Topic = topic, QuestionText = question, QuestionSubmitted = DateTime.Now, OriginalPoster = user, Link = CreateLink(conversationId, tenantId, groupId, messageId, teamName, topic), }; if (string.IsNullOrEmpty(question)) { await HandleNoQuestion(context, activity, questionModel, channelId); } else { // get courseID var courseID = SQLService.GetCourseIDByName(teamName.Trim()); var course = SQLService.GetCourse(courseID); questionModel.CourseID = courseID; await HandleQuestionWorkflow(context, activity, course, questionModel); } }