barbs
barbs

Reputation: 172

Maintain Context

I'm trying to keep the context in Watson Conversation, but it isn't working.. I already tried this answer, but it didn't worked. I'm trying to integrate the bot to the html.

I tried this in the conversation configuration:

var payload = {
workspace_id: workspace,
context: {}}; 
if (req.body) {
 if (req.body.input) {
  payload.input = req.body.input;
}
 if(req.body.context) {
  payload.context = req.body.context;
}}

And this in the code to html/javascript:

var payload = {};
var context = {}; 

function callWatson(){
alert("watson");
$.ajax({
type: 'POST',
dataType: 'JSON',
contentType: "application/json",
url: '/api/message',
data: JSON.stringify(payload)
}).done(function (json) {
if (json.output.text[0]) {
  context = payload.context;
$'<div class="message">+ json.output.text[0]+</div>').appendTo($('.container'));

function insertMessage() {
msg = $('.message-input').val();
$'<div class="message">+ msg+</div>').appendTo($('.container'));

I'm a beginner in coding and I tried coping the conversation-simple js, but I wanted something more simple and direct.. Thanks in advance!

Upvotes: 1

Views: 345

Answers (3)

Tijo Titus
Tijo Titus

Reputation: 143

No need to waste time to maintain context just delete conversation_start dialog(first dialog node) in ibm watson conversation after that give input then you will get exact output

Upvotes: 0

Simon Burns
Simon Burns

Reputation: 24

You could try using this as a starting point: https://github.com/snrubnomis/burgerbot

It's intended to be as simple as possible. The "sendMessage" function (in burgerbot.js) makes the call and then stores the returned context for use on subsequent calls.

Upvotes: -1

Sayuri Mizuguchi
Sayuri Mizuguchi

Reputation: 5330

For send message like Watson, with repository conversation-simple, you can simple add in your front-end:

var latestResponse = Api.getResponsePayload();
var context = latestResponse.context;
Api.setResponsePayload('{"output": {"text": ["Hi $name!"]},"context" : ' + JSON.stringify(context) +'}');

You can see the Javascript code use Api and getResponsePayload() and setResponsePayload function inside api.js (path public/js/api.js) to send a message like Watson. But, in your index you have to add the file for use like this line.

You can see inside my setResponsePayload I use the output and text to send the message, like payload show if Watson sends a message.

Upvotes: 2

Related Questions