Running a dial plan using AMI through PHP not working right

Hi All,

Here’s what I’ve got at the moment in PHP after login:

fputs($socket, "Action: Originate\n" );
fputs($socket, "Channel: SIP/$number@$provider\n");
fputs($socket, "Callerid: 1234\n");
fputs($socket, "Context: alertmessage\n");
fputs($socket, "Exten: s\n");
fputs($socket, "Priority: 1\n");

The above works in that the call is initiated and when its picked up, the dialplan executes correctly. This is what the dialplan looks like:

[alertmessage]
exten => s,1,Noop(Alert dial plan initiated)
same => n,Answer()
same => n,Playback(someaudio)
same => n,Hangup()

Problem #1: If the call recipient does not pick up the phone (i.e. it times out) or rejects the call, the dialplan still executes. Is there a way to stop the dial plan from executing if the recipient didn’t pick up? My understanding is that I DIALSTATUS in this case does not provide the needed information.

Alternative:

I have previously tried to use the dialplan itself to dial out, which provides me with the DIALSTATUS. The problem with that is that I don’t know how to initiate a dialplan through AMI that does not attempt to connect the 2 end points (the channel in the originate request and the one being dialed in the dialplan)?

In effect, I’m trying to do something similar to what’s on this web page: http://www.voip-info.org/wiki/view/Asterisk+auto-dial+out+deliver+message.

Thanks

Don’t Answer, wait for the far end.

Or perhaps better for both questions use application instead of exten. in AMI