5.3.8. Includes

Asterisk has a feature that enables us to use the extensions from one context within another context via the include directive. This is used to control access to different sections of the dialplan. We'll use the include functionality to allow users in our [employees] context the ability to make outbound phone calls. But first, let's cover the syntax.

The include statement takes the following form, where context is the name of the remote context we want to include in the current context:

    include => context

When we include other contexts within our current context, we have to be mindful of the order in which we including them. Asterisk will first try to match the dialed extension in the current context. If unsuccessful, it will then try the first included context (including any contexts included in the included context), and then continue to the other included contexts in the order in which they were included.

As it sits, our current dialplan has two contexts for outbound calls, but there's no way for people in the [employees] context to use them. Let's remedy that by including the two outbound contexts in the [employees] context, like this:

    [globals]
    JOHN=Zap/1
    JANE=SIP/Jane
    OUTBOUNDTRUNK=Zap/4

    [incoming]
    exten => 123,1,Answer()
    exten => 123,n,Background(enter-ext-of-person)
    exten => 123,n,WaitExten()

    exten => 1,1,Dial(${JOHN},10)
    exten => 1,n,Playback(vm-nobodyavail)
    exten => 1,n,Hangup()

    exten => 2,1,Dial(${JANE},10)
    exten => 2,n,Playback(vm-nobodyavail)
    exten => 2,n,Hangup()

    exten => i,1,Playback(pbx-invalid)
    exten => i,n,Goto(incoming,123,1)

    exten => t,1,Playback(vm-goodbye)
    exten => t,n,Hangup()

    [employees]
    include => outbound-local
    include => outbound-long-distance

    exten => 101,1,Dial(${JOHN})
    exten => john,1,Dial(${JOHN})
    exten => 102,1,Dial(${JANE})
    exten => jane,1,Dial(${JANE})

    [outbound-local]
    exten => _9NXXXXXX,1,Dial(${OUTBOUNDTRUNK}/${EXTEN:1})
    exten => _9NXXXXXX,n,Congestion()
    exten => _9NXXXXXX,n,Hangup()

    exten => 911,1,Dial(${OUTBOUNDTRUNK}/911)
    exten => 9911,1,Dial(${OUTBOUNDTRUNK}/911)

    [outbound-long-distance]
    exten => _91NXXNXXXXXX,1,Dial(${OUTBOUNDTRUNK}/${EXTEN:1})
    exten => _91NXXNXXXXXX,n,Playtones(congestion)
    exten => _91NXXNXXXXXX,n,Hangup()

These two include statements make it possible for callers in the [employees] context to make outbound calls. We should also note that for security's sake you should always make sure that your [inbound] context never allows outbound dialing. (If by chance it did, people could dial into your system, and then make outbound toll calls that would be charged to you!)