Asterisk cmd Read


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


As of Asterisk 1.0:
As of Asterisk 1.2:
As of Asterisk 1.4:

Reads a #-terminated string of 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: There are 3 options:(sin). s(skip):to return immediately if the line is not up. i(indication):Rather than playing a prompt, play an indication tone of some sort (such as the dialtone). n(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.


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

Asterisk 1.2:
  ~np~exten => s,1,Gotoif($[ "${LEN(${extensao})}" < "3"]?3:100)~/np~
  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:
  ~np~exten => s,1(start),Gotoif($[ "${LEN(${extensao})}" < "3"]?collect:pstn)~/np~
  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()

; The following example is a work-around that mostly works at catching # as part of the dial string.
  ;Written in 1.8.  Compatibility unknown.
  exten => s,1,Set(wait=2)
  exten => s,n(begin),Playback("please-dial-now")
  exten => s,n,Set(gottries=0)
  exten => s,n,Set(got=)
  exten => s,n(getmore),Set(timer=${EPOCH})
  exten => s,n,Read(get,"silence/1",,,,${wait})
  exten => s,n,Set(timer=$[${EPOCH}-${timer}])
  exten => s,n,GotoIf($[${timer}==$[${wait}+1]]?gotnothing:gotsomething)
  ;I double-check the length of get below in case the user happened to enter
  ;digits, but did so in such a manner that the epoch math thinks nothing
  ;was entered.  I suppose the same could happen with pound, but because
  ;pressing pound ends the read (and thus shortens the epoch), It is much
  ;less likely.
  exten => s,n(gotnothing),Set(gottries=$[${gottries}+1]
  exten => s,n,GotoIf($[${LEN(${get})} == 0]?reallynothing:gotdigit)
  exten => s,n(reallynothing),GotoIf($[${gottries}>1]?done:getmore)
  exten => s,n(gotsomething),Set(gottries=0)
  exten => s,n,GotoIf($[${LEN(${get})} == 0]?gotpound:gotdigit)
  exten => s,n(gotpound),Set(got=${got}#)
  exten => s,n,Goto(getmore)
  exten => s,n(gotdigit),Set(got=${got}${get})
  exten => s,n,Set(get=)
  exten => s,n,Goto(getmore)
  exten => s,n(done),SayDigits(${got})
  exten => s,n,Goto(begin)
  ; Doubt we get here:
  exten => i,1,Playback("vm-goodbye")
  exten => i,n,Hangup()
  ; here either:
  exten => u,1,Playback("vm-goodbye")
  exten => u,n,Hangup()


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()

In Asterisk 1.4: the "filename" field does not accept concatenation (file1&file2&file...). It does work in 1.8.4 and possibly earlier versions.

Asterisk | Applications | Functions | Variables | Expressions | Asterisk FAQ

See also

Created by: oej, Last modification: Mon 24 of Sep, 2012 (08:49) by dominic16y
Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+