2017-05-10 13 views
0

JavascriptおよびC#からIBMワットソン会話APIを使用するにはどうすればよいですか?JavascriptおよびC#からIBMワトソンの会話APIを消費する方法

私は以下のコードを試してみましたが、それが動作していない:RESTと

if (string.IsNullOrEmpty(context)) req = "{\"input\": {\"text\": \"" + input + "\"}, \"alternate_intents\": true}"; 
       else req = "{\"input\": {\"text\": \"" + input + "\"}, \"alternate_intents\": true}, \"context\": \"" + context + "\""; 
       using (var handler = new HttpClientHandler 
       { 
        Credentials = _NetCredential 
       }) 
       using (var client = new HttpClient(handler)) 
       { 
        var cont = new HttpRequestMessage(); 
        cont.Content = new StringContent(req.ToString(), Encoding.UTF8, "application/json"); 
        var result = await client.PostAsync(_Server, cont.Content); 
        return await result.Content.ReadAsStringAsync(); 
       } 
+0

最近、私はちょうどhttp://restsharp.org/を使用してwatsonに接続しました – Webbanditten

答えて

0

IBMワトソンAPIの作品は、あなたが簡単に任意の言語を使用して、必要ワトソンAPIのことを呼び出すためにRESTを使用することができます呼び出します。

しかし、最近では、IBM Developersがコードで使用するパッケージを作成し、IBM Developersのライブラリを使用してこのlinkのアプリケーションをビルドすることができます。

//import librarys 
using IBM.WatsonDeveloperCloud.Conversation.v1; 
using IBM.WatsonDeveloperCloud.Conversation.v1.Model; 
using System; 

namespace IBM.WatsonDeveloperCloud.Conversation.Example 
{ 
    public class ConversationServiceExample 
    { 
     private ConversationService _conversation = new ConversationService(); 
     private string _workspaceID; 
     private string _inputString = "Turn on the winshield wipers"; 

     //set username, password with Service Crentials 
     public ConversationServiceExample(string username, string password, string workspaceID) 
     { 
      _conversation.SetCredential(username, password); 
      _workspaceID = workspaceID; //workspace_id from your conversation created 

      Message(); //send message 
     } 
0

あなたがワトソンに接続するには、.NET SDKライブラリを使用することができます。

official例を確認してください。 https://github.com/watson-developer-cloud/dotnet-standard-sdk

ただし、C#でREST APIで接続する場合は、ここにドキュメントがありますhttps://www.ibm.com/watson/developercloud/conversation/api/v1/

したがって、ドキュメントに基づいてメッセージを送信するためのすばらしい汚い例は、このようになります。

  HttpClient client = new HttpClient(); 
      client.BaseAddress = new Uri("https://gateway.watsonplatform.net/conversation/api/v1/workspaces/25dfa8a0-0263-471b-8980-317e68c30488/message?version=2017-04-21"); 

      var json = "{\"input\": {\"text\": \"Turn on the lights\"}, \"context\": {\"conversation_id\": \"1b7b67c0-90ed-45dc-8508-9488bc483d5b\", \"system\": {\"dialog_stack\":[{\"dialog_node\":\"root\"}], \"dialog_turn_counter\": 1, \"dialog_request_counter\": 1}}}"; 

      var content = new StringContent(json, Encoding.UTF8, "application/json"); 
      var result = client.PostAsync(url, content).Result; 

この質問では、jQuery $ .ajaxを使用してWatson REST APIを呼び出す方法について説明します。 Use IBM watson API with jquery's $.ajax これは、jQueryを使用してJavaScriptから呼び出したい場合に役立ちます。

0

NugetまたはGithubで利用可能なWatson Developer Cloud .NET Standard SDKを使用してください。

BluemixでConversation serviceのインスタンスを作成し、対話するにはWorkspaceを作成する必要があります。

プロジェクトでは、サービスをインスタンス化し、そのインスタンスにワークスペースIDを使用してメッセージを送信できます。

// create a Language Translator Service instance 
ConversationService _conversation = new ConversationService(); 

// set the credentials 
_conversation.SetCredential("<username>", "<password>"); 

// create message request 
MessageRequest messageRequest = new MessageRequest() 
{ 
    Input = new InputData() 
    { 
    Text = "<input-string>" 
    } 
}; 

// send a message to the conversation instance 
var result = _conversation.Message("<workspace-id>", messageRequest); 
関連する問題