Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README.md #50

Merged
merged 1 commit into from Mar 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
134 changes: 80 additions & 54 deletions README.md
Expand Up @@ -8,21 +8,6 @@ You only need this library if you wish to respond to Twilio webhooks for
voice calls and SMS messages. If you only need to use the Twilio REST API's,
then you only need the [Twilio SDK for C# and .NET](https://github.com/twilio/twilio-csharp).

## Twilio.AspNet.Mvc

[![NuGet Badge](https://buildstats.info/nuget/Twilio.AspNet.Mvc)](https://www.nuget.org/packages/Twilio.AspNet.Mvc/)

### Requirements

Requires .NET 4.5.1 or later with ASP.NET MVC 3-5.

### Installation
From the Package Manager Console or Developer PowerShell, run the following command to install the latest version:
```PowerShell
Install-Package Twilio.AspNet.Mvc
```
Alternatively, use the NuGet Package Manager for Visual Studio or the NuGet window for JetBrains Rider, then search for _Twilio.AspNet.Mvc_ and install the package.

## Twilio.AspNet.Core
[![NuGet Badge](https://buildstats.info/nuget/Twilio.AspNet.Core)](https://www.nuget.org/packages/Twilio.AspNet.Core/)
### Requirements
Expand All @@ -41,28 +26,40 @@ Install-Package Twilio.AspNet.Core
```
Alternatively, use the NuGet Package Manager for Visual Studio or the NuGet window for JetBrains Rider, then search for _Twilio.AspNet.Core_ and install the package.

## Twilio.AspNet.Mvc

[![NuGet Badge](https://buildstats.info/nuget/Twilio.AspNet.Mvc)](https://www.nuget.org/packages/Twilio.AspNet.Mvc/)

### Requirements

Requires .NET 4.5.1 or later with ASP.NET MVC 3-5.

### Installation
From the Package Manager Console or Developer PowerShell, run the following command to install the latest version:
```PowerShell
Install-Package Twilio.AspNet.Mvc
```
Alternatively, use the NuGet Package Manager for Visual Studio or the NuGet window for JetBrains Rider, then search for _Twilio.AspNet.Mvc_ and install the package.

## Code Samples for either Library

### Incoming SMS
```csharp
using Twilio.AspNet.Common;
using Twilio.AspNet.Mvc; // or .Core
using Twilio.AspNet.Core; // or .Mvc for .NET Framework
using Twilio.TwiML;

namespace MyWebApplication.Controllers
public class SmsController : TwilioController
{
public class SmsController : TwilioController
// GET: Sms
public TwiMLResult Index(SmsRequest request)
{
// GET: Sms
public TwiMLResult Index(SmsRequest request)
{
var response = new MessagingResponse();
response.Message(
$"Hey there {request.From}! " +
"How 'bout those Seahawks?"
);
return TwiML(response);
}
var response = new MessagingResponse();
response.Message(
$"Hey there {request.From}! " +
"How 'bout those Seahawks?"
);
return TwiML(response);
}
}
```
Expand All @@ -73,20 +70,17 @@ By inheriting from the `TwilioController`, you get access to the `TwiML` method

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

namespace MyWebApplication.Controllers
public class VoiceController : TwilioController
{
public class VoiceController : TwilioController
// GET: Voice
public TwiMLResult Index(VoiceRequest request)
{
// GET: Voice
public TwiMLResult Index(VoiceRequest request)
{
var response = new VoiceResponse();
response.Say($"Welcome. Are you from {request.FromCity}?");
return TwiML(response);
}
var response = new VoiceResponse();
response.Say($"Welcome. Are you from {request.FromCity}?");
return TwiML(response);
}
}
```
Expand All @@ -98,27 +92,24 @@ By inheriting from the `TwilioController`, you get access to the `TwiML` method
If you can't inherit from the `TwilioController` class, you can use the `TwiML` extension methods.
```csharp
using Twilio.AspNet.Common;
using Twilio.AspNet.Mvc; // or .Core
using Twilio.AspNet.Core; // or .Mvc for .NET Framework
using Twilio.TwiML;

namespace MyWebApplication.Controllers
public class SmsController : Controller
{
public class SmsController : Controller
// GET: Sms
public TwiMLResult Index(SmsRequest request)
{
// GET: Sms
public TwiMLResult Index(SmsRequest request)
{
var response = new MessagingResponse();
response.Message(
$"Hey there {request.From}! " +
"How 'bout those Seahawks?"
);
return this.TwiML(response);
}
var response = new MessagingResponse();
response.Message(
$"Hey there {request.From}! " +
"How 'bout those Seahawks?"
);
return this.TwiML(response);
}
}
```
This sample is the same as the previous SMS webhook sample, but instead of inheriting from `TwilioController`, the `SmsController` inherits from the ASP.MVC provided `Controller`, and uses `this.TwiML` to use the `TwiML` extension method.
This sample is the same as the previous SMS webhook sample, but instead of inheriting from `TwilioController`, the `SmsController` inherits from the ASP.NET MVC provided `Controller`, and uses `this.TwiML` to use the `TwiML` extension method.

### Minimal API
Twilio.AspNet.Core also has support for Minimal APIs.
Expand Down Expand Up @@ -167,6 +158,41 @@ Here's the list of classes:
- `StatusCallbackRequest`: Holds data for tracking the status of an outbound Twilio Voice Call
- `VoiceRequest`: Holds data for incoming Voice Calls

TODO: Add SMS sample with SMS Status Callback

Note: Only MVC Controllers and Razor Pages supports model binding to typed .NET objects. In Minimal APIs and other scenario's, you'll have to write code to extract the parameters yourself.

The following sample shows how to accept inbound SMS, respond, and track the status of the SMS response.

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

public class SmsController : TwilioController
{
private readonly ILogger<SmsController> logger;

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

public TwiMLResult Index(SmsRequest request)
{
var messagingResponse = new MessagingResponse();
messagingResponse.Message(
body: $"Hey there {request.From}! How 'bout those Seahawks?",
action: new Uri("/Sms/StatusCallback"),
method: Twilio.Http.HttpMethod.Post
);
return TwiML(messagingResponse);
}

public void StatusCallback(SmsStatusCallbackRequest request)
{
logger.LogInformation("SMS Status: {Status}", request.MessageStatus);
}
}
```

As shown in the sample above, you can add an `SmsRequest` as a parameter, and MVC will bind the object for you.
The code then responds with an SMS with the `status` and `method` parameter. When the status of the SMS changes, Twilio will send an HTTP POST request to `StatusCallback` action. You can add an `SmsStatusCallbackRequest` as a parameter, and MVC will bind the object for you.