Home > Mobile >  Twilio C# Attach Cookie to response
Twilio C# Attach Cookie to response

Time:10-23

I have a DeliveryNotice function that gets called by Twilio after a Whatsapp message has been sent. In that function, I want to set a response cookie or a header so that when my user replies to the Whatsapp message, I am able to trace which message it originated from.

I have tried setting the cookies and headers but when the InboundMessage function is called when a User replies, I am unable to see the set cookie and even the set header.

This is my code:

[HttpPost("FileDeliveryNotice")]
[Consumes("application/x-www-form-urlencoded")]
public ActionResult FileDeliveryNotice([FromForm] WhatsappFileDeliveryNotice Input)
{
    var r = Request;
    HttpContext.Response.Cookies.Append("MessageSID", Input.MessageSid);
    Response.Headers.Add("MessageSID", Input.MessageSid);
    return Ok(res);
}

[HttpPost("InboundMessage")]
[Consumes("application/x-www-form-urlencoded")]
public ActionResult InboundMessage([FromForm] WhatsappInboundMessage Input)
{
    var r = Request;
    var res = whatsappIMData.Save(Input);
    return Ok(res);
}

EDIT:

TwilioClient.Init(accountSid, authToken);
var message = MessageResource.Create(
    body: messageText,
    statusCallback: new Uri($"{configuration[SDR_Core.ClassLibrary.Globals.ConfigurationGlobals.Whatsapp_Callback_Url]}Communication/Whatsapp/FileDeliveryNotice"),
    to: new Twilio.Types.PhoneNumber($"whatsapp: 6{Input.NewSendFileDetailsViewModel.MobileNumber}"),
    messagingServiceSid: tcd.TwilioMessagingServiceSID
);

CodePudding user response:

Twilio developer evangelist here.

You can only set cookies for Twilio messaging when you are responding to an incoming webhook of a message and not when you send a message with the REST API or receive an asynchronous status callback.

So, really what I mean is that you can only set those cookies on your InboundMessage route and that's why you're not seeing this.

The thing is, currently within Twilio there isn't really the idea of what message was being replied to. That's mostly because the API was based on SMS in the first place, which is solely chronological. There is the idea of responding to a message in WhatsApp, but that requires the user to specifically respond to the message and it is not supported in Twilio. So it is best to consider the messages as chronological with WhatsApp too. So you can always look at the last message you sent to see what the person is likely responding to.

  • Related