Howto: Show Caller name from FOP2 phonebook on SIP phones

I have added to my Asterisk installation a simple dialplan macro that allows me to show caller name (and company) for any incoming call that matches an entry in the FOP2 phonebook on my SIP (but also ISDN and analog, if the proper caller id protocols are configured) extensions.

This simple macro works with FOP2 2.26, Asterisk 1.6, and requires MYSQL support in Asterisk. It reads CALLERID(number) variable from Asterisk and queries the FOP2 phonebook for a similar (last 8 digits have to match) number in both "phone1" and "phone2" fields, and if it finds one it sets the Asterisk variable CALLERID(name) to a concatenation of the "name", "surname" and "company" fields from the FOP2 phonebook. That variable can then be used in further call processing, and anyway it ends up being sent to the called extension by the Dial command.

I have tested it on Debian 6 (the old stable) and requires the "asterisk-mysql" Debian package.

First, add this macro to your dialplan (extensions.conf).
You have to modify the Connect statement on the first line (db_user, db_password and db_name have to be set to the proper username, password, and database name)
exten => s,1,MYSQL(Connect connid localhost db_user db_password db_name)
exten => s,n,MYSQL(Query resultid ${connid} SELECT CONCAT(firstname,' ',lastname,' ',company) AS name FROM visual_phonebook WHERE phone1 LIKE CONCAT('%',SUBSTRING('${CALLERID(num)}',-8)) OR phone2 LIKE CONCAT('%',SUBSTRING('${CALLERID(num)}',-8)) ORDER BY LENGTH(CONCAT(phone1,phone2)) LIMIT 1 )
exten => s,n,MYSQL(Fetch fetchid ${resultid} CallerIDName)
exten => s,n,MYSQL(Clear ${resultid})
exten => s,n,MYSQL(Disconnect ${connid})
exten => s,n,Set(CALLERID(name)=${CallerIDName})

Then, when processing incoming calls, you have to call the macro before doing any internal Dial command. A very simple call processing example is like this:
exten => 5556661234,1,Macro(Fop2CidLookup)
exten => 5556661234,n,Dial(SIP/51,40)

This calls the macro and then dials a SIP extension. The variables set in the macro (specifically the CALLERID(name) variable) is passed to the Dial command autmatically and the phone shows both the number and the name from the FOP2 phonebook. If there is no match for the number, the name is an empty string.


  • not sure if FOP2 can display the caller ID of NAME, anybody can confirm ?

Sign In or Register to comment.