]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-11320 #resolve pass json string as param to ASR module
authorSeven Du <dujinfang@x-y-t.cn>
Wed, 8 Aug 2018 03:46:33 +0000 (11:46 +0800)
committerMike Jerris <mike@jerris.com>
Mon, 13 Aug 2018 19:00:39 +0000 (19:00 +0000)
src/switch_ivr_async.c

index 9f1facbaaeb63c35dc92acf23cbf27bfc97ac57a..1eae6eae1e848a2104060ff8244cd4cab8f2c223 100644 (file)
@@ -4941,6 +4941,33 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_detect_speech_init(switch_core_sessio
        return SWITCH_STATUS_SUCCESS;
 }
 
+static void asr_set_json_text_params(switch_core_session_t *session, switch_asr_handle_t *ah)
+{
+       switch_event_header_t *hp;
+       switch_event_t *event, *cevent;
+       const char *variable_prefix = "asr_json_param_";
+       switch_channel_t *channel = switch_core_session_get_channel(session);
+
+       switch_core_get_variables(&event);
+       switch_channel_get_variables(channel, &cevent);
+       switch_event_merge(event, cevent);
+
+       for (hp = event->headers; hp; hp = hp->next) {
+               char *var = hp->name;
+               char *val = hp->value;
+
+               if (!strncasecmp(var, variable_prefix, strlen(variable_prefix)) && !zstr(val)) {
+                       char *json_var = var + strlen(variable_prefix);
+
+                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "setting json param %s = %s\n", json_var, val);
+                       switch_core_asr_text_param(ah, json_var, val);
+               }
+       }
+
+       switch_event_destroy(&event);
+       switch_event_destroy(&cevent);
+}
+
 SWITCH_DECLARE(switch_status_t) switch_ivr_detect_speech(switch_core_session_t *session,
                                                                                                                 const char *mod_name,
                                                                                                                 const char *grammar, const char *name, const char *dest, switch_asr_handle_t *ah)
@@ -4949,6 +4976,8 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_detect_speech(switch_core_session_t *
        switch_status_t status;
        struct speech_thread_handle *sth = switch_channel_get_private(channel, SWITCH_SPEECH_KEY);
        const char *p;
+       int resume = 0;
+
 
        if (!sth) {
                /* No speech thread handle available yet, init speech detection first. */
@@ -4961,15 +4990,21 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_detect_speech(switch_core_session_t *
                        return SWITCH_STATUS_NOT_INITALIZED;
                }
        } else {
-               switch_ivr_resume_detect_speech(session);
+               resume = 1;
        }
 
+       asr_set_json_text_params(session, sth->ah);
+
        if (switch_core_asr_load_grammar(sth->ah, grammar, name) != SWITCH_STATUS_SUCCESS) {
                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Error loading Grammar\n");
                switch_ivr_stop_detect_speech(session);
                return SWITCH_STATUS_FALSE;
        }
 
+       if (resume) {
+               switch_ivr_resume_detect_speech(session);
+       }
+
        if ((p = switch_channel_get_variable(channel, "fire_asr_events")) && switch_true(p)) {
                switch_set_flag(sth->ah, SWITCH_ASR_FLAG_FIRE_EVENTS);
        }