twilio-labs / twilio-aspnet

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

Add extension methods to `VoiceResponse` and `MessagingResponse` to convert to `TwiMLResult` #61

Closed Swimburger closed 2 years ago

Swimburger commented 2 years ago

We could add extension methods to make the TwiML generation code more eloquent. Consider this code:

app.MapPost("/submit", () =>
{
    var voiceResponse = new VoiceResponse()
        .Say($"You pressed 1. Good choice. Goodbye.");
    return Results.Extensions.TwiML(voiceResponse);
});

Which could become:

app.MapPost("/submit",  () => 
    new VoiceResponse()
        .Say($"You pressed 1. Good choice. Goodbye.")
        .ToTwiMLResult()
);

with a convenient extension method.

dprothero commented 2 years ago

@Swimburger Seems cool. It'd be amazing if you could just return a VoiceResponse object and it automatically converts it to a TwiMLResult for you without needing any function, however. Would that just involve overriding the .ToString() method of VoiceResponse, or something else perhaps?

Swimburger commented 2 years ago

@dprothero that would get the XML into the body, but wouldn't set the content-type header. Oh, and it would probably try to convert it to JSON, not a string :/

Swimburger commented 2 years ago

Implemented by #101.