使用C#、WhatsApp和Azure函数的天天狗无法运行。

问题描述 投票:0回答:1

希望有人能帮我解决这个问题。

我一直在尝试学习一些关于azure的功能 用twilio创建一个聊天机器人。

我是按照这个博客教程来的。

https:/www.twilio.comblogdraft-daily-dog-with-c-whatsapp-and-azure-functions-2

我正确地完成了所有的步骤,但当我尝试运行它时,我得到一个错误......。

2020-05-06T01:04:27.202 [Error] Function compilation error
Microsoft.CodeAnalysis.Scripting.CompilationErrorException : Script compilation failed.
   at async Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.CreateFunctionTarget(CancellationToken cancellationToken) at D:\a\1\s\src\WebJobs.Script\Description\DotNet\DotNetFunctionInvoker.cs : 314
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.Azure.WebJobs.Script.Description.FunctionLoader`1.GetFunctionTargetAsync[T](Int32 attemptCount) at D:\a\1\s\src\WebJobs.Script\Description\FunctionLoader.cs : 55
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.GetFunctionTargetAsync(Boolean isInvocation) at D:\a\1\s\src\WebJobs.Script\Description\DotNet\DotNetFunctionInvoker.cs : 183
2020-05-06T01:04:27.247 [Warning] D:\home\site\wwwroot\TimerTrigger1\sendMessage.csx(8,26): warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
2020-05-06T01:04:27.273 [Error] run.csx(19,29): error CS4033: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.
2020-05-06T01:04:27.324 [Error] run.csx(25,9): error CS0103: The name 'responseMessage' does not exist in the current context
2020-05-06T01:04:27.354 [Error] run.csx(27,31): error CS0103: The name 'responseMessage' does not exist in the current context
2020-05-06T01:04:27.395 [Error] run.csx(27,25): error CS4033: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.
2020-05-06T01:04:27.430 [Error] run.csx(31,5): error CS4033: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.
2020-05-06T01:04:27.478 [Error] Executed 'Functions.TimerTrigger1' (Failed, Id=24245bff-8b18-47e2-8019-fec74f735540)
Script compilation failed.

我写 "复制 "了以下三个文件...。


run.csx

#r "Newtonsoft.Json"
#load "sendMessage.csx"

using System;
using System.Net;
using System.Net.Http.Headers;
using Newtonsoft.Json;

public static void Run(TimerInfo myTimer, ILogger log)
{
    var imageUrl = "https://images.dog.ceo/breeds/pinscher-miniature/n02107312_4650.jpg";

    using(var client = new HttpClient())
    {
      var apiEndPoint = "https://dog.ceo/api/breeds/image/random";
      client.BaseAddress = new Uri(apiEndPoint);
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

      var responseMessage = await
          client
              .GetAsync(apiEndPoint);

    }

    if (responseMessage.IsSuccessStatusCode)
    {
      var jsonContent = await responseMessage.Content.ReadAsStringAsync();
      dynamic data = JsonConvert.DeserializeObject(jsonContent);
      imageUrl = data.message;       
    }
    await SendMessage(imageUrl);
}

sendMessage.csx


using System;
using System.Net;
using System.Net.Http.Headers;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

public static async Task SendMessage(string imageUrl)
{

   TwilioClient.Init(
              Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID"),
              Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN")

        );

   var message =  MessageResource.Create(
        from: new PhoneNumber("whatsapp:+14155238886"),
        to: new PhoneNumber("whatsapp:" + Environment.GetEnvironmentVariable("MY_PHONE_NUMBER")),
        body: "Your daily cat!",
        mediaUrl: new List<Uri>{new Uri(imageUrl)}

        );

    Console.WriteLine("Message SID: " + message.Sid);

}

function.proj

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>  
    <ItemGroup>
        <PackageReference Include="twilio" Version="5.27.2"/>
    </ItemGroup>
</Project>

请你给我一个建议,我做错了什么?

我真的很感激你的帮助!

谢谢!!!

c# azure twilio
1个回答
0
投票

有很多编译错误,如错误信息所示。

  1. 你只能在异步方法中使用 await。
  2. 你应该在全局范围内声明responseMessage
© www.soinside.com 2019 - 2024. All rights reserved.