FreeSWITCH custom features

Some typical examples

System -> Custom feature add the new feature details. Then in Applications -> Feature codes add the new feature code pointing to your new custom code. Then add a number assignment. This takes effect immediately and also custom code edits take effect immediately.

Time of Day

The FreeSWITCH uses the c++ strftime() not e.g. the PHP one. The following custom feature code says the time, e.g. <<the time is four fifty six am>>

<action application=”answer”/>
<action application=”set” data=”actime=${strftime(%H:%M %p)}”/>
<action application=”set” data=”tts_engine=flite”/>
<action application=”set” data=”tts_voice=slt”/>
<action application=”speak” data=”the time is ${actime}”/>
<action application=”hangup”/>

 Day of Year

The FreeSWITCH uses the c++ strftime() not e.g. the PHP one. The following custom feature code says the date, e.g. <<the date is thursday  (pause) thirty th august twenty twelve>>

<action application=”answer”/>
<action application=”set” data=”acday=${strftime(%A)}”/>
<action application=”set” data=”acdate=${strftime(%d %B %Y)}”/>
<action application=”set” data=”tts_engine=flite”/>
<action application=”set” data=”tts_voice=slt”/>
<action application=”speak” data=”the date is ${acday}”/>
<action application=”speak” data=”${acdate}”/>
<action application=”hangup”/>

 Date and Time (using built-in voice)

<action application=”say” data=”en CURRENT_DATE_TIME pronounced ${strepoch()}”/>
<action application=”hangup”/>

 

What is my SIP user i.e. what is my extension.

This works if SIP username is a number

<action application=”say” data=”en NUMBER iterated ${sip_from_user}”/>
<action application=”hangup”/>

 Using system to get shell script data

You have to do this in two parts because of a little trap regarding null terminated strings in that you must ensure that what is returned is null terminated else if you try and say this you will get rubbish results. If you are using awk then to terminate that explicitly you use,

awk '{ printf "%s%c", $1, 0 }'

Saying percentage free system disk

Create a shellscript with the following,

df -kh / | tail -1 | awk '{ printf "%s%c",100-$5,0 }'

and save it to a filename e.g. I am using,

 /usr/local/freeswitch/scripts/freerootdisk.sh

Then create your custom code,

<action application=”answer”/>
<action application=”set” data=”resultdisk=${system /usr/local/freeswitch/scripts/freerootdisk.sh”/>
<action application=”set” data=”tts_engine=flite”/>
<action application=”set” data=”tts_voice=slt”/>
<action application=”speak” data=”the system disk is “/>
<action application=”speak” data=” ${resultdisk} “/>
<action application=”speak” data=”percent free “/>
<action application=”hangup”/>

Saying percentage user CPU

Create a shellscript with the following,

vmstat 1 2 | tail -1 | awk '{ printf "%s%c", $13, 0 }'

and save it to a filename e.g. I am using,

 /usr/local/freeswitch/scripts/uservmstat.sh

Then create your custom code,

<action application=”answer”/>
<action application=”set” data=”resultsay=${system /usr/local/freeswitch/scripts/uservmstat.sh}”/>
<action application=”set” data=”tts_engine=flite”/>
<action application=”set” data=”tts_voice=slt”/>
<action application=”speak” data=”the system user processor  is “/>
<action application=”speak” data=” ${resultsay}”/>
<action application=”speak” data=”percent “/>
<action application=”hangup”/>

 

Saying CPU temperature

This assumes that sensors package is installed on your machine. Create a shellscript with the following,

sensors | awk '{if (match($0, "CPU Temperature") )  {printf("%d%c",$3,0);} }'

and save it to a filename e.g. I am using,

 /usr/local/freeswitch/scripts/cputemp.sh

Then create your custom code,

<action application=”answer”/>
<action application=”set” data=”resultsay=${system /usr/local/freeswitch/scripts/cputemp.sh}”/>
<action application=”set” data=”tts_engine=flite”/>
<action application=”set” data=”tts_voice=slt”/>
<action application=”speak” data=”the system processor  temperature is “/>
<action application=”speak” data=” ${resultsay}”/>
<action application=”speak” data=”degrees Celsius “/>
<action application=”hangup”/>

 

 Ahhhh it is broken…

If you are adamant that you are passing a null terminated string then you can try this little debugging edit to the SWITCH_STANDARD_APP(speak_function) in the file,

/usr/local/src/freeswitch/src/mod/applications/mod_dptools/mod_dptools.c

Add in …

char a[1000]="";
int i = 0;

then around line 2093 add in…

while (i < strlen(text)){
 sprintf(a,"%s%x",a,text[i]);
 i++;
 }
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Saying! [%s][%s][0x%s][%s]\n", engine, voice, a, text);

Do a make install on that and then restart FreeSWITCH. Now dial the number you have assigned and look at the FreeSWITCH console for the error message. Check that the hex characters match the displayed text and do not have spurious data.