twilio-labs / twilio-aspnet

Integrate Twilio Programmable Messaging and Voice with ASP.NET Respond to webhooks with TwiML in seconds
Apache License 2.0
58 stars 30 forks source link

Extend SmsRequest with collection of media links for MMS webhook #125

Open dmitry-pavlov opened 1 year ago

dmitry-pavlov commented 1 year ago

Would be great to extend SmsRequest class wrapping the Twilio POST webhook request for incoming MMS with the property bound to media files collection. Now to retrieve the media files from Twilio webhook call on ASP.NET Core API (while I am getting incoming MMS from external phone number) I have to read from Request.Form to retrieve media related parameters.

    [HttpPost("message")]
    [AllowAnonymous]
    public async Task<TwiMLResult> MessageWebhook(SmsRequest request, CancellationToken token) {
        ...
        for (int i = 0; i < request.NumMedia; i++)
        {
            string? mediaUrl = Request.Form[$"MediaUrl{i}"];
            string? contentType = Request.Form[$"MediaContentType{i}"];
            ...
        }
    }

Would be great to just mark SmsRequest request as [FromForm] SmsRequest request and access media related items like this:

List<MediaFile> mediaFiles = request.MediaFiles;

So ASP.NET Core controller for handling SMS/MMS webhooks woul dlook like this:

using Twilio.AspNet.Common; using Twilio.AspNet.Core; // or .Mvc for .NET Framework using Twilio.TwiML;

public class WebhookController : TwilioController
{
    [HttpPost("message")]
    [AllowAnonymous]
    public async Task<TwiMLResult> MessageWebhook([FromForm] SmsRequest request, CancellationToken token) {
        ...
        foreach (var media in request.MediaFiles)
        {
            string mediaUrl = media.Url;
            string contentType = media.ContentType;
         }
        ... 
    }
}