Default Ruby dial plan line by line.
When getting started, the default Ruby dial plan can be a bit hard to understand and many people don’t really know what to edit inside. This small article is aiming at explaining the basics so you can get started easier.
The first thing to mention is that with My SIP Switch there are 2 (very) different syntaxes for the dial plans. The “old one” is based on Asterisk syntax and using lines like that: “exten => …”. The new one is based on Ruby scripting and his much more powerful. You cannot use both syntaxes in the same dial plan! You need to pick one.
Here is the default Ruby dial plan (as of July 2008 and minus a few comments for clarity):
#Ruby
sys.Log(”call from #{req.Header.From.FromURI.ToString()} to #{req.URI.User}.”)
if sys.In then
# Do your incoming call processing customisations here.
sys.Dial(”#{sys.Username}@local”)
else
# Do your outgoing call processing customisations here.
case req.URI.User
when /^303$/ then sys.Dial(”303@sip.blueface.ie”)
when /^612$/ then sys.Dial(”612@fwd.pulver.com”)
when /^\*1/ then sys.Dial(”${dst:2}@provider1″)
when /^\*2/ then sys.Dial(”${dst:2}@provider2″)
when /^\*3/ then sys.Dial(”${dst:2}@provider3″)
else sys.Dial(”provider1″)
end
end
A Ruby dial plan must start with the line (Never remove that line):
#Ruby
The lines starting by # are comments. They are not interpreted as part of the dial plan. I removed most of them here.
The line:
sys.Log(”call from #{req.Header.From.FromURI.ToString()} to #{req.URI.User}.”)
will “log” all calls into the monitoring page. That allows to keep track of what’s going on and also to debug when you are testing the service.
The syntax is the following: sys.Log(” my log here “)
If you want to insert a variable into a log, you can use #{variable_name} for instance : #{req.URI.User}, as you’ve seen in the example above.
Then, starts the actual dial plan!
Inbound and outbound calls are clearly separated with the following structure:
if sys.In then
# INbound calls here
else
# OUTbound calls here
end
When you receive a call: sys.In will be equal to “True” and only the code between “then” and “else” will be interpreted.
The next line, starting with a #, is not interpreted. It’s a comment.
Incoming calls:
sys.Dial(”#{sys.Username}@local”)
That line, when interpreted, will call the phone which is registered to My SIP Switch.
sys.Username is a variable as is, it must not be changed! It will be equal to your actual username. Don’t do anything like that: sys.JohnSmith ; that would trigger an error.
If you want to receive your incoming calls on another phone, you can edit this line like that:
sys.Dial(”123456@myprovider”)
Where 123456 is the number you want to dial with the provider: myprovider (the provider’s name used here are the ones you set up when adding a new provider, in the 1st field).
That’s it of the incoming calls on this guide.
If you need further help, you can refer to this article:
Inbound Call Management with Ruby Dial Plans
You can also refer to these threads on the forum:
http://www.mysipswitch.com/forum/viewtopic.php?t=139
http://www.mysipswitch.com/forum/viewtopic.php?t=399
Then for the outgoing calls, by default we have:
case req.URI.User
when /^303$/ then sys.Dial(”303@sip.blueface.ie”)
when /^612$/ then sys.Dial(”612@fwd.pulver.com”)
when /^\*1/ then sys.Dial(”${dst:2}@provider1″)
when /^\*2/ then sys.Dial(”${dst:2}@provider2″)
when /^\*3/ then sys.Dial(”${dst:2}@provider3″)
else sys.Dial(”provider1″)
end
case req.URI.User is another test based on “pattern” which include several possibilities: each when represent a different situation if the outgoing call can’t be match to any of those cases then my SIP switch will follow the else rules.
As pattern match tool, we are using Regular Expressions (everything between /^ and / is a regular expression).
when /^303$/ then sys.Dial(”303@sip.blueface.ie”) means that when you dial 303 on your SIP phone. You will hear Blueface speaking clock.
when /^612$/ then sys.Dial(”612@fwd.pulver.com”) means that when you dial 303 on your SIP phone. You will hear FWD speaking clock.
when /^\*1/ then sys.Dial(”${dst:2}@provider1″)
Any number starting by *1 will match this case. Note that the * is a special character so we need a \ before it. ${dst:2} is representing the number you dialed minus the 2 first digits (*1 here).
If you called your provider something different than “provider1″ you’ll need to modify this! It must be one of the names you set as provider. If you don’t do that, when calling, you’ll notice an error message in the monitoring page saying : “Cannot resolve provider1″.
when /^\*2/ then sys.Dial(”${dst:2}@provider2″)
when /^\*3/ then sys.Dial(”${dst:2}@provider3″)
Same thing with *2 and *3.
else sys.Dial(”provider1″) Forward outgoing calls to provider1.
If you dialed a number which doesn’t start by *1, *2 or *3 then this line will be used. That’s the provider by default. Note that there is not ${dst} involved, since here you don’t need to remove any digit but to dial the number as is.
If you wanted to match any UK number and dial them with a specific provider you can do something like that:
when /^0044.*/ then sys.Dial(”0${dst:4}@ukprovider”)
Any number starting by 0044 will match the case, and then we remove the 0044 and replace that by a 0 (to adjust to local dialing). Then the resulting number is called via the provider named: ukprovider.
Then we have the closing tags “end” of the IF and of the CASE. Make sure not to remove them!
Remember that, this is a basic guide so I mentioned only a few possibilities of the software. If you want to go further you can refer to our forum and blog; quite a few examples have been written there.
Guillaume
PS : Thanks Thomas for the draft you made about this article