i am using Twilio API to send and receive sms from the customers.
Every time i send the sms to my customers, i store the feilds like to, body in to my database.
I have implemented the API for send message that works fine and am simply saving the fields in to my database.
My Problem
When i receive SMS from my customers to my twilio number. i want to get the fields like from number and the body and save to my databse.
i looked that the documentation here
https://www.twilio.com/docs/api/twiml
https://www.twilio.com/blog/2012/04/get-started-with-twilio-sms-receiving-incoming-sms-quickstart.html
But this only shows how to send a response to customer when a message is received.
I want to save the received sms in to my database.
Can someone help me top get the from number and the message body when a SMS is received. tnx..
Twilio evangelist here.
Twilio passes parameters in its HTTP request as form-encoded values, so you just need to use the REQUEST object to grab them:
$from = $_REQUEST['From']
The SMS Quickstart for PHP has a more detailed example.
Hope hat helps.
The documentation for Twilio SMS responses is here:
https://www.twilio.com/docs/api/twiml/sms/twilio_request
Here is a relevant quote:
When Twilio receives a message for one of your Twilio numbers it makes
a synchronous HTTP request to the message URL configured for that
number, and expects to receive TwiML in response. Twilio sends the
following parameters with its request as POST parameters or URL query
parameters, depending on which HTTP method you've configured.
You should simply have the data fields inside of PHP's $_REQUEST[] variable.
$_REQUEST['MessageSid'] - A 34 character unique identifier for the message. May be used to later retrieve this message from the REST API.
$_REQUEST['SmsSid'] - Same value as MessageSid. Deprecated and included for backward compatibility.
$_REQUEST['AccountSid'] - The 34 character id of the Account this message is associated with.
$_REQUEST['From'] - The phone number that sent this message.
$_REQUEST['To'] - The phone number of the recipient.
$_REQUEST['Body'] - The text body of the message. Up to 1600 characters long.
$_REQUEST['NumMedia'] - The number of media items associated with your message
Here is an example query you might use with a MySQL database. You should also be sending back a proper TWIXML response to Twilio and scrub the received data before running a query like this.
$sql = "INSERT INTO messages (sid, from, body)
VALUES (
'".$_REQUEST['MessageSid']."',
'".$_REQUEST['From']."',
'".$_REQUEST['Body']."'
)";
Related
I want to record the voice message from view side and send the voice message to phone number using Twilio.
I am using Laravel PHP framework. I have already implemented to send the text message to phone number by Twilio.
As i am getting your problem is first you need to record a message and then that recorded message should be sent using twilio.
So, first you need to record the message and store it on your server or S3 bucket or whatever you are using for storage. And pass that url to your Twilio.
Secondly, you need to send that recorded message as an MMS(Multimedia message) using twilio. For that your ‘to’ and ‘from’ numbers should be capable of sending out MMS messages, you can check that on your twilio account.
For MMS sending in php check this out : https://www.twilio.com/docs/guides/how-to-send-sms-messages-in-php#sign-up-for-a-twilio-account
I have successfully attempted sending SMS to a phone number using Twilio REST API in PHP. Currently I am using a trial account. I can see logs on Twilio console too. When I attempt to send SMS to an invalid number, Twilio REST API gives me error:
Error 21614 - 'To' number is not a valid mobile number.
I can't view this error in Twilio Programmable SMS logs or Debugger Logs. So when I submit a bulk of numbers to Twilio, how can I find at the end, that which numbers failed to receive the SMSs because of error 21614, on Twilio Console? Currenty, I do not want to log it through my PHP code.
Thank you.
EDIT
If you want to go through the console you can see this link as you do not want to manage it via php
For managing at your end with StatusCallback
You need to use optional parameter StatusCallback and specify a callback URL where Twilio will POST each time your message status changes to one of the following:
queued,
failed,
sent,
delivered,
or undelivered.
Twilio will POST the MessageSid along with the other standard request parameters as well as MessageStatus and ErrorCode. If this parameter passed in addition to a MessagingServiceSid, Twilio will override the Status Callback URL of the Messaging Service. URLs must contain a valid hostname (underscores are not allowed). You can learn more about it here
you can add your php code there to keep track of every new message and keep updating the logs for that message.
Hope that helps
i'm working on an web app that has a message thread within and each time a new message is sent to a person a SMS is sent to that person, so what we want to do is to include the person's response to that SMS, lets say A sends a message to B, B receives both an email and a SMS, if B responses to A via the phone we need to be able to add that response to the thread. Is there a way to add additional information when you send a message using the API?
The sending SMS code looks like this:
$client = new Services_Twilio($accountSID,$authToken);
$sms = $client->account->messages->sendMessage("TwilioNumber",$toNumber,$message);
So, is there a way in which i could add some type of information to track this SMS thread, so when i get the response to the Request URL i could actually know that this message is being sent from B as a response from the message sent from A.
Thanks in advance.
Twilio evangelist here.
Unfortunately SMS does not have the idea of "meta-data" so there is nothing you can include that would tell you a message is in "response" to another message, but there are some solutions here.
You could create a convention of commands that are included in the actual body of the text message that tell you the message is ins response to a specific thread. But thats not super user friendly.
You're best bet is probably going to using multiple phone numbers and phone number tracking.
For example, when A sends the message to B you'll need to store both of their phone numbers along with the Twilio phone number that is receiving and relaying those messages. This allows you to map the two users together in a "thread" even though they are not directly texting each other.
But what happens when A wants to have seperate thread with both B and C? With a single Twilio phone number how do you know what thread to route their message to? The simplest way to allow this is to have multiple phone numbers. To send a message to B, A sends to 555-555-5555, to send a message to C, A sends to 555-555-6666. In this scenario you might think about buying a pool of numbers that you can draw from and recycle so you don't have to continuously buy new numbers.
Hope that helps.
I use twilio API to send SMS from my php application. When the 'to' number is malformed (invalid area code, invalid digits number etc) twilio works fine, giving me an exception.
My problem is when I try to send to a number that is in good format, however it doesn't exist. It show it as 'sent' and no exception is given. I also tried the callback option:
array('StatusCallback' => 'http://uncurler.heroku.com/v1/<some code >').
Even when i view the report here it shows as 'sent'.
Is there a way to actually know if the message was delivered? Or more importantly if it was NOT delivered? If not, is it possible from some other API?
Twilio evangelist here.
So currently the Status value does not reflect anything beyond our ability to deliver the message to the carrier. It does not reflect any information about whether or not the carrier was able to deliver the message or not.
If the carrier accepts the message from us, we mark the message as Sent.
Hope that helps.
I have created a free account in twilio for sending SMS through my website. After my registration I got a twilio number like XXX-XXX-XXXX. I am able to send message to mobile numbers but I don't know how to use this twilio number for receiving SMS. Please help me out on this.
Twilio evangelist here.
The way Twilio notifies you of both incoming SMS message and incoming voice phone calls is by using something called a webhook. This is basically an HTTP request that Twilio makes to a URL that you tell us about. Normally this URL is a web app that you've created and published to a public website.
http://en.wikipedia.org/wiki/Webhook
In your Twilio dashboard, click on the Numbers tab and you will see your trial number.
https://www.twilio.com/user/account/phone-numbers/incoming
Click the phone number and you will see two input fields, one for a Voice URL and one for a SMS url. Just put your URL's there and click the save button.
Now if you send a text message to your number, Twilio will make an HTTP request to the URL you've told us about. You can think of this like a standard Form submit. Twilio will send along parameters like the number the SMS is coming from and the body of the message. The full list of parameters we send to the SMS url is here:
https://www.twilio.com/docs/api/twiml/sms/twilio_request
Your app can grab those parameters and use them however it wants. Your app can also return TwiML instructions to us that tell us to respond to the SMS.
https://www.twilio.com/docs/api/twiml/sms/your_response
You might want to check out our quickstarts for examples of how to receive a text message and and howtos for lots more examples of both sending and receiving SMS messages.
https://www.twilio.com/docs/quickstart/php/sms/hello-monkey
https://www.twilio.com/docs/howto
Hope that helps.
Devin