Mod xml curl Ruby dialplan example

From FreeSWITCH Wiki
Jump to: navigation, search


Warning

Superseded by https://confluence.freeswitch.org/display/FREESWITCH/mod_xml_curl+Ruby+dialplan+example

 


#!/usr/bin/env ruby
#
# 2007-08-08 -- jontow _at_ zenbsd.net
#
# Database schema:
#
# CREATE TABLE dialplan (
#   did varchar(64),
#   dstid int(11)
# );
#
# CREATE TABLE destinations (
#   id int(11),
#   dst varchar(128),
#   desc varchar(256)
# );
#
# Example data:
#
# INSERT INTO dialplan VALUES ('5551212', '1');
# INSERT INTO dialplan VALUES ('5551213', '2');
# INSERT INTO destinations VALUES ('1', 'wanpipe/pri/3/a/NUMBER', 'Legacy PBX');
# INSERT INTO destinations VALUES ('2', 'sofia/switch.example.com/NUMBER@newpbx.example.com', 'New PBX');
#
# Keep in mind, NUMBER will be replaced with the actual extension at runtime.
#

require "cgi"
require "mysql"

DBHOST = "database.example.com"
DBNAME = "freeswitch"
DBUSER = "username"
DBPASS = "password"

def printheader
	puts "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"
	puts "<document type=\"freeswitch/xml\">"
	puts "<section name=\"dialplan\" description=\"Dynamic Routing\">"

	puts "<context name=\"default\">"
end

def printfooter
	puts "</context>"
	puts "</section>"
	puts "</document>"
end

dbh = Mysql.real_connect(DBHOST, DBUSER, DBPASS, DBNAME)

cgi = CGI.new
params = cgi.params

puts "Content-type: text/plain\r\n\r\n"

#
# NOTE: 
#   Change your area code (555) in this line if your provider
#   sends you 10 digits, if 7, comment it out or don't worry.
#
dstnum = params['destination_number'].to_s.sub(/^555/, '')

if dstnum.length != 7 and
   dstnum.length != 10
	exit
end

result = dbh.query("
	SELECT did,dst
	FROM dialplan AS dp
	INNER JOIN destinations AS de ON dp.dstid=de.id
	WHERE dp.did = #{dstnum}
	LIMIT 1")

if result.num_rows != 0
	result.each do |row|
		did = row[0]
		dst = row[1].sub(/NUMBER/, did)
	
		printheader
		puts "<extension name=\"#{did}\">"
		puts "  <condition field=\"destination_number\" expression=\"^(?:[0-9]{3}|)#{did}$\">"
		puts "    <action application=\"bridge\" data=\"#{dst}\" />"
		puts "  </condition>"
		puts "</extension>"
		printfooter
	end
end

exit

See Also