关于Azure Functions的Twilio(2.0)

时间:2018-11-09 14:17:59

标签: azure twilio azure-functions

我目前正在编写Azure函数代码以通过Twilio发送SMS。

using System;
using System.Threading.Tasks;
using Twilio;
using Twilio.Types;
using Twilio.Rest.Api.V2010.Account;

public static void Run(string myQueueItem, ILogger log)
{
    log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");

    string accountSid = "My_SID";
    string authToken = "MY_TOKEN";

    TwilioClient.Init(accountSid, authToken);

    var _to = new PhoneNumber("+821073884889");
    var _from = new PhoneNumber("+15672805833");

    var message = MessageResource.Create(to: _to, from: _from, body: "Hello world");
}

运行此代码时,出现以下错误消息

2018-11-09T14:06:47  Welcome, you are now connected to log-streaming service.
2018-11-09T14:07:28.742 [Information] Executing 'Functions.sendSMSFunction2' (Reason='This function was programmatically called via the host APIs.', Id=c21afe21-8af0-4dde-a730-236377f790b9)
2018-11-09T14:07:28.772 [Error] Executed 'Functions.sendSMSFunction2' (Failed, Id=c21afe21-8af0-4dde-a730-236377f790b9)
Method not found: 'Twilio.Rest.Api.V2010.Account.MessageResource Twilio.Rest.Api.V2010.Account.MessageResource.Create(Twilio.Types.PhoneNumber, System.String, Twilio.Types.PhoneNumber, System.String, System.String, System.Collections.Generic.List`1<System.Uri>, System.Uri, System.String, System.Nullable`1<System.Decimal>, System.Nullable`1<Boolean>, System.Nullable`1<Int32>, System.String, System.Nullable`1<Boolean>, System.String, ContentRetentionEnum, AddressRetentionEnum, System.Nullable`1<Boolean>, Twilio.Clients.ITwilioRestClient)'.
2018-11-09T14:08:47  No new trace in the past 1 min(s).
2018-11-09T14:09:48  No new trace in the past 2 min(s).

我在粘贴到这里之前删除了SID和令牌:)

所以问题看起来像MessageResource.Create部分(如上面提到的第4行)

当我在VS2017上运行以上代码时,它很好地发送了SMS,但在Azure函数上却卡住了:(

function.json

{
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "serviceBusTrigger",
      "direction": "in",
      "queueName": "myqueue",
      "connection": "servicebus8_RootManageSharedAccessKey_SERVICEBUS"
    }
  ]
}

function.proj

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Twilio" Version="5.21.0"/>
  </ItemGroup>

</Project>

请帮忙吗?

1 个答案:

答案 0 :(得分:0)

我发现有些代码在Azure Function v2上不起作用。

然后我将版本更改为1,它起作用了:)

如果像我这样只是从Visual Studio复制并粘贴代码行的人,请记住“未找到方法”意味着降级版本。

我希望这能防止其他人浪费自己的宝贵时间!