# Asterisk Configuration for Capturing DTMF events This sections describes how Asterisk has been setup and how to configure the mastercall-agi suite to trap DTMF events in a conversation between 2 parties. When Asterisk is used as an IVR (not this use case !), we have Bob calling Asterisk, and Asterisk answers the call and mimics an agent. It can capture dtmf events, capture sounds, playback audio, etc... Asterisk has an active part in the call. However, when Bob call Asterisk and then Asterisk forwards the call to Alice (ie, when a human calls a call center through AlloMedia), Asterisk bridges the two legs of the call (Bob->Asterisk and Asterisk->Alice), and does not yield control until the call is terminated. It is logically and technically not possible to capture DTMF or intervene in the call. The call can not be at the same time connected to Alice AND to an IVR. ## Solution : global & permanent configuration To work around this, we use the specific features normally designed to forward the call, or stop the recording, etc... Asterisk allows the definition of features, ie DTMF key combinations, that will trigger dialplan routines. For instance, on a typical IPBX, hitting * and 1 will allow the party to transfer the call to another extension. We've taken advantage of this to define each single DTMF key as a feature (called svi0, svi1, etc...) - this is done in the /etc/asterisk/features.conf: ``` ... svi0=0,peer,Gosub(featuresvi,0,1) ... ``` This tells asterisk to execute the featuresvi routine from the dialplan. It can be found in /etc/asterisk/extensions.conf: ``` [featuresvi] exten => _[#*012345678],1,Verbose(1,${STRFTIME(${EPOCH},GMT,%C%y-%m-%d %H:%M:%S)} - Ctx: ${CONTEXT} - Exten: ${EXTEN} - Clid: ${CALLERID(all)} - DTMF activated - xuniqueid: ${XUNIQUEID}) same => n,Set(last=${EXTEN}) same => n,Set(lastts=${STRFTIME(${EPOCH},,%s)}) same => n,Verbose(1,${CONTEXT} - last=${last}, lastts${lastts}) same => n,Verbose(1,${CONTEXT} - dtmf2eda.py ${XUNIQUEID} ${EPOCH} ${EXTEN}) same => n,System(PYTHONPATH=/datas-web/websites/asterisk/public/asterisk/eda-messenger/ python3.9 -m app.dtmf2eda ${XUNIQUEID} ${EPOCH} ${EXTEN} &) same => n,SendDTMF(${EXTEN},,30) same => n,Return() ``` Basically, it tells asterisk to execute a python script to send the appropriate EDA message, and replay the DTMF so the other party gets it too. There is also first a bit of logic so existing key combinations, like *9 that stops recording, continues to work. # Solution: Per scenario configuration To activate these features, the mastercall-agi.php script issues a command to Asterisk: `SET VARIABLE __DYNAMIC_FEATURES ....` The value being is taken from the ASTERISKAPI database, table SCENARIO, column `json_extra` . This column has been designed to hold miscelleanous data relating to the scenario. In our case, we just have to add: `{"feature":"svi0#svi1#svi2#svi3#svi4#svi5#svi6#svi7#svi8#svi9#svistar#svipound"}` in the relevant scenario, et voila :-) Should any scenario have the DTMF events trapping activated, it just needs to have this json_extra column updated. ### Possible conflicts Currently, the DTMF capture is designed to capture single keys, and if \* then 9 are punched less than 2 seconds appart, to stop the call recording. If no json_extra.features is defined, only the simple \*9 feature is active. However, this json_extra.features column is also used for LaPoste, which requires to have \* to stop recording, and not \*9. Should we want DTMF AND a single key (\*) to stop recording, a specific logic would need to be implemented, to differentiate a \* punched to stop recording, from another \* in the client IVR flow.