Programmatically bind a SMS response to original message

When I send an automated SMS from my app using the Twilio API, I get an Sid back in the API response as per the documentation e.g.

var message = MessageResource.Create(

           body: "McAvoy or Stewart? These timelines can get so confusing.",

           from: new Twilio.Types.PhoneNumber("+15017122661"),

           statusCallback: new Uri(https://your-bin-id-here.x.pipedream.net/sms_status_callback),

           to: new Twilio.Types.PhoneNumber("+15558675310")

       ); 

       Console.WriteLine(message.Sid);


If the recipient responds to that SMS is there a way to connect the response to the original SMS in my webhook when processing the request from Twilio i.e., does it contain the original SID? I need to group the messages together into a conversation.

I know I get the Sid in a status callback but the web hook documentation for incoming messages only contains the Sid for the new message (or that’s how it reads), but not the message being replied to. https://www.twilio.com/docs/messaging/guides/webhook-request

Is there a way to do this? Maybe with sessions or something?

Thanks,

Seamus

Answers

  • Hey Seamus, there is no way to do this I’m afraid, the SMS protocol has no information about users replying to a specific message. SMS is chronological l, you can test this by opening your phone’s SMS app and trying to reply to the second to last message you received from someone. In SMS there’s no way to do that.

    I suspect your are interested in this because you need to send notifications and get responses to those notifications and you realised you might need to send more than one notification to a user at a time but still get their responses. The best way to get around this is to use different numbers to send the different notification messages from, then when they reply you can tell which notification they were replying to by the number they sent the message to.

  • Hi Pnash,

    Thanks for getting back to me so quickly. What I am trying to do is store information about the outgoing SMS per recipient e.g. a messageId and a recipientId, and then load that data when an SMS is responded to.

    That way I can log the response against the recipient of the original SMS. Which what I'm trying to do ultimately.

    If I can't get the original sid in the response, I had the thought to use the "to" number of the SMS when sending, add that to storage with the sid and our recipientid, then when the response is processed in our callback use the "from" number of the message to load the original data, and determine the recipient that way. However, I have some concerns about whether or not we can trust that the “to” number of the send will match exactly the “from” number of the response, e.g. if the “to” number of the send contained dashes, but the “from” number of the response did not but both were the same number.

    I feel like I'm missing something here. I would imagine others have solved this problem before, how to show all messages to and from a particular recipient.


    Any help with this would be greatly appreciated.

    Seamus

  • Hey @sbarrett, ok this actually sounds like a simpler situation than I was describing, so good news I guess!

    You can absolutely use the "to" and "from" numbers to match up messages sent to and from a particular user. Twilio deals with numbers in e.164 format, which is a standard for international phone numbers. To send messages to a user, you need to format the number in e.164 format and then when Twilio delivers a webhook for a new incoming message all the numbers will also be formatted that way.

    One other thing you could look into is cookie support in Twilio webhooks. This doesn't apply when you are using the REST API to initiate sending a message, but if you are having a conversation with your user via TwiML and webhooks you can set cookies on your TwiML responses and Twilio will send those cookies back to you with the next response. Check out this tutorial on creating an SMS conversation using cookies for more detail.

  • Thanks @pnash that was very helpful

If this is an emergency, please contact Twilio Support. This is not an official Support channel. https://support.twilio.com/
Have an urgent question?
Please contact Twilio Support. This is not an official Support channel.
Contact Support