How to execute a custom dial plan fragment before sending a call to a trunk (for playing an announcement, etc.)
In a FreePBX forum post, Alex Edwards wanted to accomplish the following:
"I'd like to warn my FreePBX users when they're dialling an expensive international fixed-line, or worse, international mobile. (This is from the UK).
"I've put a long list of international codes as a route, with a route password - which is a great start.
"However, ideally I'd like to play an explanatory message too - possibly a Misc Destination / Application / Announcement? ....."
In other words, what he wanted to do was execute a bit of custom code (a dial plan fragment) prior to sending the call to a trunk. One way to do this is to use a CUSTOM trunk. Here's how Alex did it (slightly revised for clarity):
First, you need to add your custom dial plan fragment to extensions_custom.conf (if you have the Dialplan Injection module installed, you could also put this code fragment there, but for the moment we'll assume you're using extensions_custom.conf). Alex added the following to the top of that file (slightly modified for clarity):
[mobile-warn-custom]
exten => _X.,1,Answer
exten => _X.,n,Wait(1)
exten => _X.,n(begin),Noop(Playing announcement Expensive-Call-Warning)
exten => _X.,n,Playback(custom/International-Mobile-Warning,noanswer)
exten => _X.,n,Wait(1)
exten => _X.,n,Dial(mISDN/g:out/${EXTEN},300)Note that the final line gives the actual destination of the call, and will have to be tweaked for your own particular situation. Also, the Wait statements are probably optional, or could be made longer than one second if desired. The recording specified in the fourth line (International-Mobile-Warning) is a custom recording that tells the caller that the call will cost around £1 a minute, so look for a cheaper alternative! You will need to create the recording you want to use, if none of the standard Asterisk recordings will work in your situation.
Second, create a new CUSTOM trunk. For the Custom Dial String, use the following:
Local/$OUTNUM$@mobile-warn-custom
If you used something other than "mobile-warn-custom" to identify your custom dial plan then be sure to use that here as well. While you are creating your CUSTOM trunk, mouse over the words "Custom Dial String" and it will show you some suggested formats that might give you an idea of how the "Dial" argument in the final line of your custom dial plan should read (in case you are having trouble figuring that out).
Finally, create your route, and make sure it has higher priority than all the other routes that might otherwise handle calls to the same numbers. Don't forget to click the orange bar when finished.
An alternate method was suggested by user stonet. In /etc/asterisk/extensions.conf there is a context called macro-dialout-trunk-predial-hook which, according to comments in extensions.conf, is "intentially left blank so it may be safely overwritten for any custom requirements that an installation may have" and that any such "customizations to this dialplan should be made in extensions_custom.conf." What that means is you can create a [macro-dialout-trunk-predial-hook] context in /etc/asterisk/extensions_custom.conf and it will override the (intentionally blank) context in extensions.conf. The macro is called whenever a call goes out over any trunk, therefore if you can determine which trunk has been called you can execute your custome dial plan fragment.
That's the approach that stonet took. He wanted to play the message when calls went out on any of three trunks, so rather than giving the trunks names he gave them numbers. The SIP trunks on which he wanted to play the message he named 7000, 7001, 7002 and 7003, and then he used this code, which should be placed in /etc/asterisk/extensions_custom.conf:
[macro-dialout-trunk-predial-hook]
exten => s,1,NoOp(Trunk ${OUT_${DIAL_TRUNK}} selected)
exten => s,n,Gotoif($["${OUT_${DIAL_TRUNK}:0:7}" != "SIP/700"]?skip)
exten => s,n,NoOp(Playing Progress Announcement)
exten => s,n,Playback(pls-hold-while-try,noanswer)
exten => s,n(skip),MacroExit()
Note that the full trunk name is printed on the CLI by the first line, so you can see what needs to be matched in the Gotoif statement. It usually consists of the technology type, a forward slash, and the trunk name, although that can vary (especially in the case of a Custom trunk). When this method is used, you don't need to create any custom trunks, and you have more flexibility in the variables you can use for conditional jumps, but you are responsible for making sure that you correctly construct your conditional Gotos so they do what you need to do. This also assumes that the [macro-dialout-trunk-predial-hook] context will not be used by any other part of FreePBX. If you use this method, make sure that the last statement in the macro is MacroExit(), and it would probably be a good idea to read some documentation on Asterisk macros before you begin.
Keep in mind that the Gotoif statement can contain multiple conditions. For example, let's say that the two SIP trunks that we wanted to play an announcement on were named "voipcompany" and "foobar" - the Gotoif line could then look like this:
exten => s,n,GotoIf($[$["${OUT_${DIAL_TRUNK}" != "SIP/voipcompany"] & $["${OUT_${DIAL_TRUNK}}" != "SIP/foobar"]]?skip)
You can the use & (and), | (or), or ! (logical unary complement) characters. The first two are placed between expressions, while the ! is placed in front of an expression (with NO space between the ! and the expression) to give the opposite result of the expression (an expression that evaluates to true becomes false, and vise-versa). Usually it's clearer to just use != in an expression, however. See the page on Asterisk Expressions for more information.
You may want to check the forum thread to see if any additional comments have been posted that may clarify these procedures. The thing we want to illustrate here is that when you need to add a custom dial plan fragment prior to sending a call to an actual trunk, one way to do it is to use a CUSTOM trunk, send that to your custom dial plan, and then make the last line of your custom dial plan send the call to an actual trunk or other destination. The other way to do it is to use the [macro-dialout-trunk-predial-hook] macro call that's already built into FreePBX.
For those who may be trying to duplicate what Alex did, here is the list of numbers that Alex put into the route definition (to play a warning for high cost mobile calls). Note that this may or may not include all caller-pays mobile prefixes, and that the '9|xxxx.' variants at the bottom of the list are only required if you use 9 for an outside line:
00124235.
00124245.
00124255.
0012687.
00147341.
0017672.
00201.
002126.
002162.
002169.
0021891.
002207.
002209.
002215.
002216.
002226.
002233.
002234.
002235.
002236.
002239.
002246.
002250.
002256.
002267.
002289.
002299.
002307.
002308.
002309.
0023223.
0023230.
0023233.
002327.
0023328.
0023480.
0023490.
002389.
0023990.
002402.
002424.
002425.
002426.
0024322.
0024378.
0024381.
0024384.
0024385.
0024386.
0024388.
0024389.
0024390.
0024394.
0024395.
0024396.
0024397.
0024398.
0024399.
002456.
002457.
002485.
002487.
002499.
0025008.
0025191.
002538.
002547.
0025574.
002567.
002577.
002588.
002609.
002613.
0026311.
0026323.
0026391.
002658.
002659.
0026658.
002666.
002677.
0026860.
002693.
00277.
00278.
002917.
0030693.
0030694.
0030697.
0030699.
00316.
003247.
003248.
003249.
00336.
00346.
0035058.
003519.
00352621.
00352628.
00352661.
00352668.
00352691.
00352698.
003538.
003546.
003548.
003556.
0035679.
0035699.
0035796.
0035797.
0035799.
003584.
0035850.
0035948.
0035987.
0035988.
0035989.
003620.
003630.
003670.
003706.
003725.
0037365.
0037368.
0037369.
0037376.
0037378.
0037379.
003749.
0037525.
0037529.
0037533.
0037544.
003774.
003776.
0037866.
0038039.
0038050.
0038063.
0038066.
0038067.
0038068.
0038097.
00381377.
003816.
003826.
003859.
0038761.
0038762.
0038763.
0038765.
0038970.
0038971.
0038975.
0038976.
0038977.
0038978.
0039328.
0039329.
0039333.
0039338.
0039339.
0039340.
0039347.
0039348.
0039349.
0039393.
00407.
004174.
004176.
004177.
004178.
004179.
0042060.
0042072.
0042073.
0042077.
004219.
004237.
0043650.
0043660.
0043664.
0043676.
0043680.
0043681.
0043688.
0043699.
004475.
004477.
004478.
004479.
004530.
004540.
00467.
00474.
00479.
004850.
004851.
004860.
004866.
004869.
004872.
004878.
004879.
004888.
004915.
004916.
004917.
0049700.
005016.
005024.
005025.
005037.
005043.
005048.
005049.
005058.
005063.
005074.
005075.
005076.
005077.
00519.
00521.
00535.
005415.
00557.
00558.
00559.
005698.
005699.
005731.
00584.
005917.
005926.
005938.
005939.
005959.
005978.
0059894.
0059896.
0059898.
0059899.
00601.
00614.
00628.
00639.
006420.
006421.
006424.
006425.
006427.
006428.
006429.
00658.
00659.
00661.
00669.
006707.
006738.
00674555.
0067568.
0067569.
006784.
006785.
006799.
0068577.
007300.
007333.
00770.
007777.
0079.
008170.
008180.
008190.
008210.
008211.
008216.
008217.
008218.
008219.
00849.
008523.
008526.
008528.
008529.
008536.
008559.
0085620.
008613.
008615.
0088016.
0088017.
00880181.
0088019.
008869.
00905.
009192.
009193.
009194.
009197.
009198.
009199.
009230.
009231.
009232.
009233.
009234.
009235.
009236.
009375.
00947.
00959.
009607.
009609.
009613.
0096170.
0096171.
009627.
009639.
009656.
009657.
009659.
0096650.
0096654.
0096655.
0096656.
009677.
00968968.
0097059.
0097150.
0097155.
009725.
009733.
009745.
009746.
0097517.
009769.
0097798.
00989.
009929.
009945.
009957.
009959.
009965.
009989.
9|00124235.
9|00124245.
9|00124255.
9|0012687.
9|00147341.
9|0017672.
9|00201.
9|002126.
9|002162.
9|002169.
9|0021891.
9|002207.
9|002209.
9|002215.
9|002216.
9|002226.
9|002233.
9|002234.
9|002235.
9|002236.
9|002239.
9|002246.
9|002250.
9|002256.
9|002267.
9|002289.
9|002299.
9|002307.
9|002308.
9|002309.
9|0023223.
9|0023230.
9|0023233.
9|002327.
9|0023328.
9|0023480.
9|0023490.
9|002389.
9|0023990.
9|002402.
9|002424.
9|002425.
9|002426.
9|0024322.
9|0024378.
9|0024381.
9|0024384.
9|0024385.
9|0024386.
9|0024388.
9|0024389.
9|0024390.
9|0024394.
9|0024395.
9|0024396.
9|0024397.
9|0024398.
9|0024399.
9|002456.
9|002457.
9|002485.
9|002487.
9|002499.
9|0025008.
9|0025191.
9|002538.
9|002547.
9|0025574.
9|002567.
9|002577.
9|002588.
9|002609.
9|002613.
9|0026311.
9|0026323.
9|0026391.
9|002658.
9|002659.
9|0026658.
9|002666.
9|002677.
9|0026860.
9|002693.
9|00277.
9|00278.
9|002917.
9|0030693.
9|0030694.
9|0030697.
9|0030699.
9|00316.
9|003247.
9|003248.
9|003249.
9|00336.
9|00346.
9|0035058.
9|003519.
9|00352621.
9|00352628.
9|00352661.
9|00352668.
9|00352691.
9|00352698.
9|003538.
9|003546.
9|003548.
9|003556.
9|0035679.
9|0035699.
9|0035796.
9|0035797.
9|0035799.
9|003584.
9|0035850.
9|0035948.
9|0035987.
9|0035988.
9|0035989.
9|003620.
9|003630.
9|003670.
9|003706.
9|003725.
9|0037365.
9|0037368.
9|0037369.
9|0037376.
9|0037378.
9|0037379.
9|003749.
9|0037525.
9|0037529.
9|0037533.
9|0037544.
9|003774.
9|003776.
9|0037866.
9|0038039.
9|0038050.
9|0038063.
9|0038066.
9|0038067.
9|0038068.
9|0038097.
9|00381377.
9|003816.
9|003826.
9|003859.
9|0038761.
9|0038762.
9|0038763.
9|0038765.
9|0038970.
9|0038971.
9|0038975.
9|0038976.
9|0038977.
9|0038978.
9|0039328.
9|0039329.
9|0039333.
9|0039338.
9|0039339.
9|0039340.
9|0039347.
9|0039348.
9|0039349.
9|0039393.
9|00407.
9|004174.
9|004176.
9|004177.
9|004178.
9|004179.
9|0042060.
9|0042072.
9|0042073.
9|0042077.
9|004219.
9|004237.
9|0043650.
9|0043660.
9|0043664.
9|0043676.
9|0043680.
9|0043681.
9|0043688.
9|0043699.
9|004475.
9|004477.
9|004478.
9|004479.
9|004530.
9|004540.
9|00467.
9|00474.
9|00479.
9|004850.
9|004851.
9|004860.
9|004866.
9|004869.
9|004872.
9|004878.
9|004879.
9|004888.
9|004915.
9|004916.
9|004917.
9|0049700.
9|005016.
9|005024.
9|005025.
9|005037.
9|005043.
9|005048.
9|005049.
9|005058.
9|005063.
9|005074.
9|005075.
9|005076.
9|005077.
9|00519.
9|00521.
9|00535.
9|005415.
9|00557.
9|00558.
9|00559.
9|005698.
9|005699.
9|005731.
9|00584.
9|005917.
9|005926.
9|005938.
9|005939.
9|005959.
9|005978.
9|0059894.
9|0059896.
9|0059898.
9|0059899.
9|00601.
9|00614.
9|00628.
9|00639.
9|006420.
9|006421.
9|006424.
9|006425.
9|006427.
9|006428.
9|006429.
9|00658.
9|00659.
9|00661.
9|00669.
9|006707.
9|006738.
9|00674555.
9|0067568.
9|0067569.
9|006784.
9|006785.
9|006799.
9|0068577.
9|007300.
9|007333.
9|00770.
9|007777.
9|0079.
9|008170.
9|008180.
9|008190.
9|008210.
9|008211.
9|008216.
9|008217.
9|008218.
9|008219.
9|00849.
9|008523.
9|008526.
9|008528.
9|008529.
9|008536.
9|008559.
9|0085620.
9|008613.
9|008615.
9|0088016.
9|0088017.
9|00880181.
9|0088019.
9|008869.
9|00905.
9|009192.
9|009193.
9|009194.
9|009197.
9|009198.
9|009199.
9|009230.
9|009231.
9|009232.
9|009233.
9|009234.
9|009235.
9|009236.
9|009375.
9|00947.
9|00959.
9|009607.
9|009609.
9|009613.
9|0096170.
9|0096171.
9|009627.
9|009639.
9|009656.
9|009657.
9|009659.
9|0096650.
9|0096654.
9|0096655.
9|0096656.
9|009677.
9|00968968.
9|0097059.
9|0097150.
9|0097155.
9|009725.
9|009733.
9|009745.
9|009746.
9|0097517.
9|009769.
9|0097798.
9|00989.
9|009929.
9|009945.
9|009957.
9|009959.
9|009965.
9|009989.- Printer-friendly version
- Login or register to post comments


