Forwarding Twilio Recordings to Your Email With Your Own Hosting - twilio

I want to forward the voicemail recording left on my Twilio account using Twilio studio. I am following the article posted here https://support.twilio.com/hc/en-us/articles/223132287-Forwarding-Recordings-to-Your-Email but I only get empty email without audio recording link. I am new with studio and twilio so I'd appreciate your help.
The code I added to my server is the following with the exception of the email address. The name of the file is send.php.
<?php
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<Response></Response>';
$to = "your-email#example.com"; // Update with your email address
$subject = "Message from {$_REQUEST['From']}";
$message = "To listen to this message, please visit this URL: {$_REQUEST['RecordingUrl']}";
$headers = "From: webmaster#example.com"; // Update with your sending address
mail($to, $subject, $message, $headers);
On my Studio flow I added the "Record Voicemail" widget, then my "IF RECORDING COMPLETE" transition is a HTTP POST request to my file send.php with the code above. Can someone please guide me? I can't find articles or video tutorials.
Thanks!

(I'm responding as an answer since I can't add a comment)
Is the email body empty, or does it show "To listen to this message, please visit this URL:" followed by nothing?
If it's completely blank, there's an issue with the mail implementation on your server.
You could try changing the message line to the following to see everything that is coming into your script from Twilio, pay attention to the capitalization of the keys:
$message = var_export($_REQUEST,true);
If "RecordingUrl" is missing in the result, it's not being sent by Twilio.

Related

Forward VoiceMail to Email

I Need to forward voicemails to Salesforce (email-to-Case) from Twilio.
Currently using Studio and Twiml to execute my work flow.
There is a blog post the demonstrates how this could be done but it does not seem to work as expected.
https://support.twilio.com/hc/en-us/articles/223132287-Forwarding-Recordings-to-Your-Email
I've created and host the php file in Twilio, created a test studio flow that fire a webhook to the host url.
Result - i'm able to make a call, record the voicemail but then it gives me an error and then ends the call. no email received.
``<?php
/**
* This section ensures that Twilio gets a response.
*/
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<Response></Response>'; // Place the desired response (if any) here.
/**
* This section actually sends the email.
*/
/* Your email address. */
$to = "your-email#example.com";
$subject = "Message from {$_REQUEST['From']}";
$message = "You have received a message from {$_REQUEST['From']}.";
$message .= "To listen to this message, please visit this URL: {$_REQUEST['RecordingUrl']}";
$headers = "From: webmaster#example.com"; // Who should it come from?
mail($to, $subject, $message, $headers);``
receive an email in my mailbox as a test

Is there any way to get messages we send/received by using phone number in twilio?

I would like to make my client to check whether end client is received text or not and what reply he/she has sent? I always going through twilio to see whether client received sms or not? Is there any way to check it from twilio?
Twilio developer evangelist here.
You can get both incoming messages to your Twilio numbers and reports on the status of messages after you send them from Twilio using webhooks.
When you send a message you can include a StatusCallback parameter. The parameter should be a URL in your application.
$client->messages
->create(
$to,
array(
"from" => $from,
"body" => "Let's grab lunch at Milliways tomorrow!",
"statusCallback" => "https://example.com/messageStatus"
)
);
Twilio will send a POST request to the statusCallback URL each time your message status changes to one of the following: queued, failed, sent, delivered, or undelivered. It will include the original message's SID, so you can tie these webhooks back to the message you sent.
Similarly, you can get these webhook notifications for incoming messages to your Twilio numbers. For this you need to set up the incoming webhook URL to the number in your Twilio console. Set it to a URL in your application and you will receive a webhook when someone sends a message to your Twilio number. Check out this quickstart guide on receiving messages to your Twilio number with PHP.
Let me know if that helps at all.
[edit]
Thanks for the comment where you made it clear that this is after the fact, not at the time of sending.
In this case, you can absolutely list the messages by the phone number that sent them. A message resource includes a Status attribute that lists the current message state in the Twilio system, anything from "accepted" and "queued" to "sending", "sent", "delivered", "undelivered" and "failed". You can see more about these statuses in the documentation.
To get the list of messages sent from a number you can use the following code:
use Twilio\Rest\Client;
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$client = new Client($sid, $token);
// Loop over the list of messages and echo a property for each one
foreach ($client->messages->read(array("from" => $FROM_NUMBER) as $message) {
echo $message->status . " - " . $message->to . " = " . $message->body;
}
You can pull data for specific messages https://www.twilio.com/docs/api/messaging/message#delivery-related-errors
or you can simply pull all the logs

How to make call through Twilio where two people will have live conversation?

I am trying to setup a call between two people and I am able to make a call but the person who pick up the call can listen pre-recorded voice mail. I want to have live conversation between these two people. I am not getting what should be the URL and how can I set it up.
My Sample PHP Code is -
require_once "application/helpers/Services/twilio-php-master/Twilio/autoload.php";
use Twilio\Rest\Client;
// Step 2: Set our AccountSid and AuthToken from https://twilio.com/console
$AccountSid = "Axxxxxxxxxxxxxxxxxxxxxc0";
$AuthToken = "xxxxxxxxxxxxxxxxxxxxxxxxx";
// Step 3: Instantiate a new Twilio Rest Client
$client = new Client($AccountSid, $AuthToken);
try {
// Initiate a new outbound call
$call = $client->account->calls->create(
// Step 4: Change the 'To' number below to whatever number you'd like
// to call.
"+91my_number",
//$_POST['to'],
// Step 5: Change the 'From' number below to be a valid Twilio number
// that you've purchased or verified with Twilio.
"+1_twilioverified_number",
// Step 6: Set the URL Twilio will request when the call is answered.
array("url" => "http://demo.twilio.com/welcome/voice/")
);
echo "Started call: " . $call->sid;
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
Is there anyone who can help me with this.
Thank you in advance. Awaiting for reply.
Twilio developer evangelist here.
Currently your code is working fine, the thing you need to change is the URL that is listed as "Step 6" in the comments. Currently that URL points to some TwiML that reads out a message.
Instead of reading that message, you will need to provide a URL that returns some TwiML that <Dial>s another <Number>. You see, when the first part of the call connects, Twilio makes an HTTP POST request to that URL to get the instructions for what to do next.
This URL needs to be available online for Twilio to make the HTTP request to. You can either deploy this TwiML to a server of yours, use a TwiML Bin from the Twilio console or test it out using ngrok on your local development machine.
The TwiML you want should look a bit like this:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial>
<Number>YOUR_NUMBER_HERE</Number>
</Dial>
</Response>
Let me know if that helps at all.

Is it possible to forward SMS messages to a non Twilio number and retain the original FROM?

I have a requirement that when a customer sends a specific message to our Twilio long code number we need to record the message and forward it to a 3rd party. The 3rd party has a short code that handles that type of message.
I looked into redirection, but looks like its aimed at API's.
https://www.twilio.com/docs/api/twiml/sms/redirect
The message would need to have the original body and FROM phone number. From poking around the website, I don't think this is possible. Just posting in case there are some alternatives.
In your case, you need to forward received message with from number to 3rd party non twilio number. For this you need to send a message from sms_receiver url script.
Try below code :
<?php
$from_number = +1234567890 // valid sms enabled twilio number from your account
$to_number = +19876543210 // number to whom sms had to be forwarded
$message = "From : ".$_REQUEST["From"]."\n\nBody : ".$_REQUEST["Body"];
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<Response>';
echo '<Message to="'.$to_number.' from="'.$from_number.'"">';
echo $message;
echo '</Message>';
echo '</Response>';
?>

Unable to parse Twilio parameters

A Twilio beginner question: I have followed these instructions to set up a simple voicemail system that emails a notification of the voicemail, including the number called from and a link to the recording. However I have been unable to get my code to parse the parameters that are supposedly sent by Twilio.
This is the Twiml:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Record action="mail.php" />
</Response>
And this is the mail.php that it actions to:
<?php
/**
* This section ensures that Twilio gets a response.
*/
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<Response></Response>'; //Place the desired response (if any) here.
/**
* This section actually sends the email.
*/
$to = "your-email#example.com"; // Your email address.
$subject = "Message from {$_REQUEST['From']}";
$message = "You have received a message from {$_REQUEST['From']}.";
$message .= "To listen to this message, please visit this URL: {$_REQUEST['RecordingUrl']}";
$headers = "From: webmaster#example.com"; // Who should it come from?
mail($to, $subject, $message, $headers);
The voicemail is recorded and the email is sent, but the received email does not contain any values for the variables {$_REQUEST['From']} and {$_REQUEST['RecordingUrl']}, e.g. "You have received a message from . To listen to this message, please visit this URL: ."
Hopefully there is something obvious I am doing wrong here. Any suggestions would be most welcome. Thanks.

Resources