Tuesday, February 7, 2023

Some general Asterisk dialpaln code

Save a Voice recording into a MySQL database

Here is an example of an Asterisk dialplan that asks for a voice recording and then saves it into a MySQL database:

[macro-record-and-save]

exten => s,1,Answer()
exten => s,2,Record(recording:/tmp/recording)
exten => s,3,System(mysql -u [username] -p[password] -h [host] [database] -e "INSERT INTO recordings (filename) VALUES ('/tmp/recording')")
exten => s,4,Hangup()

Replace the square bracket placeholders with the values for your setup
MySQL username, password, host, and database name. 



Example IVR dialplan for Asterisk:

[ivr]
exten => s,1,Answer
exten => s,2,Wait(1)
exten => s,3,Playback(welcome-to-our-company)
exten => s,4,Wait(1)
exten => s,5,Background(press-1-for-sales)
exten => s,6,Background(press-2-for-support)
exten => s,7,Background(press-3-for-billing)
exten => s,8,Background(or-stay-on-the-line)
exten => s,9,WaitExten(10)

exten => 1,1,Goto(sales,s,1)
exten => 2,1,Goto(support,s,1)
exten => 3,1,Goto(billing,s,1)

[sales]
exten => s,1,Answer
exten => s,2,Wait(1)
exten => s,3,Playback(sales-department)
exten => s,4,Wait(1)
exten => s,5,Background(sales-representative)
exten => s,6,WaitExten(10)

[support]
exten => s,1,Answer
exten => s,2,Wait(1)
exten => s,3,Playback(support-department)
exten => s,4,Wait(1)
exten => s,5,Background(support-representative)
exten => s,6,WaitExten(10)

[billing]
exten => s,1,Answer
exten => s,2,Wait(1)
exten => s,3,Playback(billing-department)
exten => s,4,Wait(1)
exten => s,5,Background(billing-representative)
exten => s,6,WaitExten(10)

When a call comes in, they here a welcome message followed by options to press 1 for sales, 2 for support, or 3 for billing.

If the caller presses 1, they are transferred to the sales extension.
If the caller presses 2, they are transferred to the support extension.
If the caller presses 3, they are transferred to the billing extension.
If the caller does not press a key, they stay on the line for 10 seconds before being disconnected.

Each department extension plays a message indicating which department the caller has been transferred to and then waits for 10 seconds for a representative to answer.


Dialplan that asks a user for their passcode and performs a MySQL lookup to compare it:

[passcode-lookup]
exten => s,1,Answer()
exten => s,2,Background(enter-passcode)
exten => s,3,WaitExten(10)
exten => s,4,GotoIf($[${LEN(${EXTEN})} = 4]?check)
exten => s,5,Background(invalid-passcode)
exten => s,6,Goto(s,2)

; check the passcode against the database
exten => s,n(check),Set(DB_RESULT=${MYSQL(SELECT id FROM passcodes WHERE passcode='${EXTEN}')})
exten => s,n,GotoIf($[${DB_RESULT} = 0]?incorrect)
exten => s,n,Background(passcode-accepted)
exten => s,n,Hangup()

; play "incorrect passcode" and go back to the beginning
exten => s,n(incorrect),Background(incorrect-passcode)
exten => s,n,Goto(s,2)

The Answer command answers the caller.
The Background command plays a message asking the caller to enter their 4 digit passcode. The WaitExten command waits for 10 seconds for the caller to enter a 4-digit passcode.

The GotoIf command is used to check the length of the entered passcode. If the length of the entered passcode is not 4 digits, the dialplan will play "invalid passcode" prompt and go back to the beginning.

The Set command is used to perform a MySQL lookup to compare the entered passcode against the passcodes in the database. The MYSQL function returns the id of the passcode if it is found in the database, and 0 if it is not found.

The GotoIf command is used to check the result of the MySQL lookup. If the result is 0, the dialplan plays an "incorrect passcode" message and goes back to the beginning. If the passcode is found in the database, the dialplan plays a "passcode accepted" message and hangs up the call.

Note: This dialplan assumes that you have a MySQL database set up with a table named passcodes containing a column named id and a column named passcode. You will need to modify the MySQL query to match the structure of your database.

No comments:

Post a Comment

Feel free to leave a comment! If you have any information that you think should be included, please do so here and I'll get it added in.