justlooking
justlooking

Reputation: 21

How to set Asterisk call log CDR database fields from AMI ORIGINATE

I'm using the Asterisk Management Interface (AMI) in PHP to originate outbound calls. Below is a snippet of the code I'm using:

// snippet
// $num is the number to dial e.g. 0207 121 3456
// $ext is the extension use to make the call e.g. 101
// $name is the name of the caller e.g. Fred Flintstone
//
fputs($socket, "Action: Originate\r\n" );
fputs($socket, "Channel: SIP/$ext\r\n" );
fputs($socket, "Exten: $num\r\n" );
fputs($socket, "Context: from-internal\r\n");
fputs($socket, "Priority: 1\r\n" );
fputs($socket, "CallerID: \"".$name."\" <".$num.">\r\n" );
fputs($socket, "Async: yes\r\n\r\n" );

How do I set the caller name ($name) or other details written to the asteriskcdrdb for outbound calls within the originate script? Ideally I want to set the dst fields in the same way the inbound call fields are set.

Upvotes: 2

Views: 5151

Answers (3)

You can put any variable in action originate:

    fputs($socket, "Action: Originate\r\n" );
    fputs($socket, "Channel: SIP/201\r\n" );
    fputs($socket, "Exten: 1234\r\n" );
    fputs($socket, "Account: FOOBAR\r\n" );
    fputs($socket, "Context: from-internal\r\n" );
    fputs($socket, "Priority: 1\r\n" );
    fputs($socket, "WaitTime: 15\r\n" );
    fputs($socket, "Callerid: 123456\r\n" );
    fputs($socket, "Variable: CDR(userfield)=AnyData\r\n\r\n" );
    $wrets=fgets($socket,128);
    echo $wrets;

Upvotes: 1

Omar FadlAllah
Omar FadlAllah

Reputation: 1

you don't need to use Action: Setvar to set the CDR(accountcode) , all what you need is to add the below line during call origination

           fputs($socket, "Account: FOOBAR\r\n" );

and below is full example

       fputs($socket, "Action: Originate\r\n" );
        fputs($socket, "Channel: SIP/201\r\n" );
        fputs($socket, "Exten: 1234\r\n" );
        fputs($socket, "Account: FOOBAR\r\n" );
        fputs($socket, "Context: from-internal\r\n" );
        fputs($socket, "Priority: 1\r\n" );
        fputs($socket, "WaitTime: 15\r\n" );
        fputs($socket, "Callerid: 123456\r\n\r\n" );
        $wrets=fgets($socket,128);
        echo $wrets;

Upvotes: 0

alvaropgl
alvaropgl

Reputation: 850

After the originate you can parse the output to look for the channel id and then you can use Setvar to set CDR(userfield) or CDR(accouncode) ... etc.

My example:

fputs($socket, "Action: Originate\r\n");
fputs($socket, "Channel: $userExt\r\n");
fputs($socket, "Context: $AMIcntx\r\n");
fputs($socket, "Exten: $phoneNum\r\n");
fputs($socket, "Priority: 1\r\n");

$chan=true;
$channelID=0;

while (!feof($socket))
{
    if ($chan && preg_match("#Channel: ([a-zA-Z0-9\\/-]+)#", $wrets, $cm))
    {
        $channelID = $cm[1];        
        fputs($socket, "Action: Setvar\r\n");
        fputs($socket, "Channel: $channelID\r\n");
        fputs($socket, "Variable: CDR(userfield)\r\n");
        fputs($socket, "Value: FOOBAR\r\n\r\n");        
        fputs($socket, "Action: Logoff\r\n\r\n");
        $chan = false; //There are various Channel response, not overwrite.
    }
    //Further parsing of the AMI response go heres
}

SetVar sets the variable for the specified channel only. You have to send the setvar command early as possible, before the call ends.

Another way if you could edit your dialplan (i can't, because i use freepbx) is to make a specific context for the ami call, pass some variables from the originate to the context, a set this vars to the CDR in the dialplan.

Upvotes: 0

Related Questions