Search code examples
phplaraveltwiliovoipvoice

How I can talk with live Microphone Instead of using MP3 Player Twilio


Hi i have build an application using laravel and twilio and everything is working so good but what i want to do is to talk from the browser to that phone ive called instead of playing that MP3 song how i can do that

Voice-Controller.php


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Twilio\Exceptions\RestException;
use Twilio\Rest\Client;

class VoiceController extends Controller
{
  public function __construct() {
    // Twilio credentials
    $this->account_sid = env('ACCOUNT_SID');
    $this->auth_token = env('AUTH_TOKEN');
    //the twilio number you purchased
    $this->from = env('TWILIO_PHONE_NUMBER');

    // Initialize the Programmable Voice API
    $this->client = new Client($this->account_sid, $this->auth_token);
  }

  /**
   * Making an outgoing call
   */
  public function initiateCall(Request $request) {
    // Validate form input
    $this->validate($request, [
      'phone_number' => 'required|string',
    ]);

    try {
      //Lookup phone number to make sure it is valid before initiating call
      $phone_number = $this->client->lookups->v1->phoneNumbers($request->phone_number)->fetch();

      // If phone number is valid and exists
      if($phone_number) {
        // Initiate call and record call
        $call = $this->client->account->calls->create(
          $request->phone_number, // Destination phone number
          $this->from, // Valid Twilio phone number
          array(
              "record" => True,
              "url" => "http://demo.twilio.com/docs/voice.xml")
          );

        if($call) {
          echo 'Call initiated successfully';
        } else {
          echo 'Call failed!';
        }
      }
    } catch (Exception $e) {
      echo 'Error: ' . $e->getMessage();
    } catch (RestException $rest) {
      echo 'Error: ' . $rest->getMessage();
    }
  }
}

Solution

  • Twilio Programmable Voice SIP Domains, sometimes called SIP Interfaces, allow you to place and receive voice calls using a standards-based SIP endpoint by registering directly with Twilio.

    In essence, you need to follow these steps to set everything up. For the full instructions, I recommend this blog post:

    1. Purchase a Twilio number
    2. Create a SIP Domain
    3. Handle inbound and outbound calling
    4. Set up your SIP Client
    5. Make our first call!