Пример #1
0
        private Intent ImportIntentResponse(Agent agent, DialogflowIntent intent)
        {
            var newIntent = intent.ToObject <Intent>();

            intent.Responses.ForEach(res =>
            {
                var newResponse = newIntent.Responses.First(x => x.Id == res.Id);

                newResponse.Contexts = res.AffectedContexts.Select(x => new IntentResponseContext
                {
                    Name     = x.Name,
                    Lifespan = x.Lifespan
                }).ToList();

                int millSeconds = 0;

                newResponse.Messages = res.MessageList.Where(x => x.Speech != null || x.Payload != null)
                                       .Select(x =>
                {
                    if (x.Type == AIResponseMessageType.Custom)
                    {
                        return(new IntentResponseMessage
                        {
                            Payload = JObject.FromObject(x.Payload),
                            PayloadJson = JsonConvert.SerializeObject(x.Payload),
                            Type = x.Type,
                            UpdatedTime = DateTime.UtcNow.AddMilliseconds(millSeconds++)
                        });
                    }
                    else
                    {
                        var speech = JsonConvert.SerializeObject(x.Speech.GetType().Equals(typeof(String)) ?
                                                                 new List <String> {
                            x.Speech.ToString()
                        } :
                                                                 (x.Speech as JArray).Select(s => s.Value <String>()).ToList());

                        return(new IntentResponseMessage
                        {
                            Speech = speech,
                            Type = x.Type,
                            UpdatedTime = DateTime.UtcNow.AddMilliseconds(millSeconds++)
                        });
                    }
                }).ToList();

                newResponse.Parameters = res.Parameters.Select(p =>
                {
                    var rp = p.ToObject <IntentResponseParameter>();

                    // remove @sys.
                    if (rp.DataType.StartsWith("@sys."))
                    {
                        rp.DataType = rp.DataType.Substring(5);
                    }

                    if (rp.DataType.StartsWith("@"))
                    {
                        rp.DataType = rp.DataType.Substring(1);
                    }

                    rp.Prompts = p.PromptList.Select(pl => new ResponseParameterPrompt {
                        Prompt = pl.Value
                    }).ToList();
                    return(rp);
                }).ToList();
            });

            return(newIntent);
        }