
; -------------------------------------------------------------------------------------
; -------------------------------------------------------------------------------------
; ----------------------------< AHEEVA DIALPLAN SECTION >------------------------------
; -------------------------------------------------------------------------------------
; -------------------------------------------------------------------------------------


[agentbarge]
exten => h,1,Hangup

exten => _AB.,1,Answer
exten => _AB.,n,AgentBarge(${EXTEN:2})
exten => _AB.,n,Hangup

exten => ${AH_BARGE_EXTEN},1,Ringing
exten => ${AH_BARGE_EXTEN},n,Answer
exten => ${AH_BARGE_EXTEN},n,AgentBarge()
exten => ${AH_BARGE_EXTEN},n,Hangup

exten => 222,1,MixMonitor(blah.wav)
exten => 222,n,Set(AUDIOHOOK_INHERIT(MixMonitor)=yes)
exten => 222,n,Dial(SIP/222)

exten => 224,1,MixMonitor(blah.wav)
exten => 224,n,Set(AUDIOHOOK_INHERIT(MixMonitor)=yes)
exten => 224,n,Dial(SIP/224)

exten => 225,1,MixMonitor(blah.wav)
exten => 225,n,Set(AUDIOHOOK_INHERIT(MixMonitor)=yes)
exten => 225,n,Dial(SIP/225)


exten => 226,1,MixMonitor(blah.wav)
exten => 226,n,Set(AUDIOHOOK_INHERIT(MixMonitor)=yes)
exten => 226,n,Dial(SIP/226)





[agents]





exten => 4444,1,Goto(aheeva_sbr,s^DNID=${EXTEN}^RDNIS=${EXTEN},1)
exten => 1111,1,Answer()
exten => 1111,2,Playback(beep)
exten => 1111,3,Hangup()

; Requests coming from the StarPhone
exten => _AH.,1,AgentLogin(${EXTEN:2},s)

; Agent calls using a Phone



exten => 222,1,MixMonitor(sip_${EXTEN}_${UNIQUEID}.wav|av(0}V(0))
exten => 222,n,Dial(SIP/${EXTEN})
exten => 222,n,Hangup();
exten => 224,1,MixMonitor(sip_${EXTEN}_${UNIQUEID}.wav|av(0}V(0))
exten => 224,n,Dial(SIP/${EXTEN})
exten => 224,n,Hangup();
exten => 225,1,MixMonitor(sip_${EXTEN}_${UNIQUEID}.wav|av(0}V(0))
exten => 225,n,Dial(SIP/${EXTEN})
exten => 225,n,Hangup();
exten => 226,1,MixMonitor(sip_${EXTEN}_${UNIQUEID}.wav|av(0}V(0))
exten => 226,n,Dial(SIP/${EXTEN})
exten => 226,n,Hangup();








exten => _0612.,1,Set(CALLERID(number)=520460587)
exten => _0612.,n,Dial(DAHDI/g2/${EXTEN},30)
exten => _0612.,n,Hangup();

exten => _0614.,1,Set(CALLERID(number)=520460587)
exten => _0614.,n,Dial(DAHDI/g2/${EXTEN},30)
exten => _0614.,n,Hangup();


exten => _0617.,1,Set(CALLERID(number)=520460587)
exten => _0617.,n,Dial(DAHDI/g2/${EXTEN},30)
exten => _0617.,n,Hangup();


exten => _0619.,1,Set(CALLERID(number)=520460587)
exten => _0619.,n,Dial(DAHDI/g2/${EXTEN},30)
exten => _0619.,n,Hangup();

exten => _0620.,1,Set(CALLERID(number)=520460587)
exten => _0620.,n,Dial(DAHDI/g2/${EXTEN},30)
exten => _0620.,n,Hangup();


exten => _0621.,1,Set(CALLERID(number)=520460587)
exten => _0621.,n,Dial(DAHDI/g2/${EXTEN},30)
exten => _0621.,n,Hangup();

exten => _0644.,1,Set(CALLERID(number)=520460587)
exten => _0644.,n,Dial(DAHDI/g2/${EXTEN},30)
exten => _0644.,n,Hangup();


exten => _0649.,1,Set(CALLERID(number)=520460587)
exten => _0649.,n,Dial(DAHDI/g2/${EXTEN},30)
exten => _0649.,n,Hangup();

exten => _0656.,1,Set(CALLERID(number)=520460587)
exten => _0656.,n,Dial(DAHDI/g2/${EXTEN},30)
exten => _0656.,n,Hangup();

exten => _0657.,1,Set(CALLERID(number)=520460587)
exten => _0657.,n,Dial(DAHDI/g2/${EXTEN},30)
exten => _0657.,n,Hangup();


exten => _0660.,1,Set(CALLERID(number)=520460587)
exten => _0660.,n,Dial(DAHDI/g2/${EXTEN},30)
exten => _0660.,n,Hangup();



exten => _06.,1,Set(CALLERID(number)=520460587)
exten => _06.,n,Dial(DAHDI/g1/${EXTEN},30)
exten => _06.,n,Hangup();

;exten => _05.,1,Set(CALLERID(number)=520460587)
exten => _05.,1,Dial(DAHDI/g1/${EXTEN},30)
exten => _05.,n,Hangup();

; Requests coming from the StarPhone
exten => _AH.,1,AgentLogin(${EXTEN:2},s)

exten => _AGTCALL.,1,NoOp(Consult Call for ${EXTEN:7})
exten => _AGTCALL.,n,AhConsultCallDistribute(${EXTEN:7}|30|1)
exten => _AGTCALL.,n,Hangup

exten => _MCALL.,1,Set(AH_TEMP=${EXTEN:5})
exten => _MCALL.,2,Noop([${AH_TEMP}])
exten => _MCALL.,3,Set(AH_EXTEN=${CUT(AH_TEMP,^,1)})
exten => _MCALL.,4,Set(AHEEVA_TRACKNUM=${CUT(AH_TEMP,^,2)})
exten => _MCALL.,5,Goto(agents,${AH_EXTEN},1)

exten => h,1,GotoIf($["${DIALSTATUS}" = "ANSWER"]?3:2)
exten => h,n,AHEventsProxy(MSG_TYPE_TERMINATE_CALL:${AHEEVA_TRACKNUM}:${AH_PHONE_NUMBER}:${AH_RECORDID}:${EPOCH})
exten => h,n,Hangup

;This is when an agent dials a wrong number
include => agent_wrong_number
[agent_wrong_number]

exten => _X.,1,Answer
exten => _X.,n,Playback(you-dialed-wrong-number)
exten => _X.,n,Hangup

[aheeva]
include => ccsr
include => agentbarge

;this include should always be at the bottom
include => agents


;to start a SBR do something like this
;exten => 88000,1,Goto(aheeva_sbr,s^DNID=${EXTEN}^RDNIS=${EXTEN},1)

;Aheeva Skill based Routing
[aheeva_sbr]

; To allow to use in outbound calls
; Setting phone number and record id to a NoOp terminate
exten => s,1,Set(AH_PHONE_NUMBER=0)
exten => s,n,Set(AH_RECORDID=0)
exten => s,n,Set(CALLERID(name)=${CUT(CALLERID(name)|&|1)}${CUT(CALLERID(name)|&|2)})
exten => s,n,AHEventsProxy(MSG_TYPE_SBR_ROUTING_REQUEST)
exten => s,n,NoOp(aheeva_sbr-${SBRSTATUS});
exten => s,n,Goto(aheeva_sbr,aheeva_sbr-${SBRSTATUS},1)

exten => _s.,1,Set(AH_PHONE_NUMBER=0)
exten => _s.,n,Set(AH_RECORDID=0)
exten => _s.,n,Set(CALLERID(name)=${CUT(CALLERID(name)|&|1)}${CUT(CALLERID(name)|&|2)})
exten => _s.,n,AHEventsProxy(MSG_TYPE_SBR_ROUTING_REQUEST${EXTEN:1})
exten => _s.,n,Goto(aheeva_sbr,aheeva_sbr-${SBRSTATUS},1)

exten => _BR.,1,Wait(0.5)
exten => _BR.,2,Set(LIST_NAME=${CUT(AH_URL,*,2)})
exten => _BR.,3,GotoIf($["${LIST_NAME}" = ""]?4:6)
exten => _BR.,4,AHEventsProxy(MSG_TYPE_SBR_ROUTING_REQUEST^broadcast=true^phone_num=${EXTEN:2}^amdstatus=${AMDSTATUS})
exten => _BR.,5,Goto(7)
exten => _BR.,6,AHEventsProxy(MSG_TYPE_SBR_ROUTING_REQUEST^broadcast=true^phone_num=${EXTEN:2}^${LIST_NAME}^amdstatus=${AMDSTATUS})
exten => _BR.,7,Goto(aheeva_sbr,aheeva_sbr-${SBRSTATUS},1)

exten => aheeva_sbr-NORMAL,1,Hangup
exten => aheeva_sbr-NO_SCRIPT_DEFINED,1,Hangup
exten => aheeva_sbr-NO_ONE_AVAILABLE,1,Hangup

exten => aheeva_sbr-CCS_NOT_AVAILABLE,1,Goto(aheeva_ccs_not_available,s,1)
exten => aheeva_sbr-CCS_NOT_AVAILABLE,n,Hangup

exten => h,1,GotoIf($["${AH_AGENT_CHANNEL}" = "1"]?4:2)
exten => h,2,GotoIf($["${AH_MASTER_CHANNEL}" = "1"]?4:3)
exten => h,3,DumpChan( );
exten => h,4,AHEventsProxy(MSG_TYPE_TERMINATE_CALL:${AHEEVA_TRACKNUM}:${AH_PHONE_NUMBER}:${AH_RECORDID}:${EPOCH})
exten => h,5,Hangup

[ahPhone_init]
exten => _OUTSOFT.,1,Dial(IAX2/${EXTEN:7},30,tT)
exten => _OUTSOFT.,n,Hangup

exten => _OUTHARD.,1,Dial(SIP/${EXTEN:7},30,tT)
exten => _OUTHARD.,n,Hangup


exten => 222,1,MixMonitor(sip_${EXTEN}_${UNIQUEID}.wav|av(0}V(0))
exten => 222,n,Dial(SIP/${EXTEN})
exten => 222,n,Hangup();
exten => 224,1,MixMonitor(sip_${EXTEN}_${UNIQUEID}.wav|av(0}V(0))
exten => 224,n,Dial(SIP/${EXTEN})
exten => 224,n,Hangup();
exten => 225,1,MixMonitor(sip_${EXTEN}_${UNIQUEID}.wav|av(0}V(0))
exten => 225,n,Dial(SIP/${EXTEN})
exten => 225,n,Hangup();
exten => 226,1,MixMonitor(sip_${EXTEN}_${UNIQUEID}.wav|av(0}V(0))
exten => 226,n,Dial(SIP/${EXTEN})
exten => 226,n,Hangup();




[ccssbr]
exten => _CCSSBR_.,1,NoOp([${EXTEN}])
exten => _CCSSBR_.,n,AHEventsProxy(MSG_TYPE_SBR_DISTRIBUTE_NOW:${EXTEN:7})
exten => _CCSSBR_.,n,Hangup

; Outbound dial context
[aheeva_ccs]
; If we are dialing out through another Asterisk, sometimes when a call is not
; answered the DIALSTATUS gets set to CANCEL and Asterisk just aborts the DIAL
; and jumps directly to the h extension without continuing processing in the
; dialplan after the Dial application, which means that we do not send the
; DIALSTATUS to the CCS server after the dial. This is why we need to capture
; here in the h extension and send a NOANSWER.

exten => h,1,NoOp(ds= ${DIALSTATUS});
exten => h,2,GotoIf($["${DIALSTATUS}" = "ANSWER"]?6:3)
exten => h,3,GotoIf($["${DIALSTATUS}" = "CANCEL"]?4:5)
exten => h,4,AHEventsProxy(NOANSWER:${AHEEVA_TRACKNUM}:${AH_PHONE_NUMBER}:${AH_RECORDID}:${EPOCH})
exten => h,5,AHEventsProxy(MSG_TYPE_TERMINATE_CALL:${AHEEVA_TRACKNUM}:${AH_PHONE_NUMBER}:${AH_RECORDID}:${EPOCH}:${AH_AGENTID})
exten => h,6,Hangup

exten => _OUT.,1,NoOp(AHEEVA1 Variables: AH_PHONE_NUMBER=[${AH_PHONE_NUMBER}] AH_QUEUE=[${AH_QUEUE}] AH_URL=[${AH_URL}] AH_RECORDID=[${AH_RECORDID}] AH_AMD_REQUIRED=[${AH_AMD_REQUIRED}] AH_CALLERID=[${AH_CALLERID}] AHEEVA_TRACKNUM=[${AHEEVA_TRACKNUM}] AH_LEAVE_MESSAGE=[${AH_LEAVE_MESSAGE}])
exten => _OUT.,2,SetCallerId(${AH_CALLERID})

exten => _OUT.,n,Set(match=${REGEX("^0678922646" ${AH_PHONE_NUMBER})})
exten => _OUT.,n,GotoIf($["${match}" = "1"]?rien)

exten => _OUT.,n,Set(match=${REGEX("^0661728390" ${AH_PHONE_NUMBER})})
exten => _OUT.,n,GotoIf($["${match}" = "1"]?rien)

exten => _OUT.,n,Set(match=${REGEX("^061[2479]" ${AH_PHONE_NUMBER})})
exten => _OUT.,n,GotoIf($["${match}" = "1"]?meditel)

exten => _OUT.,n,Set(match=${REGEX("^062[01]" ${AH_PHONE_NUMBER})})
exten => _OUT.,n,GotoIf($["${match}" = "1"]?meditel)

exten => _OUT.,n,Set(match=${REGEX("^064[459]" ${AH_PHONE_NUMBER})})
exten => _OUT.,n,GotoIf($["${match}" = "1"]?meditel)

exten => _OUT.,n,Set(match=${REGEX("^0656" ${AH_PHONE_NUMBER})})
exten => _OUT.,n,GotoIf($["${match}" = "1"]?meditel)

exten => _OUT.,n,Set(match=${REGEX("^0657" ${AH_PHONE_NUMBER})})
exten => _OUT.,n,GotoIf($["${match}" = "1"]?meditel)

exten => _OUT.,n,Set(match=${REGEX("^066[03459]" ${AH_PHONE_NUMBER})})
exten => _OUT.,n,GotoIf($["${match}" = "1"]?meditel)

exten => _OUT.,n,Set(match=${REGEX("^067[459]" ${AH_PHONE_NUMBER})})
exten => _OUT.,n,GotoIf($["${match}" = "1" ]?meditel:maroc)

exten => _OUT.,n(rien),Dial(${TRUNK}/101010${AH_PHONE_NUMBER},30)
exten => _OUT.,n,Goto(d)
exten => _OUT.,n(maroc),Dial(${TRUNK}/${AH_PHONE_NUMBER},30)
exten => _OUT.,n,Goto(d)
exten => _OUT.,n(meditel),Dial(${TRUNKMed}/${AH_PHONE_NUMBER},30)
exten => _OUT.,n(d),NoOp(Dial Status=[${DIALSTATUS}] Hangup Cause=[${HANGUPCAUSE}])
exten => _OUT.,n,GotoIf($["${DIALSTATUS}" = "CHANUNAVAIL" & "${HANGUPCAUSE}" = "16"]?a:b)
exten => _OUT.,n(a),AHEventsProxy(MSG_TYPE_CALL_SIT:${AHEEVA_TRACKNUM}:${AH_PHONE_NUMBER}:${AH_RECORDID}:${EPOCH})
exten => _OUT.,n,Goto(c)
exten => _OUT.,n(b),AHEventsProxy(${DIALSTATUS}:${AHEEVA_TRACKNUM}:${AH_PHONE_NUMBER}:${AH_RECORDID}:${EPOCH})
exten => _OUT.,n(c),NoOp()





#include "aheeva_custom_dialplan.conf"