Asterisk abbreviated dialing using Horde Turba

This script complements the Asterisk simple php lookup up callerid name from Horde Turba. I needed a way to implement a system of abbreviated or speed dialing in Asterisk - using Horde Turba address book as a back-end, in order to replace similar functionality of a proprietary PBX system. Horde is a great, free web-based suite of groupware applications, such as an address book, an email client, a calendar client and so on. My Horde installation uses PostgreSQL - hence this script uses connects to a PostgreSQL backend. I had to add two extra columns in the "turba_objects" table - "object_abbdialphone" and "object_abbdialcode".

The way the script works is as follows:

1. User dials a three digit extension on their phone (the speed dial or abbreviated dial number).
2. Asterisk scans the Turba address book to see if any record has that extension number in the the "object_abbdialcode" column.
3. If one of the address book entries matches, then the AGI script returns the number found in the "object_abbdialphone" to be dialed out instead.
4. If not address book record matches, the script returns zero - and the dialplan plays and error message instead.

First extensions.conf:

exten => _XXX,1,NoOp(Run agi script to find abb dial in Horde Turba)
exten => _XXX,n,AGI(/var/lib/asterisk/agi-bin/abbdial.php)
exten => _XXX,n,GoToIf(${abbphonenumber}?:20)
exten => _XXX,n,Dial(IAX2/outgoing1/${abbphonenumber},,T)
exten => _XXX,n,HangUp()

exten => _XXX,20,Playback(/etc/asterisk/prompts/this_speed_dial_does_not_exist2)
exten => _XXX,n,HangUp()

Then the AGI script - I have mine at /var/lib/asterisk/agi-bin/abbdial.php:

require 'phpagi.php';

$agi = new AGI();

/* Connect to Horde database */

$db = 'horde';
$dbuser = 'asterisk';
$dbpass = '';
$dbhost = 'localhost';

$dbconn = pg_connect("host=" . $dbhost . " " . "dbname=" . $db . " " . 
    "user=" . $dbuser . " " . "password=" . $dbpass);
if (!$dbconn) {
    echo "Can't connect to database.\n";

$abbdialcode = $agi->request[agi_extension];

$sql = "SELECT object_abbdialphone FROM turba_objects WHERE " .
    "object_abbdialcode LIKE '%$abbdialcode%' LIMIT 1";

$result = pg_query($dbconn, $sql);
if (!$result) {
    echo "An error occurred running the query.\n";

//if found number
if (pg_num_rows($result)==1){
    $row = pg_fetch_array($result);
    //check if full phone field is not null
    if ($row[object_abbdialphone]) {
        $phone_no = $row[object_abbdialphone];
    //if full number field is empty, set phone number to 0
    else $phone_no = "0";

//if we can't find a record which matches the abb code, set phone number to 0
else $phone_no = "0";

$agi->set_variable("abbphonenumber", $phone_no);



Created by: xj25vm, Last modification: Mon 20 of May, 2013 (20:50 UTC)
Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+