How to distinguish incoming trunks in asterisk-java

Suppose you have asterisk 1.6 with freepbx and you are developing with the latest asterisk-java library. Somehow you wanna perform different tasks based on calls coming through different SIP trunks.

When there’s a new incoming call, asterisk-java will trigger a newChannelEvent, but there’s no information in this event that can be used for distinguish from which trunk this call is coming in.

By default the accountcode in this NewChannelEvent is null, which makes sense because the accountcode is used for billing, and no one, at least not many people, wants to record the charge of SIP trunk because that’s usually done by the SIP provider to which the trunk is registered to. But since the accountcode is null and useless here, maybe we can use it as an identifier to distinguish the incoming trunk, it may be not the smartest solution, but at least it’s a working one 😀

The solution is pretty easy. Just open extensions_custom.conf and add
exten => <username from your sip provider>,1,Set(CDR(accountcode)=<some account>)

et voilà! 😉

If you try to make a call from outside now, in this NewChannelEvent the accountcode is still null, but there will be a NewAccountCodeEvent and you can see that the accountcode is set in this event, and from the same uniqueid you can tell that both the NewChannelEvent and NewAccountCodeEvent belong to the same call.

*note: Remeber that you should never manually edit extensions_additional.conf, the file is generated by freepbx and all the changes you have done will be overrided the next time you apply some changes in freepbx