[RESOLVED]: How to identify the caller

I have to implement some features in Asterisk, which involve some database queries. I need to identify the caller and then to search the database and determine what services he has enabled/disabled.
I’ve tried to get the username from the channel name, but to do this there must be established channel. Another solution is to have separate context for every user and to set the variables there, but this seems ineffective.
I’ve searched in Google and found some recommendations to use CallerID or Account Code, but generally they are meant to be used for different purposes.
Could you recommend me a way to get the caller’s username or some other way to identify it?
Thank you in advance!

the calls are from internal devices or from pstn ? Which type of Asterisk channels do you use ?

Callers are from internal devices. I use mainly SIP (and maybe little IAX) soft or hardphones.

I think a good solution is to use the caller id, the variable ${CALLERID(num)} should give you the caller number and with this you should be able to identify the sip or iax2 device; in the doc subdir of the Asterisk archive there is the file channelvariables.txt, which shows the variables defined, perhaps you’ll find something useful inside.

Hope it helps.


Not a bad idea, but if you host PBX for several different companies for example, the data in caller-id may overlap. I’ve checked the file, but no good.
I have worked out a solution - it’s not so elegant, but works for me. Here is the code:

context internal 
        100 => &int_call(SIP,tsurko,${EXTEN},${CALLER_USERNAME});
        200 => &int_call(SIP,alpha,${EXTEN},${CALLER_USERNAME});

context usr-tsurko 
        _ZXX => {

macro int_call(proto,user,dialed_num,caller_username) 

                case BUSY:

                case CHANUNAVAIL:


Every user is in his own context. I name them usr-USERNAME. In this context I’ll put includes or something like that to the contexts of the features which he is allowed to use. In every context I match the number he dials and set a variable with his username (I still know it there). Then I call a macros to process the call.
In internal there are entries for every user, but I use realtime on my production machine and it’s not a problem.
I’ll appreciate every piece of advice for optimization of the code.
Thank you a lot for your time!

You can use accountcode, very simple and on top of that it identify the account in the CDR.

in real time sip/iax accounts it’s a field like that:
| accountcode | varchar(20) | YES | | NULL | |

And Standard config it’s
AccountCode=YourUser Account

Than, if you use an AGI in your dialplan, you can pull the data like this:

#!/usr/bin/php -q

define("AGIBIN_DIR", "/var/lib/asterisk/agi-bin");

$agi = new AGI();

$r = $agi->get_variable("CALLERIDNUM");
$callerid = $r["data"];

$r = $agi->get_variable("ACCOUNTCODE");
$accountcode = $r["data"];

$query = "select whateverdata from tbl_user where col_userid='$accountcode';";

$r = $agi->set_variable("BLA", $value);

This way you don’t have to guess who your user is, every connected device has an account code associated with it. You can interlink everything, From devices to customer database to cdr.