Examples answermachine

From FreeSWITCH Wiki
Jump to: navigation, search

This is a basic answering machine application designed for the home pbx. It will play a greeting message you have previously recorded, then beep, and record the callers message. It then emails the voice message to one or more email addresses. This way you can get your home voicemail even while on the road. When done sending the email it removes the temporary voice file to keep from filling your drive up. It has a number of configurable options that are documented in the code below.

Revision History:

  • Jan-08-2007 - Added pager/cell smtp gateway notification, fixed a few syntax bugs.
   use("TeleTone");
   use("etpan");
   
   // * answermachine.js
   // * Written by: Mike B. Murdock for use with FreeSwitch
   // * Based on original samples by Anthony Minessale II
   // * Revision: 01-08-2007
   // 
   // * 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.
   // *
   // Note: this script uses mod_spidermonkey_etpan which must be compiled and loaded. 
   // Additionally, it requires sendmail be setup and configured on your server.
   
   // Where are your emails coming from
   var eMailFrom = "you@domain.org";
   
   // List email addresses to send message to. Separate multiples with comma. Leave blank to disable emails.
   var eMailTo = "you1@domain.org, you2@domain.org, you3@domain.com";
   
   // List email addresses to send a notification to without a file attachment. Usually cell phone notification.
   var Pager_eMailTo = "3165551212@mobile.att.net";
   
   // Caller Name and Number will be attached
   var eMailSubject = "Voicemail from ";
   
   // Text of email body
   var eMailBody = "New voicemail attached.\n";
   
   // location of temp voicemail files. In a high performance app this might be a ram drive.
   var vMailPath = "/tmp/" // should end in slash
                   
   // Place your recorded greeting here
   var vMailGreetingFile = "/usr/local/freeswitch/sounds/greeting.wav"
                           
   //var BONG = "v=4000;>=0;+=2;#(60,0);v=2000;d(940,0)";
   var BONG = "v=4000;>=0;+=2;#(60,0)";
   
   // Maximum recording length in seconds (240 = 4 minutes should be enough)
   var maxreclen = 240; 
   
   // Energy level audio must fall below to be considered silence (300-500 is good starting point)
   var silencethreshold = 500;
   
   // Amount of time in seconds caller must be silent to trigger detector
   var silencehits = 5; // 3 seconds 
   
   var allDigits = "";
   
   function on_dtmf(session, type, digits, arg)
   {
       if (digits.digit == "#") {
           return allDigits;
       }
   
       if (digits.digit == "*") {
           return false; //stop the recording.
       }
   
       console_log("digit: " + digits.digit + "\n");
       allDigits += digits.digit;
       return(allDigits);
   }
   
   // If not answered answer the call
   session.answer();
   
   if (session.ready()) {
       // save the session properties for later use in case caller hangs up and we need the data.
       var sv_uuid = session.uuid;
       var sv_ani = session.ani;
       var sv_ani2 = session.ani2;
       var sv_caller_id_name = session.caller_id_name + " ";
       var sv_caller_id_num = session.caller_id_num + " ";
       var sv_destination = session.destination;
       var sv_dialplan = session.dialplan;
       var sv_name = session.name;
       var sv_network_addr = session.network_addr;
       var sv_state = session.state;
       
       console_log("Caller ID = " + sv_caller_id_name + "<" + sv_caller_id_num + ">\n");
       
       allDigits = "";
       
       // Play announcement
       var rtn;
       // session.streamFile(file, callback, callback_args, starting_sample_count);
       rtn = session.streamFile(vMailGreetingFile, on_dtmf, "");
       console_log("session.streamFile rtn=[" + rtn + "]\n");
       
       // verify that the caller is still here
       if (session.ready()) {
           
           // Pause for 1/10th second
           rtn = session.execute("sleep", "100");
           
           // Play bong
           var tts = new TeleTone(session);
           tts.addTone("d", 350.0, 440.0, 0.0);
           tts.generate(BONG); 
           
           // Record message
           console_log("Recording message\n");
           var tmp_Filename = vMailPath + sv_uuid + ".wav";
           
           // Caller still here?
           if (session.ready()) {
               rtn = session.recordFile(tmp_Filename, on_dtmf, "", maxreclen, silencethreshold, silencehits);
               console_log("session.recordFile rtn=[" + rtn + "]\n");
               
               // Caller still here?
               if (session.ready()) {
                   // play it back - for testing
                   //rtn = session.streamFile(tmp_Filename, "", on_dtmf, "");
                   
                   // Hangup
                   session.hangup
               }
               else {
                   console_log("Caller Hungup during record\n");
               }
               
               // construct the email notification
               if (eMailTo != "") {
                   var tmp_eMailSubject = eMailSubject + sv_caller_id_name + " (" + sv_caller_id_num + ")";
                   email(eMailFrom, eMailTo, "Subject: " + tmp_eMailSubject, eMailBody, tmp_Filename);
                   
                   // construct the email notification to cell phone gateway - no file attached
                   if (Pager_eMailTo != "") {
                       email(eMailFrom, Pager_eMailTo, "Subject: " + tmp_eMailSubject, tmp_eMailSubject);
                   }
               }
   
               // remove the tmp voicemail file
               rtn = fileDelete(tmp_Filename);
           }
       }
   }

See Also