Wednesday, February 15, 2017

Asterisk. Ask questions, record answers.

What this code does.
The system will ask questions, after each it will record the caller for a maximum of 15 seconds (you can change this of course) or until the users presses # key, whichever comes first.
This code allows for two questions and answers to be given.
In this example, after the 2nd answer is given, the system will play back both recordings together so you can hear it.
If caller press # the call will stop recording and continue to next questions
If caller either doesn't say anything for 3 seconds, the system continues to next question (changeable of course)
Once the questions are done, the caller is hungup on.
The system generates a .call file which will contact an extension of your choice, one that goes directly to voicemail.
The file that is recorded is saved to thevoicemail box, then the call hangsup.
Your asterisk system will send the email to the recipient using your voicemail parameters.



I've tested this in the FreePBX Distro using 13.0.190.11 worked ok as a beta test. It may need alteration for your own environments, however I am using commands that have been around a long time so should be compatible with most versions.
Code from strangers should never be tested in a production environment.

INSTALLATION
To make the code simpler, for those not versed in asterisk dialplan, any line starting with a ; semicolon is a remark line and can be safely removed for ease of viewing. Lines with ; are there to either leave a comment, or to have the system not execute that line of code.

STEP 1
ADMIN->CONFIG EDIT
open up "extensions_custom.conf"
Copy the code below between the ------ lines into the bottom of extensions_custom.conf and then save the file.
Replace code from previous BETA 1 & 2 releases
   START OF DIALPLAN CODE
   ---------------------------------------------------------------------------
[macro-questionanswer]
exten => s,1,answer()
exten => s,n,playback(/var/lib/asterisk/sounds/en/custom/question1)
exten => s,n,Record(${UNIQUEID}.ulaw,3,15,a)
exten => s,n,playback(/var/lib/asterisk/sounds/en/custom/question2)
exten => s,n,Record(${UNIQUEID}.ulaw,3,15,a)
;Generate .call file that asterisk will use to send the call to a voicemail box
;Change XXXXX to be an extension in your system that you want called and have the message left on
exten => s,n,system(echo "Channel: LOCAL/XXXXX" > /var/lib/asterisk/sounds/recording.call)
exten => s,n,system(echo "Context: macro-questionanswer1" >> /var/lib/asterisk/sounds/recording.call)
exten => s,n,system(echo "Extension: s" >> /var/lib/asterisk/sounds/recording.call)
exten => s,n,system(echo "Setvar: playfile=${UNIQUEID}" >> /var/lib/asterisk/sounds/recording.call)
exten => s,n,system(echo CallerID: "${CALLERID(name)}" ${CALLERID(number)} >> /var/lib/asterisk/sounds/recording.call)
exten => s,n,system(mv /var/lib/asterisk/sounds/recording.call /var/spool/asterisk/outgoing)
exten => s,n,hangup()
[macro-questionanswer1]
exten => s,1,wait(1)
exten => s,n,senddtmf(#)
exten => s,n,playback(/var/lib/asterisk/sounds/${playfile})
;delete sound file.
exten => s,n,system(rm /var/lib/asterisk/sounds/${playfile}.ulaw)
exten => s,n,hangup()
  -----------------------------------------------------------------------------
  END OF DIALPLAN CODE
Click on "SAVE"
If you are updating this code from previous versions, you can stop here and test it out.
SCROLL DOWN TO THE BOTTOM TO THE "TESTING IT OUT" section
STEP 2
ADMIN->CUSTOM DESTINATIONS
Create a new custom destination "questionanswer"
Set the target as
macro-questionanswer,s,1
Save those changes
STEP 3
Create a way to trigger the application to run from dialplan.
For example, create a IVR, and set one of the numeric values a caller can enter to Custom Destination -> "questionanswer""
STEP 4
Save your changes AND apply the configuration.
STEP 5
Create your question files.
Use system recordings, and either record them over your phone, or through a 3rd party sound recording application. If recording with another app, they must be saved as 8000khz MONO 16bit WAV PCM.
The recordings you make, set their name to "recording1" "recording2" "recording3"
STEP 6
in /tmp/ create a file called "email.txt"
In that file put in something like
SUBJECT: NEW RECORDING
Here is a new recording to review.
Save changes.
TESTING IT OUT
Call and leave your recordings. System should leave the message and you should get an email alert with the message attached.
HOW IT WORKS
1> As previous, your recorded questions are played, and the answers are recorded into a single file.
2> After the last question is answered, the system will hangup.
3> Then the system internally places a call to the extension that you want the system to send the voicemail through.
4> When that system picks up, assuming voicemail, the system sends a "#" tone to the extension to bypass any greetings and additional instructions that voicemail systems have.
5> The system in the background plays the message that your caller left, and sends it as a voicemail.
6> Your recipient should get the message alert in their email.
Give it a test!


.CALL FILE EXPLAINED.
If you create .txt file with the proper calling parameter information, you can drop that file in the outgoing folder in asterisk and that will make your phone call happen.
Essentially these 4 lines generate the 4 line file.
The number you want to call, this example is extension 1234
exten => s,n,system(echo "Channel: LOCAL/1234" > /var/lib/asterisk/sounds/recording.call)
The context you want executed when the destination 1234 answers
exten => s,n,system(echo "Context: macro-questionanswer1" >> /var/lib/asterisk/sounds/recording.call)
The line you want executed in the context
exten => s,n,system(echo "Extension: s" >> /var/lib/asterisk/sounds/recording.call)
Variables you want passed to the system when 1234 answers
exten => s,n,system(echo "Setvar: playfile=${UNIQUEID}" >> /var/lib/asterisk/sounds/recording.call)
The variable is the most complicated piece. UniqueID is the unique number given for each call. When you are recording the calls, we use that number as the file number. However, when we call the new number to leave the voicemail, that generates a new unique ID. So in this code we set a temporary variable of "playfile" to store the unique number that is the file name. This temporary file is, in itself unique to this call, so it wont affect things if you have multiple question answers recording simultaneously..

No comments:

Post a Comment