Beispiel #1
0
        public async Task <string> Dialogflow([FromBody] Richiesta request)
        {
            // url dialogflow
            string url    = "https://api.api.ai/v1/query?v=20150910";
            string apikey = "776c7c6814ee4d5da82a213e673f46a4";
            //Richiesta request;

            bool     status     = false;
            string   dialog_res = "";
            var      response   = new HttpResponseMessage();
            Risposta resp;
            string   res_string;
            content  cont;
            // creazione request
            HttpClient req    = new HttpClient();
            var        values = new Dictionary <string, string>
            {
                { "query", "\"" + request.content + "\"" },
                { "lang", "it" },
                { "sessionId", "somerandomthing" }
            };
            var content = new StringContent(
                JsonConvert.SerializeObject(values, Formatting.Indented),
                Encoding.UTF8,
                "application/json"
                );

            req.DefaultRequestHeaders.Add("Authorization", "Bearer " + apikey);
            //Response ret= new Response("");
            //req.DefaultRequestHeaders.Add("Content-Type", "application/json");
            try
            {
                response = await req.PostAsync(url, content);

                string responseString = await response.Content.ReadAsStringAsync();

                JObject obj = JObject.Parse(responseString);
                dialog_res = obj["result"]["fulfillment"]["speech"].ToString();
                //string diag_int = obj["result"]["metadata"]["intentName"].ToString();

                //ret = new Response(dialog_res);
                status = true;

                List <content> conts = new List <Controllers.content>();
                switch (dialog_res)
                {
                case "news":
                    News n = new News();
                    if (request.user == "")
                    {
                        foreach (News t in n.select())
                        {
                            cont = new content(t.testo, t.news);
                            conts.Add(cont);
                        }
                    }
                    else
                    {
                        foreach (News t in n.selectusernews(request.user))
                        {
                            cont = new content(t.testo, t.news);
                            conts.Add(cont);
                        }
                    }
                    resp       = new Risposta(conts, status);
                    res_string = JsonConvert.SerializeObject(resp);
                    return(res_string);

                    break;

                case "services":
                    Servizio s = new Servizio();
                    if (request.user == "")
                    {
                        foreach (Servizio t in s.select())
                        {
                            cont = new content(t.descrizione);
                            conts.Add(cont);
                        }
                    }
                    else
                    {
                        foreach (Servizio t in s.selectServiziUtente(request.user))
                        {
                            cont = new content(t.descrizione);
                            conts.Add(cont);
                        }
                    }
                    resp       = new Risposta(conts, status);
                    res_string = JsonConvert.SerializeObject(resp);
                    return(res_string);

                    break;

                default:
                    resp       = new Risposta(dialog_res, status);
                    res_string = JsonConvert.SerializeObject(resp);
                    return(res_string);

                    break;
                }
            }
            catch
            {
                status     = false;
                resp       = new Risposta("Servizio non disponibile", status);
                res_string = JsonConvert.SerializeObject(resp);
                return(res_string);
            }
        }
Beispiel #2
0
        public async Task <string> Dialogflow([FromBody] Richiesta request)
        {
            // url dialogflow
            string url    = "https://api.api.ai/v1/query?v=20150910";
            string apikey = "776c7c6814ee4d5da82a213e673f46a4";
            //Richiesta request;

            bool     status     = false;
            string   dialog_res = "";
            var      response   = new HttpResponseMessage();
            Risposta resp;
            string   res_string;
            // creazione request
            HttpClient req    = new HttpClient();
            var        values = new Dictionary <string, string>
            {
                { "query", "\"" + request.content + "\"" },
                { "lang", "it" },
                { "sessionId", "somerandomthing" }
            };
            var content = new StringContent(
                JsonConvert.SerializeObject(values, Formatting.Indented),
                Encoding.UTF8,
                "application/json"
                );

            req.DefaultRequestHeaders.Add("Authorization", "Bearer " + apikey);
            //Response ret= new Response("");
            //req.DefaultRequestHeaders.Add("Content-Type", "application/json");
            try
            {
                response = await req.PostAsync(url, content);

                string responseString = await response.Content.ReadAsStringAsync();

                JObject obj = JObject.Parse(responseString);
                dialog_res = obj["result"]["fulfillment"]["speech"].ToString();
                string diag_int = obj["result"]["metadata"]["intentName"].ToString();

                //ret = new Response(dialog_res);
                status     = true;
                resp       = new Risposta(dialog_res, diag_int, status);
                res_string = JsonConvert.SerializeObject(resp);
                return(res_string);

                /*
                 * switch (dialog_res)
                 * {
                 *  case "news":
                 *      NewsController news = new NewsController();
                 *      if (request.user == "")
                 *      {
                 *          JsonResult novita = news.Get();
                 *          List<NewsController.News> var    =  novita.Data();
                 *          foreach (object  nov in novita)
                 *          {
                 *
                 *          }
                 *      }
                 *      else
                 *      {
                 *          JsonResult novita = news.Get(request.user);
                 *      }
                 *      break;
                 *  case "services":
                 *      ServiziController serv = new ServiziController();
                 *      if (request.user == "")
                 *          ret = new Response(serv.Get(), "novità");
                 *      else
                 *          ret = new Response(serv.GetServiziUser(request.user), "novità");
                 *      break;
                 *  default:
                 *      resp = new Risposta(dialog_res, status);
                 *      string res_string = JsonConvert.SerializeObject(resp);
                 *      return res_string;
                 *      break;
                 * }*/
            }
            catch
            {
                status = false;
            }

            resp       = new Risposta("", status);
            res_string = JsonConvert.SerializeObject(resp);
            return(res_string);
        }