Wednesday, October 23, 2013

Modifying Bell Paging Schedules in Asterisk

The site that caused me to set up bell paging on our trixbox system also threw me a curveball by having an "alternate" schedule that they needed implemented every Thursday, although occasionally they need to set the "Thursday" schedule on other days of the week. I got tired of manually updating the crontab for these times, so I configured a feature code so that they could override the normally programmed time themselves.

First create a custom context to use as a destination that will run the script that modifies our crontab
edit /etc/asterisk/extensions_custom.conf ad add something like
[ext-belltoggle-custom]
exten => s,1,AGI(toggle-bells.sh)

Next create a custom destination to trigger that context
Custom destination : ext-belltoggle-custom,s,1
Description: Toggle Bells

Then create a Misc Application to trigger our custom destination:
Description: Toggle Bells
Feature Code: *281 (or whatever you like)
Feature Status: Enabled
Destination: Custom Destinations -> Toggle Bells

Create toggle-bells.sh in /var/lib/asterisk/agi-bin with the following

#!/bin/bash

# Consume all variables sent by Asterisk
while read VAR && [ -n ${VAR} ] ; do : ; done

# Answer the call.
echo "ANSWER"
read RESPONSE

# Say the letters of "Hello World"
#echo 'SAY ALPHA "Hello World" ""'
#read RESPONSE

if crontab -l | grep -q "ALTERNATE"; then
  echo "51 * * * * /var/lib/asterisk/bin/freepbx-cron-scheduler.php
1 00 * * 1,2,3,5 /usr/bin/crontab /var/lib/asterisk/scripts/normalbells
1 00 * * 4 /usr/bin/crontab /var/lib/asterisk/scripts/alternatebells
1 00 * * 0,6 /usr/bin/crontab /var/lib/asterisk/scripts/weekendbells
##NORMAL BELLS##
##Put your normal schedule here with lines like the following##
30 08 * * * /usr/sbin/asterisk -rx 'originate Local/8102@ext-paging extension 8103@ext-paging'
" | crontab -

  echo 'EXEC PLAYBACK "custom/NormalBellActivated" ""'
  read RESPONSE

elif crontab -l | grep -q "NORMAL"; then
  echo "51 * * * * /var/lib/asterisk/bin/freepbx-cron-scheduler.php
1 00 * * 1,2,3,5 /usr/bin/crontab /var/lib/asterisk/scripts/normalbells
1 00 * * 4 /usr/bin/crontab /var/lib/asterisk/scripts/alternatebells
1 00 * * 0,6 /usr/bin/crontab /var/lib/asterisk/scripts/weekendbells
##ALTERNATE BELLS##
##Put your alternate schedule here with lines like the following##
35 08 * * * /usr/sbin/asterisk -rx 'originate Local/8102@ext-paging extension 8103@ext-paging'
" | crontab -

  echo 'EXEC PLAYBACK "custom/AlternateBellActivated" ""'
  read RESPONSE

fi

exit 0

Create 3 crontab files in /var/lib/asterisk/scripts/ called normalbells alternatebells and weekendbells that will be sourced to set the schedule to the normal time each day.

They should look identical to what the asterisk user's crontab looks like when it's in one of the modes.

Finally, create two system recordings named NormalBellActivated and AlternateBellActivated that will be used to inform the user which mode they're in.

Friday, October 18, 2013

FreePBX BLF Time Conditions

My previous post was about increasing the number of Call Routing toggles in FreePBX because I thought that was what I would need to do in order to get BLF (Busy Lamp Field) working with the presence feature on our Polycom SoundPoint phones. The reason I wanted to do that was that so users could have an indication if their phone was in "night mode" or not. As it turns out there is a MUCH better way to handle this.

Step 1
In FreePBX, go to Settings -> Advanced Settings and change "Enable Custom Device States" to True

Step 2
In Applications -> Time Conditions, create your Time Condition. You'll notice that by enabling the previous option, you now have a "Generate BLF Hint" checkbox. Be sure to set that to ON, make sure you also set the "Enable Override Code" box to ON. Once you save the Time Condition, make a note of the override code assigned to it (likely something like *270)

Step 3
On your phone, enable the presence feature. For my SoundPoint IP 601, this meant setting         feature.1.enabled="{$feature_1_enabled|1}" in my server.cfg
I had tried to set feature.1.enabled="1" in sip.cfg which I had seen referenced multiple times, however it never seemed to enable the presence feature.

Step 4
Add the extension *270 to the list of watched extensions on your phone. You can provision it via config file, or set up the extension in the local directory on the phone, and while doing so, scroll down to the "Buddy Watch" option and toggle it to "Enabled"

Thursday, October 17, 2013

Increasing the number of FreePBX Call Flow Control Options

We're starting to retool our VOIP infrastructure, and consolidate down from 30+ Trixbox servers to a single FreePBX server. One issue with the amalgamation however is that by default, FreePBX only allows you 10 Call Flow Control indexes. Since we have some 30 odd sites, this wouldn't work for us. However, this fantastic blog post: http://sysadminman.net/blog/2013/daynight-aka-call-flow-control-more-than-10-4884 shows you how to increase that to the number of your choosing. I'll repost the gist here.

1. copy /var/www/html/admin/modules/daynight/functions.inc.php to /var/www/html/admin/modules/daynight/functions.inc.php.dist (good to have a backup)
2. edit /var/www/html/admin/modules/daynight/functions.inc.php and find function daynight_get_avail() { (should start around line 258)
3. under global $db; add another line that reads $NUMDAYNIGHT = 99;
4. comment out the section that starts for ($i=0;$i<=9;$i++) { and ends return $list; (since this is PHP, just add // to the start of those lines
5. under the commented out lines, add the following:

         for ($i=0;$i<=($NUMDAYNIGHT-1);$i++) {
                 $n=ceil(log10($NUMDAYNIGHT));
                 $format="%0".$n."d";
                 $j=sprintf($format,$i);
                 if (!in_array($j,$results)) {
                         $list[]=$j;
                 }
         }
         return $list;

The whole block of code from line 258 function daynight_get_avail() { to line 284 } should look like:

function daynight_get_avail() {
        global $db;
        $NUMDAYNIGHT = 99;

        $sql = "SELECT ext FROM daynight ORDER BY ext";
        $results = $db->getCol($sql);
        if(DB::IsError($results)) {
                $results = array();
        }

//      for ($i=0;$i<=9;$i++) {
//              if (!in_array($i,$results)) {
//                      $list[]=$i;
//              }
//      }
//      return $list;

        for ($i=0;$i<=($NUMDAYNIGHT-1);$i++) {
                $n=ceil(log10($NUMDAYNIGHT));
                $format="%0".$n."d";
                $j=sprintf($format,$i);
                if (!in_array($j,$results)) {
                        $list[]=$j;
                }
        }
        return $list;
}

Now if you go to Applications -> Call Flow Control you will have 99 options for Flow Toggles!