Tuesday, August 5, 2008

Asterisk functions

Asterisk functions are used in Asterisk's dialplan. Unlike dialplan applications, they cannot be used directly. Instead they return a value that could be used by the dialplan logic.

Functions are new in 1.2. Some functions may be read from, others may be written to. Please note that several of the builtin variables (including ENV and LEN) have been converted to functions for 1.2.

Functions in the below list are marked in red if they are only available in version 1.4 and higher.

Functions in the below list are marked in blue if they are only available in version 1.6 and higher.

Function names by practice are all capitalzed letters. The names ARE CASE SENSITIVE!

Function List

  • AGENT: Gets information about an Agent
  • ARRAY: Allows setting multiple variables at once
  • BASE64_DECODE: Decode a base64 string
  • BASE64_ENCODE: Encode a string into base64
  • CALLERID: Get or set Caller*ID
  • CDR: Get or set a CDR variable
  • CHANNEL: Gets/sets various pieces of information about the channel.
  • CHECKSIPDOMAIN: Checks if domain is a local domain
  • CHECK_MD5: Checks an MD5 digest. Deprecated in 1.4, since it can be done with an expression in combination with MD5.
  • Asterisk func clearhash: New in 1.6
  • CURL: Retrieves a URL
  • CUT: String parsing, based upon a delimiter
  • Asterisk func device_State): New in 1.6 allows retrieving any device state in the dialplan, as well as creating custom device states that are controllable from the dialplan
  • DB: Get or set a value in the AstDB
  • DB_DELETE: Delete a value from the AstDB; replaces the DBDel application
  • DB_EXISTS: Check to see if a key exists in the Asterisk database
  • DIALGROUP: New in 1.6: Poor man's calling queue
  • Asterisk func dialplan_exists: New in 1.6 Check for the existence of a dialplan target
  • DUNDILOOKUP: Do a DUNDi lookup of a phone number.
  • ENUMLOOKUP: General or specific querying of NAPTR records or counts of NAPTR types for ENUM or ENUM-like DNS pointers
  • ENV: Get or set an environmental variable
  • EVAL: Evaluate stored variables.
  • EXISTS: Existence Test: Returns 1 if exists, 0 otherwise
  • Asterisk func extension_state: New in 1.6 Allows retrieving the state of any extension
  • FIELDQTY: Get the number of fields, based upon a delimiter
  • FILTER: Filter the string to include only the allowed characters
  • GROUP: Can also return the name of the group set on a channel when used in a read environment
  • GROUP_COUNT: Counts the number of channels in the specified group
  • GROUP_LIST: Returns a space separated list of all of the groups set on a channel
  • GROUP_MATCH_COUNT: Counts the number of channels in the groups matching the specified pattern
  • HASH: New in 1.6
  • Asterisk func hashkeys: New in 1.6
  • Asterisk func hint: New in 1.6 Allows retrieving hint information
  • IAXPEER: Gets IAX peer information
  • Asterisk func iaxvar: Pass variables over IAX channels, somewhat similar to SIPADDHEADER() and SIP_HEADER()
  • IF: Conditional: Returns the data following '?' if true else the data following ':'
  • IFTIME: Temporal Conditional: Returns the data following '?' if true else the data following ':'
  • ISNULL: NULL Test: Returns 1 if NULL or 0 otherwise
  • KEYPADHASH: Hash the letters in the string into the equivalent keypad numbers.
  • LANGUAGE: Get or set the channel's language
  • LEN: Get the length of an arbitrary string
  • MATH: Performs Mathematical Functions
  • MD5: Computes an MD5 digest
  • MUSICCLASS: Get or set the MusicOnHold class
  • ODBC: Generic database access/queries using ODBC
  • QUEUEAGENTCOUNT: Gets the number of members currently listening on a queue. Deprecated in favor of QUEUE_MEMBER_COUNT.
  • QUEUE_MEMBER_COUNT: Count number of members answering a queue
  • QUEUE_MEMBER_LIST: Returns a list of interfaces on a queue
  • QUOTE: Quotes a given string, escaping embedded quotes as necessary
  • RAND: Choose a random number in a range
  • REALTIME: This function will read or write values from/to a RealTime repository.
  • REGEX: Regular Expression: Returns 1 if data matches regular expression.
  • SET: SET assigns a value to a channel variable
  • SHA1: Computes a SHA1 digest
  • SIPCHANINFO: Gets the specified SIP parameter from the current channel
  • SIPPEER: Gets SIP peer information
  • Asterisk func SIPAddHeader: Typically used to set Alert-Info information, e.g. ring tone .wav files
  • SIP_HEADER: Gets or sets the specified SIP header
  • SORT: Sorts a list of keys and values into a list of keys
  • SQL_ESC: Escapes a value for inclusion in an SQL call (prevent injection attacks).
  • STAT: Does a check on the specified file
  • STRFTIME: Formats an epoch into an arbitrary datetime string
  • STRPTIME: Returns the epoch of the arbitrary date/time string structured as described in the format.
  • Asterisk func sysinfo: New in 1.6 Allows retrieval of system information
  • TIMEOUT: Gets or sets timeouts on the channel.
  • Asterisk func toupper: New in 1.6 Converts a string to uppercase
  • Asterisk func tolower: New in 1.6 Converts a string to lowercase
  • TXTCIDNAME: TXTCIDNAME looks up a caller name via DNS
  • URIDECODE: Decodes an URI-encoded string.
  • URIENCODE: Encodes a string to URI-safe encoding.
  • VMCOUNT: Counts the voicemail in a specified mailbox





Using functions in Asterisk dialplans

Example usage of the function CUT:

exten => s,1,Set(foo=${CUT(bar,,2)})

The above example sets the variable foo to the function CUT whose first parameter is 'bar', second parameter is empty and third parameter is '2'.

A function is often used to determain which "part of a variable" to set. For instance, the use of the function CALLERID:

exten => s,n,Set(CALLERID(name)=Foo Fighters)
exten => s,n,Set(CALLERID(number)=700)

Functions Reference Through the CLI

The list of availble functions depends on the modules installed and loaded on the current system. To get the list of functions from the Asterisk CLI use the command show functions . This gives a list of all functions.

To get the syntax of a specific function, use the command show function FUNCNAME. This gives a longer description of the function. It is often handy to use tab completion that way to identify a function name.

1 comment:

  1. Genial dispatch and this fill someone in on helped me alot in my college assignement. Thanks you as your information.

    ReplyDelete