Twilio Messaging - Correlating SID

こ雲淡風輕ζ 提交于 2019-12-12 04:12:56

问题


We use twilio for sending message.

We are not sure how to correlate the response with the message we send. We might send multiple messages to the same Mobile. But, not sure how to correlate response with the messages we sent as the SID's are different.

Is there anyway to relate the response with the message.

Thanks


回答1:


No, SMS doesn't work like that.

I you send me 5 text messages from your cellphone and then I reply to one you have no way of telling which one I'm replying to.

It's not a Twilio limitation, the SMS standard has no provision to track replies to individual messages

As an afterthought I came up with a hacky solution to this. It's a bit involved so I guess it depends how much you want the functionality.

This works for me using Chrome beta on Android 7.0, YMMV.

Create a php script with the following code and put it on your webserver:

<?php
// increase last digit as necessary to suit string length of your variable
$smsid = substr($_SERVER["QUERY_STRING"],0,1);

// Query database for SMS id, record timestamp of request, optionally return text to be included at the beginning of the SMS reply
$msg= urlencode($databaseResult);

// Remove <?body=$msg> if you just want the link to create a blank reply. Change the phone number to your incoming Twilio number.
header( "Location: sms:+1555444333?body=$msg" )

Now sign up for a URL shortening service which passes URL parameters and create a shortened URL which points to your php script. I used tr.im.

Depending upon your volume of SMS you will have to adjust the length of your variable, but unless you spam people to death I'm going to assume a single character will be enough to identify a unique text.

Using the example tr.im/SMS as your shortened url, you append a variable to the end like so tr.im/SMS?A and put the link in your outgoing SMS. When the user clicks the link your server redirect will open the SMS app on their phone and create a text to your number. If you have included the "?body=$msg" in your php above the new message will have your text at the start.

Personally I probably wouldn't bother adding text, they might delete it before they send it anyway and it's just likely to confuse people. If you log the request variables and timestamps to your database you should be able to tie them together with the phone number as most people will send you their reply within a couple of minutes of the server request. You can also increase the length of your custom URL variable if you struggle to correlate messages. Recycle variables once you have linked a reply etc...

Finally change your Twilio configuration so your outgoing SMS present the company name instead of your Twilio number as the sender. Users cannot directly reply to messages if the sender isn't a number, so they will have to use your link.

Generate a sequential identifier for each message and append it to your link. Save the identifier to your database along with the corresponding message Sid from Twilio and the number you sent it to so you can match them up later.

Append "Click tr.im/SMS?$ to reply" to outgoing SMS, where $ is your variable.

Profit.



来源:https://stackoverflow.com/questions/45178678/twilio-messaging-correlating-sid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!