Search code examples
includeasterisk

How to use asterisk Include statement the right way?


I'm new to asterisk, and I have an issue about using include statement.

I have a context named "app-calltrace-perform" from FreePBX, used when people press *69 to trace their call.

[app-calltrace-perform]
include => app-calltrace-perform-custom
exten => s,1,Answer
...
exten => t,n,Macro(hangupcall,)

The "app-calltrace-perform" written in extensions_additional.conf which will be overwritten when users submit something about dialplan on the Web gui. So I have to write my own context "app-calltrace-perform-custom" on another file called extensions_custom.conf

[app-calltrace-perform-custom]
exten => s,1,Answer()
same => n,VERBOSE("Something here")
same => n,Playback(hello-world)
same => n,Hangup()

Note that extensions_additional.conf and extensions_custom.conf was already included from extensions.conf

Then I do dialplan reload and try again, but dialplan do not play my context at all (no verbose, no play hello-world).

I've found something useful in https://wiki.asterisk.org/wiki/display/AST/Include+Statements+Basics

Asterisk will always try to find a matching extension in the current context first, and only follow the include statement to a new context if there isn't anything that matches in the current context.

So now I dont know how to use my custom context for stuffs like this. Sorry if this a dumb question, but if you have any idea, pleas guide me through.

Edit
This is where app-calltrace-perform called from

[app-calltrace] include => app-calltrace-custom exten => *69,1,Goto(app-calltrace-perform,s,1)

Now i'm using another extension (*12345) defined in context app-calltrace-custom, It works well but is hard coded so cannot modified by the Web Gui

End Edit

Thanks in advance
Loi Dang


Solution

  • Asterisk dialplan matching work this way

    [context1]
    exten => 1,1,Noop(1)
    include =>context2
    include =>context3
    exten => i,1,Noop(invalid extension)
    [context2]
    exten => 1,1,Noop(2)
    exten => 2,1,Noop(3)
    [context3]
    exten => 1,1,Noop(4)
    exten => 2,1,Noop(5)
    exten => _X,1,Noop(other)
    

    Let's say you call context1

    when called 1 will be selected command from context1, becuase it EXIST in context. Same if you use wildcard.

    When called 2 will be selected 2 from context2, first included context When called any other number (for example 3) will be selected "other" in context3(becuase it not presented in context nor in prevous included context)

    If called 12 which not present in any contextes, will be execute "invalid" extension