/// <summary> /// On Teams Messaging Extension Submit Action Async. /// </summary> /// <param name="turnContext">turnContext.</param> /// <param name="action">action.</param> /// <param name="cancellationToken">cancellationToken.</param> /// <returns>.</returns> protected override async Task <MessagingExtensionActionResponse> OnTeamsMessagingExtensionSubmitActionAsync(ITurnContext <IInvokeActivity> turnContext, MessagingExtensionAction action, CancellationToken cancellationToken) { _telemetry.TrackEvent("OnTeamsMessagingExtensionSubmitActionAsync"); ReflectionDataRepository reflectionDataRepository = new ReflectionDataRepository(_configuration, _telemetry); try { TaskInfo taskInfo = JsonConvert.DeserializeObject <TaskInfo>(action.Data.ToString()); switch (taskInfo.action) { case "reflection": return(await OnTeamsMessagingExtensionFetchTaskAsync(turnContext, action, cancellationToken)); case "sendAdaptiveCard": try { var name = (turnContext.Activity.From.Name).Split(); taskInfo.postCreateBy = name[0] + ' ' + name[1]; taskInfo.postCreatedByEmail = await _dbHelper.GetUserEmailId(turnContext); taskInfo.channelID = turnContext.Activity.TeamsGetChannelId(); taskInfo.postSendNowFlag = (taskInfo.executionTime == "Send now") ? true : false; taskInfo.IsActive = true; taskInfo.questionRowKey = Guid.NewGuid().ToString(); taskInfo.recurrsionRowKey = Guid.NewGuid().ToString(); taskInfo.reflectionRowKey = Guid.NewGuid().ToString(); taskInfo.serviceUrl = turnContext.Activity.ServiceUrl; taskInfo.teantId = turnContext.Activity.Conversation.TenantId; taskInfo.scheduleId = Guid.NewGuid().ToString(); await _dbHelper.SaveReflectionDataAsync(taskInfo); if (taskInfo.postSendNowFlag == true) { var typingActivity = MessageFactory.Text(string.Empty); typingActivity.Type = ActivityTypes.Typing; await turnContext.SendActivityAsync(typingActivity); var adaptiveCard = _cardHelper.CreateNewReflect(taskInfo); var message = MessageFactory.Attachment(new Attachment { ContentType = AdaptiveCard.ContentType, Content = adaptiveCard }); var resultid = await turnContext.SendActivityAsync(message, cancellationToken); ReflectionDataEntity reflectData = await reflectionDataRepository.GetReflectionData(taskInfo.reflectionID); reflectData.ReflectMessageId = resultid.Id; await reflectionDataRepository.InsertOrMergeAsync(reflectData); try { var feedbackCard = _cardHelper.FeedBackCard(new Dictionary <int, List <FeedbackDataEntity> >(), taskInfo.reflectionID, taskInfo.question); Attachment attachmentfeedback = new Attachment() { ContentType = AdaptiveCard.ContentType, Content = feedbackCard, }; using var connector = new ConnectorClient(new Uri(reflectData.ServiceUrl), _configuration["MicrosoftAppId"], _configuration["MicrosoftAppPassword"]); var conversationId = $"{reflectData.ChannelID};messageid={reflectData.ReflectMessageId}"; var replyActivity = MessageFactory.Attachment(new Attachment { ContentType = AdaptiveCard.ContentType, Content = feedbackCard }); replyActivity.Conversation = new ConversationAccount(id: conversationId); var resultfeedback = await connector.Conversations.SendToConversationAsync((Activity)replyActivity, cancellationToken); reflectData.MessageID = resultfeedback.Id; // update messageid in reflection table await reflectionDataRepository.InsertOrMergeAsync(reflectData); } catch (System.Exception e) { _telemetry.TrackException(e); Console.WriteLine(e.Message.ToString()); } } return(null); } catch (Exception ex) { _telemetry.TrackException(ex); return(null); } case "ManageRecurringPosts": var postCreatedByEmail = await _dbHelper.GetUserEmailId(turnContext); var response = new MessagingExtensionActionResponse() { Task = new TaskModuleContinueResponse() { Value = new TaskModuleTaskInfo() { Height = 600, Width = 780, Title = "Track how you're feeling every day.", Url = this._configuration["BaseUri"] + "/ManageRecurringPosts/" + postCreatedByEmail + "?pathfromindex=true" }, }, }; return(response); case "OpenDetailfeedback": var responsefeedback = new MessagingExtensionActionResponse() { Task = new TaskModuleContinueResponse() { Value = new TaskModuleTaskInfo() { Height = 600, Width = 780, Title = "Track how you're feeling every day.", Url = this._configuration["BaseUri"] + "/openReflectionFeedback/" + taskInfo.reflectionID + "/" + taskInfo.feedback, }, }, }; return(responsefeedback); case "removeposts": try { var activity = Activity.CreateMessageActivity(); if (taskInfo.isDelete) { var messageId = await _dbHelper.RemoveReflectionId(taskInfo.messageID); if (messageId != null) { await turnContext.DeleteActivityAsync(messageId); } await turnContext.DeleteActivityAsync(taskInfo.messageID); activity.Text = "This post has been removed"; await turnContext.SendActivityAsync(activity); return(null); } else { return(null); } } catch (Exception ex) { _telemetry.TrackException(ex); return(null); } default: return(null); } ; } catch (Exception ex) { _telemetry.TrackException(ex); return(null); } }
/// <summary> /// RunJob. /// </summary> /// <param name="state">state.</param> private async void RunJob(object state) { _telemetry.TrackEvent("RunJob"); try { ChannelAccount channelAccount = new ChannelAccount(_configuration["MicrosoftAppId"]); Attachment attachment = new Attachment(); TeamsChannelData channelData = new TeamsChannelData() { Notification = new NotificationInfo(true) }; RecurssionDataRepository recurssionDataRepository = new RecurssionDataRepository(_configuration, _telemetry); ReflectionDataRepository reflectionDataRepository = new ReflectionDataRepository(_configuration, _telemetry); QuestionsDataRepository questiondatarepository = new QuestionsDataRepository(_configuration, _telemetry); var recurssionData = await recurssionDataRepository.GetAllRecurssionData(); foreach (RecurssionDataEntity recurssionDataEntity in recurssionData) { var reflectionData = await reflectionDataRepository.GetReflectionData(recurssionDataEntity.ReflectionID); var question = await questiondatarepository.GetQuestionData(reflectionData.QuestionID); TaskInfo taskInfo = new TaskInfo(); taskInfo.question = question.Question; taskInfo.postCreateBy = reflectionData.CreatedBy; taskInfo.privacy = reflectionData.Privacy; taskInfo.reflectionID = reflectionData.ReflectionID; var newPostCard = _cardHelper.CreateNewReflect(taskInfo); Attachment newPostCardAttachment = new Attachment() { ContentType = AdaptiveCard.ContentType, Content = newPostCard }; var PostCardFeedback = _cardHelper.FeedBackCard(new Dictionary <int, List <FeedbackDataEntity> >(), taskInfo.reflectionID, taskInfo.question);; Attachment PostCardFeedbackAttachment = new Attachment() { ContentType = AdaptiveCard.ContentType, Content = PostCardFeedback }; var proactiveNotification = await new ProactiveMessageHelper(_configuration).SendChannelNotification(channelAccount, reflectionData.ServiceUrl, reflectionData.ChannelID, "", newPostCardAttachment); if (proactiveNotification.IsSuccessful && proactiveNotification.MessageId != null) { reflectionData.ReflectMessageId = proactiveNotification.MessageId.Split("=")[1]; var feedbackproactivemessage = await new ProactiveMessageHelper(_configuration).SendChannelNotification(channelAccount, reflectionData.ServiceUrl, reflectionData.ChannelID, "", PostCardFeedbackAttachment, reflectionData.ReflectMessageId); if (feedbackproactivemessage.IsSuccessful && proactiveNotification.MessageId != null) { reflectionData.MessageID = feedbackproactivemessage.MessageId; await _dbHelper.UpdateReflectionMessageIdAsync(reflectionData); } } var calculatedNextExecutionDateTime = _dbHelper.GetCalculatedNextExecutionDateTimeAsync(recurssionDataEntity); recurssionDataEntity.NextExecutionDate = null; await recurssionDataRepository.CreateOrUpdateAsync(recurssionDataEntity); if (calculatedNextExecutionDateTime != null) { ReflectionDataEntity newreflectionDataEntity = new ReflectionDataEntity(); RecurssionDataEntity newRecurssionDataEntity = new RecurssionDataEntity(); var reflectionid = Guid.NewGuid(); var recurrsionid = Guid.NewGuid(); newreflectionDataEntity = reflectionData; newreflectionDataEntity.RowKey = Guid.NewGuid().ToString(); newreflectionDataEntity.ReflectionID = reflectionid; newreflectionDataEntity.RefCreatedDate = DateTime.Now; newreflectionDataEntity.RecurrsionID = recurrsionid; await reflectionDataRepository.CreateAsync(newreflectionDataEntity); newRecurssionDataEntity = recurssionDataEntity; newRecurssionDataEntity.RowKey = Guid.NewGuid().ToString(); newRecurssionDataEntity.ReflectionID = reflectionid; newRecurssionDataEntity.CreatedDate = DateTime.Now; newRecurssionDataEntity.RecurssionID = recurrsionid; newRecurssionDataEntity.NextExecutionDate = calculatedNextExecutionDateTime; await recurssionDataRepository.CreateAsync(newRecurssionDataEntity); } } } catch (Exception ex) { _telemetry.TrackException(ex); } }