how to set random caller ids on Asterisk based system

Setting random caller IDs on an Asterisk-based system involves modifying the dial plan and potentially using an external script or function to generate random caller IDs. Here’s how you can achieve this: Read more How to save audio stream from Asterisk to a file via WebSocket

How to set random caller IDs on the Asterisk-based system

Here we will use the rand function of asterisk to set random caller IDs on VICidial and GoAutodial. This is relevant to people using multiple Caller IDs for marketing or re-marketing purposes. Sometimes, the DID, phone numbers are blocked by spam marking by some applications like True Caller as it is used to cold call.

Please note that your Carrier provider should allow you to use the caller IDs you will be using as random. In India, this is generally limited to service providers. If you need more DIDs, you need to contact the service providers. In general, they used to provide 30 DID per PRI. However, this can be increased after requesting service providers.

Before implementing, we must choose a Range (Minimum and Maximum).  Minimum is the low DID value and Maximum is the higher DID you have. Now you have to set your display as below:

For eg: Consider the DID Range as 01142002001 to 01142002031

Option 1: Using Dialplan

Step 1: Configure Asterisk Dial Plan

  1. Open your Asterisk dial plan configuration file, usually located at /etc/asterisk/extensions.conf.
  2. Identify the context where you want to set the random caller ID. Add logic to generate and set a random caller ID.

Example:

[set-random-cid]
exten => _X.,1,NoOp(Setting Random Caller ID)
same => n,Set(RANDOMCID=${RAND(1000000000,9999999999)})
same => n,Set(CALLERID(all)=Random CID <${RANDOMCID}>)
same => n,NoOp(Caller ID set to ${CALLERID(all)})
same => n,Dial(SIP/${EXTEN},20) ; Replace with your dial command
same => n,Hangup()

Here:

  • ${RAND(1000000000,9999999999)} generates a random 10-digit number.
  • CALLERID(all) sets the caller ID with the format “Name <Number>”.

Step 2: Reload Asterisk

After saving changes to extensions.conf, reload the dial plan:

asterisk -rx “dialplan reload”

Option 2: Using PHP AGI Script

This method is useful when you have numbers that are out of range or when numbers are placed in a text file. A random number will be used as caller ID while the server dials out.

Step 1: Create a file named randomcid.php under /var/lib/asterisk/agi-bin.

Step 2: Copy the below script and paste to randomcid.php

#!/usr/bin/php
<?php
include ‘phpagi-2.20/phpagi.php’;
$agi = new AGI();
$numbers = file(‘/var/lib/asterisk/agi-bin/cidsrandom-list.txt’);
$cid = array_rand($numbers, 1);
$newCID = trim($numbers[$cid]);
$agi->set_variable(“CALLERID(num)”, $newCID);

?>

Step 3: Create a file named cids-list.txt under /var lib/asterisk/agi-bin and fill your numbers in a single column. here is an example.

01142002001
01142002002
01142002003

Step 4: Its Time now to write a dialplan with AGI function.

For plain asterisk

exten => _9X.,1,AGI(randomcidcheck.php)
exten => _9X.,2,Dial(SIP/obcalltrunk/${EXTEN:1})
exten => _9X.,3,Hangup

For VICIdial/GoAutodial

exten => _9X.,1,AGI(agi://127.0.0.1:4577/call_log)
exten => _9X.,2,AGI(randomcidcheck.php)
exten => _9X.,3,Dial(SIP/obcalltrunk/${EXTEN:1},,tTo)
exten => _9X.,4,Hangup

Step 5: Now all done. Just make a test call.

One thought on “how to set random caller ids on Asterisk based system

Comments are closed.