Exemplo n.º 1
0
        private Intent ImportIntentUserSays(Agent agent, DialogflowIntent intent, string fileName)
        {
            // void id confict
            intent.Id   = Guid.NewGuid().ToString();
            intent.Name = intent.Name.Replace("/", "_");
            // load user expressions
            if (fileName.Contains("Default Fallback Intent"))
            {
                intent.UserSays = (intent.Responses[0].MessageList[0].Speech as JArray)
                                  .Select(x => new DialogflowIntentExpression
                {
                    Data = new List <DialogflowIntentExpressionPart>
                    {
                        new DialogflowIntentExpressionPart
                        {
                            Text = x.ToString()
                        }
                    }
                }).ToList();
            }
            else
            {
                string expressionFileName = fileName.Replace(intent.Name, $"{intent.Name}_usersays_{agent.Language}");
                if (File.Exists(expressionFileName))
                {
                    string expressionJson = File.ReadAllText($"{expressionFileName}");
                    intent.UserSays = JsonConvert.DeserializeObject <List <DialogflowIntentExpression> >(expressionJson);

                    intent.UserSays.ForEach(say =>
                    {
                        // remove @sys.ignore
                        say.Data.Where(x => x.Meta == "@sys.ignore").ToList().ForEach(x => x.Meta = null);

                        // remove @sys.
                        say.Data.Where(x => x.Meta != null && x.Meta.StartsWith("@sys.")).ToList().ForEach(x => x.Meta = x.Meta.Substring(5));

                        // remove @
                        say.Data.Where(x => x.Meta != null && x.Meta.StartsWith("@")).ToList().ForEach(x => x.Meta = x.Meta.Substring(1));

                        // calculate offset
                        for (int i = 1; i < say.Data.Count; i++)
                        {
                            say.Data[i].Start = String.Join("", say.Data.Select(x => x.Text).Take(i)).Length;
                        }
                    });
                }
            }

            var newIntent = ImportIntentResponse(agent, intent);

            newIntent.Contexts = intent.ContextList.Select(x => new IntentInputContext {
                Name = x
            }).ToList();

            return(newIntent);
        }
 public bool PutIntent([FromBody] DialogflowIntent intent)
 {
     return(true);
 }
 public string PostIntent([FromBody] DialogflowIntent intent)
 {
     return("");
 }
Exemplo n.º 4
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);
        }