【问题标题】:How to check message errors with the new Twilio API?如何使用新的 Twilio API 检查消息错误?
【发布时间】:2018-02-10 14:01:12
【问题描述】:

我正在将一些使用旧的 Twilio C# API 的代码升级到新的 API,但现在我无法弄清楚您在发送消息后如何检查错误。我能看到的所有示例都没有错误检查。所以旧代码是这样的:

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);

但是有了我开始使用的新 API 样式:

var twilioResponseMessage = MessageResource.Create(
    to: new PhoneNumber(to),
    from: new PhoneNumber(_from),
    body: message
);

...响应消息对象上没有RestException 属性。我要如何检查RestException 的状态和代码?

【问题讨论】:

标签: c# twilio


【解决方案1】:

这里是 Twilio 开发者宣传员。

恐怕我们没有关于在发送 SMS 消息时捕获错误的具体文档。不过,我可以尝试举个例子(我并不是真正的 C# 开发人员,但如果我错了,我敢打赌 Devin 可以纠正我?)。它应该看起来像这样:

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}"));
}

the quickstart guide to making phone calls with C# 中有一个进行 API 调用和捕获错误的示例。

让我知道这是否有帮助。

编辑

看了你的评论后,我意识到还有更多!

您的原始代码尝试使用字母数字 ID 发送消息,如果失败,则回退到常规数字。实际上,在 Twilio 中使用 Messaging Services 有更好的方法来实现这一点。

消息服务包含一系列围绕号码池发送 SMS 消息的功能。在您的情况下,可以使用字母发件人 ID 和后备号码设置消息传递服务。然后,当您 use the service to send an SMS 时,它将首先尝试 alpha 发件人 ID 并回退到您的号码,就像您的代码当前所做的那样。这可以消除应用程序中的一级错误捕获,并将该逻辑留给消息传递服务。您的代码只需要处理联系 API 时出现的错误。

你会这样做。

在您的 Twilio 控制台中创建消息服务。将您的备用号码添加到号码池中。 Add an alphanumeric sender ID to the copilot features。获取消息服务 sid 并在以下代码中使用它:

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);
}

我相信使用这样的消息服务,并以这种方式处理异常将复制您已经拥有的行为,将 alpha 发送者/备用号码逻辑留给 Twilio,并让您更新您的库。

【讨论】:

  • 我想要弄清楚的是与以前所做的新的等价物。之前,它显然是检查 HTTP 400,然后检查 RestException.Code,然后再检查 Status 是否“失败”。我不太确定新 API 会是什么样子。
  • 您正在检查 400 错误,因为您试图查看是否需要在不使用字母数字 ID 的情况下重试。我已经用我认为现在解决这个问题的最佳方法更新了我的答案。
  • @philnash 快速入门不再有 try/catch 的示例 :( twilio.com/docs/quickstart/csharp/rest/call-request
  • @BarryMSIH 你说得对,这里有一节处理错误:twilio.com/docs/libraries/csharp-dotnet/…
【解决方案2】:

这是来自 twilio 开发网站的示例:https://www.twilio.com/docs/libraries/csharp-dotnet/usage-guide#handling-errors

    {
        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}");
    }

【讨论】:

    猜你喜欢
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 2019-01-06
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    相关资源
    最近更新 更多