/* 
 * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
 * Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
 *
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
 *
 * The Initial Developer of the Original Code is
 * Anthony Minessale II <anthmct@yahoo.com>
 * Portions created by the Initial Developer are Copyright (C)
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 * 
 * Anthony Minessale II <anthmct@yahoo.com>
 *
 *
 * dialer.js ASR Demonstration Application
 *
 */
include("js_modules/SpeechTools.jm");

var dft_min = 100;
var dft_confirm = 600;

var asr = new SpeechDetect(session, "speechlib", "127.0.0.1");
asr.AutoUnload = true;
asr.debug = 1;
asr.setTTS("cepstral", "david");


numberObtainer = new SpeechObtainer(asr, 1, 5000);
numberObtainer.setGrammar("number", "ABNFPhone.gram", "result", dft_min, dft_confirm, true);
numberObtainer.setTopSound("Say a phone number please");
numberObtainer.setBadSound("I didn't understand you, please try again");
numberObtainer.addRegEx("x{0,1}(\\d*)");

yesnoObtainer = new SpeechObtainer(asr, 1, 5000);
yesnoObtainer.setGrammar("yesno", "pizza/yesno.gram", "result", 500, 250, true);
yesnoObtainer.setBadSound("I didn't understand you, please try again");
yesnoObtainer.addItem("yes,no");

session.answer();
session.execute("sleep", "1000");


function get_yn(ob, text) {
    while(session.ready()) {
        ob.setTopSound(text);
        var yn = ob.run();
        if (ob.needConfirm) {
            yesnoObtainer.setTopSound("I couldn't hear you, " + text);
            continue;
        }
        return yn;
    }
}

while(session.ready()) {
    items = numberObtainer.run();
    var yn;
    //var interp = new XML(asr.lastDetect); 
    //asr.speak("I heard you say " + interp.input + " , and i gather the phone number is, " + items[0]);

    yn = get_yn(yesnoObtainer, items[0] + ", should i dial that number?");
        
    if (yn[0] == "yes") {
        asr.speak("ok, stand by");
        session.execute("transfer", items[1]);
    } else {
        asr.speak("ok, I will not dial that number");
    }
 }