如何从AMI ORIGINATE设置Asterisk呼叫日志CDR数据库字段

时间:2013-02-18 20:41:03

标签: php asterisk cdr

我正在使用PHP中的Asterisk管理界面(AMI)来发起出站呼叫。下面是我正在使用的代码片段:

// 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" );

如何为原始脚本中的出站呼叫设置呼叫者姓名($name)或写入asteriskcdrdb的其他详细信息?理想情况下,我想设置dst字段的方式与设置入站呼叫字段的方式相同。

3 个答案:

答案 0 :(得分:0)

originate之后,您可以解析输出以查找通道ID,然后您可以使用Setvar设置CDR(用户区)或CDR(accouncode)......等。

我的例子:

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仅设置指定通道的变量。在呼叫结束之前,您必须尽早发送setvar命令。

另一种方法,如果你可以编辑你的拨号方案(我不能,因为我使用freepbx)是为ami调用创建一个特定的上下文,将一些变量从origin发送到上下文,这是一个变量到CDR的集合在拨号方案中。

答案 1 :(得分:0)

您不需要使用操作:Setvar设置CDR(帐户代码),您需要的是在呼叫始发期间添加以下行

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

以下是完整示例

       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;

答案 2 :(得分:0)

您可以将任何变量应用于原始:

    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;