简体   繁体   中英

how to consume IBM watson conversation api from javascript and C#

How do I consume IBM watson conversation api from javascript and C#?

I tried the below code, but it is not working:

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();
                }

IBM Watson API's work with REST calls, you can simply use any language and use REST to call the Watson API's that you want.

But, recently, IBM Developers created the package to use in your code, and, you can use the library from IBM Developers to built apps with this link .

Check the official example:

//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
        }

You could use the .NET SDK library to connect to Watson. https://github.com/watson-developer-cloud/dotnet-standard-sdk

But if you want to connect by the REST API in C#. Here is the documentation https://www.ibm.com/watson/developercloud/conversation/api/v1/

So a quick dirty example to send a message based off the documentation would be like this.

            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;

This question talks about using jQuery $.ajax to call the Watson REST API. Use IBM watson API with jquery's $.ajax That could be helpful if you want to call from javascript with jQuery.

Use the Watson Developer Cloud .NET Standard SDK available on Nuget or Github .

You will need to create an instance of the Conversation service on Bluemix and create a Workspace to interact with.

In your project you can instantiate the service and message that instance using your 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);

I had to add version ("2018-02-16") to the ConversationService constructor and pass also credentials through the constructor to make it work for me (based on example by @taj):

using IBM.WatsonDeveloperCloud.Conversation.v1.Model;
using IBM.WatsonDeveloperCloud.Conversation.v1;

static void Main(string[] args)
{
    // create a Language Translator Service instance
    ConversationService _conversation = new ConversationService("<username>", "<password>", "<version>");

    //  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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM