Switch statement / function in dialplan hook, or some cleaner way to reuse code

Is there a better way (less redundant) to write this?
Assuming that instead of just 3 extensions like I have shown, there are actually a lot more:

[macro-dialout-trunk-predial-hook]
exten => s,1,Noop(Entering user defined context macro-dialout-trunk-predial-hook in extensions_custom.conf)
exten => s,n,ExecIF($["${OUT_${DIAL_TRUNK}_SUFFIX}"!=""]?Set(trunk_name=${OUT_${DIAL_TRUNK}_SUFFIX}):Set(trunk_name=${OUT_${DIAL_TRUNK}}))
exten => s,n,Noop(Trunk Name: ${trunk_name})
exten => s,n,GoSubIf($["${trunk_name}"="Trunk1" & "${FROMEXTEN}"="201"]?label_ext201)
exten => s,n,GoSubIf($["${trunk_name}"="Trunk1" & "${FROMEXTEN}"="202"]?label_ext202)
exten => s,n,GoSubIf($["${trunk_name}"="Trunk1" & "${FROMEXTEN}"="203"]?label_ext203)
exten => s,n(label_ext201),GoSub(func-set-sipheader,s,1(X-CUSTOM-HEADER-1,some_value_here))
exten => s,n,GoSub(func-set-sipheader,s,1(X-CUSTOM-HEADER-2,some_value_here))
exten => s,n,MacroExit
exten => s,n(label_ext202),GoSub(func-set-sipheader,s,1(X-CUSTOM-HEADER-1,some_value_here))
exten => s,n,GoSub(func-set-sipheader,s,1(X-CUSTOM-HEADER-2,some_value_here))
exten => s,n,MacroExit
exten => s,n(label_ext203),GoSub(func-set-sipheader,s,1(X-CUSTOM-HEADER-1,some_value_here))
exten => s,n,GoSub(func-set-sipheader,s,1(X-CUSTOM-HEADER-2,some_value_here))
exten => s,n,MacroExit

If there is a field in Advanced settings for extensions that you don’t use, e.g. Account Code, put the two values there, separated by some character that doesn’t appear in the values.

Then, your hook could set the headers from that item, using CUT to choose the proper value.

Beware that if you choose Account Code, that data appears in the CDRs.

That doesn’t really help with less or cleaner code, because I would still have a ton of

exten => s,n,GoSubIf($[“${trunk_name}”=“Trunk1” & “${FROMEXTEN}”=“201”]?label_ext201)
exten => s,n(label_ext201),GoSub(func-set-sipheader,s,1(X-CUSTOM-HEADER-1,some_value_here))
exten => s,n,GoSub(func-set-sipheader,s,1(X-CUSTOM-HEADER-2,some_value_here))
exten => s,n,MacroExit

(One set of those 4 lines for every extension)

But, your method would eliminate hard coding those actual string values in the .conf file, so that is a good idea for that.

Ok, I tested a demo config, by first setting:

[macro-dialout-trunk-predial-hook]
exten => s,1,Set(acctcode=${DB(AMPUSER/${FROMEXTEN}/accountcode)})
exten => s,n,GotoIf($["${acctcode}" = ""]?done)
exten => s,n,GoSub(func-set-sipheader,s,1(X-Foo,${CUT(acctcode,&,1)})
exten => s,n,GoSub(func-set-sipheader,s,1(X-Bar,${CUT(acctcode,&,2)})
exten => s,n(done),MacroExit()

Then, set Account Code for my extension to abc&def .
On an outbound call, the INVITE contained:
X-Foo: abc
X-Bar: def

You should be able to modify this for your requirement.
Please post the final result, as it may help other users.

1 Like

Oh, I see what you mean. The acctcode is already based on extension so there is no need to compare the FROMEXTEN. That looks great. Yes, I will try this and then reply here with the final results.

That works beautifully. The only change I made to your code (other than changing X-Foo and X-Bar to my own header names) was commenting out the GotoIf line, because I want the additional headers included even if they are empty.

Thanks again.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.