text stringlengths 0 152 |
|---|
It’s fairly unlikely (unless you have a typo) that you’d purposefully implement some‐ |
thing like our examples. However, when you perform math or a comparison with an |
unset channel variable, this is effectively what you’re doing. |
The examples we’ve used to show you how conditional branching works are not inva‐ |
lid. We’ve first initialized the variable and can clearly see that the channel variable |
we’re using in our comparison has been set, so we’re safe. But what if you’re not |
always so sure? |
170 |
| |
Chapter 10: Deeper into the Dialplan |
In Asterisk, strings do not need to be double- or single-quoted like in many program‐ |
ming languages. In fact, if you use a double or single quote, it is a literal construct in |
the string. If we look at the following snippets of an extension... |
same => n,Set(TEST_1=foo) |
same => n,Set(TEST_2='foo') |
same => n,NoOp(Are TEST_1 and TEST_2 equiv? $[${TEST_1} = ${TEST_2}]) |
...we need to note that the value returned by our comparison in the NoOp() will not be |
a value of 1 (values match; or true) the return value will be 0 (values do not match; or |
false). |
We can use this to our advantage when performing comparisons by wrapping our |
channel variables in single or double quotes. By doing this we make sure even when |
the channel variable might not be set, our comparison will be valid syntax. |
In the following example, we would get an error: |
exten => 212,1,NoOp() |
same => n,GotoIf($[ ${TEST} != valid ]?error_handling) |
same => n,Hangup() ; We're getting an error and ending up here |
same => n(error_handling),Playback(goodbye) |
same => n,Hangup() |
However, we can circumvent this by wrapping what we’re comparing in extra charac‐ |
ters (in this case quotes). The same example, but made valid: |
exten => 213,1,NoOp() |
same => n,GotoIf($[ "${TEST}" != "valid" ]?error_handling) |
same => n,Hangup() |
same => n(error_handling),Playback(goodbye) |
same => n,Hangup() |
Even if ${TEST} hasn’t been set (in other words it does not exist and therefore has no |
value), we’re still doing a comparison of something: |
$["" != "valid"] |
If you get into the habit of recognizing these situations and using the wrapping and |
prefixing techniques we’ve outlined, you’ll write much safer dialplans. |
Note again that the quote character doesn’t have any special meaning here. We used it |
because it’s a logical character for this purpose. The following works too: |
same => n,GotoIf($[_${TEST}_ != _valid_]?error_handling) |
;OR |
same => n,GotoIf($[AAAAA${TEST}AAAAA != AAAAAvalidAAAAA]?error_handling) |
Not all characters will work, as some may have other meanings to Asterisk and cause |
problems. Stay with the quote character and you should be fine. |
The classic example of conditional branching is affectionately known as the “psycho- |
ex” logic. If the caller ID number of the incoming call matches the phone number of |
Conditional Branching |
| |
171 |
somebody you never want to talk to again, Asterisk gives a different message than it |
ordinarily would to any other caller. While somewhat simple and primitive, it’s a good |
example for learning about conditional branching within the Asterisk dialplan. |
This example uses the CALLERID() function, which allows us to retrieve the caller ID |
information on the inbound call. Let’s assume for the sake of this example that the |
victim’s phone number is 888-555-1212:4 |
exten => 214,1,NoOp(CALLERID(num): ${CALLERID(num)} CALLERID(name): ${CALLERID(name)}) |
same => n,GotoIf($[ ${CALLERID(num)} = 8885551212 ]?reject:allow) |
same => n(allow),Dial(${UserA_DeskPhone}) |
same => n,Hangup() |
same => n(reject),Playback(abandon-all-hope) |
same => n,Hangup() |
In priority 1, we call the GotoIf() application. It tells Asterisk to go to priority label |
reject if the caller ID number matches 8885551212, and otherwise to go to priority |
label allow (we could have simply omitted the label name, causing the GotoIf() to |
fall through).5 If the caller ID number matches, control of the call goes to priority |
label reject, which plays back a subtle hint to the undesired caller. Otherwise, the |
call attempts to dial the recipient on the channel referenced by the UserA_DeskPhone |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.