Dial Plan Variables
Along with the functions a Ruby dial plan can call, there are
values it can read as properties of the sys object.
- In: true if the call is an incoming one, received for one of your SIP accounts from anywhere else.
- Out: true if the call is an outgoing one, placed by one of your SIP accounts after it authenticated.
- DialPlanName: the name of the dial plan that is running.
- Username: the username of the SIP account whose call is running the dial plan: the account that placed an outgoing call, or the account an incoming call is for. On the previous SIPSorcery this was the name you signed in to the web portal with.
sys.Log("The currently executing dial plan name is #{sys.DialPlanName}.")
sys.Log("This call is for the SIP account #{sys.Username}.")
The previous SIPSorcery also had sys.Trace, which turned an emailed trace of the dial plan on and off,
and sys.LastDialled, which held the result of the last sys.Dial. Every call's trace is now
recorded in Call History, so sys.Trace = true is accepted and does
nothing, and sys.LastDialled is no longer available.
The call request
As well as the sys properties, the SIP request that started the call is available as
req. Everything about the request can be read through it: the SIP URI, the SIP headers and the
body. It can be read but not changed.
sys.Log("Call request received with SIP URI #{req.URI.ToString()}.")
sys.Log("The number dialled is #{req.URI.User}.")
sys.Log("The From header on the call request is #{req.Header.From.ToString()}.")
sys.Log("The caller's address is #{req.Header.From.FromURI.ToAOR()}.")
sys.Log("The Call-ID is #{req.Header.CallId}.")
A custom SIP header on the request can be read by name. A header that isn't there reads as nil.
req.Header.GetUnknownHeaderValue("X-Account-Code"), the spelling some older dial plans use, works too.
code = req.Header.get_unknown_header_value("X-Account-Code")
sys.Log("The account code was #{code}.")
The code sample below loads all the custom SIP headers from the request into a Ruby hash. Thanks to bobpaul for the original sample.
# Load the custom headers from a SIP request into a Ruby hash.
custHeader = {}
req.Header.UnknownHeaders.each do |header|
_, name, value = header.match(/(.*): (.*)/).to_a
custHeader[name] = value
end
account_code = custHeader["X-Account-Code"]
sys.Log("The account code was #{account_code}.")