F9's website has a page where you can type in a phone number and check their rates for the number, for example:
http://www.future-nine.com/A2BCustomer_ ... 2125551212
My first intention was to get that page with sys.WebGet and retrieve the rate from there, but that was before I discovered that sys.WebGet truncates the result to 1024 bytes which is not enough to reach to the rate. Then I decided to use YQL (Yahoo Query Language) to convert the output into JSON (more compact) and get rid of everything but the destination name and the rate using XPath. Copy the following link and paste it into browser to try corresponding query in YQL console:
http://developer.yahoo.com/yql/console/ ... D'%2F%2Fem'
(Unfortunately, phpBB doesn't accept long URLs)
Select "JSON" and click on "Test"... you'll see the destination ("us and canada") and rate ("$0.01") in console output window. The whole JSON string is less than 650 bytes, so it fits into 1024-byte limitation imposed by sys.WebGet.
Now it was the time to do some coding. Suppose we have dial string in dials, like this:
dials = '0312125551212@F9'
where 03 is Future-nine's dialprefix specifying 'white' rate (00 and 02 - grey rate and 04 - premium rate), 12125551212 - the number you wish to dial and finally, @F9 specifies SIP provider's name. Here is the code:
Code: Select all
F9_key = 'insert key value here'
if dials =~ /^(00|02|03|04)(.*)@F9/
sys.ExtendScriptTimeout(10)
plan = { '00' => 'grey', '02' => 'grey', '03' => 'white', '04' => 'premium' }[$1]; num = $2
url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'http%3A%2F%2Fwww.future-nine.com%2FA2BCustomer_UI%2Frates.php%3Fwhichrate%3D#{plan}%26called%3D#{num}%26key%3D#{F9_key}'%20and%20xpath%3D'%2F%2Fem'&format=json"
js = sys.WebGet(url,6).to_s
if js =~ /"em":\["(.*)","(.*)"\]/
sys.Log("Future-nine '#{plan}' rate for #$1 is #$2")
sys.Respond(100,"Rate is #$2")
end
end
sys.Dial(dials)
Seeing call rates on the screen is good enough for most uses, but I decided to make one step further and have my dialplan report of changing rates by e-mail. Basic idea was to save current rate in a database key named like:
F9_Rate_USANDCANADA
and send e-mail to myself whenever current rate is different from the one in
database. Resulting code:
Code: Select all
F9_key = 'insert key value here'
if dials =~ /^(00|02|03|04)(.*)@F9/
sys.ExtendScriptTimeout(10)
plan = { '00' => 'grey', '02' => 'grey', '03' => 'white', '04' => 'premium' }[$1]; num = $2
url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'http%3A%2F%2Fwww.future-nine.com%2FA2BCustomer_UI%2Frates.php%3Fwhichrate%3D#{plan}%26called%3D#{num}%26key%3D#{F9_key}'%20and%20xpath%3D'%2F%2Fem'&format=json"
js = sys.WebGet(url,6).to_s
if js =~ /"em":\["(.*)","(.*)"\]/
dest = $1; rate = $2 # save for the future
sys.Log("Future-nine '#{plan}' rate for #$1 is #$2")
sys.Respond(100,"Rate is #$2")
key = 'F9_Rate_' + dest.upcase.gsub(/[^A-Z0-9]/,'')
if (oldrate = sys.DBRead(key).to_s).empty?
sys.DBWrite(key,rate)
sys.Email('myemail@addr.com',"F9 '#{plan}' rate for #{dest} is #{rate}","Called number: #{num}")
else
if oldrate != rate
sys.Email('myemail@addr.com',"F9 '#{plan}' rate for #{dest} changed #{oldrate} => #{rate}", 'As %subj')
sys.DBWrite(key,rate)
end
end
end
end
sys.Dial(dials)