Search code examples
phptwiliotext-to-speechtwilio-api

use text to speech in making call on twilio php api


Good day,

Im creating reminder app that calls a number in certain time, currently I was using an uploaded mp3 file on my server:

here the code:

$sid = "ACxxxxxxxxxx";
$token = "2xxxxxxxxx";
$client = new Client($sid, $token);
$call = $client->calls->create(
         "$phone_number_to","$phone_number_from", 
          array("url" => 
          "https://xxxxx.com/asset/mp3/reminder.mp3")
            );
$csid = $call->sid;

the above code works, but now I wanted to use the text to speech feature on twilio to have a more customized voicemail per reminder..

how do I do this using $client-> api? Im not really familiar on how TwiML works though, maybe thats why im confused.

thanks!


Solution

  • You change this line of your current code "url" => "https://xxxxx.com/asset/mp3/reminder.mp3" so that the URL points at the url hosting the script you want to use to generate your dynamic TwiML.

    Then use the php TwiML library to generate the TwiML, it's pretty straightforward. We have a database with all out customers details in, I use code something along these lines to get their details based on caller ID and have Twilio greet them by first name:

    $booked = SELECT * FROM table WHERE phone = $caller;
    $name = explode(" ", $booked->name);
    $firstname = $name[0];
    $response->say("Hello $firstname. Thanks for calling......");
    

    It's ok but it's a bit robotic. We ended up extracting the 50 most common first names from the database and having a voiceover artist record greetings for each one. For callers with one of those 50 names we serve a specific mp3 file, everyone else gets the robot.