convert text to voice using twilio in php - php

I want to convert text to voice message and send to users phone no
currently i am using making call api with TwiMLTM
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('/path/to/twilio-php/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "xxx";
$token = "{{ auth_token }}";
$client = new Services_Twilio($sid, $token);
$call = $client->account->calls->create("+343443", "+3444", "http://demo.twilio.com/docs/voice.xml", array(
"SendDigits" => "1234#",
"Method" => "GET"
));
echo $call->sid;
it is working,but this make call to user,but we need to voice message
Note : message is coming from textarea

Twilio Evangelist here.
This is a little tricky. Twilio allows you to make phone calls, so if the dialled number is answered by voicemail, you can leave a message. If it is answered by a human being, then you'll need to interact with them. It is possible to use the if_machine parameter when creating a call to detect an answering machine. However you cannot 'send' a voice mail as if sending an SMS or an email. You need to make the call, and decide how to handle that depending on who/what answers.
You could try sending the message as an SMS however, which would deliver the exact text to the user:
$sms = $client->account->messages->sendMessage("+343443", "+3444", $message_text);
However, if the number you are sending to is not able to receive an SMS, then you need to make the call and interact with either a human or answering machine.
Best of luck!

Related

Sending optional parameters to Twilio Studio to trigger Flow using PHP SDK

I'm trying to send optional parameters to a Twilio Studio Flow Trigger using PHP. I followed the example shown in the Twilio Studio REST API docs and was successful triggering a new Flow using the (required) sender and recipient phone numbers.
use Twilio\Rest\Client;
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "my_auth_token";
$twilio = new Client($sid, $token);
$execution = $twilio->studio->v1->flows("FWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
->executions
->create("+15555559876", "+15555551234");
After getting the basic communications working, I now want to pass a couple parameters to the Flow. Unfortunately, I couldn't find any PHP examples that include optional parameters. (The docs mention how to access parameter values in the widgets with {{flow.data.parameterName}}, but not how to generate the request in PHP.)
The answer is probably easy and obvious but I can't figure it out and would appreciate any guidance.
Received an answer from Twilio Support.
$data = ["parameters" => ["foo" => "bar"]];
$flow = $twilio->studio->v2->flows("FWxxxxx");
$flow->executions->create($to, $from, $data);
Optional parameters are now passing through and can be used by widgets in the Studio Flow.

Twilio sending message Using twilio library

Sending twilio message from my api returns this error
Twilio sending message The requested resource /2010-04-01/Accounts//Messages.json was not found
this is how I send a message. anyone incountered a problem?
$this->client = new \Services_Twilio($sid, $token);
return $this->client->account->messages->sendMessage($from ,$to, $message);
this is the documentation I followed
https://www.twilio.com/docs/api/rest/sending-sms
How do I create Messages.json
I used this https://github.com/twilio/twilio-php on laravel 4.2
Another Twilio developer evangelist here. Think I might be able to help.
The error message you got was this:
The requested resource /2010-04-01/Accounts//Messages.json was not found
The key point is the URL, particularly the double slash in the middle. That is where your Account Sid should be, thus leading to the 404 error.
In this case, I would double check how you are setting $sid. Make sure it is assigned before you try to create the Twilio client object.
Hi Twilio developer evangelist here.
Sorry to hear you're having trouble with your code.
You seem to not have posted your complete code, so I don't see where you actually require the library.
I have however written an example which worked for me.
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = 'AC';
$token = '01';
$client = new \Services_Twilio($sid, $token);
$message = $client->account->messages->sendMessage(
'+44', // From a valid Twilio number
'+44', // Text this number
"Hello monkey!"
);
I have removed some sensitive data on the code, but if you replace that with your token and numbers, you should be able to send a text message correctly.
Also, just in case you have the contents of the 'Services' folder elsewhere, make sure your paths are correct.
You have entered messages when it should be sms_messages.
CHANGE
$this->client->account->messages->sendMessage($from ,$to, $message);
INTO
$this->client->account->sms_messages->create($from ,$to, $message);
Also make sure that you are running this code inside a class otherwise you need to remove the $this-> from your code and use a local varible.

How to put Twilio call on hold

I am trying to implement following on Twilio, but not sure why I am not able to get it done correctly. I have initiated a call using standard Twilio procedures from twilio.device.connect. After a call is initiated, I am updating the call to new url in order to put it on hold.
$client = new Services_Twilio($accountSid, $authToken);
$call = $client->account->calls->get($call_sid);
$call->update(
array(
"Url" => "http://localhost/voice.xml",
"Method" => "POST",
)
);
Now here instead of putting end user on hold it just disconnects the call, and play music on my side. Why it is happening?
Twilio evangelist here.
I'd suggest checking to see if Twilio is logging any errors:
https://www.twilio.com/user/account/monitor/alerts
If you are trying to redirect Twilio to "http://localhost", thats not going to work because Twilio obviously does not know how to reach the localhost running on your own machine.
If you want to expose a web server running on your own local machine to the internet via a public URL check out an awesome tool called ngrok.
the reason is that after the <Play> tag in your
"http://localhost/voice.xml" file.. there is no further TwiML that gets executed.
The solution is to redirect the call back to its original state.

Twilio Callback URL not Called - PHP & Wordpress

I'm trying to integrate Twilio onto my Wordpress site.
The idea is to allow users to type in their phone number to get a download link to our app. We've put up a simple form here - http://evntr.co/download - and upon submitting the form, the EvntrPHP.php code is run.
Using template code, we've easily been able to get the form to send a message to a verified number (the one currently in the To field) using a free Twilio number. However, when we add the StatusCallback parameter, it never calls our callback.php code. Both EvntrPHP.php and callback.php are in the root directory - evntr.co/.
<?php
require 'twiliophp/Services/Twilio.php';
$AccountSid = "--------";
$AuthToken = "---------";
$client = new Services_Twilio($AccountSid, $AuthToken);
$phonenum = $_POST["phonenum"];
$callbackURL = "https://evntr.co/callback.php";
$client->account->messages->create(array(
'To' => "XXXXXXXXXX",
'From' => "+XXXXXXXXXX",
'Body' => "Test Message",
'StatusCallback' => "https://evntr.co/callback.php",
));
?>
My understanding is that the flow should be like this:
user navigates to evntr.co/download
user submits the form with their number
form calls EvntrPHP.php and sends a text message to their number
Twilio POSTs to callback.php whenever the status of the message changes (Sent, Delivered, Canceled, etc).
However, every time I submit the form, the message is sent and the page just stays at evntr.co/EvntrPHP.php and never loads callback.php. Maybe this is a misunderstanding on my part with how Callback URLs work? Or maybe the StatusCallback parameter doesn't work with a free Twilio number?
Twilio developer evangelist here.
You are correct that the Twilio callbacks are not working as you expect. As McCann points out, the request is made asynchronously from Twilio to the URL you supply. You can use the callback to keep track of the progress of the message, but not affect the request the user has made.
So, in your example you either want to render something after you have sent the message:
<?php
require 'twiliophp/Services/Twilio.php';
// other stuff
$client->account->messages->create(array(
'To' => $phonenum,
'From' => "+14708655xxx",
'Body' => "Test Message",
'StatusCallback' => "https://evntr.co/callback.php",
));
?>
<h1>You should receive an SMS, click the link in the SMS to download the app on the platform of choice.</h1>
With some more style than a plain <h1> of course! Or, you could redirect to a page with a success message on. (PHP is not my strongest subject, but discussions of redirects are rife on this StackOverflow question)
Good luck with the app, and let me know if you have any more Twilio questions!
[edit]
As discussed in the comments, if you want to see if the API request was successful you'll want to do something like:
<?php
require 'twiliophp/Services/Twilio.php';
// other stuff
try {
$client->account->messages->create(array(
'To' => $phonenum,
'From' => "+14708655xxx",
'Body' => "Test Message",
'StatusCallback' => "https://evntr.co/callback.php",
));
// Success! Redirect to success page!
header("Location: http://evntr.co/success.php");
die();
} catch (Services_Twilio_RestException $e) {
// Something went wrong!
// Do something about it!
}
?>
So, wrapping the API call in a try/catch block and responding appropriately should catch most errors from incorrect phone numbers or other API errors. It won't guarantee that the SMS has been delivered (you'll get that from the callback webhook) but it will guarantee that you've done all you can to get the SMS sent.
The callback doesn't work like you think it does.
Call End Callback (StatusCallback) Requests
After receiving a call, requesting TwiML from your app, processing it, and finally ending the call, Twilio will make an asynchronous HTTP request to the StatusCallback URL configured for the called Twilio number (if there is one). By providing a StatusCallback URL for your Twilio number and capturing this request you can determine when a call ends and receive information about the call.
-- https://www.twilio.com/docs/api/twiml/twilio_request

How to make browser-phone call with twilio

i am currently stacked with twilio client call where i have to call a user from browser. Lets see the scenario.
start snippet:
include 'twilio/Services/Twilio/Capability.php';
include 'twilio/Services/Twilio.php';
$accountSid = 'ACxxxxxxxxxxxxxxx';
$authToken = 'xxxxxxxxxxxxxxxxx';
$token = new Services_Twilio_Capability($accountSid, $authToken);
$token->allowClientOutgoing('APXXXXXXXXXXX');
$client = new Services_Twilio($accountSid, $authToken);
$call = $client->account->calls->create("twilio Number", "client number", "https://www.mysite.com/twilio/callback", array());
And my call back goes like this:
function callback(){
$xml = "<Response><Say>Placing your call now.</Say><Dial callerId='twilio verified number' timeout='40' record='true'></Dial></Response>";
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" . $xml;
exit();
}
I dont know what exactly the $from, $to parameters with in the calls->create() function takes. If we are initially initiating the call to $to then, it should work.
My requirement is to call a client to client number from my browser. Yes, i see the referencees here but stacked again.
Twilio evangelist here.
Looks like your current code is using the REST library to initiate the call. This send a message to Twilio to initiate a phone call from Twilio to some phone. All of that happens on the server, not in the browser.
In your case it sounds like you want to actually make a phone call right from the browser. To do that you need to use Twilio Client, which is a JavaScript framework that lets you create an audio connection from a browser to Twilio, and the connection is made using WebRTC (or Flash if needed). Its the job of the Twilio Client SDK to figure out how to create and manage the actual audio connection between your browser and Twilio.
http://www.twilio.com/client
https://www.twilio.com/docs/tutorials/walkthrough/browser-calls/php/laravel
This is different than initiating a call using the REST API. When you initiate an outbound call using the REST API, the API does not actually manage the audio connection. Your using the API simply to tell Twilio to initiate a phone call between Twilio and a phone (or to send a text message), or to ask Twilio for information (like getting list of send SMS messages). The management of the actual audio connection is handled by Twilios internal systems.
The first part of your PHP code is correct you need to create a Twilio Capability token. That token is used by our JavaScript library. This tutorial shows the JavaScript code that you can use to make a call from your browser to Twilio:
https://www.twilio.com/docs/tutorials/walkthrough/browser-calls/php/laravel
Once the browser connects to Twilio, Twilio will make a request to the URL you've set on your TwiML App (the string starting with "AP" that you specified when you created the capability token). You can return whatever TwiML you like from this URL, including using the <Dial> very to tell Twilio to bridge the Twilio Client connection into a <Conference>.
Hope that helps.
Devin
This will be helpful. If you look through the code, there is some api (i think callFromBrowser()) which does the trick.

Categories