Пример #1
0
        /// <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);
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Detects an intent from a user-built <see cref="DF2QueryInput"/>.
        /// </summary>
        /// <param name="input">The user-built <see cref="DF2QueryInput"/>.</param>
        /// <param name="talker">The user who is talking to the chatbot.</param>
        /// <param name="languageCode">The language code of the request.</param>
        public void DetectIntentFromGenericInput(DF2QueryInput input, string talker,
                                                 string languageCode = "")
        {
            if (languageCode.Length == 0)
            {
                languageCode = accessSettings.LanguageCode;
            }

            StartCoroutine(DetectIntent(input, talker));
        }
Пример #3
0
        /// <summary>
        /// Makes a POST request to Dialogflow for detecting an intent from text.
        /// </summary>
        /// <param name="text">The input text.</param>
        /// <param name="talker">The ID of the entity who is talking to the bot.</param>
        /// <param name="languageCode">The language code of the request.</param>
        public void DetectIntentFromText(string text, string talker, string languageCode = "")
        {
            if (languageCode.Length == 0)
            {
                languageCode = accessSettings.LanguageCode;
            }

            DF2QueryInput queryInput = new DF2QueryInput();

            queryInput.Text              = new DF2TextInput();
            queryInput.Text.Text         = text;
            queryInput.Text.LanguageCode = languageCode;

            StartCoroutine(DetectIntent(queryInput, talker));
        }
        //@hoatong
        /// <summary>
        /// Makes a POST request to Dialogflow for detecting an intent from text.
        /// </summary>
        /// <param name="talker">The ID of the entity who is talking to the bot.</param>
        /// <param name="languageCode">The language code of the request.</param>
        public void DetectIntentFromAudio(string audio64, string talker, string languageCode = "")
        {
            if (languageCode.Length == 0)
            {
                languageCode = accessSettings.LanguageCode;
            }

            DF2QueryInput queryInput = new DF2QueryInput();

            queryInput.AudioConfig = new DF2AudioConfig();

            queryInput.AudioConfig.LanguageCode = languageCode;

            StartCoroutine(DetectIntent(queryInput, talker, audio64));
        }
Пример #5
0
        /// <summary>
        /// Makes a POST request to Dialogflow for detecting an intent from an event.
        /// </summary>
        /// <param name="eventName">The name of the event.</param>
        /// <param name="parameters">The parameters of the event.</param>
        /// <param name="talker">The ID of the entity who is talking to the bot.</param>
        /// <param name="languageCode">The language code of the request.</param>
        public void DetectIntentFromEvent(string eventName, Dictionary <string, object> parameters,
                                          string talker, string languageCode = "")
        {
            if (languageCode.Length == 0)
            {
                languageCode = accessSettings.LanguageCode;
            }

            DF2QueryInput queryInput = new DF2QueryInput();

            queryInput.Event              = new DF2EventInput();
            queryInput.Event.Name         = eventName;
            queryInput.Event.Parameters   = parameters;
            queryInput.Event.LanguageCode = languageCode;

            StartCoroutine(DetectIntent(queryInput, talker));
        }
 public DF2Request(string session, DF2QueryInput queryInput)
 {
     Session    = session;
     QueryInput = queryInput;
 }