3

I'm currently working with Wit.ai on a webpage in CodePen.io. I was wondering if it is possible to retrieve a Wit.ai bot's text response ("Bot says") using the HTTP API.

For example: If a user was to prompt the bot with:

How's it going?

I would like to, after making an API call with this message, be able to retrieve the bot's typical response:

I am well, thank you human.

I've looked through the HTTP API Documentation. It seems like this done using a a 'POST' to api.wit.ai/converse. The JSON response contains a "msg" field, which is exactly what I need! Here is the code I'm currently using:

$.ajax({
    url: baseUrl + "converse",
    data: {
      'q': text, // The message to send the bot
      'session_id': "123abc",
      'access_token' : accessToken // Authorisation key for using our bot
    },
    dataType: 'json',
    crossDomain: true,
    method: 'POST',

    success: function(data) {
      prepareResponse(data);
    },
    error: function() {
      respond(messageInternalError);
    }
});

However, Wit.Ai does not support CORS at the moment, the only way to make Cross-domain requests is using JSONP, which only works for GET requests. As can be expected, the code above results in HTTP 400 error.

Can anyone confirm whether it is possible to use the HTTP API to retrieve a bot's textual response to a user message? Is there a work around to what I am currently doing?

Allan of Sydney
  • 1,410
  • 14
  • 23

2 Answers2

1

Same trouble here, I did find a work-around, because I was calling it from my nodejs app. ran curl instead, Here's the code.

var sys = require('util')

var exec = require('child_process').exec;
var child;
child = exec("curl -XPOST 'https://api.wit.ai/converse?v=20160330&session_id=123abc&q=screw%20http%20api%20calls' \\ -H \"Content-Type: application/json\" \\ -H \"Accept: application/json\" \\  -H 'Authorization: Bearer $token'", function (error, stdout, stderr) {
    res.send(stdout)
    //sys.print('stderr: ' + stderr);
    if (error !== null) {
        console.log('exec error: ' + error);
    }
});

});

1

I had the same issue recently. In order to successfully get a conversation, I had to create a local server first, that will handle all user requests and send them to Wit's server.

Used node.js, express, nodemon and sync-request.

Here's the server.js file:

var express = require('express')
var request = require('sync-request');

var port = 8001;
var app = express()

app.get('/converse', function (req, res) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  var message = req.query.q;
  var sessionID = req.query.session_id;
  var response = request('POST', 'https://api.wit.ai/converse?q=' + message + '&session_id=' + sessionID, {
    'headers': {
      'Content-Type': 'application/json; charset=utf8',
      "Accept": "application/json",
      "Authorization": "Bearer $place-app-token"
    }
  });
  res.send(JSON.parse(response.getBody('utf8')));
})
app.listen(port);

Server will send a POST request with the headers configuration Wit needs.

User will send a GET request to the local server. Used jQuery, as suggested in documentation: https://wit.ai/docs/http/20160526#cross-domain-link

Here's the front-end request:

$.ajax({
  url: 'http://127.0.0.1:8001/converse',
  data: {
    'q': text,
    'session_id': sessionID
  },
  method: 'GET',
  crossDomain: true,
  success: function(response) {
    console.log(response, response.msg);
  }
});