How to use Asterisk Dialplan Variables and Global Variables on Debian 12

To use Asterisk Dialplan Variables and Global Variables on Debian 12

Introduction:

Asterisk is an open-source telephony platform which are classified in to two types of variables: Dialplan Variables and Global Variables. These variables are used to store and manipulate data within the Asterisk dialplan and can be accessed and modified during the call processing.

Dialplan Variables: Dialplan variables are variables that are defined and used within a specific context or extension in the Asterisk dialplan. They are used to store temporary data that is relevant to a particular call or call leg. Dialplan variables are scoped to the context in which they are defined and are typically used to pass information between different parts of the dialplan.

Global Variables: Global variables are variables that have a global scope and are accessible from anywhere within the Asterisk dialplan. They are typically used to store configuration settings, constants, or values that need to be shared across multiple contexts or extensions. Global variables are defined in the Asterisk configuration files, such as extensions.conf or other included configuration files.

Procedure

Step 1: Check the OS version by using following command.

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 12 (bookworm)
Release:	12
Codename:	bookworm

Step 2: Go to asterisk location by using following command.

root@linuxhelp:~# cd /etc/asterisk/

Step 3: Edit the extensions.conf file for create dialplan variable and global variable by using following command.

root@linuxhelp:/etc/asterisk# vim extensions.conf

Step 4: Login to the Asterisk CLI Console by using following command.

root@linuxhelp:/etc/asterisk# sudo asterisk -rvvvvvvvvvvvvvv
Asterisk 20.4.0, Copyright (C) 1999 - 2022, Sangoma Technologies Corporation and
 others.
Created by Mark Spencer <markster@digium.com>
Asterisk comes with ABSOLUTELY NO WARRANTY; type 'core show warranty' for detail
s.
This is free software, with components licensed under the GNU General Public
License version 2 and other licenses; you are welcome to redistribute it under
certain conditions. Type 'core show license' for details.
=========================================================================
Connected to Asterisk 20.4.0 currently running on linuxhelp (pid = 97146)

Step 5: Reload the Dialplan by using following command.

linuxhelp*CLI> dialplan reload
Dialplan reloaded.
  == Setting global variable 'GLOBAL_VAR' to 'This is a global variable'
    -- Time to scan old dialplan and merge leftovers back into the new: 0.000011
 sec
    -- Time to restore hints and swap in new dialplan: 0.000019 sec
    -- Time to delete the old dialplan: 0.000013 sec
    -- Total time merge_contexts_delete: 0.000043 sec
    -- pbx_config successfully loaded 1 contexts (enable debug for details).

Step 6: Check the 112 extension as shown in below image.

linuxhelp*CLI> 
    -- Executing [110@internal:1] Set("PJSIP/112-00000027", "my_variable=Hello, 
World") in new stack
    -- Executing [110@internal:2] Dial("PJSIP/112-00000027", "PJSIP/110, 20") in
 new stack
    -- Called PJSIP/110
    -- PJSIP/110-00000028 is ringing
    -- Nobody picked up in 20000 ms
    -- Executing [110@internal:3] Playback("PJSIP/112-00000027", "sorry_didnt_ge
t") in new stack
       > 0x7fcb8c05ce90 -- Strict RTP learning after remote address set to: 192.
168.6.102:8000
       > 0x7fcb8c05ce90 -- Strict RTP switching to RTP target address 192.168.6.
102:8000 as source
    -- <PJSIP/112-00000027> Playing 'sorry_didnt_get.ulaw' (language 'en')
    -- Executing [110@internal:4] Hangup("PJSIP/112-00000027", "Hello, World") i
n new stack
[Sep 27 03:31:41] WARNING[98130][C-00000020]: pbx_builtins.c:926 pbx_builtin_han
gup: Invalid cause given to Hangup(): "Hello, World"
  == Spawn extension (internal, 110, 4) exited non-zero on 'PJSIP/112-00000027'

Step 7: Check the 110 extension as shown in below image.

linuxhelp*CLI> 
    -- Executing [112@internal:1] NoOp("PJSIP/110-00000029", "This is a global v
ariable") in new stack
    -- Executing [112@internal:2] Dial("PJSIP/110-00000029", "PJSIP/112, 20") in
 new stack
    -- Called PJSIP/112
    -- PJSIP/112-0000002a is ringing
    -- Nobody picked up in 20000 ms
    -- Executing [112@internal:3] Playback("PJSIP/110-00000029", "sorry_didnt_ge
t") in new stack
       > 0x7fcb8c05ce70 -- Strict RTP learning after remote address set to: 210.
18.155.184:51546
       > 0x7fcb8c05ce70 -- Strict RTP qualifying stream type: audio
       > 0x7fcb8c05ce70 -- Strict RTP switching source address to 192.168.6.102:
51546
    -- <PJSIP/110-00000029> Playing 'sorry_didnt_get.ulaw' (language 'en')
    -- Executing [112@internal:4] Hangup("PJSIP/110-00000029", "") in new stack
  == Spawn extension (internal, 112, 4) exited non-zero on 'PJSIP/110-00000029'

Step 8: Exit the Asterisk Console by using following command.

linuxhelp*CLI> exit
Asterisk cleanly ending (0).
Executing last minute cleanups

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to use Asterisk Dialplan Variables and Global Variables on Debian 12. Your feedback is much welcome.

FAQ
Q
What is the advantage of using Global Variables in Asterisk?
A
Global Variables provide a way to centralize and manage shared data and configuration settings across your Asterisk configuration. They can be accessed from any part of the dialplan, making them useful for storing constants, configuration parameters, or values that need to be accessed globally.
Q
How are Global Variables defined in Asterisk, and where are they typically configured?
A
Global Variables are defined in the Asterisk configuration files, often within a [globals] section in the extensions.conf file or other included configuration files. Here's an example of defining a Global Variable:
plaintextCopy code
[globals] my_global_variable = 100
Q
Can Dialplan Variables be used to share data between different contexts in Asterisk?
A
Dialplan Variables are primarily scoped to the context or extension in which they are defined. However, you can use channel variables or Global Variables to share data between different contexts or extensions.
Q
How do I set a value for a Dialplan Variable in Asterisk?
A
You can set a Dialplan Variable using the Set application within a specific extension in your Asterisk dialplan. For example:
plaintextCopy code
exten => 123,1,Set(my_variable=42)
This sets a Dialplan Variable named my_variable with a value of 42 within extension 123.
Q
What is the main difference between Dialplan Variables and Global Variables in Asterisk?
A
The main difference is their scope. Dialplan Variables are scoped to a specific context or extension within the dialplan and are used for temporary data relevant to a single call or call leg. Global Variables, on the other hand, have a global scope and can be accessed from anywhere within the Asterisk configuration, making them suitable for storing shared data and configuration settings.