[Freeswitch-users] Tips adding ASR to a existing script.
broken dash
brokendash at gmail.com
Sat Sep 18 00:54:44 PDT 2010
I'm wanting to add ASR to this javascript I've attached below... I've
seen the pizza ordering demo as an example but I'm just not sure where
to get started really an I'm looking for a little guidance. My script
below has only 4 possible options (dtmf 1-4) and I'm simply just
wanting to add the ability for a caller to speak the choice they would
like ie: 1,2,3,or 4 while retaining the ability for the caller to
enter the selection using dtmf. Any assistance would be appreciated...
:-)
Brian
# cat autoattendant_5002.js
var condition = true;
var domain = "home"; //by default this is the ipv4 address of
FreeSWITCH used for transfer to voicemail
var digitmaxlength = 0;
var objdate = new Date();
var adjusthours = 0; //Adjust Server time that is set to GMT 7 hours
var adjustoperator = "-"; //+ or -
if (adjustoperator == "-") {
var objdate2 = new
Date(objdate.getFullYear(),objdate.getMonth(),objdate.getDate(),(objdate.getHours()
- adjusthours),objdate.getMinutes(),objdate.getSeconds());
}
if (adjustoperator == "+") {
var objdate2 = new
Date(objdate.getFullYear(),objdate.getMonth(),objdate.getDate(),(objdate.getHours()
+ adjusthours),objdate.getMinutes(),objdate.getSeconds());
}
var Hours = objdate2.getHours();
var Mins = objdate2.getMinutes();
var Seconds = objdate2.getSeconds();
var Month = objdate2.getMonth() + 1;
var Date = objdate2.getDate();
var Year = objdate2.getYear()
var Day = objdate2.getDay()+1;
var exit = false;
dialed_extension = session.getVariable("dialed_extension");
domain_name = session.getVariable("domain_name");
domain = session.getVariable("domain");
us_ring = session.getVariable("us-ring");
caller_id_name = session.getVariable("caller_id_name");
caller_id_number = session.getVariable("caller_id_number");
effective_caller_id_name = session.getVariable("effective_caller_id_name");
effective_caller_id_number = session.getVariable("effective_caller_id_number");
outbound_caller_id_name = session.getVariable("outbound_caller_id_name");
outbound_caller_id_number = session.getVariable("outbound_caller_id_number");
session.execute("set", "ignore_early_media=true");
session.execute("set", "hangup_after_bridge=true");
session.execute("set", "continue_on_fail=true");
session.execute("set", "call_timeout=45");
session.execute("export", "call_timeout=45");
session.execute("set", "ringback="+us_ring); //set to ringtone
session.execute("set", "transfer_ringback="+us_ring); //set to ringtone
//console_log( "info", "Auto Attendant Server Time is: "+Hours+":"+Mins+" \n" );
function get_sofia_contact(extension,domain_name, profile){
if (profile == "auto") {
profile = "external";
session.execute("set",
"sofia_contact_"+extension+"=${sofia_contact("+profile+"/"+extension+"@"+domain_name+")}");
sofia_contact =
session.getVariable("sofia_contact_"+extension);
if (sofia_contact == "error/user_not_registered") {
profile = "internal";
session.execute("set",
"sofia_contact_"+extension+"=${sofia_contact("+profile+"/"+extension+"@"+domain_name+")}");
sofia_contact =
session.getVariable("sofia_contact_"+extension);
}
}
else {
session.execute("set",
"sofia_contact_"+extension+"=${sofia_contact("+profile+"/"+extension+"@"+domain_name+")}");
sofia_contact = session.getVariable("sofia_contact_"+extension);
}
console_log( "info", "sofia_contact "+profile+":
"+sofia_contact+".\n" );
return sofia_contact;
}
function mycb( session, type, obj, arg ) {
try {
if ( type == "dtmf" ) {
console_log( "info", "digit: "+obj.digit+"\n" );
if ( obj.digit == "#" ) {
//console_log( "info", "detected pound sign.\n" );
exit = true;
return( false );
}
dtmf.digits += obj.digit;
if ( dtmf.digits.length >= digitmaxlength ) {
exit = true;
return( false );
}
}
} catch (e) {
console_log( "err", e+"\n" );
}
return( true );
} //end function mycb
condition=true;
if (condition) {
//action
//console_log( "info", "action call now don't wait for dtmf\n" );
var dtmf = new Object( );
dtmf.digits = "";
if ( session.ready( ) ) {
session.answer( );
digitmaxlength = 1;
while (session.ready() && ! exit ) {
session.streamFile(
"/usr/local/freeswitch/recordings/greeting-main.wav", mycb, "dtmf 20"
);
if (session.ready()) {
if (dtmf.digits.length == 0) {
dtmf.digits += session.getDigits(1, "#",
20000); // 20 seconds
if (dtmf.digits.length == 0) {
}
else {
break; //dtmf found end the while loop
}
}
}
}
//pickup the remaining digits
dtmf.digits += session.getDigits(4, "#", 3000); //allow up to 5 digits
if ( dtmf.digits.length > "0" ) {
if ( dtmf.digits == "1" ) { //
session.execute("transfer", "3973 XML default"); //
}
else if ( dtmf.digits == "2" ) { //
session.execute("playback", "shout://stream.infowars.com"); //
}
else if ( dtmf.digits == "d" ) { //
session.execute("bridge", "sofia/internal/5002@${domain}"); //
}
else if ( dtmf.digits == "3" ) { //
session.execute("playback",
"shout://ice.somafm.com/groovesalad"); //
}
else if ( dtmf.digits == "4" ) { //
session.execute("bridge", "sofia/gateway/home/lisaworking"); //
}
else {
//console_log( "info", "default option when
there is no matching dtmf found\n" );
session.execute("bridge",
"sofia/internal/5002@${domain}"); //
}
}
} //end if session.ready
}
More information about the FreeSWITCH-users
mailing list