void append_event_message(switch_core_session_t *session, vmivr_profile_t *profile, switch_event_t *phrase_params, switch_event_t *msg_list_event, size_t current_msg) { char *varname; char *apicmd; char *total_msg = NULL; if (!msg_list_event || !(total_msg = switch_event_get_header(msg_list_event, "VM-List-Count")) || current_msg > atoi(total_msg)) { /* TODO Error MSG */ return; } varname = switch_mprintf("VM-List-Message-%" SWITCH_SIZE_T_FMT "-UUID", current_msg); apicmd = switch_mprintf("json %s %s %s %s", profile->api_profile, profile->domain, profile->id, switch_event_get_header(msg_list_event, varname)); switch_safe_free(varname); jsonapi2event(session, phrase_params, profile->api_msg_get, apicmd); /* TODO Set these 2 header correctly */ switch_event_add_header(phrase_params, SWITCH_STACK_BOTTOM, "VM-Message-Type", "%s", "new"); switch_event_add_header(phrase_params, SWITCH_STACK_BOTTOM, "VM-Message-Number", "%"SWITCH_SIZE_T_FMT, current_msg); switch_event_add_header_string(phrase_params, SWITCH_STACK_BOTTOM, "VM-Message-Private-Local-Copy", "False"); switch_safe_free(apicmd); }
void vmivr_menu_select_greeting_slot(switch_core_session_t *session, vmivr_profile_t *profile) { vmivr_menu_t menu = { "std_select_greeting_slot" }; const char *result; int gnum = -1; /* Initialize Menu Configs */ menu_init(profile, &menu); result = vmivr_menu_get_input_set(session, profile, &menu, "X"); if (result) gnum = atoi(result); if (gnum != -1) { char * cmd = switch_core_session_sprintf(session, "%s %s %s %d", profile->api_profile, profile->domain, profile->id, gnum); if (vmivr_api_execute(session, profile->api_pref_greeting_set, cmd) == SWITCH_STATUS_SUCCESS) { char *str_num = switch_core_session_sprintf(session, "%d", gnum); char *cmd = switch_core_session_sprintf(session, "json %s %s %s %d %s", profile->api_profile, profile->domain, profile->id); switch_event_t *phrases = jsonapi2event(session, profile->api_pref_greeting_get, cmd); ivre_playback_dtmf_buffered(session, switch_event_get_header(menu.event_phrases, "selected_slot"), str_num, phrases, NULL, 0); if (switch_true(switch_event_get_header(phrases, "VM-Message-Private-Local-Copy"))) { const char *file_path = switch_event_get_header(phrases, "VM-Preference-Greeting-File-Path"); if (file_path && unlink(file_path) != 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Failed to delete temp file [%s]\n", file_path); } } switch_event_destroy(&phrases); } else { ivre_playback_dtmf_buffered(session, switch_event_get_header(menu.event_phrases, "invalid_slot"), NULL, NULL, NULL, 0); } } menu_free(&menu); }
void vmivr_menu_navigator(switch_core_session_t *session, vmivr_profile_t *profile) { switch_channel_t *channel = switch_core_session_get_channel(session); switch_event_t *msg_list_params = NULL; size_t msg_count = 0; size_t current_msg = 1; size_t next_msg = current_msg; size_t previous_msg = current_msg; char *cmd = NULL; int retry; /* Different switch to control playback of phrases */ switch_bool_t initial_count_played = SWITCH_FALSE; switch_bool_t skip_header = SWITCH_FALSE; switch_bool_t skip_playback = SWITCH_FALSE; switch_bool_t msg_deleted = SWITCH_FALSE; switch_bool_t msg_undeleted = SWITCH_FALSE; switch_bool_t msg_saved = SWITCH_FALSE; vmivr_menu_t menu = { "std_navigator" }; /* Initialize Menu Configs */ menu_init(profile, &menu); if (!menu.event_keys_dtmf || !menu.event_phrases) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing Menu Phrases or Keys in menu '%s'\n", menu.name); goto done; } /* Get VoiceMail List And update msg count */ cmd = switch_core_session_sprintf(session, "json %s %s %s %s %s", profile->api_profile, profile->domain, profile->id, profile->folder_name, profile->folder_filter); msg_list_params = jsonapi2event(session, profile->api_msg_list, cmd); if (msg_list_params) { msg_count = atol(switch_event_get_header(msg_list_params,"VM-List-Count")); if (msg_count == 0) { goto done; } } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "API message list return invalid result : %s(%s)\n", profile->api_msg_list, cmd); goto done; } /* TODO Add Detection of new message and notify the user */ for (retry = menu.ivr_maximum_attempts; switch_channel_ready(channel) && retry > 0; retry--) { switch_core_session_message_t msg = { 0 }; char cid_buf[1024] = ""; menu_instance_init(&menu); switch_event_add_header(menu.phrase_params, SWITCH_STACK_BOTTOM, "IVR-Retry-Left", "%d", retry); previous_msg = current_msg; ivre_init(&menu.ivre_d, menu.dtmfa); /* Prompt related to previous Message here */ append_event_message(session, profile, menu.phrase_params, msg_list_params, previous_msg); if (msg_deleted) { msg_deleted = SWITCH_FALSE; ivre_playback(session, &menu.ivre_d, switch_event_get_header(menu.event_phrases, "ack"), "deleted", menu.phrase_params, NULL, 0); } if (msg_undeleted) { msg_undeleted = SWITCH_FALSE; ivre_playback(session, &menu.ivre_d, switch_event_get_header(menu.event_phrases, "ack"), "undeleted", menu.phrase_params, NULL, 0); } if (msg_saved) { msg_saved = SWITCH_FALSE; ivre_playback(session, &menu.ivre_d, switch_event_get_header(menu.event_phrases, "ack"), "saved", menu.phrase_params, NULL, 0); } switch_event_del_header(menu.phrase_params, "VM-Message-Flags"); /* Simple Protection to not go out of msg list scope */ if (next_msg == 0) { next_msg = 1; } else if (next_msg > msg_count) { next_msg = msg_count; ivre_playback_dtmf_buffered(session, switch_event_get_header(menu.event_phrases, "no_more_messages"), NULL, NULL, NULL, 0); } current_msg = next_msg; /* Prompt related the current message */ append_event_message(session, profile, menu.phrase_params, msg_list_params, current_msg); /* Used for extra control in phrases */ switch_event_add_header(menu.phrase_params, SWITCH_STACK_BOTTOM, "VM-List-Count", "%"SWITCH_SIZE_T_FMT, msg_count); /* Display MSG CID/Name to caller */ switch_snprintf(cid_buf, sizeof(cid_buf), "%s|%s", switch_str_nil(switch_event_get_header(menu.phrase_params, "VM-Message-Caller-Number")), switch_str_nil(switch_event_get_header(menu.phrase_params, "VM-Message-Caller-Name"))); msg.from = __FILE__; msg.string_arg = cid_buf; msg.message_id = SWITCH_MESSAGE_INDICATE_DISPLAY; switch_core_session_receive_message(session, &msg); /* Save in profile the current msg info for other menu processing AND restoration of our current position */ profile->current_msg = current_msg; profile->current_msg_uuid = switch_core_session_strdup(session, switch_event_get_header(menu.phrase_params, "VM-Message-UUID")); /* TODO check if msg is gone (purged by another session, notify user and auto jump to next message or something) */ if (!skip_header) { if (!initial_count_played) { cmd = switch_core_session_sprintf(session, "json %s %s %s", profile->api_profile, profile->domain, profile->id); jsonapi_populate_event(session, menu.phrase_params, profile->api_msg_count, cmd); initial_count_played = SWITCH_TRUE; // TODO ivre_playback(session, &menu.ivre_d, switch_event_get_header(menu.event_phrases, "msg_count"), NULL, menu.phrase_params, NULL, 0); } if (msg_count > 0) { ivre_playback(session, &menu.ivre_d, switch_event_get_header(menu.event_phrases, "say_msg_number"), NULL, menu.phrase_params, NULL, 0); ivre_playback(session, &menu.ivre_d, switch_event_get_header(menu.event_phrases, "say_date"), NULL, menu.phrase_params, NULL, 0); } } if (msg_count > 0 && !skip_playback) { /* TODO Update the Read date of a message (When msg start, or when it listen compleatly ??? To be determined */ ivre_playback(session, &menu.ivre_d, switch_event_get_header(menu.event_phrases, "play_message"), NULL, menu.phrase_params, NULL, 0); } skip_header = SWITCH_FALSE; skip_playback = SWITCH_FALSE; ivre_playback(session, &menu.ivre_d, switch_event_get_header(menu.event_phrases, "menu_options"), NULL, menu.phrase_params, NULL, menu.ivr_entry_timeout); if (menu.ivre_d.result == RES_TIMEOUT) { ivre_playback_dtmf_buffered(session, switch_event_get_header(menu.event_phrases, "timeout"), NULL, NULL, NULL, 0); } else if (menu.ivre_d.result == RES_INVALID) { ivre_playback_dtmf_buffered(session, switch_event_get_header(menu.event_phrases, "invalid"), NULL, NULL, NULL, 0); } else if (menu.ivre_d.result == RES_FOUND) { /* Matching DTMF Key Pressed */ const char *action = switch_event_get_header(menu.event_keys_dtmf, menu.ivre_d.dtmf_stored); /* Reset the try count */ retry = menu.ivr_maximum_attempts; action: if (action) { if (!strcasecmp(action, "skip_intro")) { /* Skip Header / Play the recording again */ skip_header = SWITCH_TRUE; } else if (!strcasecmp(action, "next_msg")) { /* Next Message */ next_msg++; } else if (!strcasecmp(action, "prev_msg")) { /* Previous Message */ next_msg--; } else if (!strcasecmp(action, "delete_msg")) { /* Delete / Undelete Message */ const char *msg_flags = switch_event_get_header(menu.phrase_params, "VM-Message-Flags"); if (!msg_flags || strncasecmp(msg_flags, "delete", 6)) { const char *action_on_delete = switch_event_get_header(menu.event_settings, "Nav-Action-On-Delete"); cmd = switch_core_session_sprintf(session, "%s %s %s %s", profile->api_profile, profile->domain, profile->id, switch_event_get_header(menu.phrase_params, "VM-Message-UUID")); vmivr_api_execute(session, profile->api_msg_delete, cmd); msg_deleted = SWITCH_TRUE; if (action_on_delete) { action = action_on_delete; goto action; } else { skip_header = skip_playback = SWITCH_TRUE; } } else { cmd = switch_core_session_sprintf(session, "%s %s %s %s", profile->api_profile, profile->domain, profile->id, switch_event_get_header(menu.phrase_params, "VM-Message-UUID")); vmivr_api_execute(session, profile->api_msg_undelete, cmd); msg_undeleted = SWITCH_TRUE; } } else if (!strcasecmp(action, "save_msg")) { /* Save Message */ cmd = switch_core_session_sprintf(session, "%s %s %s %s", profile->api_profile, profile->domain, profile->id, switch_event_get_header(menu.phrase_params, "VM-Message-UUID")); vmivr_api_execute(session, profile->api_msg_save, cmd); msg_saved = SWITCH_TRUE; } else if (!strcasecmp(action, "callback")) { /* CallBack caller */ const char *cid_num = switch_event_get_header(menu.phrase_params, "VM-Message-Caller-Number"); if (cid_num) { /* TODO add detection for private number */ switch_core_session_execute_exten(session, cid_num, "XML", profile->domain); } else { /* TODO Some error msg that the msg doesn't contain a caller number */ } } else if (!strncasecmp(action, "menu:", 5)) { /* Sub Menu */ void (*fPtr)(switch_core_session_t *session, vmivr_profile_t *profile) = vmivr_get_menu_function(action+5); if (fPtr) { fPtr(session, profile); } } else if (!strcasecmp(action, "return")) { /* Return */ retry = -1; } } } /* IF the API to get the message returned us a COPY of the file menu.ivre_dally (temp file create from a DB or from a web server), delete it */ if (switch_true(switch_event_get_header(menu.phrase_params, "VM-Message-Private-Local-Copy"))) { const char *file_path = switch_event_get_header(menu.phrase_params, "VM-Message-File-Path"); if (file_path && unlink(file_path) != 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Failed to delete temp file [%s]\n", file_path); } } menu_instance_free(&menu); } done: switch_event_destroy(&msg_list_params); menu_free(&menu); return; }
void mtvm_menu_main(switch_core_session_t *session, vmivr_profile_t *profile) { switch_channel_t *channel = switch_core_session_get_channel(session); switch_event_t *msg_list_params = NULL; size_t msg_count = 0; size_t current_msg = 1; size_t next_msg = current_msg; size_t previous_msg = current_msg; char *cmd = NULL; int retry; /* Different switch to control playback of phrases */ switch_bool_t initial_count_played = SWITCH_FALSE; switch_bool_t skip_header = SWITCH_FALSE; switch_bool_t msg_deleted = SWITCH_FALSE; switch_bool_t msg_undeleted = SWITCH_FALSE; switch_bool_t msg_saved = SWITCH_FALSE; vmivr_menu_profile_t menu = { "std_navigator" }; /* Initialize Menu Configs */ populate_profile_menu_event(profile, &menu); if (!menu.event_keys_dtmf || !menu.event_phrases) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing Menu Phrases and Keys\n"); return; } /* Get VoiceMail List And update msg count */ cmd = switch_core_session_sprintf(session, "json %s %s %s", profile->api_profile, profile->domain, profile->id); msg_list_params = jsonapi2event(session, NULL, profile->api_msg_list, cmd); msg_count = atol(switch_event_get_header(msg_list_params,"VM-List-Count")); /* TODO Add Detection of new message and notify the user */ for (retry = MAX_ATTEMPT; switch_channel_ready(channel) && retry > 0; retry--) { dtmf_ss_t loc; char *dtmfa[16] = { 0 }; switch_event_t *phrase_params = NULL; switch_event_create(&phrase_params, SWITCH_EVENT_REQUEST_PARAMS); append_event_profile(phrase_params, profile, menu); populate_dtmfa_from_event(phrase_params, profile, menu, dtmfa); previous_msg = current_msg; /* Simple Protection to not go out of msg list scope */ /* TODO: Add Prompt to notify they reached the begining or the end */ if (next_msg == 0) { next_msg = 1; } else if (next_msg > msg_count) { next_msg = msg_count; } current_msg = next_msg; captureMenuInitialize(&loc, dtmfa); /* Prompt related to previous Message here */ append_event_message(session, profile, phrase_params, msg_list_params, previous_msg); if (msg_deleted) { msg_deleted = SWITCH_FALSE; captureMenu(session, &loc, switch_event_get_header(menu.event_phrases, "ack"), "deleted", phrase_params, NULL, 0); } if (msg_undeleted) { msg_undeleted = SWITCH_FALSE; captureMenu(session, &loc, switch_event_get_header(menu.event_phrases, "ack"), "undeleted", phrase_params, NULL, 0); } if (msg_saved) { msg_saved = SWITCH_FALSE; captureMenu(session, &loc, switch_event_get_header(menu.event_phrases, "ack"), "saved", phrase_params, NULL, 0); } /* Prompt related the current message */ append_event_message(session, profile, phrase_params, msg_list_params, current_msg); /* TODO check if msg is gone (purged by another session, notify user and auto jump to next message or something) */ if (!skip_header) { if (!initial_count_played) { cmd = switch_core_session_sprintf(session, "json %s %s %s", profile->api_profile, profile->domain, profile->id); jsonapi2event(session, phrase_params, profile->api_msg_count, cmd); initial_count_played = SWITCH_TRUE; captureMenu(session, &loc, switch_event_get_header(menu.event_phrases, "msg_count"), NULL, phrase_params, NULL, 0); } if (msg_count > 0) { captureMenu(session, &loc, switch_event_get_header(menu.event_phrases, "say_msg_number"), NULL, phrase_params, NULL, 0); captureMenu(session, &loc, switch_event_get_header(menu.event_phrases, "say_date"), NULL, phrase_params, NULL, 0); } } if (msg_count > 0) { /* TODO Update the Read date of a message (When msg start, or when it listen compleatly ??? To be determined */ captureMenu(session, &loc, switch_event_get_header(menu.event_phrases, "play_message"), NULL, phrase_params, NULL, 0); } skip_header = SWITCH_FALSE; captureMenu(session, &loc, switch_event_get_header(menu.event_phrases, "menu_options"), NULL, phrase_params, NULL, DEFAULT_IVR_TIMEOUT); if (loc.result == RES_TIMEOUT) { /* TODO Ask for the prompt Again IF retry != 0 */ } else if (loc.result == RES_INVALID) { /* TODO Say invalid option, and ask for the prompt again IF retry != 0 */ } else if (loc.result == RES_FOUND) { /* Matching DTMF Key Pressed */ const char *action = switch_event_get_header(menu.event_keys_dtmf, loc.dtmf_stored); /* Reset the try count */ retry = MAX_ATTEMPT; if (action) { if (!strcasecmp(action, "skip_intro")) { /* Skip Header / Play the recording again */ skip_header = SWITCH_TRUE; } else if (!strcasecmp(action, "next_msg")) { /* Next Message */ next_msg++; } else if (!strcasecmp(action, "prev_msg")) { /* Previous Message */ next_msg--; } else if (!strcasecmp(action, "delete_msg")) { /* Delete / Undelete Message */ if (strncasecmp(switch_event_get_header(phrase_params, "VM-Message-Flags"), "delete", 6)) { cmd = switch_core_session_sprintf(session, "%s %s %s %s", profile->api_profile, profile->domain, profile->id, switch_event_get_header(phrase_params, "VM-Message-UUID")); mt_api_execute(session, profile->api_msg_delete, cmd); msg_deleted = SWITCH_TRUE; /* TODO Option for auto going to next message or just return to the menu (So user used to do 76 to delete and next message wont be confused) */ next_msg++; } else { cmd = switch_core_session_sprintf(session, "%s %s %s %s", profile->api_profile, profile->domain, profile->id, switch_event_get_header(phrase_params, "VM-Message-UUID")); mt_api_execute(session, profile->api_msg_undelete, cmd); msg_undeleted = SWITCH_TRUE; } } else if (!strcasecmp(action, "save_msg")) { /* Save Message */ cmd = switch_core_session_sprintf(session, "%s %s %s %s", profile->api_profile, profile->domain, profile->id, switch_event_get_header(phrase_params, "VM-Message-UUID")); mt_api_execute(session, profile->api_msg_save, cmd); msg_saved = SWITCH_TRUE; } else if (!strncasecmp(action, "menu:", 5)) { /* Sub Menu */ void (*fPtr)(switch_core_session_t *session, vmivr_profile_t *profile) = mtvm_get_menu_function(action+5); if (fPtr) { fPtr(session, profile); } } else if (!strcasecmp(action, "return")) { /* Return */ retry = -1; } } } /* IF the API to get the message returned us a COPY of the file locally (temp file create from a DB or from a web server), delete it */ if (switch_true(switch_event_get_header(phrase_params, "VM-Message-Private-Local-Copy"))) { const char *file_path = switch_event_get_header(phrase_params, "VM-Message-File-Path"); if (file_path && unlink(file_path) != 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Failed to delete temp file [%s]\n", file_path); } } switch_event_destroy(&phrase_params); } switch_event_destroy(&msg_list_params); free_profile_menu_event(&menu); return; }
void vmivr_menu_main(switch_core_session_t *session, vmivr_profile_t *profile) { switch_channel_t *channel = switch_core_session_get_channel(session); vmivr_menu_t menu = { "std_main_menu" }; int retry; /* Initialize Menu Configs */ menu_init(profile, &menu); if (!menu.event_keys_dtmf || !menu.event_phrases) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing Menu Phrases or Keys in menu '%s'\n", menu.name); goto end; } for (retry = menu.ivr_maximum_attempts; switch_channel_ready(channel) && retry > 0; retry--) { char *cmd = NULL; menu_instance_init(&menu); switch_event_add_header(menu.phrase_params, SWITCH_STACK_BOTTOM, "IVR-Retry-Left", "%d", retry); ivre_init(&menu.ivre_d, menu.dtmfa); cmd = switch_core_session_sprintf(session, "json %s %s %s %s", profile->api_profile, profile->domain, profile->id, profile->folder_name); jsonapi2event(session, menu.phrase_params, profile->api_msg_count, cmd); //initial_count_played = SWITCH_TRUE; ivre_playback(session, &menu.ivre_d, switch_event_get_header(menu.event_phrases, "msg_count"), NULL, menu.phrase_params, NULL, 0); ivre_playback(session, &menu.ivre_d, switch_event_get_header(menu.event_phrases, "menu_options"), NULL, menu.phrase_params, NULL, menu.ivr_entry_timeout); if (menu.ivre_d.result == RES_TIMEOUT) { ivre_playback_dtmf_buffered(session, switch_event_get_header(menu.event_phrases, "timeout"), NULL, NULL, NULL, 0); } else if (menu.ivre_d.result == RES_INVALID) { ivre_playback_dtmf_buffered(session, switch_event_get_header(menu.event_phrases, "invalid"), NULL, NULL, NULL, 0); } else if (menu.ivre_d.result == RES_FOUND) { /* Matching DTMF Key Pressed */ const char *action = switch_event_get_header(menu.event_keys_dtmf, menu.ivre_d.dtmf_stored); /* Reset the try count */ retry = menu.ivr_maximum_attempts; if (action) { if (!strncasecmp(action, "new_msg:", 8)) { void (*fPtr)(switch_core_session_t *session, vmivr_profile_t *profile) = vmivr_get_menu_function(action+8); profile->folder_filter = VM_MSG_NEW; if (fPtr) { fPtr(session, profile); } } else if (!strncasecmp(action, "saved_msg:", 10)) { void (*fPtr)(switch_core_session_t *session, vmivr_profile_t *profile) = vmivr_get_menu_function(action+10); profile->folder_filter = VM_MSG_SAVED; if (fPtr) { fPtr(session, profile); } } else if (!strcasecmp(action, "return")) { /* Return to the previous menu */ retry = -1; } else if (!strncasecmp(action, "menu:", 5)) { /* Sub Menu */ void (*fPtr)(switch_core_session_t *session, vmivr_profile_t *profile) = vmivr_get_menu_function(action+5); if (fPtr) { fPtr(session, profile); } } } } menu_instance_free(&menu); } end: menu_free(&menu); }