Conference Calls With Twilio and Symfony

Brief introduction to Twilio and Symfony

Twilio is a cloud communications platform as a service, Twilio allows developers to programmatically make and receive phone calls, exchange text messages and other communication functions using it’s API.

Symfony is a PHP web application framework that was heavily inspired by the Spring Framework, with a set of reusable PHP libraries.

Twilio Setup

Register a Twilio account where you will get your twilio_sid and twilio_token which we will use later in this post to make a conference, you will also need to get a Twilio number which can be done for free on a trial account, on which you will get a curtain number of dollars to test your software for free.

Installation – Twilio can easily be installed with composer with the following command :

composer require twilio/sdk

After installing the SDK we will need to make it accessible in our application, which we can do by defining it as a service, in our services.yml we will add the following :

services:
   # ...
   twilio.client:
       class: Twilio\Rest\Client
       arguments: ['%twilio_sid%', '%twilio_token%']
  
   # Add an alias for the twilio.client service
   Twilio\Rest\Client: '@twilio.client'

1. We will also need a place to store our twilio parameters, we can do this in the parameters.yml file, where we will add the following :

parameters:
   # ...
   twilio_sid: your_acc_sid_here
   twilio_token: your_auth_token_here
   twilio_number: 'your_phone_number_here'

Creating a conference call

Getting our twilio_sid and twilio_token from our parameters.yml, which we put in our parameters.yml in the last step of our Twilio Setup guide.

$sid = $this->getContainer()->getParameter('twilio_sid');
$token = $this->getContainer()->getParameter('twilio_token');

2. Instantiating our Twilio Client with the following line :

$twilio = new Client($sid, $token);

3. Adding participants to a conference :

    $participantOne = $twilio
    ->conferences(1)
    ->participants
    ->create($twilioNumber,$participantOneDialingNumber, [
        'statusCallback' => 'link_to_backend_route'
    ]);
$participantTwo = $twilio
    ->conferences(1)
    ->participants
     →create($twilioNumber,$participantTwoDialingNumber,);

Here we add two participants to a conference, the variable $twilioNumber is where you will place the Twilio number that you got when you registered your Twilio account, and the variables $participantOneDialingNumber and $participantTwoDialingNumber are the telephone numbers of the participants which you want to add to the conference.

4. Callback functions – When we added $participantOne to the conference, we added a statusCallback function in the create function, which will trigger once the $participantOne has left the conference, allowing us to access the response which Twilio will send, and this additional information about the characteristics of the conference can come in handy if you want to do more then just add two people to a conference.