2016-11-08 38 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

這對我來說是新的....但看起來它應該做你想做的。 –