Call Us Today! 877.742.2583




Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Expand
titleClick here to expand ToC
Panel
borderColorlightgray
bgColor#f7f7f7
borderWidth1
borderStyledotted

Table of Contents
maxLevel3
indent1em
excludeAbout
stylenone
excludeAbout
printablefalse

Usage

Code Block
languagexml
<action application="cidlookup" data="$1"/>

...

mod_cidlookup only requires the modules that support your selected lookup methods. If any of the supporting modules are missing (mod_curl, local database, mod_memcache) that part of the functionality will be disabled. memcache support requires the mod_memcache module to be loaded.

Installing

...

Tell FreeSWITCH to compile in this module by editing modules.conf in /usr/src/freeswitch/trunk and uncomment the following line:

Code Block
languagexml
themeEmacs
languagexml
titlemodules.conf
applications/mod_cidlookup

Now recompile FreeSWITCH:

Code Block
languagebash
themeEmacs
languagebash
titlelinux command line
make ; make install

Now tell FreeSWITCH to load cidlookup and supporting modules by adding to or uncommenting their entries in modules.conf.xml in $FS_ROOT/conf/autoload_configs:

Code Block
languagexml
themeEmacs
languagexml
titlemodules.conf.xml
<load module="mod_memcache"/>
<load module="mod_cidlookup"/>

...

Sample configuration file

Code Block
languagesql
themeEmacs
languagesql
titleSample cidlookup configuration
collapsetrue
<configuration name="cidlookup.conf" description="cidlookup Configuration">
  <settings>
    <param name="url" value="https://api.opencnam.com/v2/phone/${caller_id_number}?format=pbx&account_sid=ACCOUNTSID&auth_token=AUTHTOKEN"/>
    <param name="cache" value="false"/>

    <param name="odbc-dsn" value="phone:phone:phone"/>
    <param name="sql" value="
SELECT name||' ('||type||')' AS name
  FROM phonebook p JOIN numbers n ON p.id = n.phonebook_id
  WHERE n.number='${caller_id_number}'
  LIMIT 1
    "/>
    <param name="citystate-sql" value="
    SELECT ratecenter||' '||state as name
      FROM npa_nxx_company_ocn
      WHERE npa = ${caller_id_number:1:3} AND nxx = ${caller_id_number:4:3}
      LIMIT 1
      "/>
  </settings>
</configuration>
Code Block
languagexml
themeEmacs
languagexml
titleTrivial sample configuration
collapsetrue
 <configuration name="cidlookup.conf" description="cidlookup Configuration">
  <settings>
    <param name="url" value="https://api.opencnam.com/v2/phone/+${caller_id_number}"/>
    <param name="cache" value="false"/>
  </settings>
</configuration>

Sample SQL Schema - PostgresSQL (not sqlite)

Code Block
languagesql
themeEmacs
languagesql
titleCreate sample SQL schema
create table phonebook(id serial PRIMARY KEY, name text '', notes text default 'h'); create table p_numbers(id serial PRIMARY KEY, phonebook_id integer, number text default '', type text default 'h'); create unique index number on p_numbers (number); alter table p_numbers add constraint numbers_phonebook_id_fkey FOREIGN KEY (phonebook_id) REFERENCES phonebook(id) ON DELETE CASCADE;
Code Block
languagesql
themeEclipse
languagesql
titleSample schema for PostgreSQL
collapsetrue
phone=> \d phonebook
                           Table "fs.phonebook"
 Column |  Type   |                       Modifiers
--------+---------+--------------------------------------------------------
 id     | integer | not null default nextval('phonebook_id_seq'::regclass)
 name   | text    | not null
 notes  | text    | not null default ''::text
Indexes:
    "phonebook_pkey" PRIMARY KEY, btree (id)

phone=> \d p_numbers
                              Table "fs.p_numbers"
    Column    |  Type   |                       Modifiers                       
--------------+---------+--------------------------------------------------------
 id           | integer | not null default nextval('p_numbers_id_seq'::regclass)
 phonebook_id | integer |
 number       | text    | not null default ''::text
 type         | text    | not null default 'h'::text
Indexes:
    "p_numbers_pkey" PRIMARY KEY, btree (id)
    "i_numbers" btree (number)
Foreign-key constraints:
    "numbers_phonebook_id_fkey" FOREIGN KEY (phonebook_id) REFERENCES phonebook(id) ON DELETE CASCADE

...

To verify that mod_cidlookup is functioning correctly and using the options from your config file, type this in fs_cli:

Code Block
languagexml
themeEmacs
languagexml
titlefs_cli
cidlookup status

+OK
 url: https://api.opencnam.com/v2/phone/${caller_id_number}?format=pbx
 cache: false
 cache-expire: 86400
 odbc-dsn: phone
 sql:  SELECT name||' ('||type||')' AS name FROM phonebook p JOIN numbers n ON p.id = n.phonebook_id WHERE n.number='${caller_id_number}' LIMIT 1
 ODBC Compiled: true

To test a sample lookup, invoke cidlookup with a properly formatted telephone number:

Code Block
languagexml
themeEmacs
languagexml
titlefs_cli
cidlookup 17035911635

HAYNES,FRANK

...

The cidlookup application sets the channel variable caller_id_name if the lookup succeeds. The easiest way to use it is to put the following block towards the top of your dialplan/public.xml

Code Block
languagexml
themeEmacs
languagexml
titleDialplan example
collapsetrue
    <extension name="cid_number_cleanup" continue="true">
      <condition field="caller_id_number" expression="^(?:\+)(\d+)$">
        <action application="set" data="effective_caller_id_number=$1" inline="true"/>
      </condition>
    </extension>

    <extension name="cid_name_cleanup" continue="true">
      <condition field="caller_id_name" expression="^(?:\+)(\d+)$">
        <action application="set" data="effective_caller_id_name=$1" inline="true"/>
      </condition>
    </extension>

    <extension name="cid_lookup-country_code_1" continue="true">
      <condition field="${module_exists(mod_cidlookup)}" expression="true"/>
      <condition field="caller_id_name" expression="^(?:\+)(\d+)$|^$"/>
      <condition field="caller_id_number" expression="^(?:\+1|1)?([2-9]\d\d[2-9]\d{6})$">
        <action application="cidlookup" data="$1"/>
      </condition>
    </extension>

...

The documentation isn't quite clear that what we're really trying to set here is effective_caller_id_name (vs caller_id_name). For an inbound route it would look something like this:

Code Block
languagexml
themeEmacs
languagexml
collapsetrue
<extension name="Demo Inbound" >
   <condition field="context" expression="public"/>
   <condition field="destination_number" expression="12024561000">
       <action application="set" data="call_direction=inbound"/>
       <action application="answer"/>
       <action application="sleep" data="1000"/>
       <action application="set" data="caller_id_name=${cidlookup(${caller_id_number})}"/>
       <action application="set" data="effective_caller_id_name=${caller_id_name}"/>
       <action application="ivr" data="Your_IVR"/>
   </condition>
</extension>

...

One way to load this data into PostgreSQL is with the following DDL

Code Block
languagesql
themeEmacs
languagesql
titlePostgreSQL DDL
collapsetrue
CREATE TABLE npa_nxx_company_ocn (
    npa smallint NOT NULL,
    nxx smallint NOT NULL,
    company_type text,
    ocn text,
    company_name text,
    lata integer,
    ratecenter text,
    state text
);
CREATE UNIQUE INDEX npanxx_idx ON npa_nxx_company_ocn USING btree (npa, nxx);

You can then load the data using psql with

Code Block
languagesql
themeEmacs
languagesql
titlepsql
collapsetrue
phone=> \copy npa_nxx_company_ocn from 'npa-nxx-companytype-ocn.csv' with csv
phone=> select count(*) from npa_nxx_company_ocn ;
 count
--------
 163900
(1 row)

...

Some phones, including Polycom and Snom, support setting the callee's name once the call is complete. This allows the caller to see the name of the person being called as well as the number. Using cidlookup the name could come out of your corporate directory and if that doesn't work will be queried against a CNAM lookup service. Assuming you've already normalized your number to E.164 (without the leading +), just add the following to your dialplan prior to the bridge:

Code Block
languagexml
themeEmacs
languagexml
titleCallee Dialplan XML
<action application="export" data="callee_id_name=${cidlookup($1)}" />

...

If you need to query more than 60 requests per hour, or prefer to have more accurate Caller ID information (with real-time CNAM queries), you can create an OpenCNAM account on their website, deposit funds into your account, then update your cidlookup.conf.xml configuration file appropriately. Once you've created an OpenCNAM account, you'll notice that on your dashboard page you have two account tokens at the top of your page: an Account SID and Auth Token. To make FreeSWITCH use OpenCNAM's Professional tier and thereby perform only real-time CNAM queries, modify your cidlookup.conf.xml file's URL attribute thus:

Code Block
languagexml
themeEmacs
languagexml
titleOpenCNAM Professional config
collapsetrue
<configuration name="cidlookup.conf" description="cidlookup Configuration">
 <settings>
  <param name="url" value="https://api.opencnam.com/v2/phone/${caller_id_number}?format=pbx&account_sid=YOUR_ACCOUNT_SID&auth_token=YOUR_AUTH_TOKEN"/>
  <param name="cache" value="false"/>
 </settings>
</configuration>

...

Prerequisite is the php5-curl library that must be installed on your server. Adjust this as required.

Code Block
languagephp
themeEmacs
languagephp
titlePHP web server config
collapsetrue
<?php

/*********************************************************
CONFIG
*********************************************************/

$apiKey = '******';



/*******************************************************
BELOW BE DRAGONS
*******************************************************/

$cid = $_GET['cid'];
$check=strlen($cid);

// Do a few checks for the input... e.g. only lookup swiss numbers
if($check == 10) {
        // 10 digits as required
} elseif($check == 11) {
        $sub1 = substr($cid, 0 ,2);
        $sub2 = substr($cid, 2);
        // check if swiss country code is added
        if($sub1 == '41') {
                $cid = '0' . $sub2;
        }
} elseif ($check == 9) {
        // check if leading 0 is missing
        $cid = '0' . $cid;
}

// Run query
$q = 'http://tel.search.ch/api/?key=' . $apiKey . '&pos=1&maxnum=1&was=' . $cid;


$ch=curl_init();
curl_setopt($ch,        CURLOPT_URL,            $q);
curl_setopt($ch,        CURLOPT_HEADER,         0);
curl_setopt ($ch,       CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);
curl_close($ch);

// As http://tel.search.ch provides an xml result, you have to filter out the number
$result = explode('<tel:name>', $result);
$result = explode('</tel:name>', $result[1]);
$result = $result[0];

if($result == '') {
        // If there is no result, then assign the cid as result. If you do more checks, then you may want to comment the following line out
         $result = $cid;
} else {
         $result = $result;
}

$result = utf8_encode($result);

echo $result;

?>

...

Save that script somewhere on your server and enhance the freeswitch/conf/autoload_configs/cidlookup.conf.xml with this

Code Block
languagexml
themeFadeToGrey
languagexml
titlecidlookup.conf.xml
<param name="url" value="http://<server address>/cid/index.php?cid=${caller_id_number}"/>

...

To do this, first create the following Lua script:

Code Block
languagephp
themeEmacs
languagephp
titlecid_bg.lua
collapsetrue
--[[
  Lookup the CID name, and set the effective_caller_id_name variable.
  This script will run in the background, so we need to set the variable via the UUID.
]]
api = freeswitch.API();
uuid = argv[1];
if not uuid or uuid == "" then return end;
number = api:executeString("uuid_getvar " .. uuid .. " caller_id_number");
name = api:executeString("cidlookup " .. number);
api:executeString("uuid_setvar " .. uuid .. " effective_caller_id_name " .. name);

Next, call the script from the "public" dialplan as follows:

Code Block
languagexml
themeEmacs
languagexml
titleDialplan context public
collapsetrue
  <extension name="incoming main">
    <condition field="destination_number" expression="^(12025551212|18035551212)$" require-nested="false">
      <condition field="${cond(${caller_id_name} == ${caller_id_number} ?true:false)" expression="^true$">
        <action application="set" data="api_result=${luarun cid_bg.lua ${uuid}}"/>
      </condition>
      <action application="answer"/>
      <action application="sleep" data="1000"/>
      <action application="ivr" data="main_ivr"/>
      <action application="transfer" data="operator_vmail XML default"/>
    </condition>
  </extension>