How to block numbers from a list in VitalPBX

Block Calls Based on a MySQL Database in VitalPBX Using a Custom Context

The Dial Plan in VitalPBX is highly flexible and user-friendly, allowing for efficient implementation of various customizations. Its open architecture makes it easy to integrate with third-party databases, enabling advanced solutions such as blocking calls based on large lists of numbers. This integration capability simplifies the management and control of unwanted calls, providing an adaptable environment to meet specific business communication needs.

In this guide, we will walk through the steps to implement a system in VitalPBX that blocks calls to specific phone numbers by querying a MySQL database. This setup is especially useful for scenarios where you need to manage and block a large list of numbers dynamically, such as preventing calls to fraudulent numbers, high-cost destinations, or personal numbers in a work environment.

By using a custom context in the Asterisk dial plan, you can easily query the database to check if a dialed number is prohibited and play a custom message before terminating the call. The database is indexed to efficiently handle thousands or millions of numbers, ensuring scalability and performance as the list grows.

Steps Overview:

  1. Set up the MySQL database – Create a table to store prohibited numbers and index it for fast querying.
  2. Configure Asterisk to access the MySQL database – Set up database connections in Asterisk to allow it to query MySQL during call processing.
  3. Create a custom context in the Asterisk dial plan – Write a custom context that queries the database for blocked numbers and takes appropriate action (e.g., blocking the call and playing a message).
  4. Integrate the custom context into VitalPBX – Modify the call routing in VitalPBX to ensure calls are processed through the new custom context.

 

By following these steps, you can easily create a robust system for blocking calls to prohibited numbers while maintaining flexibility and scalability for future needs.

Here are a few examples of situations where this implementation could be useful:

1. Create the MySQL Database

First, you’ll need to create a table in MySQL to store the blocked phone numbers. Let’s assume you have a database called blocked_numbers with a table named numbers that contains a column phone_number.

				
					mysql -u root
				
			

Create Database and table

				
					CREATE DATABASE blocked_numbers;
USE blocked_numbers;
CREATE TABLE numbers (id INT AUTO_INCREMENT PRIMARY KEY, phone_number VARCHAR(20) NOT NULL, UNIQUE (phone_number), INDEX (phone_number));

				
			

Create the user and password to access the database

				
					GRANT ALL PRIVILEGES ON blocked_numbers.* TO 'vitalpbx'@'localhost' IDENTIFIED BY 'vitalpbx' WITH GRANT OPTION;
FLUSH PRIVILEGES;

				
			

You can add blocked numbers to this table using the following command:

				
					INSERT INTO numbers (phone_number) VALUES ('1234567890');
				
			

2.- Configure database access in Asterisk’s odbc.ini and res_odbc.conf files.

Edit the odbc.ini file.

				
					nano /etc/odbc.ini
				
			

Add the following content at the end.

				
					[MySQL-Block-Number]
Description  = VPBX Block Number
Driver       = MariaDB Unicode
Database     = blocked_numbers
User         = vitalpbx
Password     = vitalpbx
Server       = localhost
Socket       = /var/run/mysqld/mysqld.sock

				
			

Edit the res_odbc.ini file.

				
					nano /etc/asterisk/res_odbc.conf
				
			

Add the following content at the end

				
					[blocknumber]
enabled => yes
dsn => MySQL-Block-Number
username => vitalpbx
password => vitalpbx
pre-connect => yes
				
			

Now we proceed to store the database query function, which will be used from the dial plan. Create the following file.

				
					nano /etc/asterisk/vitalpbx/func_odbc__50-block.conf
				
			

Add the following content.

				
					[BLOCK-NUMBER]
writehandle=blocknumber
readsql=SELECT phone_number FROM numbers WHERE phone_number='${SQL_ESC(${ARG1})}'

				
			

After making sure that the changes are loaded, run the command:

				
					asterisk -rx"core reload"
				
			

And if we want to verify if everything loaded correctly, we execute the following command:

				
					asterisk -rx"odbc show all"
				
			

And we should see the following:

				
					ODBC DSN Settings
-----------------

  Name:   asterisk
  DSN:    MySQL-asterisk
    Number of active connections: 1 (out of 1)
    Logging: Disabled

  Name:   vitalpbx
  DSN:    MySQL-vitalpbx
    Number of active connections: 1 (out of 1)
    Logging: Disabled

  Name:   blocknumber
  DSN:    MySQL-Block-Number
    Number of active connections: 1 (out of 1)
    Logging: Disabled
				
			

Finally, we create our context, which will perform the query in the database.

				
					nano /etc/asterisk/vitalpbx/extensions__70-block-number.conf
				
			

Add the following content.

				
					[check_blocked_number]
exten => _[-+*#0-9a-zA-Z].,1,NoOp(Checking if the number is prohibited)
 same => n,Set(CALLED_NUMBER=${EXTEN})
 same => n,Set(NUMBER=${ODBC_BLOCK-NUMBER(${CALLED_NUMBER})})
 same => n,GotoIf($[${ISNULL(${NUMBER})}]?:blocked)
 same => n,NoOp(Number allowed)
 same => n,GotoIf($[${DIALPLAN_EXISTS(${ARS},${EXTEN},1)}=1]?:invalid)
 same => n,Goto(${ARS},${EXTEN},1)

 same => n(blocked),NoOp(Blocked number)
 same => n,Playback(the-number-u-dialed&is&privacy-blocked)
 same => n,Hangup()

 same => n(invalid),Goto(invalid-dest-cos,s,1)
 same => n,Hangup()

				
			

Now we go to our VitalPBX and in PBX/Class of Service/Dialing Restriction Rules we add a new rule and configure the context created previously.

In Custom Rules Context we write the context configured previously.

After saving the configuration, go to Class of Service and select the “All Permission” Class of Service and in Dial Restriction select the Rule created previously.

Note:

You can perfectly create a new class of service and associate it with the extensions that will have this restriction.

We apply changes and proceed to perform the tests, for which we recommend adding a couple of numbers to the database.

Here are a few examples of situations where this implementation could be useful:

  1. Blocking Calls to Fraudulent or Spam Numbers


Many organizations need to block numbers associated with scams, unwanted telemarketing, or fraud. With this solution, you can maintain an updated list of telemarketing or scammer numbers in the database, preventing employees or users from making calls to these numbers.

  1. Restricting Calls to High-Cost Destinations


Companies with phone plans that aim to control costs on international or premium calls can use this solution to block calls to specific high-cost numbers. By having a list of prohibited numbers, the company can avoid unnecessary or unauthorized expenses.

  1. Call Filtering in Customer Service Centers


In call centers, certain customer numbers can be blocked if they are problematic or have abused the service. This helps prevent the misuse of call center resources and ensures that agents are only attending to customers who comply with the service terms.

  1. Blocking Calls to Personal Numbers in Work Environments


Some companies may have policies that block calls to personal, family, or private numbers from corporate lines. This implementation would allow you to add personal numbers to the database to prevent unauthorized calls, helping reduce the misuse of corporate resources.

These are just a few examples, but using a database to manage blocked numbers offers great flexibility to adapt to various business or security needs.

Conclusion

By following the steps outlined in this guide, you can effectively block calls based on a dynamically managed list of phone numbers stored in a MySQL database. The use of a custom context in VitalPBX allows you to integrate Asterisk’s powerful querying capabilities with a MySQL database, making it easy to manage large lists of prohibited numbers.

Indexing the phone number column significantly improves the performance of queries, enabling you to scale this solution to handle thousands, or even more, blocked numbers efficiently. With this setup, you can quickly add or remove numbers from the database, ensuring that your system remains flexible and responsive, even as the list of blocked numbers grows.

This method provides a highly scalable and customizable way to manage call blocking in VitalPBX, combining the flexibility of MySQL with the robust features of Asterisk.

Our Latest Post