Showing posts with label dial plan. Show all posts
Showing posts with label dial plan. Show all posts

Wednesday, November 2, 2011

Call-Back Service for IP-Telephony users Part-II


Continuing with the Auto-Call-Back service. In the previous part of this post we collected Callers numbers in a Call Back Queue for each IP-PBX user. Once we've List saved for each user, next thing would be to enable the service users to call to the service listen to their Callers Numbers and if Service user wants to call-back then dial it else move onto next caller in the list.

See the Flow-diagram on right for the above.

What we'll learn:

1- Dial-plan function DB_EXIST
2- Dial-plan Application SayDigits, and Read.
3- Misc: See some more scenarios of using previously used functions and applications.

Call-Back Service Dial-Plan code:


exten => 747,1,NOOP(PBX User ${CALLERID(num)} Reading Call-Back Data)
same => n,Answer()
same => n,GOTOIF(${DB_EXISTS(call-back/${CALLERID(num)})}?:hangup)
same => n,SET(CBQ=${DB(call-back/${CALLERID(num)})})
same => n,Playback(vm-youhave)
same => n,SayDigits(${FIELDQTY(CBQ,-)})
same => n,Playback(vm-messages)
same => n,SET(i=${FIELDQTY(CBQ,-)})
same => n,WHILE($["${i}" >= "1"])
same => n,Playback(vm-from-phonenumber)
same => n,SayDigits(${CUT(CBQ,,${i})})
same => n,Read(INPUT,vm-dialout,1)
same => n,SET(DIALME=${CUT(CBQ,,${i})})
same => n,GOTOIF($["${INPUT}" == "1"]?yes)
same => n,EXECIF($["${SaveCBQ}" == ""]?SET(SaveCBQ=${CUT(CBQ,,${i})}):SET(SaveCBQ=${CUT(CBQ,,${i})}-${SaveCBQ}))
same => n,SET(i=$[${i} - 1])
same => n,EndWhile()
same => n,hangup()
same => n(yes),SET(ARRAY(CBQ,j)=${CUT(CBQ,,1-$[${i} - 1])}-${SaveCBQ},${i})
same => n,NOOP([CBQ:1]= ${CBQ:1} :: [CBQ:0:-1]=${CBQ:0:-1} :: [CBQ:-1]=${CBQ:-1})
same => n,EXECIF($["${CBQ:-1}" == "-"]?SET(CBQ=${CBQ:0:-1}))
same => n,EXECIF($["${CBQ:0:1}" == "-"]?SET(CBQ=${CBQ:1}))
same => n,SET(DB(call-back/${CALLERID(num)})=${CBQ})
same => n,Dial(SIP/${DIALME})
same => n(hangup),Hangup()

So, once UserK calls into CBS number 747, we'll follow the flow-chart and try establish call between the userK and the callers.


All Dial-Plan Code:

[default]
exten => _X.,1,NOOP(Incoming Call from ${CALLERID(num)} to ${EXTEN})
same => n,Answer()
same => n,SET(DEST=${EXTEN})
same => n,Dial(SIP/${EXTEN},10)
same => n,Voicemail(${EXTEN}@default,d(register-callback))
same => n,Hangup()


exten => 747,1,NOOP(PBX User ${CALLERID(num)} Reading Call-Back Data)
same => n,Answer()
same => n,GOTOIF(${DB_EXISTS(call-back/${CALLERID(num)})}?:hangup)
same => n,SET(CBQ=${DB(call-back/${CALLERID(num)})})
same => n,Playback(vm-youhave)
same => n,SayDigits(${FIELDQTY(CBQ,-)})
same => n,Playback(vm-messages)
same => n,SET(i=${FIELDQTY(CBQ,-)})
same => n,WHILE($["${i}" >= "1"])
same => n,Playback(vm-from-phonenumber)
same => n,SayDigits(${CUT(CBQ,,${i})})
same => n,Read(INPUT,vm-dialout,1)
same => n,SET(DIALME=${CUT(CBQ,,${i})})
same => n,GOTOIF($["${INPUT}" == "1"]?yes)
same => n,EXECIF($["${SaveCBQ}" == ""]?SET(SaveCBQ=${CUT(CBQ,,${i})}):SET(SaveCBQ=${CUT(CBQ,,${i})}-${SaveCBQ}))
same => n,SET(i=$[${i} - 1])
same => n,EndWhile()
same => n,hangup()
same => n(yes),SET(ARRAY(CBQ,j)=${CUT(CBQ,,1-$[${i} - 1])}-${SaveCBQ},${i})
same => n,NOOP([CBQ:1]= ${CBQ:1} :: [CBQ:0:-1]=${CBQ:0:-1} :: [CBQ:-1]=${CBQ:-1})
same => n,EXECIF($["${CBQ:-1}" == "-"]?SET(CBQ=${CBQ:0:-1}))
same => n,EXECIF($["${CBQ:0:1}" == "-"]?SET(CBQ=${CBQ:1}))
same => n,SET(DB(call-back/${CALLERID(num)})=${CBQ})
same => n,Dial(SIP/${DIALME})
same => n(hangup),Hangup()


[register-callback]
exten => s,1,SET(CALLERID(num)=${RAND(100,110)})
same => n,NOOP(Caller ${CALLERID(num)} Registering Call-Back for User ${DEST})
same => n,NOOP(do some call-back tricks here)
same => n,SET(CBQ=${DB(call-back/${DEST})})
same => n,GOTOIF($["${CBQ}" == ""]?first:sec)
same => n(first),Set(DB(call-back/${DEST})=${CALLERID(num)})
same => n,GOTO(jump)
same => n(sec),Macro(duplicate-check,${CALLERID(num)})
same => n,GOTOIF($["${RESULT}" == "1"]?jump)
same => n,Set(DB(call-back/${DEST})=${CALLERID(num)}-${CBQ})
same => n(jump),NOOP(Playback(thanks-willb-called-shortly))
same => n,Hangup()

exten => i,1,GOTO(s,1)

[macro-duplicate-check]
exten => s,1,NOOP(${CBQ} Checked for Duplicate ${ARG1})
exten => s,n,SET(COUNT=${FIELDQTY(CBQ,-)})
exten => s,n,SET(ARRAY(i,RESULT)=1,0)
exten => s,n,WHILE($["${i}" <= "${COUNT}"])
exten => s,n,NOOP(${CUT(CBQ,,${i})} == ${ARG1})
exten => s,n,EXECIF($["${CUT(CBQ,,${i})}" == "${ARG1}"]?GOTO(found):SET(i=$[${i} + 1]))
exten => s,n,Endwhile()
exten => s,n,MacroExit()
exten => s,n(found),SET(RESULT=1)
exten => s,n,MacroExit()


Monday, October 24, 2011

Asterisk Dial-plan exercise [Speed-Dial]

Creating a Speed-Dial Functionality :
This was a test exercise I gave in one my teaching classes here last year. The idea is to develop a Speed-Dial functionality context for PBX users.

What we'll learn:
1- Connect to MySQL DB from Dial-plan.
2- Use of Asterisk Dial-plan functions CUT, FIELDQTY.
3- Use of GOTOIF Dial-plan application.
4- Use of restricted extension pattern matching by use of [ ].

Problem Statement:
The users should be able to edit their own speed dial-lists via web-interface. And whenever a SIP user dials single digit extension ranging from 0-9 should be able to speed-dial to their contacts saved in DB.

(The Php web-interface for editing contacts isn't included here, it shouldn't be difficult to make a simple page for this.)

Solution Algorithm:


Final Dial-Plan Code:
Corresponding Dial-plan for the above Algorithm is as follows.A few exceptions were handled where if no user speed-dial lists exists or no user data existed.

[Speed-Dial]
exten => _[0-9],1,NOOP(Starting Speed-Dial functionality)
same => n,MYSQL(Connect connid localhost asteriskuser ast_pass user_prefs)
same => n,MYSQL(Query resultid ${connid} SELECT contact_list FROM speed_dial_list WHERE user='${CALLERID(num)}')
same => n,MYSQL(Fetch fetchid ${resultid} UserList)
same => n,MYSQL(Clear ${resultid})
same => n,MYSQL(Disconnect ${connid})
same => n,GOTOIF($["${fetchid}" < "1"]?not_found:found)
same => n(not_found),Playback(No-User-List-Found)
same => n,Goto(h,1)
same => n(found),NOOP(User Speed-Dial List Found: ${UserList})
same => n,NOOP(Find if user Dialed Speed-Dial index exists)
same => n,SET(List_Length=${FIELDQTY(UserList,-)})
same => n,GOTOIF($["${EXTEN}" >= "${List_Length}"]?not_found:lengthok)
same => n(lengthok),NOOP(Now User Contact for Speed-Dialed exten ${EXTEN} Exists for Sure)
same => n,NOOP(Fetch the Speed-Dial-Contact from ${UserList})
same => n,SET(UserContact=${CUT(UserList,,${EXTEN})})
same => n,NOOP(Found user Contact ${UserContact} Corresponding to ${EXTEN})
same => n,DIAL(SIP/${UserContact})
same => n,Hangup()

Now Database turn.
Connect to MySQL and Create table "speed_dial_list" and push some dummy values corresponding to PBX users

#mysql
mysql>use asterisk
mysql>CREATE TABLE `speed_dial_list` (`user` varchar(80) NOT NULL default '',`contact_list` varchar(555) NOT NULL default '');
mysql>INSERT into speed_dial_list(user,contact_list) VALUES ('100','110-132-400-161-101-123'),('200','100-201-115-101'),('110','201-200-119'),('210','150-161-110-199-923452167923');
mysql>GRANT all on asterisk.* to 'asteriskuser'@'localhost' Identified by 'ast_pass';
mysql>flush privileges;

Sample output on Asterisk CLI when SIP User 100 dialled Speed-Dial Extension '3'



So, we made a Speed-Dial context here, include this context in your [default] dial-plan context and all one-digit extensions will be matched in Speed-Dial context and perform the functionality if anything matches else it'll be skipped.