OpenCNAM for Asterisk cidnam lookup

Long ago, in a world much different from today, one could check 411.com, anywho, and even google for Caller ID names. Those days are gone my friends. It’s slowly becoming a pay to play world…

I just played with a solution from OpenCNAM that was pretty freaking simple. There’s a negativeit ain’t free. The cost is nominal… $0.004/lookup. That means that $10 will get you 2500 lookups.

Before you get all crazy on me — yes, there is a free “hobbyist” version. I think it’s worthless, since all the cellphones I tried returned the cidnam value of “currently unavailable for Hobbyist Tier users.”

I digress.

I chose Perl to integrate opencnam into my Asterisk dialplan. Why? Because I like Perl. Yes, I’ve heard of Python, and Lua, and Ruby, and when it comes down to it… I like Perl.

Here’s how I chose to integrate the lookup:

Asterisk Dialplan

exten => _X.,1,Verbose(3,Look an incoming call. Yay.)
exten => _X.,n,AGI(get-opencnam.pl,${CALLERID(num):-10})
exten => _X.,n,Verbose(3,result: ${OPENCNAM})

get-opencnam.pl

#!/usr/bin/perl -w
use strict;
$|=1;
my ($phone, $url, $apikey, $authkey, $result);

while(<STDIN>) {
    chomp;
    last unless length($_);
}

if ($ARGV[0]) {
    $phone = &URLEncode($ARGV[0]);
} else {
    &setvar("OPENCNAM", "No Phone");
    &setvar("CALLERID(name)", "Unknown");
    &printverbose("OPENCNAM: No CALLFROM received.",2);
    exit(0);
}

#Get the cid
$apikey = "APIKEY";
$authkey = "AUTHKEY";
$url = "api.opencnam.com/v2/phone/";

$result = qx(curl -m 2 -s https://$apikey:$authkey@$url+1$phone?format=text);

#or free version would be...

#$result = qx(curl -m 2 -s https://api.opencnam.com/v2/phone/+1$phone);

if ($result) {
    &setvar("OPENCNAM", "$result");
    &setvar("CALLERID(name)", "$result");
    &printverbose("OPENCNAM: $result.",2);
} else {
    &setvar("OPENCNAM", "FAIL");
    &setvar("CALLERID(name)", "Unknown");
    &printverbose("OPENCNAM: Timeout or error",2);
}

sub URLEncode {
   my $theURL = $_[0];
   $theURL =~ s/([W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg;
   return $theURL;
}

sub setvar {
    my ($var, $val) = @_;
    print STDOUT "SET VARIABLE $var "$val" n";
    while(<STDIN>) {
        m/200 result=1/ && last;
    }
    return;
}

sub printverbose {
    my ($var, $val) = @_;
    print STDOUT "VERBOSE "$var" $valn";
    while(<STDIN>) {
        m/200 result=1/ && last;
    }
    return;
}

The beauty is you can choose when to run this… considerations (for you to save money) would be:

  • If cnam already exists, skip the check.
  • Run a local check first… for example, using a local phonebook or db lookup
  • Only running calls from the PSTN
  • etc.

Happy coding.

Fred Posner provides VoIP consulting services through The Palner Group and LOD.com. In 2010, Fred and his wife, Yeni Monroy, opened Bearkery, in Gainesville, Florida. Contact Fred at qxork.com. Even better, make Yeni happy and buy a cookie!

7 Replies to “OpenCNAM for Asterisk cidnam lookup”

  1. Hey Fred,

    I’m the CTO at OpenCNAM, love your post! Just wanted to let you know that we’ve actually got an Asterisk module that’s open source you can use (you can see it on GitHub here: https://github.com/telephonyresearch/asterisk-opencnam).

    Also: our Hobbyist Tier is getting some awesome improvements right now. We currently have slghtly over 100 million CNAM entries available for free (but different companies imposes different rules for caching on us) — so it’s becoming quite a respectable product for causal users 🙂

    Lastly, I’d love to hook you up with some funding for your account! Please shoot me an email: rdegges@opencnam.com

  2. Hi Fred,

    I have integrated with OpenCNAM as well. I found it to be completely easy although I find that I get a lot of responses like “Cell Phone, OH” or “Cell Phone, NY”.

    Have you found a good way to get actual caller names for cell phones?

    @Randall: Perhaps you may know as well since it is at the heart of your business.

    Thanks,
    -Keith

  3. Hey everyone,

    Over the last several months we have enhanced the accuracy, coverage and speed of OpenCNAM. Please let me know if you have any questions or would like to test the Professional Tier. I would be happy to issue a bit of test credit so you can see the results for yourself.

    Best,
    Brent
    brent.james@telo.com

  4. This looks worth trying. For the database inquiry I was wondering if anyone already knows a way to try something like seeing if the incoming call’s number is already in a user’s Google Voice contact database – particularly useful for folks using their Google Voice credentials within Asterisk. For extra credit, if the caller is not already there perhaps prompt with call screening.

    1. Hi Robert,

      Haven’t really looked at this code in a couple years, the version on github was last updated 2 years ago.

      The google contacts sounds like a nightmare as you’d have to download the entire contact list and have credentials for each user. Looks like there’s no way to query google contacts via phone number.

      This being said, a database lookup isn’t a crazy idea and then you can always run a weekly/whenever sync of the database to google contacts.

      1. Hi Fred,

        This is Brent from OpenCNAM by Telo. Since you created your repo, we have launched OpenCNAM V3. There are some really powerful new benefits available through the OpenCNAM Plus service level with V3 that include international support (better than 50% coverage outside of North America) and greatly improved North American coverage (up to 87% coverage including toll-free and the elusive wireless and VoIP numbers that have been gaps in traditional CNAM coverage). New accounts get $0.50 to test with, but anyone requires a little more, just use live chat or the toll-free number and let one of us know.

        It’s still the same simple API, but with a lot more power! Let us know if we can be of any assistance to you. We are huge fans of open source, so if there is any way we can help get the word out or let devs play with our products please let us know!

        Cheers,
        Brent
        Telo

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.