Search code examples
c#.net-coretwiliotwilio-apitwilio-programmable-chat

How to capture the sender number in twilio sms "receive and reply to inbound sms" in .netcore?


I'm testing out Twilio SMS and I faced a couple of issues. I'm trying to do an inbound SMS and send a reply to the user with the message body that the user sent.

        [HttpPost("Receive")]
    public TwiMLResult Receive()
    {
        var messagingResponse = new MessagingResponse();
        messagingResponse.Message("Hello to you too");

        return TwiML(messagingResponse);
    }

This is what I have done so far,
1. when I reply to the Twilio number I'm getting this message. How can I append the message the user sends into this message? Example: If the user sends "Hello", the respond message should be "Hello to you too and you said Hello"

  1. I need to capture the sender's number to use later. How can I capture the number and if the answer is to add a statusCallback URL, How to add it here?

Thanks


Solution

  • The request body from an inbound SMS has a POST form with fields for from, to and body.

    So to get the senders number you could use this code:

    var from = Request.Form["From"];
    

    Other fields

    var to = Request.Form["To"]; // your twilio number
    var body = Request.Form["Body"]; // the message they sent
    

    There is a more detailed example in Twilio docs on conversations