You are on page 1of 1

Home Topics Tools Support Us thecodeblocks Sign in Subscribe

ASTERISK

Some Predefined Channel


Variables in Asterisk
Kevin Durant
Jun 28, 2023 6 min

There are some channel variables set by Asterisk that you can refer to in your dial
plan definitions. Asterisk-defined variables, in contrast to user-defined variables, are
case sensitive.

${CHANNEL}
The ${CHANNEL} variable contains the name of the current channel. It typically
consists of a channel technology prefix, followed by a unique identifier. For example,
"SIP/1234-00000123" or "DAHDI/2-1". You can use this variable to log or display the
channel name in your dial plan or custom applications. Here's an example:

exten => 100,1,Answer()


same => n,Verbose(1, Current channel: ${CHANNEL})
same => n,Hangup()

In this example, when the call reaches extension 100, the channel name is displayed
using the ${CHANNEL} variable. The Verbose application logs the channel name in
the Asterisk console.

${CALLERID(num)}
The ${CALLERID(num)} variable represents the Caller ID number of the calling party.
It contains the incoming caller's phone number. You can use this variable for call
routing, database lookups, or to customize call handling based on the caller's
number. Here's an example:

exten => 200,1,Answer()


same => n,Set(caller_num=${CALLERID(num)})
same => n,Verbose(1, Caller ID number: ${caller_num})
same => n,Hangup()

In this example, when the call reaches extension 200, the caller's number is stored in
the caller_num variable using ${CALLERID(num)}. The Verbose application logs the
caller's number in the Asterisk console.

${CALLERID(name)}
Similar to ${CALLERID(num)}, ${CALLERID(name)} holds the Caller ID name associated
with the calling party. It represents the incoming caller's name if available. You can
use this variable to personalize call handling or display the caller's name on a display
device. Here's an example:

exten => 200,1,Answer()


same => n,Set(caller_num=${CALLERID(num)})
same => n,Verbose(1, Caller ID number: ${caller_num})
same => n,Hangup()

In this example, when the call reaches extension 300, the caller's name is stored in
the caller_name variable using ${CALLERID(name)}. The Verbose application logs the
caller's name in the Asterisk console.

${EXTEN}
The ${EXTEN} variable holds the current extension being dialed or processed in the
dial plan. It provides information about the intended destination of the call. You can
utilize this variable to determine the specific extension dialed and perform specific
actions or routing based on the dialed extension. Here's an example:

exten => _4XXX,1,NoOp(Dialed extension: ${EXTEN})


same => n,Hangup()

In this example, when a call reaches an extension starting with "4" followed by three
digits, the ${EXTEN} variable captures the dialed extension. The NoOp application
logs the dialed extension in the Asterisk console.

${CONTEXT}
The ${CONTEXT} variable stores the current context in the dial plan. A context
defines a section of the dial plan that determines call routing and call handling rules.
It represents the section of the dial plan where the call is currently located. This
variable is useful for conditional routing or executing different actions based on the
current context. Here's an example:

[internal]
exten => 500,1,Set(context_name=${CONTEXT})

${UNIQUEID}
The ${UNIQUEID} variable contains the unique identifier assigned to the channel
when it was created. It provides a unique reference to the call throughout its
lifecycle. This identifier is useful for tracking and correlating events related to a
specific call. Here's an example:

exten => 600,1,Answer()


same => n,Set(unique_id=${UNIQUEID})
same => n,Verbose(1, Unique ID: ${unique_id})
same => n,Hangup()

In this example, when the call reaches extension 600, the unique ID of the channel is
stored in the unique_id variable using ${UNIQUEID}. The Verbose application logs
the unique ID in the Asterisk console.

${DIALEDPEERNAME}
In outbound calls, the ${DIALEDPEERNAME} variable stores the name of the called
peer. It represents the peer or endpoint being dialed to establish the outbound call.
You can use this variable to extract the called party's name and perform actions
specific to the dialed peer. Here's an example:

exten => 700,1,Dial(SIP/provider/${EXTEN})


same => n,Set(dialed_peer=${DIALEDPEERNAME})
same => n,Verbose(1, Dialed Peer Name: ${dialed_peer})
same => n,Hangup()

In this example, when the call reaches extension 700, an outbound call is made to a
SIP provider using the ${EXTEN} variable. The ${DIALEDPEERNAME} variable
captures the name of the called peer, and the Verbose application logs it in the
Asterisk console.

${DIALEDPEERNUMBER}
Similar to ${DIALEDPEERNAME}, ${DIALEDPEERNUMBER} holds the number dialed
to reach the called peer in outbound calls. It contains the dialed number, which can
be extracted and used for call routing, database lookups, or customized call
handling. Here's an example:

exten => 800,1,Dial(SIP/provider/${EXTEN})


same => n,Set(dialed_number=${DIALEDPEERNUMBER})
same => n,Verbose(1, Dialed Number: ${dialed_number})
same => n,Hangup()

In this example, when the call reaches extension 800, an outbound call is made to a
SIP provider using the ${EXTEN} variable. The ${DIALEDPEERNUMBER} variable
captures the dialed number, and the Verbose application logs it in the Asterisk
console.

${CALLERID(dnid)}
The ${CALLERID(dnid)} variable represents the Dialed Number Identification Service
(DNIS) information. It contains the number that was dialed to reach the current
context. This variable can be used to extract the dialed number and perform specific
actions or routing based on the dialed number, such as IVR (Interactive Voice
Response) menu selections. Here's an example:

exten => 900,1,Set(dialed_number=${CALLERID(dnid)})


same => n,Verbose(1, Dialed Number: ${dialed_number})
same => n,Hangup()

In this example, when the call reaches extension 900, the ${CALLERID(dnid)} variable
captures the dialed number, and the Verbose application logs it in the Asterisk
console.

${CHANNEL(language)}
The ${CHANNEL(language)} variable stores the language setting for the channel. It is
often used to determine the preferred language for playing prompts or interacting
with the caller. Here's an example:

exten => 1000,1,Answer()


same => n,Set(channel_language=${CHANNEL(language)})
same => n,Verbose(1, Channel Language: ${channel_language})
same => n,Hangup()

In this example, when the call reaches extension 1000, the ${CHANNEL(language)}
variable captures the language setting of the channel, and the Verbose application
logs it in the Asterisk console.

${SIP_CODEC}
The ${SIP_CODEC} variable contains the codec negotiated for the SIP channel. It
represents the audio codec being used for the call. This variable is particularly useful
for implementing custom call handling based on the codec in use. Here's an
example:

exten => 1100,1,Answer()


same => n,Set(sip_codec=${SIP_CODEC})
same => n,Verbose(1, SIP Codec: ${sip_codec})
same => n,Hangup()

In this example, when the call reaches extension 1100, the ${SIP_CODEC} variable
captures the codec negotiated for the SIP channel, and the Verbose application logs
it in the Asterisk console.

${STRFTIME}
The ${STRFTIME} variable allows you to retrieve the current date and time in a
specified format. It can be used to generate timestamps or time-related information
during the call. Here's an example:

exten => 1200,1,Answer()


same => n,Set(timestamp=${STRFTIME(${EPOCH},,%Y-%m-%d %H:%M:%
same => n,Verbose(1, Current Timestamp: ${timestamp})
same => n,Hangup()

${CHANNEL(state)}
The ${CHANNEL(state)} variable represents the state of the current channel. It
indicates the current state of the channel, such as "Ring", "Up", "Down", or "Ringing".
Here's an example:

exten => 1600,1,Answer()


same => n,Wait(1)
same => n,Set(channel_state=${CHANNEL(state)})
same => n,Verbose(1, Channel State: ${channel_state})
same => n,Hangup()

In this example, when the call reaches extension 1600, the ${CHANNEL(state)}
variable captures the state of the channel after a brief wait. The Verbose application
logs the channel state in the Asterisk console.

${MIXMONITOR_FILENAME}
The ${MIXMONITOR_FILENAME} variable contains the file name of the recorded
call when the MixMonitor application is used. It allows you to access the recorded
file name for further processing or storage. Here's an example:

exten => 1800,1,Answer()


same => n,MixMonitor(/path/to/recording.wav)
same => n,Wait(10)
same => n,Verbose(1, Recording File: ${MIXMONITOR_FILENAME})
same => n,Hangup()

${DIALSTATUS}
To get the state of the called channel in an Asterisk dial plan, you can use the
${DIALSTATUS} channel variable. The ${DIALSTATUS} variable stores the status of
the last attempted dial operation. Here's an example of how to use it:

exten => 1000,1,Answer()


same => n,Dial(SIP/device1)
same => n,Verbose(1, Dial Status: ${DIALSTATUS})
same => n,Hangup()

In this example, when the call reaches extension 1000, the Dial application is used to
dial the SIP endpoint "device1". After the dial operation, the ${DIALSTATUS} variable
captures the status of the call. The Verbose application is then used to log the dial
status in the Asterisk console.

Possible values for ${DIALSTATUS} include:

"CHANUNAVAIL": The requested channel is unavailable or does not exist.

"CONGESTION": The called channel is congested and the call cannot be


completed.

"BUSY": The called channel is busy.

"NOANSWER": The called party did not answer the call.


"ANSWER": The called party answered the call.

"CANCEL": The call was canceled before it could be answered.

"DONTCALL": The caller requested to stop the call.

"TORTURE": The call was considered too demanding for the system.

By accessing the ${DIALSTATUS} variable, you can perform different actions or make
decisions based on the state of the called channel within your dial plan.

READ NEXT

Simplifying Telecommunication Routing: An Introduction to Dial Plans


In the world of telecommunication systems, efficient call routing is essential to ensure smooth
and effective communication. Dial plans play a crucial role in streamlining the process,…
KEVIN DURANT JUN 28, 2023

COMMENTS (0)

Start the conversation


Become a member of thecodeblocks to start commenting.

Sign up now

Already a member? Sign in

Subscribe to thecodeblocks
Don't miss out on the latest news and tutorials

Subscribe now

Sign up privacy & policy

   

Copyright by @ thecodeblocks

You might also like