/// <summary> /// Resets the specified session. /// </summary> /// <param name="session">The name of the session to reset.</param> private IEnumerator ClearSessionRequest(string session) { // Gets the JWT access token. string accessToken = string.Empty; while (!JwtCache.TryGetToken(accessSettings.ServiceAccount, out accessToken)) { yield return(JwtCache.GetToken(accessSettings.CredentialsFileName, accessSettings.ServiceAccount)); } string url = string.Format( "https://dialogflow.googleapis.com/v2/projects/{0}/agent/sessions/{1}/contexts", accessSettings.ProjectId, session); UnityWebRequest deleteRequest = new UnityWebRequest(url); deleteRequest.method = "DELETE"; deleteRequest.SetRequestHeader("Authorization", "Bearer " + accessToken); deleteRequest.SetRequestHeader("Content-Type", "application/json"); yield return(deleteRequest.SendWebRequest()); if (deleteRequest.isHttpError || deleteRequest.isNetworkError) { Debug.LogError(deleteRequest.responseCode + ": " + deleteRequest.error); } else { SessionCleared?.Invoke(session); } }
/// <summary> /// Sends a <see cref="DF2QueryInput"/> object as a HTTP request to the remote /// chatbot. /// </summary> /// <param name="queryInput">The input request.</param> /// <param name="session">The session ID, i.e., the ID of the user who talks to the chatbot.</param> private IEnumerator DetectIntent(DF2QueryInput queryInput, string session) { // Gets the JWT access token. string accessToken = string.Empty; while (!JwtCache.TryGetToken(accessSettings.ServiceAccount, out accessToken)) { yield return(JwtCache.GetToken(accessSettings.CredentialsFileName, accessSettings.ServiceAccount)); } // Prepares the HTTP request. var settings = new JsonSerializerSettings(); settings.NullValueHandling = NullValueHandling.Ignore; settings.ContractResolver = new CamelCasePropertyNamesContractResolver(); DF2Request request = new DF2Request(session, queryInput); // Adds the input contexts and the entities. request.QueryParams = new DF2QueryParams(); request.QueryParams.Contexts = inputContexts.ToArray(); inputContexts.Clear(); request.QueryParams.SessionEntityTypes = inputEntities.ToArray(); inputEntities.Clear(); string jsonInput = JsonConvert.SerializeObject(request, settings); byte[] body = Encoding.UTF8.GetBytes(jsonInput); string url = string.Format(PARAMETRIC_DETECT_INTENT_URL, accessSettings.ProjectId, session); UnityWebRequest df2Request = new UnityWebRequest(url, "POST"); df2Request.SetRequestHeader("Authorization", "Bearer " + accessToken); df2Request.SetRequestHeader("Content-Type", "application/json"); df2Request.uploadHandler = new UploadHandlerRaw(body); df2Request.downloadHandler = new DownloadHandlerBuffer(); yield return(df2Request.SendWebRequest()); // Processes response. if (df2Request.isNetworkError || df2Request.isHttpError) { DetectIntentError?.Invoke(JsonConvert.DeserializeObject <DF2ErrorResponse>(df2Request.downloadHandler.text)); } else { string response = Encoding.UTF8.GetString(df2Request.downloadHandler.data); DF2Response resp = JsonConvert.DeserializeObject <DF2Response>(response); ChatbotResponded?.Invoke(resp); for (int i = 0; i < resp.queryResult.outputContexts.Length; i++) { DF2Context context = resp.queryResult.outputContexts[i]; string[] cName = context.Name.ToLower().Split('/'); if (reactionContexts.ContainsKey(cName[cName.Length - 1])) { reactionContexts[cName[cName.Length - 1]](context); } } } }
IEnumerator StartVoiceRequest(string url, object parameter) { string accessToken = string.Empty; while (!JwtCache.TryGetToken( "*****@*****.**", out accessToken)) { yield return(JwtCache.GetToken( "coffee-wnsicn-d620de1e3173", "*****@*****.**")); } byte[] samples = (byte[])parameter; //TODO: convert float[] samples into bytes[] //byte[] sampleByte = new byte[samples.Length * 4]; //Buffer.BlockCopy(samples, 0, sampleByte, 0, sampleByte.Length); string sampleString = System.Convert.ToBase64String(samples); if (samples != null) { UnityWebRequest postRequest = new UnityWebRequest(url, "POST"); RequestBody requestBody = new RequestBody(); requestBody.queryInput = new QueryInput(); requestBody.queryInput.audioConfig = new InputAudioConfig(); requestBody.queryInput.audioConfig.audioEncoding = AudioEncoding.AUDIO_ENCODING_UNSPECIFIED; //TODO: check if that the sample rate hertz requestBody.queryInput.audioConfig.sampleRateHertz = 16000; requestBody.queryInput.audioConfig.languageCode = "en"; requestBody.inputAudio = sampleString; string jsonRequestBody = JsonUtility.ToJson(requestBody, true); Debug.Log(jsonRequestBody); byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonRequestBody); postRequest.SetRequestHeader("Authorization", "Bearer " + accessToken); postRequest.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw); postRequest.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer(); postRequest.SetRequestHeader("Content-Type", "application/json"); yield return(postRequest.SendWebRequest()); if (postRequest.isNetworkError || postRequest.isHttpError) { Debug.Log(postRequest.responseCode); Debug.Log(postRequest.error); } else { Debug.Log("Response: " + postRequest.downloadHandler.text); // Or retrieve results as binary data byte[] resultbyte = postRequest.downloadHandler.data; string result = System.Text.Encoding.UTF8.GetString(resultbyte); Debug.Log("Result String: " + result); ResponseBody content = (ResponseBody)JsonUtility.FromJson <ResponseBody>(postRequest.downloadHandler.text); Debug.Log(content.queryResult.fulfillmentText); Debug.Log(content.queryResult.intent.displayName); string responseintent = content.queryResult.intent.displayName; Debug.Log("Return: " + responseintent); showPanel(responseintent); // Debug.Log(content.outputAudio); // Debug.Log(audioSource); audioSource.clip = WavUtility.ToAudioClip(System.Convert.FromBase64String(content.outputAudio), 0); audioSource.Play(); } } else { Debug.LogError("The audio file is null"); } }