;Version("3.3.1255.143")
;InformationalVersion("3.3.1255.0")

[nixxis-callback-v2]

exten => s,1,NoOp(Getting a callback IVR call)
 same => n,Set(ACCOUNTCODE=${SIP_HEADER(x-cr-acc)})
 same => n,Set(ACTIVITY=${SIP_HEADER(x-cr-act)})
 same => n,Set(ANI=${SIP_HEADER(x-cr-ani)})
 same => n,Set(IVRCONFIG=${SIP_HEADER(x-cr-ivrcfg)})
 same => n,Set(IVRDIR=${SIP_HEADER(x-cr-ivrdir)})
 same => n,Set(LAN=${SIP_HEADER(x-cr-lan)})
 same => n,Set(VAR=${SIP_HEADER(x-cr-var)})

 same => n,GotoIf($["${SIP_HEADER(x-cr-iex)}" = "http"]?ivrInfoHttp:ivrInfoDone)

 same => n(ivrInfoHttp),Set(NixxisAppServerUri=${SIP_HEADER(x-cr-hsr)})
 same => n,Set(CURLOPT(httptimeout)=3)
 same => n,Set(RequestUri=${NixxisAppServerUri}/sip?action=getIvrInfo&sipid=${SIPCALLID}&version=${CUT(NixxisFileVersion,\",2)})
 same => n,Set(RawAnswer=${CURL(${RequestUri})})
 same => n,GoSub(ParseIvrInfo(${RawAnswer}))

 same => n(ivrInfoDone),ExecIf($["${ACTIVITY}" = ""]?Congestion)
 same => n,Set(CHANNEL(language)=${LAN})
 same => n,Set(Config=${FILE(${NixxisSoundPath}/${ACCOUNTCODE}/Configs/${IVRCONFIG})})
 same => n,GoSub(ParseSettings(${Config}))

 same => n,NoOp(Account set to: ${ACCOUNTCODE})
 same => n,NoOp(Activity set to: ${ACTIVITY})
 same => n,NoOp(Language set to: ${LAN})
 same => n,NoOp(VARIABLES set to: ${VAR})

 same => n,Answer
 same => n,Wait(2)
 same => n,Set(TIMEOUT(absolute)=14400)
 same => n,Set(PLAYDIR=${NixxisSoundPath}/${ACCOUNTCODE})
 same => n,GoTo(nixxis-callback-v2-menu,s,1)

 same => n(ParseSettings),NoOp(Parsing settings)
 same => n,While($["${SET(AnswerPart=${SHIFT(ARG1,\n)})}" != ""])
 same => n,Set(PartName=${CUT(AnswerPart,=,1)})
 same => n,Set(PartValue=${CUT(AnswerPart,=,2)}) 
  same => n,Set(${PartName}=${SHIFT(PartValue,.)})
 same => n,EndWhile
 same => n,Return

 same => n(ParseIvrInfo),NoOp(Parsing IvrInfo)
 same => n,While($["${SET(AnswerPart=${SHIFT(ARG1,&)})}" != ""])
 same => n,Set(PartName=${CUT(AnswerPart,=,1)})
 same => n,Set(PartValue=${CUT(AnswerPart,=,2)})
 same => n,ExecIf($["${PartName}" = "x-cr-acc"]?Set(ACCOUNTCODE=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-ani"]?Set(ANI=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-act"]?Set(ACTIVITY=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-ivrcfg"]?Set(IVRCONFIG=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-ivrdir"]?Set(IVRDIR=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-lan"]?Set(LAN=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-var"]?Set(VAR=${PartValue}))
 same => n,EndWhile
 same => n,Return


[nixxis-callback-v2-menu]

exten => s,1,NoOp(prompt: ${MSGANNOUNCE${LAN}})
 same => n,ExecIf($["${MSGANNOUNCE${LAN}}" != "" & "${LAN}" != ""]?Playback(${PLAYDIR}/${MSGANNOUNCE${LAN}}))
 same => n,ExecIf($["${MSGANNOUNCE}" != ""]?Playback(${PLAYDIR}/${MSGANNOUNCE}))
 same => n,GotoIf($["${ASKNUMBER}" = "1"]?nixxis-callback-v2-NoCallerID,s,1)
 same => n,GotoIf($["${ANI}" = ""]?nixxis-callback-v2-NoCallerID,s,1)
 same => n,GotoIf($["${ANI}" = "Unknown"]?nixxis-callback-v2-NoCallerID,s,1)
 same => n,GotoIf($["${ANI}" = "asterisk"]?nixxis-callback-v2-NoCallerID,s,1)
 same => n,Set(CALLBACKNUMBER=${ANI})
 same => n,Goto(nixxis-callback-v2-CallerID,s,1)


[nixxis-callback-v2-CallerID]

exten => s,1(request),NoOp(CallerID detected, Playing number)
 same => n,ExecIf($["${MSGCURRENTNR${LAN}}" != "" & "${LAN}" != ""]?Background(${PLAYDIR}/${MSGCURRENTNR${LAN}}))
 same => n,ExecIf($["${MSGCURRENTNR}" != ""]?Background(${PLAYDIR}/${MSGCURRENTNR}))
 same => n,SayDigits(${CALLBACKNUMBER})
 same => n,ExecIf($["${MSGTOACCEPT${LAN}}" != "" & "${LAN}" != ""]?Background(${PLAYDIR}/${MSGTOACCEPT${LAN}}))
 same => n,ExecIf($["${MSGTOACCEPT}" != ""]?Background(${PLAYDIR}/${MSGTOACCEPT}))
 same => n,ExecIf($["${MSGTOREENTER${LAN}}" != "" & "${LAN}" != ""]?Background(${PLAYDIR}/${MSGTOREENTER${LAN}}))
 same => n,ExecIf($["${MSGTOREENTER}" != ""]?Background(${PLAYDIR}/${MSGTOREENTER}))
 same => n,WaitExten(4)
 same => n,GoTo(s,request)

exten => 1,1,NoOp(Got confirmation)
 same => n,SendText(CRIVRcallbackNr:${CALLBACKNUMBER})
 same => n,Set(IVRCHOICES=CRIVRcallbackNr:${CALLBACKNUMBER})
 same => n,GotoIf($["${ASKDATE}" = "1"]?nixxis-callback-v2-AskASAP,s,1)
 same => n,GotoIf($["${ASKTIME}" = "1"]?nixxis-callback-v2-AskASAP,s,1)
 same => n,Goto(nixxis-callback-v2-PlaceCallbackRequest,s,1)

exten => 2,1,NoOp(Got request to re-enter number)
 same => n,GoTo(nixxis-callback-v2-NoCallerID,s,request)


exten => i,1,NoOp(Invalid response)
 same => n,ExecIf($["${MSGNOTVALID${LAN}}" != "" & "${LAN}" != ""]?Background(${PLAYDIR}/${MSGNOTVALID${LAN}}))
 same => n,ExecIf($["${MSGNOTVALID}" != ""]?Background(${PLAYDIR}/${MSGNOTVALID}))
 same => n,GoTo(nixxis-callback-v2-NoCallerID,s,request)


[nixxis-callback-v2-NoCallerID]

exten => s,1(request),NoOp(No CallerID detected, request number)
 same => n,ExecIf($["${MSGREQUESTNR${LAN}}" != "" & "${LAN}" != ""]?Background(${PLAYDIR}/${MSGREQUESTNR${LAN}}))
 same => n,ExecIf($["${MSGREQUESTNR}" != ""]?Background(${PLAYDIR}/${MSGREQUESTNR}))
 same => n,WaitExten(4)
 same => n,GoTo(s,request)

exten => _X.,1,NoOp(Got: ${EXTEN})
 same => n,Set(CALLBACKNUMBER=${FILTER(0123456789,${EXTEN})})
 same => n,GotoIf($["${VALIDATOR}" = ""]?nixxis-callback-v2-CallerID,s,1)
 same => n,Set(VALIDATED=${REGEX("${VALIDATOR}" ${CALLBACKNUMBER})}) 
 same => n,GotoIf($["${VALIDATED}" = "0"]?nixxis-callback-v2-NoCallerID,i,1)
 same => n,GoTo(nixxis-callback-v2-CallerID,s,1)


exten => i,1,NoOp(Invalid response)
 same => n,ExecIf($["${MSGNOTVALID${LAN}}" != "" & "${LAN}" != ""]?Background(${PLAYDIR}/${MSGNOTVALID${LAN}}))
 same => n,ExecIf($["${MSGNOTVALID}" != ""]?Background(${PLAYDIR}/${MSGNOTVALID}))
 same => n,GoTo(nixxis-callback-v2-NoCallerID,s,request)

[nixxis-callback-v2-AskASAP]

exten => s,1(request),NoOp(Asking ASAP or Later)
 same => n,ExecIf($["${MSGCALLASAP${LAN}}" != "" & "${LAN}" != ""]?Background(${PLAYDIR}/${MSGCALLASAP${LAN}}))
 same => n,ExecIf($["${MSGCALLASAP}" != ""]?Background(${PLAYDIR}/${MSGCALLASAP}))
 same => n,ExecIf($["${MSGCALLLATER${LAN}}" != "" & "${LAN}" != ""]?Background(${PLAYDIR}/${MSGCALLLATER${LAN}}))
 same => n,ExecIf($["${MSGCALLLATER}" != ""]?Background(${PLAYDIR}/${MSGCALLLATER}))
 same => n,WaitExten(4)
 same => n,GoTo(s,request)

exten => 1,1,NoOp(CallBack ASAP)
 same => n,Goto(nixxis-callback-v2-PlaceCallbackRequest,s,1)

exten => 2,1,NoOp(CallBack programming requested)
 same => n,GotoIf($["${ASKDATE}" = "1"]?nixxis-callback-v2-AskDate,s,1)
 same => n,GotoIf($["${ASKTIME}" = "1"]?nixxis-callback-v2-AskTime,s,1)
 same => n,GoTo(nixxis-callback-v2-PlaceCallbackRequest,s,1)

exten => i,1,NoOp(Invalid response)
 same => n,ExecIf($["${MSGNOTVALID${LAN}}" != "" & "${LAN}" != ""]?Background(${PLAYDIR}/${MSGNOTVALID${LAN}}))
 same => n,ExecIf($["${MSGNOTVALID}" != ""]?Background(${PLAYDIR}/${MSGNOTVALID}))
 same => n,GoTo(nixxis-callback-v2-CheckASAP,s,request)


[nixxis-callback-v2-AskDate]

exten => s,1(request),NoOp(Requesting Date)
 same => n,ExecIf($["${MSGASKDATE${LAN}}" != "" & "${LAN}" != ""]?Background(${PLAYDIR}/${MSGASKDATE${LAN}}))
 same => n,ExecIf($["${MSGASKDATE}" != ""]?Background(${PLAYDIR}/${MSGASKDATE}))
 same => n,WaitExten(4)
 same => n,GoTo(s,request)

exten => _XXXX,1,NoOp(Got: ${EXTEN})
 same => n,GoSub(CheckDate(${EXTEN}))
 same => n,GotoIf($[${VALIDDATE} < 0]?nixxis-callback-v2-AskDate,i,1)
 same => n,Set(CALLBACKDATE=${VALIDDATE})
 same => n,SendText(CRIVRcallbackDate:${CALLBACKDATE})
 same => n,Set(IVRCHOICES=${IVRCHOICES}\,CRIVRcallbackDate:${CALLBACKDATE})
 same => n,GotoIf($["${ASKTIME}" = "1"]?nixxis-callback-v2-AskTime,s,1)
 same => n,Goto(nixxis-callback-v2-PlaceCallbackRequest,s,1)

 same => n(CheckDate),NoOp(Checking date)
 same => n,Set(MONTHLENGTHHELPER=312931303130313130313031)
 same => n,Set(CURRENTYEAR=${STRFTIME(${EPOCH},,%Y)})
 same => n,Set(CURRENTMONTHDATE=${STRFTIME(${EPOCH},,%m%d)})
 same => n,Set(REQUESTEDMONTHDATE=${ARG1:2:2}${ARG1:0:2})
 same => n,ExecIf($[${REQUESTEDMONTHDATE} >= ${CURRENTMONTHDATE}]?Set(REQUESTEDYEAR=${CURRENTYEAR}):Set(REQUESTEDYEAR=$[${CURRENTYEAR}+1]))
 same => n,Set(VALIDDATE=${REQUESTEDYEAR}${REQUESTEDMONTHDATE})
 same => n,ExecIf($[${ARG1:2:2} > 12 ]?Set(VALIDDATE=-1)) 
  same => n,Set(INDEX=$[(${ARG1:2:2}-1)*2])
 same => n,NoOp(${MONTHLENGTHHELPER:${INDEX}:2})
 same => n,ExecIf($[${VALIDDATE} >= 0 & ${MONTHLENGTHHELPER:${INDEX}:2} < ${ARG1:0:2} ]?Set(VALIDDATE=-1)) 
 same => n,ExecIf($[${VALIDDATE} >= 0 & ${ARG1:2:2} = 2 & ${ARG1:0:2} = 29 & ${CURRENTYEAR}%4 = 0 & ${CURRENTYEAR}%100 != 0 ]?Set(VALIDDATE=-1))
 same => n,ExecIf($[${VALIDDATE} >= 0 & ${ARG1:2:2} = 2 & ${ARG1:0:2} = 29 & ${CURRENTYEAR}%400 = 0 ]?Set(VALIDDATE=-1))
 same => n,Return



exten => i,1,NoOp(Invalid response)
 same => n,ExecIf($["${MSGNOTVALID${LAN}}" != "" & "${LAN}" != ""]?Background(${PLAYDIR}/${MSGNOTVALID${LAN}}))
 same => n,ExecIf($["${MSGNOTVALID}" != ""]?Background(${PLAYDIR}/${MSGNOTVALID}))
 same => n,GoTo(nixxis-callback-v2-AskDate,s,request)



[nixxis-callback-v2-AskTime]

exten => s,1(request),NoOp(Requesting Time)
 same => n,ExecIf($["${MSGASKTIME${LAN}}" != "" & "${LAN}" != ""]?Background(${PLAYDIR}/${MSGASKTIME${LAN}}))
 same => n,ExecIf($["${MSGASKTIME}" != ""]?Background(${PLAYDIR}/${MSGASKTIME}))
 same => n,WaitExten(4)
 same => n,GoTo(s,request)

exten => _XXXX,1,NoOp(Got: ${EXTEN})
 same => n,GoSub(CheckTime(${EXTEN}))
 same => n,GotoIf($[${VALIDHOUR} < 0]?nixxis-callback-v2-AskTime,i,1)
 same => n,Set(CALLBACKTIME=${VALIDHOUR})
 same => n,SendText(CRIVRCBTIME:${CALLBACKTIME})
 same => n,Set(IVRCHOICES=${IVRCHOICES}\,CRIVRcallbackTime:${CALLBACKTIME})
 same => n,Goto(nixxis-callback-v2-PlaceCallbackRequest,s,1)

 same => n(CheckTime),NoOp(Checking time)
 same => n,Set(VALIDHOUR=${ARG1})
 same => n,ExecIf($[${ARG1:0:2} > 23 ]?Set(VALIDHOUR=-1))
 same => n,ExecIf($[${VALIDHOUR} >= 0 & ${ARG1:2:2} > 59 ]?Set(VALIDHOUR=-1))
 same => n,Return


exten => i,1,NoOp(Invalid response)
 same => n,ExecIf($["${MSGNOTVALID${LAN}}" != "" & "${LAN}" != ""]?Background(${PLAYDIR}/${MSGNOTVALID${LAN}}))
 same => n,ExecIf($["${MSGNOTVALID}" != ""]?Background(${PLAYDIR}/${MSGNOTVALID}))
 same => n,GoTo(nixxis-callback-v2-AskTime,s,request)



[nixxis-callback-v2-PlaceCallbackRequest]

exten => s,1,NoOp(Placed callback request to appserver)
 same => n,SendText(CRIVRpositive:1)
 same => n,Set(IVRCHOICES=${IVRCHOICES}\,CRIVRpositive:1)
 same => n,GoTo(nixxis-callback-v2-RequestOK,s,1)


[nixxis-callback-v2-RequestOK]

exten => s,1,NoOp(Request Succeeded)
 same => n,ExecIf($["${ENQUEUE}" = "1"]?Set(IVRCHOICES=${IVRCHOICES}\,CRIVRenqueue))
 same => n,SendText(${IVRCHOICES}\,CRIVRletdrop\,CRIVRend)

 same => n,ExecIf($["${MSGTHANKYOU${LAN}}" != "" & "${LAN}" != ""]?Background(${PLAYDIR}/${MSGTHANKYOU${LAN}}))
 same => n,ExecIf($["${MSGTHANKYOU}" != ""]?Background(${PLAYDIR}/${MSGTHANKYOU}))
 same => n,Goto(nixxis-callback-v2-end,s,1)

 


[nixxis-callback-v2-RequestFailed]

exten => s,1,NoOp(Request failed)
 same => n,ExecIf($["${MSGSORRY${LAN}}" != "" & "${LAN}" != ""]?Background(${PLAYDIR}/${MSGSORRY${LAN}}))
 same => n,ExecIf($["${MSGSORRY}" != ""]?Background(${PLAYDIR}/${MSGSORRY}))
 same => n,Goto(nixxis-callback-v2-end,s,1)
 

[nixxis-callback-v2-end]

exten => s,1,NoOp(Done with IVR sending END)
;same => n,ExecIf($["${ENQUEUE}" = "1"]?Set(IVRCHOICES=${IVRCHOICES}\,CRIVRenqueue))

same => n,Set(i=1)
same => n,While($[${i} < 3])

same => n,SendText(${IVRCHOICES}\,CRIVRdrop\,CRIVRend)
same => n,Set(i=$[${i} + 1])
same => n,Wait(5)
same => n,EndWhile
same => n,HangUp()

