Monday, April 2, 2018

Collect a pin number value in asterisk and write it to a file


**WHAT IT DOES**
This dialplan will ask the user to enter in a pin number, and then it will save it to a file.
Tons more you can do with this and tweaks of course, but this is an example of what you can do.

I'm using all the default recordings in asterisk, so they aren't perfect prompts, but good enough for you to test with.

**Installation**
1> Open up /etc/asterisk/extensions_custom.conf

2> Paste the code between the snips into this file.
===== snip ======
[macro-pincollection]
exten => s,1,Answer()

;we set a variable to prevent looping over and over in IVR, either by accident or malicious
exten => s,n,Set(Attempts=1)

;throw a entry in the asterisk log that system is requesting a PIN entry
exten => s,n(begin),NooP(&&&&&&& Collecting PIN info for ${CALLERID(all)} attempt ${Attempts} &&&&&)

exten => s,n,Playback(please-enter-your&pin_number)

;system listens for DTMF and records it in variable "pinnumber"
;the syntax at the end= 7=MAX DIGITS, 1=attempts if no data, 10=timeout.   The ",," value i'm not using in this code example)
exten => s,n,Read(pinnumber,then-press-pound,7,,1,10)

;system checks for blank value and asks again
exten => s,n,ExecIf($["${pinnumber}"=""]?goto(macro-pincollection,s,1))

;systems plays back the number they entered in to caller
exten => s,n,Playback(you-entered)
exten => s,n,SayDigits(${pinnumber})

;system asks them to press 1 to accept or 2 to retry
exten => s,n,Playback(if-this-is-correct-press)
exten => s,n,SayDigits(1)
exten => s,n,Playback(otherwise-press)
exten => s,n,SayDigits(2)

;system listens for the value entered
exten => s,n,Read(digi,,1)

;this section we test for invalid entries.  On their 4 try we hangup.  We dont want someone looping over and over again.
exten => s,n,Set(Attempts=${MATH(${Attempts}+1,i)})

;if they reach their 4th attempt, system plays some messages and hangsup.
;Change the "4" to a different value if you want to increase/decrease chances
;Change the "HANGUP to RETURN" if you want the system to go back to a different location as set in the CUSTOM DESTINATIONS section

exten => s,n,ExecIf($["${Attempts}"="4"]?playback(sorry-youre-having-problems))
exten => s,n,ExecIf($["${Attempts}"="4"]?playback(hangup-try-again))
exten => s,n,ExecIf($["${Attempts}"="4"]?HANGUP())

;if user presses 1 to confirm, system sends the clal on
exten => s,n,ExecIf($["${digi}"="1"]?goto(writevalue))

;if callers presses any other digit, system will re-ask them to enter in their number
exten => s,n,goto(macro-pincollection,s,begin)

;If they press "1" as being a valid entry, the entry is written into the file.
;system writes the pin collected to the file 'pincollected.txt'
;saved as "DDMMYYY-HH:MM:SS, CALLERid, UNIQUE NUMBER (to trace call in logs easier if needed), PINNUMBER
exten => s,n(writevalue),system(echo "${STRFTIME(${EPOCH},,%d%m%Y-%H:%M:%S)},${CALLERID(all)},${UNIQUEID},${pinnumber}" >> /tmp/pincollected.txt)

;uncomment next line to attach the pinnumber to the callerid.  IE "john smith" <5551234->
;exten => s,n,set(CALLERID(num)=${CALLERID(num)}-${pinnumber})

;uncomment next line to send the call to a voicemail box after your are done.  this is 900 unavailable message
;exten => s,n,Voicemail(900@default,u)




exten => s,n,Return
===== snip ======

3> Save these changes

4> Open up FreePBX

5> Go to ADMIN-CUSTOM DESTINATIONS

6> Create a new destination
TARGET = macro-pincollection,s,1
Description= Collect Pin and save
Notes= blank (you can put something in if you want)
Return = YES
DESTINATION= IVR – ivrtype
(the destination in this case is what you want to have happen when they enter their pin number.  In my example I wanted the code to continue on, but if you say “no” for RETURN then the system will hangup after the call.  if you want the call to hangup, just select return=NO

7> Save changes

8> Open up your IVR and put in an entry in it to go to this custom destination
example:
If they press “2” in your ivr, select “CUSTOM DESTINATION” then select “Collect Pin and save”

9> Save your changes, then submit.

10> TEST!

Dial the number that will trigger this.  In my example I all the IVR then pressed “2”
System will ask you to enter your pin, then will play it back to confirm and then if “YES” is selected, it will save it to /tmp/pincollected.txt **DDMMYY-HHMMSS, CLID info, unique ID, pin#entered**

02042018-11:51:38,John Smith <123-555-1165>,1522680683.98551,123
02042018-11:54:49,Jane Doe <123-555-4455>,1522680864.98560,1234

No comments:

Post a Comment