示例#1
0
        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();
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            var config = new AIConfiguration("5a0161c9367f4951a61a0ef52b49c2d6", SupportedLanguage.PortugueseBrazil);

            _ApiAi = new ApiAiSDK.ApiAi(config);
            Console.WriteLine("Digite uma mensagem...");
            WaitText();
            Console.WriteLine("Terminou");
            Console.ReadLine();
        }
示例#3
0
 public AIWrapper()
 {
     //var config = new AIConfiguration("7c0f43c6-03e4-424c-82b5-6c96eefd16c3", "ac7c938ad4b6459191353c8eeae6584c", SupportedLanguage.Spanish);
     var config = new AIConfiguration("7c0f43c6-03e4-424c-82b5-6c96eefd16c3", "850f2c7c364a441081ff135bddb29c22 ", SupportedLanguage.English);
     apiAi = new ApiAi(config);
 }
示例#4
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
            }));
        }
示例#5
0
        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);
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral = taskInstance.GetDeferral();
     
            taskInstance.Canceled += OnTaskCanceled;
            
            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null)
            {

                var config = new AIConfiguration("cb9693af-85ce-4fbf-844a-5563722fc27f",
                           "40048a5740a1455c9737342154e86946",
                           SupportedLanguage.English);

                apiAi = new ApiAi(config);
                apiAi.DataService.PersistSessionId();
                
                try
                {
                    voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                    voiceServiceConnection.VoiceCommandCompleted += VoiceCommandCompleted;
                    var voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();
                    var recognizedText = voiceCommand.SpeechRecognitionResult?.Text;

                    switch (voiceCommand.CommandName)
                    {
                        case "type":
                            {
                                var aiResponse = await apiAi.TextRequestAsync(recognizedText);
                                await apiAi.LaunchAppInForegroundAsync(voiceServiceConnection, aiResponse);
                            }
                            break;
                        case "unknown":
                            {
                                if (!string.IsNullOrEmpty(recognizedText))
                                {
                                    var aiResponse = await apiAi.TextRequestAsync(recognizedText);
                                    if (aiResponse != null)
                                    {
                                        await apiAi.SendResponseToCortanaAsync(voiceServiceConnection, aiResponse);
                                    }
                                }
                            }
                            break;

                        case "greetings":
                            {
                                var aiResponse = await apiAi.TextRequestAsync(recognizedText);
                                
                                var repeatMessage = new VoiceCommandUserMessage
                                {
                                    DisplayMessage = "Repeat please",
                                    SpokenMessage = "Repeat please"
                                };

                                var processingMessage = new VoiceCommandUserMessage
                                {
                                    DisplayMessage = aiResponse?.Result?.Fulfillment?.Speech ?? "Pizza",
                                    SpokenMessage = ""
                                };

                                var resp = VoiceCommandResponse.CreateResponseForPrompt(processingMessage, repeatMessage);
                                await voiceServiceConnection.ReportSuccessAsync(resp);
                                break;
                            }

                        default:
                            if (!string.IsNullOrEmpty(recognizedText))
                            {
                                var aiResponse = await apiAi.TextRequestAsync(recognizedText);
                                if (aiResponse != null)
                                {
                                    await apiAi.SendResponseToCortanaAsync(voiceServiceConnection, aiResponse);
                                }
                            }
                            else
                            {
                                await SendResponse("Can't recognize");
                            }
                            
                            break;
                    }
                    
                }
                catch(Exception e)
                {
                    var message = e.ToString();
                    Debug.WriteLine(message);
                }
                finally
                {
                    serviceDeferral?.Complete();
                }
            }
        }
 public void Initialize(AIConfiguration config)
 {
     this.config = config;
     apiAi = new ApiAi(this.config);
 }