Asterisk Dialplan Best Practices Syntax

Home » Asterisk Users » Asterisk Dialplan Best Practices Syntax
Asterisk Users 4 Comments

Hi,

I’ve two yocto questions about the syntax of dialplan:

1. What’s the “official” notation of each line: “=>” or “=” ? In the wiki of Asterisk, I see very often “=>”, however, what’s the reason for both syntaxes authorized ? Historical ?

2. To write info in logs/console, you have two commands: NoOp and Verbose. Verbose seems to be better, because you can define a log level. Are you agree or another command fits better for logs ?

Thanks for your responses.

Regards.

4 thoughts on - Asterisk Dialplan Best Practices Syntax

  • I’m not ‘official,’ but I have a strong preference for just ‘=.’ Using
    ‘=>’ seems clunky, ugly, and unnecessary.

    This is kind of a pet peeve of mine. Why use the misguided ‘side effect’
    of an application when there is a specific, ‘more featured,’ application for that purpose? A ‘noop’ is a contraction of ‘no operation’ meaning it should do nothing but act as a placeholder.

    Two other areas of ‘best practices’ I’m a strong believer in are:
    alphabetize wherever possible, and use white space to improve readability.

    For example, here’s a ‘sanitized’ sip.conf snippet from a popular provider’s web site:

    [xxx-inbound]
    type=friend dtmfmode=auto host=xxx.yyy.zzz context=inbound

    username=xxx secret=yyy

    allow=all insecure=port,invite canreinvite=no

    [xxx-outbound]
    type=friend dtmfmode=auto host=xxx.yyy.zzz username=xxx fromuser=xxx secret=xxx trustrpid=yes sendrpid=yes allow=all canreinvite=no

    Pretty ugly and difficult to read. With a little whitespace and alphabetizing we get:

    [xxx-inbound]
    allow = all
    canreinvite = no
    context = inbound
    dtmfmode = auto
    host = xxx.yyy.zzz
    insecure = port,invite
    secret = yyy
    type = friend
    username = xxx

    [xxx-outbound]
    allow = all
    canreinvite = no
    dtmfmode = auto
    fromuser = xxx
    host = xxx.yyy.zzz
    secret = xxx
    sendrpid = yes
    trustrpid = yes
    type = friend
    username = xxx

    Now, the major sections are easy to ‘visually delineated.’ Finding the
    ‘secret’ is much easier now. Comparing a ‘working’ extension with a
    ‘broken’ extension will be much easier as well.

    I use the same formatting in the dialplan. This snippet is from extensions.conf.sample:

    [outbound-freenum2]
    exten => _X!,1,Verbose(2,Performing ISN lookup for ${EXTEN})
    same => n,Set(SUFFIX=${CUT(EXTEN,*,2-)})
    same => n,GotoIf($[“${FILTER(0-9,${SUFFIX})}” != “${SUFFIX}”]?fn-CONGESTION,1)
    same => n,Set(TIMEOUT(absolute)800)
    same => n,Set(isnresult=${ENUMLOOKUP(${EXTEN},sip,,1,freenum.org)})
    same => n,GotoIf($[“${isnresult}” != “”]?from)
    same => n,Set(DIALSTATUS=CONGESTION)
    same => n,Goto(fn-CONGESTION,1)
    same => n(from),Set(__SIPFROMUSER=${CALLERID(num)})
    same => n,GotoIf($[“${GLOBAL(FREENUMDOMAIN)}” = “”]?dial)
    same => n,Set(__SIPFROMDOMAIN=${GLOBAL(FREENUMDOMAIN)})
    same => n(dial),Dial(SIP/${isnresult},40)
    same => n,Goto(fn-${DIALSTATUS},1)

    exten => fn-BUSY,1,Busy()

    exten => _f[n]-.,1,NoOp(ISN: ${DIALSTATUS})
    same => n,Congestion()

    With a little whitespace, this becomes much more readable:

    [outbound-freenum2]
    exten = _X!,1, Verbose(2,Performing ISN lookup for ${EXTEN})
    same = n, Set(SUFFIX=${CUT(EXTEN,*,2-)})
    same = n, GotoIf($[“${FILTER(0-9,${SUFFIX})}” != “${SUFFIX}”]?fn-CONGESTION,1)
    same = n, Set(TIMEOUT(absolute)800)
    same = n, Set(isnresult=${ENUMLOOKUP(${EXTEN},sip,,1,freenum.org)})
    same = n, GotoIf($[“${isnresult}” != “”]?from)
    same = n, Set(DIALSTATUS=CONGESTION)
    same = n, Goto(fn-CONGESTION,1)
    same = n(from), Set(__SIPFROMUSER=${CALLERID(num)})
    same = n, GotoIf($[“${GLOBAL(FREENUMDOMAIN)}” = “”]?dial)
    same = n, Set(__SIPFROMDOMAIN=${GLOBAL(FREENUMDOMAIN)})
    same = n(dial), Dial(SIP/${isnresult},40)
    same = n, Goto(fn-${DIALSTATUS},1)

    exten = fn-BUSY,1, Busy()

    exten = _f[n]-.,1, NoOp(ISN: ${DIALSTATUS})
    same = n, Congestion()

    Compare the effort to find where the ‘dial’ is in the ‘before’ and
    ‘after.’

  • 2015-06-26 17:11 GMT+02:00 Steve Edwards :

    It’s interesting because from my point of view, I prefer to use ‘=>’, because, to me, ‘=’ is for config files. The dialplan is a programming language, not a config file. If you like buzzwords, it’s a Domain Specific Language.

    However, it’s completely a religious point of view, Asterisk will process exactly the same dialplan with ‘=’ or with ‘=>’. I was interested in by the story that will explain to me why Asterisk support both syntaxes, if somebody remembers.

    Thank you Steve for your answers in point 2., any tips to improve readability of dialplan is interesting to me.

    2. To write info in logs/console, you have two commands: NoOp and Verbose.

  • The only language I’m familiar with (I’m primarily a ‘c’ guy) that uses
    ‘=>’ is PHP. In PHP, ‘=>’ is used with associative arrays.

    I tend to think of a dialplan as a group of one dimensional linear arrays with the name of the array being the context concatenated with the exten so for me, a simple assignment makes sense.

  • Hey;
    Don’t forget Perl. I’m not sure what everyone else calls it, but most Perl programmers call it a “fat comma”.

    From Chromatic’s “Modern Perl” book: The only language I’m familiar with (I’m primarily a ‘c’ guy) that uses ‘=>’
    is PHP. In PHP, ‘=>’ is used with associative arrays.

    I tend to think of a dialplan as a group of one dimensional linear arrays with the name of the array being the context concatenated with the exten so for me, a simple assignment makes sense.