static void Main(string[] args)
        {            
            var config = new AIConfiguration(SUBSCRIPTION_KEY, ACCESS_TOKEN, SupportedLanguage.English);
            var apiAi = new ApiAi(config);
            string rl = "";

            while (rl == "")
            {
                var sw = Stopwatch.StartNew();
                var response = apiAi.TextRequest("hello");
                sw.Stop();

                Console.WriteLine("api sync answer: {0}, time: {1} ms", response.Result.Action, sw.ElapsedMilliseconds.ToString());

                sw = Stopwatch.StartNew();
                apiAi.TextRequestStart("hello");
                System.Threading.Thread.Sleep(250);
                response = apiAi.TextRequestFinish();
                sw.Stop();

                Console.WriteLine("api async answer: {0}, time: {1} ms", response.Result.Action, sw.ElapsedMilliseconds.ToString());


                rl = Console.ReadLine();
            }
        }
Exemple #2
0
        static void WaitText()
        {
            string        text    = Console.ReadLine();
            RequestExtras extras  = new RequestExtras();
            AIRequest     request = new AIRequest(text);

            request.SessionId = _SessionId;
            AIResponse response = _ApiAi.TextRequest(request);

            HandleResponse(response);
        }
Exemple #3
0
        public IActionResult GetResponseFromDialogFlow(string question, string context, string conversationId)
        {
            var config = new AIConfiguration("27a5dfa1bde94f2eb109717143748e78", SupportedLanguage.Italian);

            var apiAi = new ApiAiSDK.ApiAi(config);

            switch (context)
            {
            case "mail":
                try
                {
                    var addr = new System.Net.Mail.MailAddress(question);
                    question = "mailUtente";
                }
                catch (Exception)
                {
                    context  = "InvalidMail";
                    question = "mailUtenteNonValida";
                }
                break;

            case "seriale":

                question = "serialeUtente";

                var requests = _dbContext.Get <Freight>(Consts.FreightTable).FindAll().AsQueryable();

                var freight = requests.FirstOrDefault(x => x.FreightDescriptions == question);

                if (freight != null && freight.HasSupport)
                {
                    return(Ok(new
                    {
                        data = "I dati relativi al suo ticket sono : seriale : " + freight.FreightDescriptions +
                               "marca : " + freight.Brand + "modello :" + freight.Model,
                        context = ""
                    }));
                }
                else
                {
                    return(Ok(new
                    {
                        data = "Il suo ticket è stato rifiutato",
                        context = ""
                    }));
                }

            case "nomecognome":

                question = "nomeCognomeUtente";

                break;
            }

            var contexts = new List <AIContext> {
                new AIContext {
                    Name = context, Lifespan = 1
                }
            };

            var requestExtras = new RequestExtras
            {
                Contexts = contexts
            };

            var response = apiAi.TextRequest(question, requestExtras);

            var responseText = response.Result.Fulfillment.Speech;

            if (responseText.Contains("0") && responseText.Contains("#"))
            {
                var link = responseText.Substring(responseText.IndexOf("#", StringComparison.Ordinal) + 1);
                responseText = responseText.Remove(responseText.IndexOf("#", StringComparison.Ordinal));
                responseText = responseText.Replace("0", link);
            }

            var messageDescriptor = new MessageDescriptor
            {
                OpenTicket     = response.Result.Metadata.IntentName.Contains("ConfermaAperturaTicket"),
                Question       = question,
                Response       = responseText,
                SessionId      = response.SessionId,
                TimeStamp      = DateTime.Now,
                ConversationId = conversationId,
                TicketRefused  = response.Result.Metadata.IntentName.Contains("RifiutoAperturaTicket"),
                IntentName     = response.Result.Metadata.IntentName
            };

            _dbContext.Set(messageDescriptor, Consts.MessageDescriptorTable);

            return(Ok(new
            {
                data = responseText,
                context = response.Result.Contexts.FirstOrDefault()?.Name
            }));
        }
        public static ApiAiResult ApiAiBotRequest(string request)
        {
            var result = new ApiAiResult();

            if (String.IsNullOrWhiteSpace(request))
            {
                result.Errors.Add("Напишите, пожалуйста, Ваш запрос! Что-то пошло не так");
                return(result);
            }

            // Конфигурация агента Api.ai
            var config = new AIConfiguration(ClientAccessToken, SupportedLanguage.Russian);
            var apiAi  = new ApiAiSDK.ApiAi(config);

            // Ответ
            var response = apiAi.TextRequest(request);

            if (response == null || response.Result == null || response.Result.Parameters == null)
            {
                result.Errors.Add("Напишите, пожалуйста, Ваш запрос! Что-то пошло не так");
                return(result);
            }

            if (response.Result.Parameters.ContainsKey("Platform"))
            {
                string platform = response.Result.Parameters["Platform"].ToString();

                if (platform == "223-ФЗ" || platform == "44-ФЗ" || platform == "615-ПП РФ" || platform == "Имущественные торги" ||
                    platform == "Электронный магазин ЗМО")
                {
                    result.Platform = platform;
                }
                else
                {
                    result.Platform = null;
                }
            }

            if (response.Result.Parameters.ContainsKey("Role"))
            {
                string role = response.Result.Parameters["Role"].ToString();

                if (role == "Поставщик" || role == "Заказчик" || role == "Продавец" || role == "Покупатель" || role == "ОВР")
                {
                    result.Role = role;
                }
                else
                {
                    result.Role = null;
                }
            }

            if (response.Result.Parameters.ContainsKey("Type"))
            {
                string type = response.Result.Parameters["Type"].ToString();

                if (type == "ИП" || type == "ФЛ" || type == "ЮЛ")
                {
                    result.Type = type;
                }
                else
                {
                    result.Type = null;
                }
            }

            return(result);
        }