Home > front end >  How to receive video call events in ASP.NET for Twilio Video
How to receive video call events in ASP.NET for Twilio Video

Time:04-09

I am creating a webhook for the status callback for video calls with Twilio. The issue is that I want to filter events that occur so that I can do some other things with that information. How do I receive those events when I create a video call?

CodePudding user response:

In ASP.NET Core you can handle webhooks using MVC or Minimal APIs (Endpoints).


MVC controllers are able to bind the form parameters to strongly-typed object parameters (also called model binding) or to simple parameters.

Here's a controller that will bind the form parameters to an instance of RoomStatusRequest and then log some of its properties:

using Microsoft.AspNetCore.Mvc;
using RoomStatusCallback.Models;

namespace RoomStatusCallback.Controllers;

public class RoomController : Controller
{
    private readonly ILogger<RoomController> logger;

    public RoomController(ILogger<RoomController> logger)
    {
        this.logger = logger;
    }

    public ActionResult Status(RoomStatusRequest statusRequest)
    {
        logger.LogInformation(@"Room created
Name: {RoomName}
Sid: {RoomSid}
Status: {RoomStatus}
Type: {RoomType}",
            statusRequest.RoomName,
            statusRequest.RoomSid,
            statusRequest.RoomStatus,
            statusRequest.RoomType
        );
        return Ok();
    }
}

You can create the RoomStatusRequest class anywhere in your project:

namespace RoomStatusCallback.Models;

public class RoomStatusRequest
{
    public string AccountSid { get; set; }
    public string RoomName { get; set; }
    public string RoomSid { get; set; }
    public string RoomStatus { get; set; }
    public string RoomType { get; set; }
    public string StatusCallbackEvent { get; set; }
    public DateTime Timestamp { get; set; }
}

The form parameters will differ depending on the type of event you're receiving, so you'll need to add/remove properties depending on your needs. This class is not all-encompassing to bind all status events properties.

Alternatively, you can have MVC bind to simple parameters like this:

using Microsoft.AspNetCore.Mvc;
using RoomStatusCallback.Models;

namespace RoomStatusCallback.Controllers;

public class RoomController : Controller
{
    private readonly ILogger<RoomController> logger;

    public RoomController(ILogger<RoomController> logger)
    {
        this.logger = logger;
    }

    public ActionResult Status(
        string roomName,
        string roomSid,
        string roomStatus,
        string roomType
    )
    {
        logger.LogInformation(@"Room created
Name: {RoomName}
Sid: {RoomSid}
Status: {RoomStatus}
Type: {RoomType}",
            roomName,
            roomSid,
            roomStatus,
            roomType
        );
        return Ok();
    }
}

Whether you configured the StatusCallbackMethod as POST or GET, MVC will bind it from the form parameters or query string parameters respectively.


For Minimal APIs (Endpoints), you can accept the parameters like this for POST requests:

app.MapPost("/minimal/room/status", async (HttpRequest request) =>
{
    var form = await request.ReadFormAsync();

    var roomName = form["RoomName"];
    var roomSid = form["RoomSid"];
    var roomStatus = form["RoomStatus"];
    var roomType = form["RoomType"];
    app.Logger.LogInformation(@"Room created
Name: {RoomName}
Sid: {RoomSid}
Status: {RoomStatus}
Type: {RoomType}",
        roomName,
        roomSid,
        roomStatus,
        roomType
    );

    return Results.Ok();
});

Grab the form from the request, and then use string indexing to retrieve individual parameters.

And for GET requests:

app.MapGet("/minimal/room/status", (
    [FromQuery] string roomName,
    [FromQuery] string roomSid,
    [FromQuery] string roomStatus,
    [FromQuery] string roomType
) =>
{
    app.Logger.LogInformation(@"Room created
Name: {RoomName}
Sid: {RoomSid}
Status: {RoomStatus}
Type: {RoomType}",
        roomName,
        roomSid,
        roomStatus,
        roomType
    );

    return Results.Ok();
});

app.Run();

You can use individual parameter binding like in MVC. You don't need to specify [FromQuery] but I like to do so. If you don't use [FromQuery], ASP.NET will also look in the route values, headers, and more to bind to your parameters.

For all the samples above, the parameter and property names have to match those in the form parameters, although you can use certain attributes to change that, but I'd advise to research MVC Model Binding and Minimal API Parameter Binding to learn more about that.

You can find the source code for these samples and a sample to create the video room in this GitHub repository.

  • Related