Using OTE’s 11888 to provide fast CLID data for FreePBX/Asterisk

For those using FreePBX (a configuration/administrative interface that manages Asterisk) there is a CallerID Lookup module, published by the FreePBX team. The module allows you to interface with data sources using several methods, one of them being http.

Like many companies, we maintain several systems that can be easily integrated with our VoIP PBX system, but we also receive a number of calls from the public, viz. companies interested in advertising on GEO|ADS, adding their business to AthensBook or ThessBook, or asking for a quote for a project. OTE recently revamped its aging whitepages.gr site and incorporated its content into their ‘umbrella’ 11888 business directory operation. The new site is faster, more beautiful and written in a more professional manner, as far as the markup is concerned, which makes it a perfect source of caller id information for a large number of telephone subscribers in Greece. Around one hour of Ruby scripting using Mechanize, Sinatra and some custom transliteration tables to convert the scraped names to their latinised counterparts (Cisco phones don’t seem to like Greek characters all that much) resulted in the following script:


# encoding: utf-8
require 'rubygems'
require 'sinatra'
require 'mechanize'
require "unicode_utils/downcase"
require "unicode_utils/titlecase"

GreekChars = Array['α','β','γ','δ','ε','ζ','η','θ','ι','κ','λ','μ','ν','ξ','ο','π','ρ','σ','τ','υ','φ','χ','ψ','ω','ς']
LatinChars = Array['a','b','g','d','e','z','i','th','i','k','l','m','n','x','o','p','r','s','t','i','f','ch','ps','o','s']
SpecChars = Array['ο','α','ε']


class XmlParser <; Mechanize::File
	attr_reader :xml
	def initialize(uri = nil, response = nil, body = nil, code = nil)
		@xml = Nokogiri::XML(body)
		super uri, response, body, code
	end
end


def transl(inString)
	outString = ""
	inString = UnicodeUtils.downcase(inString)
	
	specCharFound = nil

	inString.each_char do |ch|
		if (GreekChars.include?(ch))

			if (specCharFound != nil)
				if (specCharFound == "ο")
					if (ch == "υ")
						outString += "u"
						specCharFound = nil
						next
					end
				elsif (specCharFound == "α")
					if (ch == "υ")
						outString += "f"
						specCharFound = nil
						next
					elsif (ch == "ι")
						outString += "e"
						specCharFound = nil
						next
					end
				elsif (specCharFound == "ε")
					if (ch == "υ")
						outString += "f"
						specCharFound = nil
						next
					end
				end

				outString += LatinChars[GreekChars.index(ch)]
				specCharFound = nil

			else 
				if (SpecChars.include?(ch))
					specCharFound = ch
				end
				outString += LatinChars[GreekChars.index(ch)]
			end
		else
			outString += ch
		end	
	end
	UnicodeUtils.titlecase(outString)
end

def searchOTE(phoneNo)
	athensNo = /^(21\d{8})/
	greekNo = /^30(\d*)/

	match = athensNo.match phoneNo

	if (match == nil)
		match = greekNo.match phoneNo
	end


	if (match != nil) 
		pageurl = "http://11888.ote.gr/web/guest/white-pages/search?who=" + match[1]
		
		a = Mechanize.new
		page = a.get(pageurl)

		result = page.parser.xpath("//*[@id="_whitepagessearchportlet_WAR_yellowpagesportlet_fm"]/div[3]/div[3]/div/div[1]/div[1]/h3").to_s

		filterRegEx = /<;h3>;(.*)<;span>;<;\/span>;/mi

		filtResult = filterRegEx.match result

		if (filtResult != nil)
			"[OTE] " + transl(filtResult[1]).gsub(/<;\/?.*?>;/,'').gsub(/\n/,' - ')
		end
	end
end


def searchAll(phoneNumber) 

	content_type 'text/plain'

# ADD OTHER SEARCH SOURCES HERE, SUCH AS YOUR PROPRIETARY CRM SYSTEM ETC. 

	res = searchOTE(phoneNumber)
	"#{res}"
end



get '/' do 

	"#{searchAll(params[:phone])}"
	
end

In our version, we interface with some additional internal systems (viz. we have several lookups performed serially in searchAll), so we only ‘hit’ OTE’s servers when we don’t get a result back from those ‘higher-frequency’ stores. Additional support can be provided, of course, via LDAP for internal corporate directories, ENUM, etc. Clearly this is not the most rubyesque script ever, nor the most elegant one, its transliteration is crude (and arguably incomplete), but it seems to work fine at least for our purposes. Coupled with the caching support that the FreePBX clid module supports, the lookup performance is more than acceptable for ‘frequent’ callers. In our tests, OTE alone returns a result in less than 0.8s on average, which is good enough in almost every scenario; even if it were longer, in corporate PBX environments, IVRs, Queues allow for pretty ‘slow’ lookups anyway.

The code above is provided under the MIT License. This means you can do whatever you want with it, as long as you retain the license block (see below) in every piece of software that derives from or contains it. Obviously, by using this piece of code you accept the license, understand that is not warranted for any purpose, assume all responsibility pertaining its use, are liable for any claim etc. Here’s the requisite legalese:

Copyright (C) 2012 Cosmix.org

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

2 Responses to “Using OTE’s 11888 to provide fast CLID data for FreePBX/Asterisk”

  1. floom says:

    Very nice! Github repo or Gist?

  2. cosmix says:

    Thanks floom! It’s now on github

Have your say.

Write in the language of the post. Comments are meant to encourage on-topic discussion. For general comments, observations, complaints (e.g. about the site), you can use the form found in the Contact page. Make sure you've read the Terms of Use before commenting.

Comments Feed for this post Comments Feed for this entry.