;Version("3.1.2.140")
;InformationalVersion("3.1.2.0")

[nixxis-amd]

exten => amd,1,Answer
 same => n,Set(TIMEOUT(absolute)=14400)
 same => n,Set(ACCOUNTCODE=${SIP_HEADER(x-cr-acc)})
 same => n,Set(RINGDURATION=${SIP_HEADER(x-cr-ringduration)})
 same => n,Set(AMD-RD=${SIP_HEADER(x-cr-amd-rd)})
 same => n,Set(AMD-IS=${SIP_HEADER(x-cr-amd-is)})
 same => n,Set(AMD-G=${SIP_HEADER(x-cr-amd-g)})
 same => n,Set(AMD-AGS=${SIP_HEADER(x-cr-amd-ags)})
 same => n,Set(AMD-TAT=${SIP_HEADER(x-cr-amd-tat)})
 same => n,Set(AMD-MWL=${SIP_HEADER(x-cr-amd-mwl)})
 same => n,Set(AMD-IBWS=${SIP_HEADER(x-cr-amd-bws)})
 same => n,Set(AMD-MNOW=${SIP_HEADER(x-cr-amd-mnow)})
 same => n,Set(AMD-ST=${SIP_HEADER(x-cr-amd-st)})
 same => n,Set(AMD-PM=${SIP_HEADER(x-cr-amd-pm):0:1})
 same => n,Set(AMD-PU=${SIP_HEADER(x-cr-amd-pm):1:1})
 same => n,Set(AMD-PH=${SIP_HEADER(x-cr-amd-pm):2:1})
 same => n,Set(AMD-PROMPT=${SIP_HEADER(x-cr-amd-prompt)})
 same => n,Set(PLAYDIR=${NixxisSoundPath}/${ACCOUNTCODE})
 same => n,Set(ACTIVITY=${SIP_HEADER(x-cr-act)})
 
 same => n,Playback(silence/1)
 same => n,GotoIf($[ ${RINGDURATION} < ${AMD-RD} ]?machinering)
 same => n,GotoIf($["${AMD-TAT}" == "0" ]?notsureskip)


 same => n,AMD(${AMD-IS},${AMD-G},${AMD-AGS},${AMD-TAT},${AMD-MWL},${AMD-IBWS},${AMD-MNOW},${AMD-ST})
 same => n,SendText(CRIVRAMDstatus:${AMDSTATUS}\,CRIVRAMDcause:${AMDCAUSE})
 same => n,NoOp(done with AMD)

 same => n,GotoIf($["${AMDSTATUS}" == "MACHINE" ]?machine)
 same => n,GotoIf($["${AMDSTATUS}" == "HUMAN" ]?human)
 same => n,GotoIf($["${AMDSTATUS}" == "NOTSURE" ]?notsure)
 same => n(result),SendText(CRIVRAMDstatus:${AMDSTATUS}\,CRIVRAMDcause:${AMDCAUSE}\,CRIVRAMDend)
 same => n,Wait(15)
 same => n,HangUp()

 same => n(machinering),NoOp(Answering machine detected due to short ring)
 same => n,Set(AMDSTATUS=MACHINE)
 same => n,Set(AMDCAUSE=SHORTRING)
 same => n,SendText(CRIVRAMDstatus:${AMDSTATUS}\,CRIVRAMDcause:${AMDCAUSE})

 same => n(machine),NoOp(Answering machine detected)
 same => n,GotoIf($["${AMD-PM}" == "0" ]?result)
 same => n,GotoIf($["${AMD-PM}" == "" ]?result)
 same => n,WaitForSilence(2000)
 same => n,ExecIf($["${AMD-PROMPT}" != ""]?PlayBack(${PLAYDIR}/${AMD-PROMPT}))
 same => n,ExecIf($["${AMD-PROMPT}" == ""]?PlayBack(${NixxisSoundPath}/amdprompts/${ACTIVITY})
 same => n,Goto(result)

 same => n(human),NoOp(Human detected)
 same => n,GotoIf($["${AMD-PH}" == "0" ]?result)
 same => n,GotoIf($["${AMD-PH}" == "" ]?result)
 same => n,ExecIf($["${AMD-PROMPT}" != ""]?PlayBack(${PLAYDIR}/${AMD-PROMPT}))
 same => n,ExecIf($["${AMD-PROMPT}" == ""]?PlayBack(${NixxisSoundPath}/amdprompts/${ACTIVITY})
 same => n,Goto(result)

 same => n(notsureskip),NoOp(Detection skipped)
 same => n,Set(AMDSTATUS=NOTSURE)
 same => n,Set(AMDCAUSE=SKIPPED)
 same => n,SendText(CRIVRAMDstatus:${AMDSTATUS}\,CRIVRAMDcause:${AMDCAUSE})

 same => n(notsure),NoOp(Not sure detected)
 same => n,GotoIf($["${AMD-PU}" == "0" ]?result)
 same => n,GotoIf($["${AMD-PU}" == "" ]?result)
 same => n,ExecIf($["${AMD-PROMPT}" != ""]?PlayBack(${PLAYDIR}/${AMD-PROMPT}))
 same => n,ExecIf($["${AMD-PROMPT}" == ""]?PlayBack(${NixxisSoundPath}/amdprompts/${ACTIVITY})
 same => n,Goto(result)

