Asterisk Hunting Groups for incoming calls

The hunting group distributes the incoming calls to all registered agents.
If there are three agents )extension 500, 501 and 502) registered, the first incoming call is redirected to extension 500, the second to 501 and the third to 502. The next incoming call is redirected to 500, then 501 ...

The following extensions are defined:
  • 100 -> the call get transfered to the next available extension in hunting group 100
  • 108 -> the user adds his own extension to (the end) of avalable extensions list
  • 109 -> the user removes his own extension from the avalable extensions list

extensions.conf


;get the next available extension from hunting group 100 (command g)
exten => 100,n,agi,hunting.php|100|g
exten => 100,n,dial,SIP/${HUNTEXTEN}
exten => 100,n,hangup

;add current extension from hunting group (command a)
exten => 108,1,answer
exten => 108,n,set,HUNTGROUP=100
exten => 108,n,agi,hunting.php|${HUNTGROUP}|a|${CALLERID(num)}
exten => 108,n,playback,hunting-add
exten => 108,n,playback,hunting-yourextension
exten => 108,n,playback,hunting-togroup
exten => 108,n,saydigits,${HUNTGROUP}
exten => 108,n,hangup

;remove current extension from hunting group (command r)
exten => 109,1,answer
exten => 109,n,set,HUNTGROUP=100
exten => 109,n,agi,hunting.php|${HUNTGROUP}|r|${CALLERID(num)}
exten => 109,n,playback,hunting-remove
exten => 109,n,playback,hunting-yourextension
exten => 109,n,playback,hunting-fromgroup
exten => 109,n,saydigits,${HUNTGROUP}
exten => 109,n,hangup

;add given extension from hunting group (command a)
exten => 208,1,answer
exten => 208,n,set,HUNTGROUP=100
exten => 208,n,set,HUNTEXTEN=120
exten => 208,n,agi,hunting.php|${HUNTGROUP}|a|${HUNTEXTEN}
exten => 208,n,playback,hunting-add
exten => 208,n,saydigits,${HUNTEXTEN}
exten => 208,n,playback,hunting-togroup
exten => 208,n,saydigits,${HUNTGROUP}
exten => 208,n,hangup

;remove given extension from hunting group (command r)
exten => 209,1,answer
exten => 209,n,set,HUNTGROUP=100
exten => 209,n,set,HUNTEXTEN=120
exten => 209,n,agi,hunting.php|${HUNTGROUP}|r|${HUNTEXTEN}
exten => 209,n,playback,hunting-remove
exten => 209,n,saydigits,${HUNTEXTEN}
exten => 209,n,playback,hunting-fromgroup
exten => 209,n,saydigits,${HUNTGROUP}
exten => 209,n,hangup


hunting.php


#!/usr/bin/php -q
<?php
//hunting groups for asterisk
//copyright (c) 2007 felix simmet

function loadgroup($group) {
	$filename="$huntingfilename$group";		//build filename
	if(file_exists($filename)) {		//make sure file exists
		$filehandler=fopen($filename,"r");		//open file for read
		while(!feof($filehandler)) {			//read while not end of file
			$line=fgets($filehandler);			//read line
			$line=trim($line);			//remove trailing spaces & CR & LF
			if($line!="") {			//if line is not empty
			$extensions[]=$line;			//add extension
			}
		}
	fclose($filehandler);			//close file
	}
}

function savegroup($group) {
	$filename="$huntingfilename$group";			//build filename
	$filehandler=fopen($filename,"w");			//open file for write
	foreach($extensions as $key => $value) {			//work with every item

		if($value!="") {			//if value is not empty
    	fwrite($filehandler,$value . "\n");			//write extension + CR
    }

	}
	fclose($filehandler);			//close file
}

function addextension($extension) {
	if(!in_array($extension,$extensions)) {			//extension is not in extensions-list
		$extensions[]=$extension;			//add to extensions list
		return true;			//quit function and return true
	} else {			//not in list ->
		return false;			//quit function and return false
	}
}

function removeextension($extension) {
	$key=array_search($extension,$extensions);
	if($key!==false) {			//extension was found
		$extensions[$key]="";			//remove extensions (set empty)
	} else {			//extension not found
		return false;
	}
}

function getnextextension() {
	$nextextension=$extensions[0];			//get first extension
	array_shift($extensions);			//remove first extension
	$extensions[]=$nextextension;			//add extension at the end
	return $nextextension;			//returns next extension
}

# --- settings
#the filename where the extensions are stored (group number will be added at the end of the filename)
$huntingfilename="/tmp/huntinggroup-";

# --- internal variables
$extensions=array();

# --- set some php parameters for agi interface
ob_implicit_flush(false);
set_time_limit(30);
error_reporting(0);

# --- open stdin & stdout for agi communications
$stdin=fopen("php://stdin", "r");
$stdout=fopen("php://stdout", "w");
    
# --- get agi variables
while(!feof($stdin)) {
	$temp=fgets($stdin);
	$temp=str_replace("\n","",$temp);
	$s=explode(":",$temp);
	$agivar[$s[0]]=trim($s[1]);
	if(($temp=="") || ($temp=="\n"))
	{
		break;
	}
} 

# --- get variables from asterisk
$group=$argv[1];
$command=$argv[2];
$extension=$argv[3];

if(($group!="") && ($command!="")) {	//enough arguments???
	loadgroup($group);			//load current group settings
	
		if($command=="g") {			//get next extension -> put to variable HUNTEXT
			$huntext=getnextextension();
			fwrite($stdout,"SET VARIABLE HUNTEXTEN $huntext\n");	//write agi command
		}

	if(($extension!="")) {			//enough arguments???
		if($command=="a") {			//add given extension
			addextension($extension);
		}
		
		if($command=="r") {		//remove given extension
			removeextension($extension);
		}
	}
	
	savegroup($group);		//save group settings
}

# --- close all files
fclose($stdin);
fclose($stdout);
    
exit;
?>


create-voice.sh


#!/bin/sh
echo "Adding" | text2wave -F 8000 > /var/lib/asterisk/sounds/hunting-add.wav
echo "Removing" | text2wave -F 8000 > /var/lib/asterisk/sounds/hunting-remove.wav
echo "your extension" | text2wave -F 8000 > /var/lib/asterisk/sounds/hunting-yourextension.wav
echo "to hunting group" | text2wave -F 8000 > /var/lib/asterisk/sounds/hunting-togroup.wav
echo "from hunting group" | text2wave -F 8000 > /var/lib/asterisk/sounds/hunting-fromgroup.wav
exit 0

The hunting group distributes the incoming calls to all registered agents.
If there are three agents )extension 500, 501 and 502) registered, the first incoming call is redirected to extension 500, the second to 501 and the third to 502. The next incoming call is redirected to 500, then 501 ...

The following extensions are defined:
  • 100 -> the call get transfered to the next available extension in hunting group 100
  • 108 -> the user adds his own extension to (the end) of avalable extensions list
  • 109 -> the user removes his own extension from the avalable extensions list

extensions.conf


;get the next available extension from hunting group 100 (command g)
exten => 100,n,agi,hunting.php|100|g
exten => 100,n,dial,SIP/${HUNTEXTEN}
exten => 100,n,hangup

;add current extension from hunting group (command a)
exten => 108,1,answer
exten => 108,n,set,HUNTGROUP=100
exten => 108,n,agi,hunting.php|${HUNTGROUP}|a|${CALLERID(num)}
exten => 108,n,playback,hunting-add
exten => 108,n,playback,hunting-yourextension
exten => 108,n,playback,hunting-togroup
exten => 108,n,saydigits,${HUNTGROUP}
exten => 108,n,hangup

;remove current extension from hunting group (command r)
exten => 109,1,answer
exten => 109,n,set,HUNTGROUP=100
exten => 109,n,agi,hunting.php|${HUNTGROUP}|r|${CALLERID(num)}
exten => 109,n,playback,hunting-remove
exten => 109,n,playback,hunting-yourextension
exten => 109,n,playback,hunting-fromgroup
exten => 109,n,saydigits,${HUNTGROUP}
exten => 109,n,hangup

;add given extension from hunting group (command a)
exten => 208,1,answer
exten => 208,n,set,HUNTGROUP=100
exten => 208,n,set,HUNTEXTEN=120
exten => 208,n,agi,hunting.php|${HUNTGROUP}|a|${HUNTEXTEN}
exten => 208,n,playback,hunting-add
exten => 208,n,saydigits,${HUNTEXTEN}
exten => 208,n,playback,hunting-togroup
exten => 208,n,saydigits,${HUNTGROUP}
exten => 208,n,hangup

;remove given extension from hunting group (command r)
exten => 209,1,answer
exten => 209,n,set,HUNTGROUP=100
exten => 209,n,set,HUNTEXTEN=120
exten => 209,n,agi,hunting.php|${HUNTGROUP}|r|${HUNTEXTEN}
exten => 209,n,playback,hunting-remove
exten => 209,n,saydigits,${HUNTEXTEN}
exten => 209,n,playback,hunting-fromgroup
exten => 209,n,saydigits,${HUNTGROUP}
exten => 209,n,hangup


hunting.php


#!/usr/bin/php -q
<?php
//hunting groups for asterisk
//copyright (c) 2007 felix simmet

function loadgroup($group) {
	$filename="$huntingfilename$group";		//build filename
	if(file_exists($filename)) {		//make sure file exists
		$filehandler=fopen($filename,"r");		//open file for read
		while(!feof($filehandler)) {			//read while not end of file
			$line=fgets($filehandler);			//read line
			$line=trim($line);			//remove trailing spaces & CR & LF
			if($line!="") {			//if line is not empty
			$extensions[]=$line;			//add extension
			}
		}
	fclose($filehandler);			//close file
	}
}

function savegroup($group) {
	$filename="$huntingfilename$group";			//build filename
	$filehandler=fopen($filename,"w");			//open file for write
	foreach($extensions as $key => $value) {			//work with every item

		if($value!="") {			//if value is not empty
    	fwrite($filehandler,$value . "\n");			//write extension + CR
    }

	}
	fclose($filehandler);			//close file
}

function addextension($extension) {
	if(!in_array($extension,$extensions)) {			//extension is not in extensions-list
		$extensions[]=$extension;			//add to extensions list
		return true;			//quit function and return true
	} else {			//not in list ->
		return false;			//quit function and return false
	}
}

function removeextension($extension) {
	$key=array_search($extension,$extensions);
	if($key!==false) {			//extension was found
		$extensions[$key]="";			//remove extensions (set empty)
	} else {			//extension not found
		return false;
	}
}

function getnextextension() {
	$nextextension=$extensions[0];			//get first extension
	array_shift($extensions);			//remove first extension
	$extensions[]=$nextextension;			//add extension at the end
	return $nextextension;			//returns next extension
}

# --- settings
#the filename where the extensions are stored (group number will be added at the end of the filename)
$huntingfilename="/tmp/huntinggroup-";

# --- internal variables
$extensions=array();

# --- set some php parameters for agi interface
ob_implicit_flush(false);
set_time_limit(30);
error_reporting(0);

# --- open stdin & stdout for agi communications
$stdin=fopen("php://stdin", "r");
$stdout=fopen("php://stdout", "w");
    
# --- get agi variables
while(!feof($stdin)) {
	$temp=fgets($stdin);
	$temp=str_replace("\n","",$temp);
	$s=explode(":",$temp);
	$agivar[$s[0]]=trim($s[1]);
	if(($temp=="") || ($temp=="\n"))
	{
		break;
	}
} 

# --- get variables from asterisk
$group=$argv[1];
$command=$argv[2];
$extension=$argv[3];

if(($group!="") && ($command!="")) {	//enough arguments???
	loadgroup($group);			//load current group settings
	
		if($command=="g") {			//get next extension -> put to variable HUNTEXT
			$huntext=getnextextension();
			fwrite($stdout,"SET VARIABLE HUNTEXTEN $huntext\n");	//write agi command
		}

	if(($extension!="")) {			//enough arguments???
		if($command=="a") {			//add given extension
			addextension($extension);
		}
		
		if($command=="r") {		//remove given extension
			removeextension($extension);
		}
	}
	
	savegroup($group);		//save group settings
}

# --- close all files
fclose($stdin);
fclose($stdout);
    
exit;
?>


create-voice.sh


#!/bin/sh
echo "Adding" | text2wave -F 8000 > /var/lib/asterisk/sounds/hunting-add.wav
echo "Removing" | text2wave -F 8000 > /var/lib/asterisk/sounds/hunting-remove.wav
echo "your extension" | text2wave -F 8000 > /var/lib/asterisk/sounds/hunting-yourextension.wav
echo "to hunting group" | text2wave -F 8000 > /var/lib/asterisk/sounds/hunting-togroup.wav
echo "from hunting group" | text2wave -F 8000 > /var/lib/asterisk/sounds/hunting-fromgroup.wav
exit 0

Created by: fsi2000, Last modification: Mon 23 of Jul, 2007 (08:15 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+