PHP API track Twilio Responses - php

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.

Related

Twilio sms reply feature

I've created a system based on cron job which generates new tasks for user. When new task is generated cron file sends a sms to user using twilio sms api. Now I want make something like when user reply to that sms with word "Done" that task associated with sms will be completed in my database.
I've set reply URL in twilio account. And everything is working fine. My point is how can I complete that task in database. Is there any feature exist in twilio that can help me to send extra parameter as task id, so that I can fetch that id from reply and update the status of task.
Twilio developer evangelist here.
There's no way in SMS to reply to a specific message (try it yourself in your SMS app on your phone), so there's no ID that you can pass around secretly within conversations.
There are a few ways you can work around this.
If the user is sent only one task at a time, then you can record what their current task is and when you receive the message saying "done" tick that one off and send them the next task.
You could send them a task ID within the message, and tell them to include that ID when they respond to say they are done. You could then parse the ID out of the message and mark it as done in your database.
Or, and this might be too far, you could use a pool of numbers to send tasks out such that each live task for a user has a unique number the user responds to. Then you can look up the task based on the user and the number they replied to. Then you can free up the number for a new task.
Let me know if this helps at all.

Track conversation chain from different server sessions to one number

I'm creating an app which will allow all users to initiate conversation with page owner using Chat-to-SMS service.
Problem is that I'm not sure how to track conversations since there would be one conversation chain from SMS Provider <-> page owner. When owner clicks reply, it needs to know which session user it needs to send the message to.
Basically, I need to do opposite what is being explained here: https://www.twilio.com/docs/quickstart/php/sms/tracking-conversations
How should I accomplish this? I can't quite wrap my head around this.
Twilio developer evangelist here.
If you are sending all the chat messages to just one SMS number then there is no easy way to tie replies back to the original message. (As a quick experiment, if you open an SMS conversation in your own phone and try to reply to any message that wasn't the latest one, you'll see it's not possible.)
There are workarounds though.
You could, when forwarding the message, generate an ID for it. Then get your page owner to include that ID when responding to that message, that way you can route the message back to the original sender and strip out the ID.
Alternatively, when replying you could always respond to the last message that came in. This relies on there not being much traffic, allowing the page owner to respond before the next message arrives. This is error prone though.
Another alternative is purchasing a new number for each new conversation. You could expire the number after a predetermined amount of time. This is made much easier with Twilio Proxy, which was announced recently and is currently in preview.
Let me know if that helps at all.

How to record and store Twilio SMS in MySQL with no additional action?

What I am trying to accomplish is quite simple. I want to store all received SMS messages from a Twilio number in a database. Upon checking Twilio's documentation, their TwiML word should be able to do this, but no attribute relates to this:
https://www.twilio.com/docs/api/twiml/sms
Twilio developer evangelist here.
When your Twilio number gets sent an SMS, your application receives a webhook at the URL you supply in the settings for the number. For each messages, the webhook sends a number of parameters telling you about the message. The documentation for that can be found here: https://www.twilio.com/docs/api/twiml/sms/twilio_request.
You might want to take a look at our PHP Quickstart tutorial which will give you a good idea of how to get the details from the request. You will then just have to pick which attributes you want to store in your database.
Let me know if this helps.

twilio sms api - is my sms delivered or NOT-delivered to end user?

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.

How to receive SMS to a twilio number

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

Categories