AMI Event AgentCalled

Hi,
I am new to php and have this problem,i cant figure out how to fix it.
I am trying to get the AMI Event AgentCalled and send it via curl to my web server.
My php script

<?php

$host="localhost";
$username="myusername";
$secret="mypassword";

$socket = fsockopen($host,"5038", $errno, $errstr,10);
      if (!$socket){
        echo "$errstr ($errno)\n";
        }else{
            fputs($socket, "Action: Login\r\n");
            fputs($socket, "UserName: $username\r\n");
            fputs($socket, "Secret: $secret\r\n\r\n");
            fputs($socket, "Action: WaitEvent\r\n");
            fputs($socket, "Action: Logoff\r\n\r\n");

   while(!feof($socket)){

  $result=fread($socket,4096);

		$r=explode("\n",$result);
		print_r($r);
		//catch the event name
		$event=explode(" ",$r[0]);
		echo "Event name1 is $event[1]\n";
		//Match the Event
			if (preg_match("/AgentCalled/","$event[1]")){
				//extract parameters from AgentCalled
				foreach( $r as $key=>$value){
				echo " $key : $value\n";
				}
				//clean variables
				$Event=cleanstr($r[0]);
				$MemberName=cleanstr($r[32]);
				$Queue=cleanstr($r[30]);
				$CallerIDName=cleanstr($r[6]);
				$CallerIDNum=cleanstr($r[5]);
				//echo count_chars($member,3);
				//clean variables from special characters.We could use trim($var,"\r")
				$Event=trim($Event);
				$MemberName=trim($MemberName);
				$Queue=trim($Queue);
				$CallerIDName=trim($CallerIDName);
				$CallerIDNum=trim($CallerIDNum);

				# Setup request to send json via POST.
				$ch = curl_init();
				$payload = json_encode( array( "Event"=>$Event,"MemberName"=>$MemberName,"Queue"=>$Queue, "CallerIDName"=> $CallerIDName,"CallerIDNum"=>$CallerIDNum) );
				//echo "<pre>$payload</pre>";
				curl_setopt($ch, CURLOPT_URL,"https://test.com/api/AgentCalled");
				curl_setopt($ch, CURLOPT_POST, 1);
				curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
				curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
				# Return response instead of printing.
				curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
				# Send request.
				$response = curl_exec($ch);
				$result = json_decode($response,true);
				curl_close($ch);
				echo "eventAgentCalled Hit";
			}						
			continue;
		} 

//	}
} 

fclose($socket);
function cleanstr($var){
$var = explode(" ",$var);
 $var=$var[1];
return($var);

}
?>

Outcome:

Array
(
    [0] => Event: AgentCalled
    [1] => Privilege: agent,all
    [2] => Channel: SIP/xxxxxxxxxx-0000000f
    [3] => ChannelState: 6
    [4] => ChannelStateDesc: Up
    [5] => CallerIDNum: xxxxxxxxx
    [6] => CallerIDName: xxxx\u03a0\u0391\u03a0\u03a0\u0391\u03a3 \u03a7\u03a1\u0397\u03a3\u03a4\u039f\u03a3
    [7] => ConnectedLineNum: <unknown>
    [8] => ConnectedLineName: <unknown>
    [9] => Language: en
    [10] => AccountCode: 
    [11] => Context: ext-queues
    [12] => Exten: 595
    [13] => Priority: 65
    [14] => Uniqueid: 1631432002.27
    [15] => Linkedid: 1631432002.27
    [16] => DestChannel: Local/1000@from-queue-00000003;1
    [17] => DestChannelState: 0
    [18] => DestChannelStateDesc: Down
    [19] => DestCallerIDNum: +302610639501
    [20] => DestCallerIDName: <unknown>
    [21] => DestConnectedLineNum: xxxxxxxx
    [22] => DestConnectedLineName: xxxxx\u03a0\u0391\u03a0\u03a0\u0391\u03a3 \u03a7\u03a1\u0397\u03a3\u03a4\u039f\u03a3
    [23] => DestLanguage: en
    [24] => DestAccountCode: 
    [25] => DestContext: from-queue
    [26] => DestExten: 595
    [27] => DestPriority: 1
    [28] => DestUniqueid: 1631432003.28
    [29] => DestLinkedid: 1631432002.27
    [30] => Queue: 595
    [31] => Interface: Local/1000@from-queue/n
    [32] => MemberName: 1000
    [33] => 
    [34] => 
)
Event name1 is AgentCalled
 0 : Event: AgentCalled
 1 : Privilege: agent,all
 2 : Channel: SIP/xxxxxxx-0000000f
 3 : ChannelState: 6
 4 : ChannelStateDesc: Up
 5 : CallerIDNum: xxxxxxxx
 6 : CallerIDName: xxxxx\u03a0\u0391\u03a0\u03a0\u0391\u03a3 \u03a7\u03a1\u0397\u03a3\u03a4\u039f\u03a3
 7 : ConnectedLineNum: <unknown>
 8 : ConnectedLineName: <unknown>
 9 : Language: en
 10 : AccountCode: 
 11 : Context: ext-queues
 12 : Exten: 595
 13 : Priority: 65
 14 : Uniqueid: 1631432002.27
 15 : Linkedid: 1631432002.27
 16 : DestChannel: Local/1000@from-queue-00000003;1
 17 : DestChannelState: 0
 18 : DestChannelStateDesc: Down
 19 : DestCallerIDNum: +xxxxxxxxxx
 20 : DestCallerIDName: <unknown>
 21 : DestConnectedLineNum: xxxxxxxxxxxx
 22 : DestConnectedLineName: xxxxxxxx\u03a0\u0391\u03a0\u03a0\u0391\u03a3 \u03a7\u03a1\u0397\u03a3\u03a4\u039f\u03a3
 23 : DestLanguage: en
 24 : DestAccountCode: 
 25 : DestContext: from-queue
 26 : DestExten: 595
 27 : DestPriority: 1
 28 : DestUniqueid: 1631432003.28
 29 : DestLinkedid: 1631432002.27
 30 : Queue: 595
 31 : Interface: Local/1000@from-queue/n
 32 : MemberName: 1000
 33 : 
 34 : 
eventAgentCalled Hit

Everything is working fine.
BUT if i add eventfilter=Event: QueueCallerJoin
in my manager.conf the the outcome is

Array
(
    [0] => Event: QueueCallerJoin
    [1] => Privilege: agent,all
    [2] => Channel: SIP/xxxxxxx-00000019
    [3] => ChannelState: 6
    [4] => ChannelStateDesc: Up
    [5] => CallerIDNum: xxxxxxxxxxx
    [6] => CallerIDName: xxx\u03a0\u0391\u03a0\u03a0\u0391\u03a3 \u03a7\u03a1\u0397\u03a3\u03a4\u039f\u03a3
    [7] => ConnectedLineNum: <unknown>
    [8] => ConnectedLineName: <unknown>
    [9] => Language: en
    [10] => AccountCode: 
    [11] => Context: ext-queues
    [12] => Exten: 595
    [13] => Priority: 65
    [14] => Uniqueid: 1631434390.46
    [15] => Linkedid: 1631434390.46
    [16] => Queue: 595
    [17] => Position: 1
    [18] => Count: 1
    [19] => 
    [20] => Event: AgentCalled
    [21] => Privilege: agent,all
    [22] => Channel: SIP/xxxxxxxxx-00000019
    [23] => ChannelState: 6
    [24] => ChannelStateDesc: Up
    [25] => CallerIDNum: xxxxxxxxx
    [26] => CallerIDName: xxxx\u03a0\u0391\u03a0\u03a0\u0391\u03a3 \u03a7\u03a1\u0397\u03a3\u03a4\u039f\u03a3
    [27] => ConnectedLineNum: <unknown>
    [28] => ConnectedLineName: <unknown>
    [29] => Language: gr
    [30] => AccountCode: 
    [31] => Context: ext-queues
    [32] => Exten: 595
    [33] => Priority: 65
    [34] => Uniqueid: 1631434390.46
    [35] => Linkedid: 1631434390.46
    [36] => DestChannel: Local/1000@from-queue-00000004;1
    [37] => DestChannelState: 0
    [38] => DestChannelStateDesc: Down
    [39] => DestCallerIDNum: +xxxxxxxxx
    [40] => DestCallerIDName: <unknown>
    [41] => DestConnectedLineNum: xxxxxxxxxxxx
    [42] => DestConnectedLineName: xxxxxxx\u03a0\u0391\u03a0\u03a0\u0391\u03a3 \u03a7\u03a1\u0397\u03a3\u03a4\u039f\u03a3
    [43] => DestLanguage: en
    [44] => DestAccountCode: 
    [45] => DestContext: from-queue
    [46] => DestExten: 595
    [47] => DestPriority: 1
    [48] => DestUniqueid: 1631434390.47
    [49] => DestLinkedid: 1631434390.46
    [50] => Queue: 595
    [51] => Interface: Local/1000@from-queue/n
    [52] => MemberName: 1000
    [53] => 
    [54] => 
)
Event name1 is QueueCallerJoin

Means i can not catch the AgentCalled Event.
Any help to fix it ???

Thank you

Please explain how you think the event filter should behave, in some detail, as, to me, it seems to be behaving as documented.

You can’t have two actions in one request, which is probably why you got two AgentCalled events back, rather than logging off after the first one.

I need the first outcome even if i have eventfilter=Event: QueueCallerJoin enabled in manager.conf.
That way i will be able to catch the AgentCalled Event.With my current code i cant now.
I dont need to catch the QueueCallerJoin at this place.

By the way i have only one AgentCalled Event back.The second appearance in the outcome is the echo " $key : $value\n";

You need to add a filter for that, if you have any filters at all.

This is definitely missing a newline after WaitEvent.

Can you please help me out by modifying my code ?
It would be very helpful.
Thank you

Nothing is missing here, I wrote similar works, and it works perfectly

<?php



$socket = fsockopen(“127.0.0.1”,“5038”, $errno, $errstr,10);
if (!$socket){
echo “$errstr ($errno)\n”;
}else{
fputs($socket, “Action: Login\r\n”);
fputs($socket, “UserName: admin\r\n”);
fputs($socket, “Secret: mypass456\r\n\r\n”);
fputs($socket, “Action: WaitEvent\r\n”);
fputs($socket, “Action: Logoff\r\n\r\n”);

while(!feof($socket)){
$result=fread($socket,5000);

if (preg_match("/\b$argv[1]\b/i", $result,$match)){

//echo $result;
$result=explode("\n",$result);
foreach($result as $key=>$value){
echo “$key : $value\n”;
}

}
}
}
fclose($socket);

?>







root@asterisk-dominicana:~# php ami.php Newchannel
0 : Event: Newchannel
1 : Privilege: call,all
2 : Channel: SIP/102-0000abd1
3 : ChannelState: 0
4 : ChannelStateDesc: Down
5 : CallerIDNum: 102
6 : CallerIDName: <unknown>
7 : ConnectedLineNum: <unknown>
8 : ConnectedLineName: <unknown>
9 : Language: en
10 : AccountCode:
11 : Context: home
12 : Exten: 13052362323
13 : Priority: 1
14 : Uniqueid: 1481351003.64411
15 : Linkedid: 1481351003.64411
16 :
17 :
0 : Event: Newchannel
1 : Privilege: call,all
2 : Channel: SIP/400-0000abd2
3 : ChannelState: 0
4 : ChannelStateDesc: Down
5 : CallerIDNum: <unknown>
6 : CallerIDName: phone
7 : ConnectedLineNum: <unknown>
8 : ConnectedLineName: <unknown>
9 : Language: en
10 : AccountCode:
11 : Context: pstn
12 : Exten: s
13 : Priority: 1
14 : Uniqueid: 1481351003.64412
15 : Linkedid: 1481351003.64411
16 :
17 :

Maybe there is a bug in the parsing of WaitEvent, but:

http://www.asteriskdocs.org/en/3rd_Edition/asterisk-book-html-chunk/AMI_id248378.html#AMI_id248495

The other possibility is that falling off the end of the TCP stream is logging you out, and you aren’t noticing the error about the missing Logoff.

Can Someone please help me out on my first post ?

ambiorixg12
With the code you posted i have still unwanted outcome

0 : Event: QueueCallerJoin
1 : Privilege: agent,all
2 : Channel: SIP/xxxxxxx-0000009c
3 : ChannelState: 6
4 : ChannelStateDesc: Up
5 : CallerIDNum: xxxxxxxxxxxxxxx
6 : CallerIDName: xxxxxx\u03a0\u0391\u03a0\u03a0\u0391\u03a3 \u03a7\u03a1\u0397\u03a3\u03a4\u039f\u03a3
7 : ConnectedLineNum: <unknown>
8 : ConnectedLineName: <unknown>
9 : Language: en
10 : AccountCode: 
11 : Context: ext-queues
12 : Exten: 595
13 : Priority: 65
14 : Uniqueid: 1631510794.207
15 : Linkedid: 1631510794.207
16 : Queue: 595
17 : Position: 1
18 : Count: 1
19 : 
20 : Event: AgentCalled
21 : Privilege: agent,all
22 : Channel: SIP/xxxxxxxxxx-0000009c
23 : ChannelState: 6
24 : ChannelStateDesc: Up
25 : CallerIDNum: xxxxxxxxxx
26 : CallerIDName: xxxxx\u03a0\u0391\u03a0\u03a0\u0391\u03a3 \u03a7\u03a1\u0397\u03a3\u03a4\u039f\u03a3
27 : ConnectedLineNum: <unknown>
28 : ConnectedLineName: <unknown>
29 : Language: en
30 : AccountCode: 
31 : Context: ext-queues
32 : Exten: 595
33 : Priority: 65
34 : Uniqueid: 1631510794.207
35 : Linkedid: 1631510794.207
36 : DestChannel: Local/1000@from-queue-0000000e;1
37 : DestChannelState: 0
38 : DestChannelStateDesc: Down
39 : DestCallerIDNum: xxxxxxxxx
40 : DestCallerIDName: <unknown>
41 : DestConnectedLineNum: xxxxxxxx
42 : DestConnectedLineName: xxxxxx\u03a0\u0391\u03a0\u03a0\u0391\u03a3 \u03a7\u03a1\u0397\u03a3\u03a4\u039f\u03a3
43 : DestLanguage: en
44 : DestAccountCode: 
45 : DestContext: from-queue
46 : DestExten: 595
47 : DestPriority: 1
48 : DestUniqueid: 1631510794.208
49 : DestLinkedid: 1631510794.207
50 : Queue: 595
51 : Interface: Local/1000@from-queue/n
52 : MemberName: 1000
53 : 
54 : 

[root@pbx home]# nohup /usr/bin/php /home/eventAgentCalledAmbiorix.php AgentCalled

What is exactly your issue with the QueueCallerJoin event ?

I need only this outcome

Your filter should be “Event: AgentCalled

Looks like Event: QueueCallerJoin and Event: AgentCalled are linked and returned on the same stream, but you can solve this problem easily on the PHP side with one of the following

  1. create a new array with the value returned on the main array and add only the key and values you need

  2. remove all key and values after Event: AgentCalled

Will try your solution.
Will let you know the result.
Thank you very much for your help.

I finaly managed to make it work.
The problem is that (Event: QueueCallerJoin and Event: AgentCalled) and (Event: QueueCallerLeave and Event: AgentConnect) are SOMETIMES ,not always, on the same stream.
I created two new arrays with the value returned and send with curl these two arrays separately.
Thanks for your help.

1 Like