Twilio Response Class not found - twilio

I am new to Twilio. To learn the basics, I followed the instructions here:
https://www.twilio.com/docs/howto/walkthrough/click-to-call/php/laravel#12
At first, my phone would ring, and I would receive a generic message. Impressed, I upgraded my account. Now I receive a call where a voice says "We're sorry an application error has occurred."
I checked my Alerts in Twilio, and found Error: 12100 - Document parse failure
So I checked the url of my outbound.php and realized that there is a PHP error here. The error is
Fatal error: Class 'Response' not found in
/home/......./outbound.php on line 16
After some searching, I can't find anyone else discussing this same problem. Finally, the worst part, I can't even find any reference to a Response class in the Twilio Helper Library.
Here is my entire code block for the page in question.
<?php
error_reporting(E_ALL);
require_once 'twilio-library/Services/Twilio.php';
// A message for Twilio's TTS engine to repeat
$sayMessage = 'Thanks for contacting our sales department. If this were a
real click to call application, we would redirect your call to our
sales team right now using the Dial tag.';
$twiml = new Services_Twilio_Twiml();
$twiml->say($sayMessage, array('voice' => 'alice'));
// $response->dial('+12345675309');
$response = Response::make($twiml, 200);
$response->header('Content-Type', 'text/xml');
return $response;
?>
If I change this file to static, well formatted XML then the error stops.

Twilio developer evangelist here.
The tutorial you were following was based within the Laravel framework, which is where the Response class was expecting to come from.
If you are using the PHP TwiML builder in just a plain PHP file, you should be able to just print the $twiml. I'd probably add a Content-Type of text/xml as well, just to be safe. Like this:
<?php
error_reporting(E_ALL);
require_once 'twilio-library/Services/Twilio.php';
// A message for Twilio's TTS engine to repeat
$sayMessage = 'Thanks for contacting our sales department. If this were a
real click to call application, we would redirect your call to our
sales team right now using the Dial tag.';
$twiml = new Services_Twilio_Twiml();
$twiml->say($sayMessage, array('voice' => 'alice'));
// $twiml->dial('+12345675309');
header('Content-type: application/xml');
print $twiml;
?>
Let me know if this helps at all!

Related

How to make a customable message in Twilio Voice call

i've successfully make a voice call with twilio like this:
$call = $client->calls->create(
$toNumber, $fromNumber,
array("url" => "http://demo.twilio.com/docs/voice.xml")
);
the above code will call to the $toNumber with twilio demo message, when i try to use TwiML to create the dynamic message like this:
$response = new TwiML();
$response->say('Votre otp est 2234', ['voice' => 'woman', 'language' => 'fr']);
$call = $client->calls->create(
$toNumber, $fromNumber,
$response
);
i got an error like this:
[HTTP 400] Unable to create record: Url parameter is required. For
more information, see http://www.twilio.com/docs/api/twiml
Twilio developer evangelist here.
You can't send the TwiML to Twilio when you make a call like that. You need to send a URL that Twilio will send an HTTP request to when the call connects (like in the original example). So, to fix your code you should replace the demo URL with your own URL.
$call = $client->calls->create(
$toNumber, $fromNumber,
array("url" => $YOUR_URL_HERE)
);
That URL can be your own application, or something like a TwiML Bin or Twilio Function. Check out this article to see how to respond with TwiML to a request from Twilio.
I notice that you are building something for one time passwords too. You might be interested in checking out the Authy API that can implement and deliver OTP codes via calls, SMS and app.

Twilio Not doing anything when receiving SMS

I'm creating a sample application that will post alerts to the website in the event of a hurricane or service outage. I'm not using Laravel.
I set the URL of the page in my account settings. The first time I sent a message I received a HTTP error that it had timed out without being given a reponse. I edited the XML and tried again.
I'm not getting anything in the database and I'm not getting the response. I also wrote a sample page that posts a value to see if it would work and it did. It posted it into the database and showed correctly formatted XML.
<?php
$response = 'This number cannot handle automated replies...';
$twiml1 = '<response><sms>';
$twiml2 = '</sms></response>';
require_once '../settings/db.php';
if (isset($_POST['body'])) {
$body = strip_tags($_POST['body']);
$sql = "INSERT INTO alerts (message) VALUES ('$body')";
$result = $db->query($sql);
if ($result) {
$response = 'Thanks. Your message was posted on the website.';
} else {
$response = 'There was a query error.';
}
}
header('Content-type: application/xml');
echo $twiml1;
echo $response;
echo $twiml2;
Twilio developer evangelist here.
Parameters that are sent via webhooks from Twilio are case sensitive and start with a capital letter. The text for an incoming message is sent as the Body parameter so checking for $_POST['body'] won't work.
I'd update your conditional to:
if (isset($_POST['Body'])) {
$body = strip_tags($_POST['Body']);
// The rest
}
Also, just to note, the <Sms> element has been deprecated. I'd use the <Message> element instead. The tags are case sensitive too, so I'd update the TwiML section to this:
$twiml1 = '<Response><Message>';
$twiml2 = '</Message></Response>';
Let me know if that helps at all.

Twilio API PHP Page Records can not be deserialized

Im trying to get a list of phone numbers under my Twilio account. Im using their example below, I took out the sid and token for security reasons.
include('/lib/Vendor/autoload.php');
use Twilio\Rest\Client;
$sid = "";
$token = "";
$client = new Client($sid,$token);
foreach ($client->incomingPhoneNumbers->read() as $number) {
echo $number->phoneNumber;
}
However I get this error:
Fatal error: Uncaught exception
'Twilio\Exceptions\DeserializeException' with message 'Page Records
can not be deserialized' in
E:\websites\twiliosite\lib\Vendor\twilio\sdk\Twilio\Page.php:90
Any idea what Im doing wrong? I googled this and couldnt find anything.
Twilio developer evangelist here.
Looks like that is thrown here: https://github.com/twilio/twilio-php/blob/master/Twilio/Page.php#L90
I've only taken a glance at the code, but it looks to me like if a response body doesn't contain the page of data that was expected and isn't empty then it throws that error.
This could perhaps happen if the connection was interrupted and the page of data was delivered incomplete. That might explain why it worked for you on the second attempt.

Dial a Number Twilio Number to Trigger TWILM Bin

We are attempting to create a workflow that will ultimately connect a lead from a contact form, with a business owner.
The workflow is as follows:
1) Lead fills in contact form
2) Using Stamplay integration with Unbounce, the lead receives a text asking them if they wanted to be contacted "Now", or "Later".
Let's go with lead says "Now"
3) Lead says "Now", which will access a webhook URL to decide on what to do next.
In this particular case, saying "Now", will trigger a TWIML bin to dial the business owner. If the business owner doesn't pick up/busy, then we send a text to the lead asking them to send a follow-up text with a 'name' and 'date/time'.
4) The lead replies with a text with this information, and then both the business owner and lead receive separate notifications about the appointment.
I have been able to successfully go through this whole workflow when a user directly dials the Twilio number (not programatically with keywords yet, which is where I need help).
When a call comes in -> TWIML bin
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Pause length="4"/>
<Say>Please hold, while I connect your call.</Say>
<Pause length="4"/>
<Dial timeout="10"> business owner number </Dial>
<Pause length="4"/>
<Sms>I am currently unavailable. If you'd like me to get in touch, pls reply back with your name, and a time that would work best for you. Thanks, Adam</Sms>
</Response>
When a SMS is received -> webhook URL
<?php
// Require the bundled autoload file - the path may need to change
// based on where you downloaded and unzipped the SDK
require __DIR__ . '/twilio-php-master/Twilio/autoload.php';
// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$sid = 'xyz';
$token = 'xyz';
$client = new Client($sid, $token);
$number = $_POST['From'];
$body = $_POST['Body'];
//Sends a message to the owner
$sms = $client->account->messages->create(
// Cell of owner
'12345',
array(
// A Twilio phone number you purchased at twilio.com/console
'from' => "78900",
// Lead's reply sent to owner asNotification
'body' => "Hi <name>. You have a new lead. The information for this lead is: $body. You can contact them at $number"
)
);
//Sends a message to the lead
$sms = $client->account->messages->create(
// Cell of Lead
$number,
array(
// A Twilio phone number you purchased at twilio.com/console
'from' => "78900",
// Notification Message sent to Lead
'body' => "This is a confirmation that I have received your information, and will be in contact with you soon. Thanks, <name>."
)
);
Where I am encountering issues is having the lead text "Now", to trigger a phone call between the business owner and lead.
This is the code that I have been attempting to use, except that I have been receiving 11200- HTTP retrieval failure non-stop. I have also attempted to use $client->account->calls->create, as that's what I used to successfully send messages.
// Read TwiML at this URL when a call connects (attempt to connect to owner)
$call = $client->calls->create(
'lead-number', // Call this number
'78900', // From a valid Twilio number
array(
'url' => TWIML Bin of Interest
)
);
Anyone have any idea what I could do?
Check out the example of creating a dynamic response:
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Twiml;
$response = new Twiml;
$body = $_REQUEST['Body'];
if( $body == 'hello' ){
$response->message('Hi!');
}else if( $body == 'bye' ){
$response->message('Goodbye');
}
print $response;
In your case you'll modify for if "now" is in the $body you can create the call which your code looks fine for.
$call = $client->calls->create(
"+1415XXXXXXX", "+1415XXXXXXX",
array("url" => "link_to_twiml_bin")
);
In regards to the 11200 HTTP retrieval error, take a look at the possible solutions here especially:
Make sure your web server allows HTTP POST requests to static
resources (if the URL refers to .xml or .html files)

How can I record incoming calls on Twilio?

I have a website that uses Twilio to allow people to use our temporary numbers to receive SMS messages received during verification processes etc. It is becomming more common that companies are switching to audio verification instead so I want to start recording all calls received and displaying them in the existing HTML table using the HTML5 <audio> tag.
Here is the existing code:
<tbody>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Get the PHP helper library from twilio.com/docs/php/install
require_once('twilio/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "";
$token = "";
$client = new Services_Twilio($sid, $token);
$messages = $client->account->messages->getIterator(0, 50, array(
'To' => $_SERVER['QUERY_STRING'] // this is the number
));
foreach ($messages as $message) {
echo "<tr><td>" . $message->from . "</td><td>" . $message->date_sent . "</td><td>" . $message->body . "</td></tr>";
}
?>
</tbody>
</table>
How can I build in to that the recorded calls received? I want to keep it in date/time order within the eixsting SMS messages, if that makes sense.
Twilio developer evangelist here.
You can absolutely record calls with Twilio.
When you create a call, you just need to include the parameter Record=true in the REST API request to create a call. Then, if you include a statusCallback parameter that points to a URL on your server, then you will receive a webhook to that URL when the call is complete that includes a link to the recording.
You can also fetch the latest recordings from the API. You can get recordings in wav or mp3 format, which you can then use in the HTML <audio> element.
I'm not sure how you have set up your date ordered SMS table, but hopefully this helps. Let me know if there is anything else I can help with.

Resources