/// <summary>
        /// 查找解决方案
        /// </summary>
        /// <param name="stepContext"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        private async Task <DialogTurnResult> FindSolutionAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            // 创建请求时的标题
            var subject = $"From Aden Test Bot:{stepContext.Context.Activity.RemoveRecipientMention().Replace("\r", "").Replace("\n", "")}";
            // 获取答案
            var results = await _qna.GetAnswersAsync(stepContext.Context);

            // 匹配到解决方案
            if (results.Any())
            {
                // QnA Maker 答案中存放的是解决方案 ID
                var answer = results.FirstOrDefault();
                var data   = new TaskFetchValueModel
                {
                    Data = answer.Answer,
                    Type = TaskFetchTypes.Solution
                };
                var card = new ThumbnailCard
                {
                    Buttons = new List <CardAction> {
                        new CardAction("invoke", "查看解决方案", value: new { type = "task/fetch", data })
                    }
                };
                var activity = stepContext.Context.Activity.CreateReply("为您找到以下解决方案:");
                activity.Attachments.Add(card.ToAttachment());
                // 发送找到解决方案的消息,包含“查看解决方案”按钮
                await stepContext.Context.SendActivityAsync(activity);

                // 并自动发起请求
                var member = await TeamsInfo.GetMemberAsync(stepContext.Context, stepContext.Context.Activity.From.Id, cancellationToken);

                var requester = member.GetRequesterName();
                var request   = new AddRequestRequestModel
                {
                    Request = new AddRequestRequest(subject, requester)
                };
                var result = await _request.AddRequestAsync(request);

                if (result.ResponseStatus.StatusCode == 2000)
                {
                    // TODO 记录自动创建的请求,后续可对这个请求追踪状态变化
                    var reply = MessageFactory.Text($"已自动创建请求【[{subject}](https://helpme.adenservices.com/WorkOrder.do?woMode=viewWO&woID={result.Request.Id})】。");
                    await stepContext.Context.SendActivityAsync(reply, cancellationToken);

                    // 15 分钟后自动修改这个请求的状态
                    BackgroundJob.Schedule <RequestHelper>(r => r.ResolvedRequestAsync(result.Request.Id), TimeSpan.FromMinutes(15));
                }

                // 结束对话
                return(await stepContext.EndDialogAsync(null, cancellationToken).ConfigureAwait(false));
            }
            return(await stepContext.NextAsync(subject, cancellationToken).ConfigureAwait(false));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 提交请求表单时
        /// </summary>
        /// <param name="turnContext"></param>
        /// <param name="taskModuleRequest"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected override async Task <TaskModuleResponse> OnTeamsTaskModuleSubmitAsync(ITurnContext <IInvokeActivity> turnContext, TaskModuleRequest taskModuleRequest, CancellationToken cancellationToken)
        {
            InputWorkOrderModel model = null;

            if (taskModuleRequest.Data is JObject @object)
            {
                model = @object.ToObject <InputWorkOrderModel>();
            }

            if (!string.IsNullOrEmpty(model?.Subject))
            {
                var request = await CreateRequestFromModelAsync(model, turnContext, cancellationToken);

                var result = await _request.AddRequestAsync(request);

                if (result.ResponseStatus.StatusCode == 2000)
                {
                    var reply      = MessageFactory.Text($"已为您提交请求[{model.Subject}]({GetRequestUrl(result.Request.Id)})。");
                    var resultCard = CreateResultAdaptiveCardAttachment(result.Request);
                    await turnContext.SendActivityAsync(reply, cancellationToken);

                    return(new TaskModuleResponse
                    {
                        Task = new TaskModuleContinueResponse
                        {
                            Value = new TaskModuleTaskInfo()
                            {
                                Title = "发起请求",
                                Height = 500,
                                Width = "medium",
                                Card = resultCard
                            }
                        }
                    });
                }
                else
                {
                    // TODO 提交失败
                }
            }

            return(new TaskModuleResponse
            {
                Task = new TaskModuleMessageResponse()
                {
                    Value = "数据提交有误",
                },
            });
        }