Wednesday, July 8, 2009

How to read DTMF and store in the database in Asterisk

Aim :

To Read a variable in the form for DTMF tones as pressed by the caller.

For example if you would like your users to call up the system and record there inputs in the database and then make use of Asterisk to perform what ever tasks with those recorded inputs.

Syntax :

Read(variable[,filename][,maxdigits][,option][,attempts][,timeout])

Reads a #-terminated string o
f digits a certain number of times from the user in to the given variable.
  • variable: variable where the user's response will be stored.
  • filename: file to play before reading digits.
  • maxdigits: maximum acceptable number of digits. Stops reading after maxdigits have been entered (without requiring the user press the '#' key). Defaults to 0 - no limit - wait for the user press the '#' key. Any value below 0 means the same. Max accepted value is 255.
  • option: may be 'skip' to return immediately if the line is not up, or 'noanswer' to read digits even if the line is not up.
  • attempts: if greater than 1, that many attempts will be made in the event no data is entered.
  • timeout: Timeout in seconds. If greater than 0, that value will override the default timeout.

Read should disconnect if the function fails or errors out.

[edit]

Example

; The Following Example uses Read and Say digits to read 3 digits from user, saying each one as user types.

Asterisk 1.2:
exten => s,1,Gotoif($[ "${LEN(${extensao})}" < "3"]?3:100)
exten => s,n,NoOp(Executing - ${extensao} - )
exten => s,n,Read(digito||1)
exten => s,n,SayDigits(${digito})
exten => s,n,Set(extensao=${extensao}${digito})
exten => s,n,GoTo(s,1)
exten => s,100,GoTo(from-pstn,s,1)
exten => h,1,hangup()

Asterisk 1.4, 1.6:
exten => s,1(start),Gotoif($[ "${LEN(${extensao})}" < "3"]?collect:pstn)
exten => s,n,NoOp(Executing - ${extensao} - )
exten => s,n(collect),Read(digito,,1)
exten => s,n,SayDigits(${digito})
exten => s,n,Set(extensao=${extensao}${digito})
exten => s,n,GoTo(start)
exten => s,n(pstn),GoTo(from-pstn,s,1)
exten => h,1,hangup()


[edit]

Notes

Pipe characters ( | ) have been changed to commas in Asterisk 1.6.
In Asterisk 1.4, either pipes OR commas will work. Using commas will ease future upgrades.

'#' is the End-Of-Input Key. It is NOT possible to accept '#' with Read()

Warning:
In Asterisk 1.4: the "filename" field does not accept concatenation (file1&file2&file...).