2016-11-08 40 views
-4

我对这项技术很陌生,但我想在.NET应用程序中使用Watson的API对话。我如何在.NET中调用Watson云服务?c#/。net中的IBM Watson Conversation API客户端的示例网络

+3

请将其发布在西班牙网站上。本网站仅为英文版。 – Carcigenicate

+1

Por支持preguntarlo [aqui](http://es.stackoverflow.com/)。 –

+1

不过要说清楚,这是一个不好的问题,用英语问的问题仍然不清楚或者过分。 – Marcin

回答

1

我认为IBM对'简单'有相当远的理解。他们的sample apps比较隐晦。最重要的是,他们最近烧毁/弃用了他们的旧API。这里是new API description。您需要先获取一些watsone凭证。

您应该可以像使用其他RESTful API一样使用v1 Converstaions API。我喜欢Flurl这个任务。

namespace WhatsOn 
{ 
    using System; 
    using System.Text; 
    using System.Linq; 
    using System.Threading.Tasks; 
    using Flurl; 
    using Flurl.Http; 
    using Newtonsoft.Json; 

    public class Program 
    { 
     public static void Main() 
     { 
      TalkToWatson().Wait(); 
     } 

     public static async Task TalkToWatson() 
     { 
      var baseurl = "https://gateway.watsonplatform.net/conversation/api"; 
      var workspace = "25dfa8a0-0263-471b-8980-317e68c30488"; 
      var username = "...get your own..."; 
      var password = "...get your own..."; 
      var context = null as object; 
      var input = Console.ReadLine(); 
      var message = new { input = new { text = input }, context }; 

      var resp = await baseurl 
       .AppendPathSegments("v1", "workspaces", workspace, "message") 
       .SetQueryParam("version","2016-11-21") 
       .WithBasicAuth(username, password) 
       .AllowAnyHttpStatus() 
       .PostJsonAsync(message); 

      var json = await resp.Content.ReadAsStringAsync(); 

      var answer = new 
      { 
       intents = default(object), 
       entities = default(object), 
       input = default(object), 
       output = new 
       { 
        text = default(string[]) 
       }, 
       context = default(object) 
      }; 

      answer = JsonConvert.DeserializeAnonymousType(json, answer); 

      var output = answer?.output?.text?.Aggregate(
       new StringBuilder(), 
       (sb,l) => sb.AppendLine(l), 
       sb => sb.ToString()); 

      Console.ForegroundColor = ConsoleColor.White; 
      Console.WriteLine($"{resp.StatusCode}: {output}"); 

      Console.ForegroundColor = ConsoleColor.Gray; 
      Console.WriteLine(json); 
      Console.ResetColor(); 
     }   
    } 
} 
1

您可以使用REST界面调用任何Watson Cloud服务,如前面的答案所示。只要确保正确格式化JSON负载,您需要的所有信息都在Conversation API Reference中。

这就是说,有一个SDK for .NET虽然它可能仍然是不成熟的。您可以在GitHub上看到Watson Developer Cloud上的所有当前SDK和实用程序。

+0

以及如何?:https://github.com/watson-developer-cloud/dotnet-standard-sdk#installing-the-watson-net-standard-sdk。 –

+1

这对我来说是新的....但看起来它应该做你想做的。 –