Avatar of FishMonger
FishMonger
Flag for United States of America asked on

Macro to set callerid on DAHDI channels for outbound calls through sip trunk

We have 5 servers in a cluster with 96 DAHDI channels each and need to find a way to set the outbound callerid without having to write 96 individual config blocks in each of the chan_dahdi.conf files.

In addition to the sip trunk DID phone number each line also has a 5 digit number which we use internally.  We have a mysql database of the person's name, DID, 5 digit ext, and server they're on, but does not include their DAHDI channel.

My thought is to use a macro that runs an agi script to query the db and set the callerid, but I'm not sure how to write the macro.  If the caller is in the [homeoffice] context and the macro is in the [outboundsiptrunk] context, how do I extract the proper info needed to pass to the agi script?
IP Telephony

Avatar of undefined
Last Comment
FishMonger

8/22/2022 - Mon
FishMonger

ASKER
>> the macro is in the [outboundsiptrunk] context

That's not worded well.  The macro is being called from within the [outboundsiptrunk] context.
grblades

Here is an example that I use. To call it you just use the following command. It picks up the original caller by looking at the callerid of the extension and looks up the correct callerid number to use in a database.
${FOO} can be set to 'autowithold' to also look in the data to see whether callerid should be witheld.
$(FOO) set to 'withheld' will force the callerid to be witheld.
${FOO} set to 'force' followed by a number will force the callerid to be set to that number.
AGI(sm-setoutgoingcid.agi,${FOO})

<?php
include_once "ez_sql.php";

function writevar($variable, $value)
  {
  fwrite(STDOUT, "SET VARIABLE " .$variable ." " .$value ."\n");
  fflush(STDOUT);
  return fgets(STDIN);
  }

$agivars = array();
while (!feof(STDIN))
  {
   $agivar = trim(fgets(STDIN));
   if ($agivar === '')
     break;
   $agivar = explode(':', $agivar);
   $agivars[$agivar[0]] = trim($agivar[1]);
  }
extract($agivars);

// if withheld then withhold number
if ($agi_arg_1 == "withheld")
  writevar("CALLERPRES()","prohib");

$callerid="12345678";  \\ YOUR DEFAULT OUTGOING CALLERID
$qry="SELECT withhold, cid FROM autooutgoingcid WHERE extension='" .$agi_callerid ."'";

if ( $result = $db->get_row($qry))
  {
  if ( ($agi_arg_1 == "autowithheld")  && ($result->withhold == "yes") )
    writevar("CALLERPRES()","prohib");
  if (strlen($result->cid) > 3)
    $callerid = $result->cid;
  }

if (strncmp($agi_arg_1,"force",5) == 0)
  $callerid=substr($agi_arg_1,5);

writevar("CALLERID(num)",$callerid);
?>

Open in new window

FishMonger

ASKER
Thanks for the suggestions, but I'm not sure if that will do what I need.  I'm not in the office today, so will have to wait until tomorrow to run some tests.

Just so I know we're on the same page,  here's the basic config I'm thinking of,  but have not tested.

[homeoffice]

ignorepat => 9
include => outboundsiptrunk
; a few other includes

exten => 10180,1,Dial(DAHDI/45,,Tt)


[outboundsiptrunk]

exten => _9xxxxxxx,1,Macro(setcid,arg1)  ; what global var do I need to pass to have the macro then pass exten 10180 to the agi script?
exten => _9xxxxxxx,n,Dial(SIP/${EXTEN:1}@XO)


[macro-setcid]

exten => s,1,AGI(setcid.pl, ${ARG1})


***************

Since I'm a Perl guy, not PHP, my script would be something like the following.


#!/usr/bin/perl

use strict;
use warnings;
use Asterisk::AGI;

$|++;  # turn off buffering

my $agi    = Asterisk::AGI->new;
my %input  = $agi->ReadParse;
my $exten  = $agi->get_variable('callerid');
my $cid    = query_db($exten);

$agi->set_callerid($cid);

sub query_db {
    my $exten = shift;
    my $cid;
    
    # code that queries the db and assigns $cid
    
    return $cid;
}

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
grblades


exten => _9xxxxxxx,1,Macro(setcid,arg1)  ; what global var do I need to pass to have the macro then pass exten 10180 to the agi script?

Assuming you have the phone 10180 is setup to have 10180 as its default callerid then this information will be sent to the agi script as part of the standard set of variables (agi_callerid in this specific case). See http://www.voip-info.org/wiki/view/Asterisk+AGI
FishMonger

ASKER
We are not (or at least what to avoid) hard coding the callerid for individual extensions.  When we dial out through the sip trunk, the default callerid for any extension is the DID number that we use to authenticate with our provider.

So, if we don't hard code the callerid, would asterisk default to using the extension (10180 in this case) when it passes the vars to the agi script or would it set it to "unknown"?  I guess I'll test that tomorrow, but I assume it will be set to "unknown".

The agi_extension var would seem to be what I want, but my reading of the wiki would indicate otherwise.
ASKER CERTIFIED SOLUTION
grblades

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
FishMonger

ASKER
Well, while I was having a day off they decided they could wait the extra day for me come back in ant write the agi script so they hard coded the callerid and several other parameters for all 480 extensions.  Personally I think that's a messy way of handling it, but it's not the first time where they did this kind of thing.

We just migrated our home office from Centrex lines to the SIP trunk and from the start (which has been a 6 month process) I brought up the callerid subject multiple times and was told not to worry about because we were going to use a single callerid for everyone.  Then on Saturday just before I went home they were in a panic because the CEO was upset because no-one had their proper callerid.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.