ダイアルプランでのifとループ
ダイヤルプランの設定手順
asteriskでは変数が使え、変数の値に応じて、特定の処理(if)や特定回数のループが行える。
以下のようにして、extensions.conf に記述して SIPクライアントから 12345 に向けてコールしてみる。
extensions.conf
[ivr-menu] exten => s,1,Set(CNT=1) exten => s,2,Ringing() exten => s,3,Wait(5) exten => s,4,Answer() exten => s,5,Wait(5) exten => s,6,Background(vm-enter-num-to-call) exten => s,7,WaitExten(10) exten => s,8,GotoIf($[${CNT} >= 3]?11:9) exten => s,9,Set(CNT=$[${CNT} + 1]) exten => s,10,Goto(s,6) exten => s,11,playback(tt-monkeysintro) exten => s,12,Hangup() exten => s,13,Congestion() [default] exten => 12345,1,Goto(ivr-menu,s,1)
下記はSIPクライアントから 12345 へコールした際のログ
ログはダイアルプランの内容が1行ずつ表示される。
1行でもダイアルプランに記述ミスがあると、それ以降の処理は行わないので注意。
下記の例では上記のダイアルプランの内容をすべて実行できていることがわかる。
[root@asterisk asterisk]# asterisk -vvvvvvvr asterisk*CLI> == Using SIP RTP CoS mark 5 -- Executing [12345@default:1] Goto("SIP/2001-00000018", "ivr-menu,s,1") in new stack -- Goto (ivr-menu,s,1) -- Executing [s@ivr-menu:1] Set("SIP/2001-00000018", "CNT=1") in new stack -- Executing [s@ivr-menu:2] Ringing("SIP/2001-00000018", "") in new stack -- Executing [s@ivr-menu:3] Wait("SIP/2001-00000018", "5") in new stack -- Executing [s@ivr-menu:4] Answer("SIP/2001-00000018", "") in new stack -- Executing [s@ivr-menu:5] Wait("SIP/2001-00000018", "5") in new stack -- Executing [s@ivr-menu:6] BackGround("SIP/2001-00000018", "vm-enter-num-to-call") in new stack -- <SIP/2001-00000018> Playing 'vm-enter-num-to-call.gsm' (language 'en') -- Executing [s@ivr-menu:7] WaitExten("SIP/2001-00000018", "10") in new stack -- Timeout on SIP/2001-00000018, continuing... -- Executing [s@ivr-menu:8] GotoIf("SIP/2001-00000018", "0?11:9") in new stack -- Goto (ivr-menu,s,9) -- Executing [s@ivr-menu:9] Set("SIP/2001-00000018", "CNT=2") in new stack -- Executing [s@ivr-menu:10] Goto("SIP/2001-00000018", "s,6") in new stack -- Goto (ivr-menu,s,6) -- Executing [s@ivr-menu:6] BackGround("SIP/2001-00000018", "vm-enter-num-to-call") in new stack -- <SIP/2001-00000018> Playing 'vm-enter-num-to-call.gsm' (language 'en') -- Executing [s@ivr-menu:7] WaitExten("SIP/2001-00000018", "10") in new stack -- Timeout on SIP/2001-00000018, continuing... -- Executing [s@ivr-menu:8] GotoIf("SIP/2001-00000018", "0?11:9") in new stack -- Goto (ivr-menu,s,9) -- Executing [s@ivr-menu:9] Set("SIP/2001-00000018", "CNT=3") in new stack -- Executing [s@ivr-menu:10] Goto("SIP/2001-00000018", "s,6") in new stack -- Goto (ivr-menu,s,6) -- Executing [s@ivr-menu:6] BackGround("SIP/2001-00000018", "vm-enter-num-to-call") in new stack -- <SIP/2001-00000018> Playing 'vm-enter-num-to-call.gsm' (language 'en') -- Executing [s@ivr-menu:7] WaitExten("SIP/2001-00000018", "10") in new stack -- Timeout on SIP/2001-00000018, continuing... -- Executing [s@ivr-menu:8] GotoIf("SIP/2001-00000018", "1?11:9") in new stack -- Goto (ivr-menu,s,11) -- Executing [s@ivr-menu:11] Playback("SIP/2001-00000018", "tt-monkeysintro") in new stack -- <SIP/2001-00000018> Playing 'tt-monkeysintro.gsm' (language 'en') -- Executing [s@ivr-menu:12] Hangup("SIP/2001-00000018", "") in new stack == Spawn extension (ivr-menu, s, 12) exited non-zero on 'SIP/2001-00000018
上記の extensions.conf 内の シーケンス番号を n にしてタグをつけて下記のようにしても実行できる。
すると n なので後々ダイアルプランを追加したり、削除したいという際にも変更が楽になる。
[ivr-menu] exten => s,1,Set(CNT=1) exten => s,n,Ringing() exten => s,n,Wait(7) exten => s,n,Answer() exten => s,n,Wait(5) exten => s,n(rtry),Background(vm-enter-num-to-call) exten => s,n,WaitExten(10) exten => s,n,GotoIf($[${CNT} >= 3]?s,exits:s,loops) exten => s,n(loops),Set(CNT=$[${CNT} + 1]) exten => s,n,Goto(s,rtry) exten => s,n(exits),playback(tt-monkeysintro) exten => s,n,Wait(10) exten => s,n,Hangup() exten => s,n,Congestion() [default] exten => 12345,1,Goto(ivr-menu,s,1)
上記のダイアルプランのログ
[root@asterisk asterisk]# asterisk -vvvvvvvr asterisk*CLI> == Using SIP RTP CoS mark 5 -- Executing [12345@default:1] Goto("SIP/2001-0000001b", "ivr-menu,s,1") in new stack -- Goto (ivr-menu,s,1) -- Executing [s@ivr-menu:1] Set("SIP/2001-0000001b", "CNT=1") in new stack -- Executing [s@ivr-menu:2] Ringing("SIP/2001-0000001b", "") in new stack -- Executing [s@ivr-menu:3] Wait("SIP/2001-0000001b", "7") in new stack -- Executing [s@ivr-menu:4] Answer("SIP/2001-0000001b", "") in new stack -- Executing [s@ivr-menu:5] Wait("SIP/2001-0000001b", "5") in new stack -- Executing [s@ivr-menu:6] BackGround("SIP/2001-0000001b", "vm-enter-num-to-call") in new stack -- <SIP/2001-0000001b> Playing 'vm-enter-num-to-call.gsm' (language 'en') -- Executing [s@ivr-menu:7] WaitExten("SIP/2001-0000001b", "10") in new stack -- Timeout on SIP/2001-0000001b, continuing... -- Executing [s@ivr-menu:8] GotoIf("SIP/2001-0000001b", "0?s,exits:s,loops") in new stack -- Goto (ivr-menu,s,9) -- Executing [s@ivr-menu:9] Set("SIP/2001-0000001b", "CNT=2") in new stack -- Executing [s@ivr-menu:10] Goto("SIP/2001-0000001b", "s,rtry") in new stack -- Goto (ivr-menu,s,6) -- Executing [s@ivr-menu:6] BackGround("SIP/2001-0000001b", "vm-enter-num-to-call") in new stack -- <SIP/2001-0000001b> Playing 'vm-enter-num-to-call.gsm' (language 'en') -- Executing [s@ivr-menu:7] WaitExten("SIP/2001-0000001b", "10") in new stack -- Timeout on SIP/2001-0000001b, continuing... -- Executing [s@ivr-menu:8] GotoIf("SIP/2001-0000001b", "0?s,exits:s,loops") in new stack -- Goto (ivr-menu,s,9) -- Executing [s@ivr-menu:9] Set("SIP/2001-0000001b", "CNT=3") in new stack -- Executing [s@ivr-menu:10] Goto("SIP/2001-0000001b", "s,rtry") in new stack -- Goto (ivr-menu,s,6) -- Executing [s@ivr-menu:6] BackGround("SIP/2001-0000001b", "vm-enter-num-to-call") in new stack -- <SIP/2001-0000001b> Playing 'vm-enter-num-to-call.gsm' (language 'en') -- Executing [s@ivr-menu:7] WaitExten("SIP/2001-0000001b", "10") in new stack -- Timeout on SIP/2001-0000001b, continuing... -- Executing [s@ivr-menu:8] GotoIf("SIP/2001-0000001b", "1?s,exits:s,loops") in new stack -- Goto (ivr-menu,s,11) -- Executing [s@ivr-menu:11] Playback("SIP/2001-0000001b", "tt-monkeysintro") in new stack -- <SIP/2001-0000001b> Playing 'tt-monkeysintro.gsm' (language 'en') -- Executing [s@ivr-menu:12] Wait("SIP/2001-0000001b", "10") in new stack -- Executing [s@ivr-menu:13] Hangup("SIP/2001-0000001b", "") in new stack == Spawn extension (ivr-menu, s, 13) exited non-zero on 'SIP/2001-0000001b'