Execute a script when SIP extension answers a call?

Hi.

Im running Asterisk 1.4 and FreePBX 2.5 on a Ubuntu Server.

I want that asterisk executes a perl or php script, when an incoming call is answered by one of my sip devices.

The script should get the caller phone number, the called number and the extension number which takes the call via variables.

This is what i tried until now. Of cause this is not the final script. I only want to use /bin/echo to test functionality:

[from-internal-custom]
exten => _X.,1,Answer()
exten => _X.,n,Dial(SIP/${EXTEN}||M(test^${CALLERID(num)^${EXTEN}^${CHANNEL}))
exten => _X.,n,Hangup()

[macro-test]
exten => s,1,NoOp(Macro test)
exten => s,n,System(/bin/echo "caller=${ARG1} exten=${ARG2} called=${ARG3}" >> /tmp/test)

Unfortunately it does not work. If i make a call its automaticly canceled.
There is no new file created in /tmp.

This is my log:

-- Executing [2003@from-internal:1] Answer("SIP/2000-082fd8b8", "") in new stack
-- Executing [2003@from-internal:2] Dial("SIP/2000-082fd8b8", "SIP/2003||M(test^2000") in new stack
== Spawn extension (from-internal, 2003, 2) exited non-zero on 'SIP/2000-082fd8b8'
-- Executing [h@from-internal:1] Macro("SIP/2000-082fd8b8", "hangupcall") in new stack
-- Executing [s@macro-hangupcall:1] GotoIf("SIP/2000-082fd8b8", "1?skiprg") in new stack
-- Goto (macro-hangupcall,s,4)
-- Executing [s@macro-hangupcall:4] GotoIf("SIP/2000-082fd8b8", "1?skipblkvm") in new stack
-- Goto (macro-hangupcall,s,7)
-- Executing [s@macro-hangupcall:7] GotoIf("SIP/2000-082fd8b8", "1?theend") in new stack
-- Goto (macro-hangupcall,s,9)
-- Executing [s@macro-hangupcall:9] Hangup("SIP/2000-082fd8b8", "") in new stack
== Spawn extension (macro-hangupcall, s, 9) exited non-zero on 'SIP/2000-082fd8b8' in macro 'hangupcall'
== Spawn extension (macro-hangupcall, s, 9) exited non-zero on 'SIP/2000-082fd8b8' 

What is wrong?

Thanks in advance.

Greetz
André

Anything in /var/log/asterisk/full ? Can it be a permissions issue ?

Thanks for the hint.

"app.c: Missing closing parenthesis for argument ‘M’ in string… "

Now it works :smile:

I have a new problem now.

To get it work for external calls i had to change the context. Thats how it looks now.

[from-pstn-custom]
exten => _XXXX.,1,Answer()
exten => _XXXX.,n,Dial(SIP/${EXTEN}||M(test^${CALLERID(num)}^${EXTEN}^${CHANNEL}))
exten => _XXXX.,n,Hangup()

[macro-test]
exten => s,1,NoOp(Macro TEST)
exten => s,n,System(/bin/echo "caller=${ARG1} exten=${ARG2} called=${ARG3}" >> /tmp/test)

Unfortunately SIP/${EXTEN} becomes now SIP/03514123456 and not SIP/2003 like before.

But i need the number of the sip device which answers the call.

How can i reach this?

That is something that you have to decide. In place of ${EXTEN} you need to decide which sip account it should call.

But this decission should be done by my ring group.

No way to get this solved?

Can i set ${Exten} to be the answering device somehow?

Even better i could replace SIP/${EXTEN} with my TimeCondition which is configured in FreePBX…

You can replace it with what ever you want. All SIP/${EXTEN} says is to call SIP device X. X Is the extension that the call went to.

I know.

My Problem is, that the extension is the phoned number.

What i need as variable is the number of the sip device (e.g. my ata286 which as 2003) which answers the call.

I need to replace ${Exten} with this devices number.

I don’t understand the issue. Just put in what you want to call.