Saturday, August 13, 2011

Using Asterisk call files with FreePBX (using Local channel)

Call files are perhaps one of the coolest things you can do with Asterisk. Just literally dump the file in a particular directory and voila, you can make a call. Past few days, I’ve been playing around with it quite a bit and would like to share some things with you on using the Local channel which is most beneficial since you may wish to take advantage of the dialplans and call flows defined in FreePBX (for instance).
Now, the Local channel does a lot such as checks if voicemail is enabled, call forwarding (follow-me) and other customization to the user’s account. It also would support the Device and User mode if that’s being used. The Local channel rather than using technology channels directly can help with several things again for example restrictions that may apply (context) for a particular user. Using channels like SIP/1000 and IAX/1000 will literally bypass all the good stuff that may have been setup.
In conjunction with asterisk call files e.g. .call. You can do lots! including lots of automation and what not just like how you would do using AMI or any AGI stuff if you know about them. Here I would like to show you how to take advantage of the Local channel in call files.
Channel: Local/2000@from-internal/n
CallerID: <0386892800>
MaxRetries: 0
RetryTime: 1
WaitTime: 10
Context: from-internal
Extension: 86892888
Priority: 1 
Archive: no
Set: AMPUSER=2000
Now try modifying these above values to match your specific needs, e.g. channel 2000 is a user 2000 attached to device 1000, change that, change Extension to the destination you wish to bridge a call and here’s what’s important for device/user mode, you must specify the AMPUSER value otherwise, the macro-set-callerid will not be able to set right variables such as outbound CID for you since the Local/2000 will eventually be using SIP/1000 and SIP 1000 doesn’t really have anything more than just a connection agreement (username/password).
Create this file anywhere and then dump it into /var/spool/asterisk/outgoing and voila, you’ve got call.
Also, would like to share a script to automatically create and move files for you; note this will work with FreePBX 2.8 or higher and Asterisk 1.8.
1) nano /root/callgenerator.sh
2) Paste the script below

#!/bin/bash
user=$1
dial=$2

if [[ "$user" -eq "" ]]; then
    echo "ERROR No User / Destination Defined"
    exit 2
fi

if [[ "$dial" -eq "" ]]; then
    echo "ERROR No Destination Number Specified"
    exit 2
fi

# generate call file
mydate=`date +%d%m%y_%H%M%S`
filename="$mydate.U-$user-D-$dial.call"
#
# SUPPORT ONLY SIP
echo -e "Channel: Local/$dial@from-internal/n
CallerID:
MaxRetries: 0
RetryTime: 1
WaitTime: 10
Context: from-internal
Extension:*60
Priority: 1
Archive: no
Set: AMPUSER=$user" > /var/spool/asterisk/$filename

# Call now
# File ops
chown asterisk:asterisk /var/spool/asterisk/$filename
mv /var/spool/asterisk/$filename /var/spool/asterisk/outgoing/ > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
    echo "ERROR Parsefile crashed"
    exit 2
else
    echo Start calling..
fi

exit 0


3) Make it executable chmod +x /root/callgenerator.sh
Test it out like this /root/callgenerator.sh 2000 986892888 (where 2000 is the user’s phone that will first ring and once picked up, it will proceed with the other leg of the call i.e. 986892888, its important here to use from-internal otherwise, your dialplan routing and prefix handingling (in this case, the number 9) will not be stripped off when calling).
Post a Comment