Run a script when a call is answered

I want to run a bash script when an external call is answered and pass the caller id and the extension that answered as variables. I know that there are similar questions, and I’ve spent a while trying to decipher them. I’ve figured out that I probably need to do something with custom destinations and extensions. And I think I’m finally gaining a grasp on how dialplans work. But I still haven’t been able to figure out where I need to call the script. I imagine there is a dialplan somewhere that deals with calls being answered, or a certain number that correlates to an answered call, but I haven’t found it. I’ve been trying for a while to figure this out, but I’ve hit a lot of walls in the process. Any help would be incredibly appreciated.

It sounds like you are trying to get something like a CRM module working, which would be a perfect application for the FreePBX Commercial CRM Module.

That isn’t actually what I’m trying to do. This script is just a step towards a more complicated end goal. I need to be able to run that script when an external inward call is answered. Even some advice to point me in the right direction would be appreciated.

OK - that was advice in the form of a possible solution, but that’s fine.

Here are a couple more “starter points” that might lead you towards the vague goal you still haven’t shared.

  • There are hooks for “Caller ID SuperFecta”. You can start there and add a new scheme or use the hooks like Superfecta does.
  • The FreePBX CRM module has the capability to call a URL which you can then use for all sorts of things that sound a little like the vague thing you are trying to do.

Hello @matthewljensen,

If you have problems using the Asterisk server for doing that (AMI, REST etc.),
You can always use an action url directly from your phone (Yealink, Grandstream etc.).

You can develop a web service that will accept the parameters that the ip phone will send it after answering the call.

Thank you,

Daniel Friedman

I use a macro-auto-blkvm in extensions_override_freepbx.conf:

[macro-auto-blkvm]
include => macro-auto-blkvm-custom
exten => s,1,Set(__MACRO_RESULT=)
exten => s,n,Set(CFIGNORE=)
;exten => s,n(weasels),System(sh script1.sh ${CALLERID(num)})
exten => s,n,Noop(${REALCALLERIDNUM} - ${CALLERID(num)})
exten => s,n,Noop(${REALCALLERIDNUM} - ${CALLERID(num)})
exten => s,n,Set(MASTER_CHANNEL(CFIGNORE)=)
exten => s,n,Set(FORWARD_CONTEXT=from-internal)
exten => s,n,Set(MASTER_CHANNEL(FORWARD_CONTEXT)=from-internal)
exten => s,n,Macro(blkvm-clr,)
exten => s,n,ExecIf($[!${REGEX("[^0-9]" ${DIALEDPEERNUMBER})} && “${DB(AMPUSER/${AMPUSER}/cidname)}” != “”]?Set(MASTER_CHANNEL(CONNECTEDLINE(num))=${DIALEDPEERNUMBER}))
exten => s,n,ExecIf($[!${REGEX("[^0-9]" ${DIALEDPEERNUMBER})} && “${DB(AMPUSER/${AMPUSER}/cidname)}” != “”]?Set(MASTER_CHANNEL(CONNECTEDLINE(name))=${DB(AMPUSER/${DIALEDPEERNUMBER}/cidname)}))

;–== end of [macro-auto-blkvm] ==–;

[func-apply-sipheaders]
include => func-apply-sipheaders-custom
exten => s,1,Noop(Applying SIP Headers to channel)
exten => s,n,Noop(${REALCALLERIDNUM} - ${CALLERID(num)})
exten => s,n,Noop(${REALCALLERIDNUM} - ${CALLERID(num)})
exten => s,n,Set(SIPHEADERKEYS=${HASHKEYS(SIPHEADERS)})
exten => s,n,While($["${SET(sipkey=${SHIFT(SIPHEADERKEYS)})}" != “”])
exten => s,n,Set(sipheader=${HASH(SIPHEADERS,${sipkey})})
exten => s,n,SIPAddHeader(${sipkey}: ${sipheader})
exten => s,n,Set(PJSIP_HEADER(add,${sipkey})=${sipheader})
exten => s,n,EndWhile
exten => s,n,Return()

;–== end of [func-apply-sipheaders] ==–;

Thanks. I’ll do some testing tonight. I don’t want to risk messing something up during office hours.

Hey. Thanks for the help in setting this up. But I’m having trouble getting the caller id of the actual caller. The macro you gave me only sends the answerer caller id, which is the extension which answers. From all my reading, it seems that ${CALLERID(num)}) should give me what I want, but like I said, it just gives the extension which answered. How can I pass both the extension answering and the id of the caller?

Pastebin your CLI log of call here!

In my example I have
Dialplan:
exten => s,n,Noop(${REALCALLERIDNUM} - ${CALLERID(num)})

CLI:
Executing [s@macro-auto-blkvm:3] NoOp(“PJSIP/2000-00000007”, “1001 - 2000”) in new stack

1001 - Client
2000 - Agent

Is this the part you are looking for?

– Executing [s@macro-auto-blkvm:1] Set(“SIP/250-0000004e”, “_MACRORESULT=”) in new stack
– Executing [s@macro-auto-blkvm:2] Set(“SIP/250-0000004e”, “CheckProtocolo=1”) in new stack
– Executing [s@macro-auto-blkvm:3] GotoIf(“SIP/250-0000004e”, “1?macro-auto-blkvm,s,weasels:macro-auto-blkvm,s,iguanas”) in new stack
– Goto (macro-auto-blkvm,s,4)
– Executing [s@macro-auto-blkvm:4] System(“SIP/250-0000004e”, “sh /scripts/checkdb.sh 250”) in new stack

Later On:

-- Executing [s@macro-auto-blkvm:5] Goto("SIP/250-0000004e", "macro-auto-blkvm,s,7") in new stack
-- Goto (macro-auto-blkvm,s,7)
-- Executing [s@macro-auto-blkvm:7] ExecIf("SIP/250-0000004e", "0?Set(MASTER_CHANNEL(CONNECTEDLINE(num))=250)") in new stack
-- Executing [s@macro-auto-blkvm:8] ExecIf("SIP/250-0000004e", "0?Set(MASTER_CHANNEL(CONNECTEDLINE(name))=Test)") in new stack

Try this example:

[macro-auto-blkvm]
include => macro-auto-blkvm-custom
exten => s,1,Set(__MACRO_RESULT=)
exten => s,n,Set(CFIGNORE=)
exten => s,n,Noop(${REALCALLERIDNUM} - ${CALLERID(num)})
exten => s,n,Noop(${REALCALLERIDNUM} - ${CALLERID(num)})
exten => s,n,Noop(${REALCALLERIDNUM} - ${CALLERID(num)})
;exten => s,n,System(sh script1.sh ${REALCALLERIDNUM} - ${CALLERID(num)})
exten => s,n,Set(MASTER_CHANNEL(CFIGNORE)=)
exten => s,n,Set(FORWARD_CONTEXT=from-internal)
exten => s,n,Set(MASTER_CHANNEL(FORWARD_CONTEXT)=from-internal)
exten => s,n,Macro(blkvm-clr,)
exten => s,n,ExecIf($[!${REGEX("[^0-9]" ${DIALEDPEERNUMBER})} && "${DB(AMPUSER/${AMPUSER}/cidname)}" != ""]?Set(MASTER_CHANNEL(CONNECTEDLINE(num))=${DIALEDPEERNUMBER}))
exten => s,n,ExecIf($[!${REGEX("[^0-9]" ${DIALEDPEERNUMBER})} && "${DB(AMPUSER/${AMPUSER}/cidname)}" != ""]?Set(MASTER_CHANNEL(CONNECTEDLINE(name))=${DB(AMPUSER/${DIALEDPEERNUMBER}/cidname)}))

;--== end of [macro-auto-blkvm] ==--;

This is just an example, I suggest studying the functions and applications of asterisk!

just confirming that this works only when landing on the ring group, otherwise can use [macro-dial-one]'s dial vall by adding M() to it
might end up injecting line like this if you don’t want all extensions to run macro just those start with 0 for example

exten => s,n,ExecIf($[ ${CALLERID(number):0:1} = 0 ],System,echo ‘Answered call from ${CALLERID(name)} at ${CALLERID(number)}’ | mail -s ‘Call answered by ${CALLERID(number)}’ ‘[email protected]’)