Reputation: 30013
I'm upgrading some code that was using the old Twilio C# API to the new API, and I can't figure out how you're meant to check for errors after sending the message now. All the examples I can see do no error checking. So the old code is this:
var attemptWithAlphanumericId = client.SendMessage(fromName, to, message);
if (attemptWithAlphanumericId.RestException == null)
{
return !string.Equals(attemptWithAlphanumericId.Status, "failed", StringComparison.OrdinalIgnoreCase);
}
if (attemptWithAlphanumericId.RestException.Status == "400"
&& attemptWithAlphanumericId.RestException.Code == "21212")
{
var attemptWithFallbackNumber = client.SendMessage(fallbackNumber, to, message);
if (attemptWithFallbackNumber.RestException == null)
{
return !string.Equals(attemptWithFallbackNumber.Status, "failed", StringComparison.OrdinalIgnoreCase);
}
throw new InvalidOperationException(attemptWithFallbackNumber.RestException.Message);
}
throw new InvalidOperationException(attemptWithAlphanumericId.RestException.Message);
But with the new API style which I've started to use:
var twilioResponseMessage = MessageResource.Create(
to: new PhoneNumber(to),
from: new PhoneNumber(_from),
body: message
);
... there's no RestException
property on the response message object. How am I meant to check RestException
's status and code?
Upvotes: 2
Views: 2674
Reputation: 3787
This is the example from the twilio dev site: https://www.twilio.com/docs/libraries/csharp-dotnet/usage-guide#handling-errors
try
{
var message = MessageResource.Create(
body: "This is the ship that made the Kessel Run in fourteen parsecs?",
from: new Twilio.Types.PhoneNumber("+15017122661"),
to: new Twilio.Types.PhoneNumber("+15558675310")
);
Console.WriteLine(message.Sid);
}
catch (ApiException e)
{
Console.WriteLine(e.Message);
Console.WriteLine($"Twilio Error {e.Code} - {e.MoreInfo}");
}
Upvotes: 5
Reputation: 73057
Twilio developer evangelist here.
We don't have specific docs on catching errors when sending SMS messages I'm afraid. I can try to give an example though (I'm not really a C# developer, but if I'm wrong I bet Devin can correct me 😄). It should look a little like this:
try {
var twilioResponseMessage = MessageResource.Create(
to: new PhoneNumber(to),
from: new PhoneNumber(_from),
body: message
);
catch (TwilioException e) {
Response.Write(string.Format($"Error: {e.Message}"));
}
There is an example of making API calls and catching errors in the quickstart guide to making phone calls with C#.
Let me know if that helps at all.
edit
After reading your comment, I realise there is more to this!
Your original code is trying to send a message using an alphanumeric ID and if that fails, fallback to a regular number. There is actually a better way to achieve this using Messaging Services within Twilio.
Messaging Services wrap up a bunch of features around number pooling for sending SMS messages. In your case, a messaging service can be set up with an alpha sender ID and a fallback number. Then, when you use the service to send an SMS it will try the alpha sender ID first and fallback to your number, just like your code currently does. This can eliminate one level of catching errors in your application and leave that logic to the messaging service. Your code only needs to deal with errors in contacting the API then.
Here's how you'd do that.
Create a messaging service in your Twilio console. Add your fallback number to the number pool. Add an alphanumeric sender ID to the copilot features. Take the messaging service sid and use it in the following code:
try {
var message = MessageResource.Create(
to: new PhoneNumber(to),
messagingServiceSid: "YOUR_MESSAGING_SERVICE_SID",
body: message
);
return !string.Equals(message.Status, "failed", StringComparison.OrdinalIgnoreCase);
catch (TwilioException e) {
throw new InvalidOperationException(e.Message);
}
I believe that using the messaging service like this, and handling the exception in this way will replicate the behaviour you already had, leave the alpha sender/fallback number logic to Twilio and let you update your library.
Upvotes: 2