Home > Software design >  C# Twilio Whatsapp Inbound Message return a MediaUrl
C# Twilio Whatsapp Inbound Message return a MediaUrl

Time:11-10

I have a message with a quick reply button that I want to send to my users. The quick reply button just says accept. Once the user clicks on it, my server then is supposed to return a link to a file to them.

This works well so far. However, I noticed that the url is sent purely as a text url and not parsed as a file. Is there a way to let the Api know that I want it to be parsed as a File instead of text?

The url is structured like this: https://location.website.com/filename.pdf

This is my code for the Inbound Message Function:

[HttpPost("InboundMessage")]
[Consumes("application/x-www-form-urlencoded")]
public TwiMLResult InboundMessage([FromForm] WhatsappInboundMessage Input)
{
    var em = whatsappMessageLogData.GetAll(Input.From, TenantId);
    if (em.Count > 0)
    {
        em = em.OrderByDescending(x => x.CreatedOn).ToList();
        var latestItem = em.FirstOrDefault();
        var difference = (DateTime.Now - latestItem.CreatedOn).TotalHours;
        if (difference < 2)
        {
            var message = new Message();
            message.Body($"You can also access the file here: {latestItem.FileAccessLink}");
            message.Media(link);
            response.Append(message);
            return TwiML(response);
        }

    }
    var res = whatsappIMData.Save(Input);
    response.Message(res);
    return TwiML(response);
}

This is the twiml response. I only get the url.

<?xml version="1.0" encoding="utf-8"?>
<Response>
  <Message>
    <Body>You can also access the file here: https://localhost:44332/FileReads/Read?Key=bD4PgsE0Vqq4yMnA5S43WTourBmh3I</Body>
    <Media>https://www.filelink.pdf</Media>
  </Message>
</Response>

CodePudding user response:

Twilio developer evangelist here.

To return files as a media attachment you should nest a <Media> element within the <Message>:

        if (difference < 2)
        {
            var message = new Message();
            message.Media(latestItem.Link);
            message.Body("Here's the latest items's link");
            response.Append(message);
            return TwiML(response);
        }

See the documentation for <Message> for more details and examples.

  • Related