Esempio n. 1
0
        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();
                }
            }
        }