0

I am starting to use Wit.ai to enhance a small bot I made. I am able to make a request to the wit.ai by doing:

function sendToWitAI($query){
    $witRoot = "https://api.wit.ai/message?";
    $witVersion = "20170822";

    $witURL = $witRoot . "v=" . $witVersion . "&q=" . $query;

    $ch = curl_init();
    $header = array();
    $header[] = "Authorization: Bearer xxxxxxxx";

    curl_setopt($ch, CURLOPT_URL, $witURL);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $server_output = curl_exec ($ch);
    curl_close ($ch);

    return $server_output;
}

However, when receiving the output I just get the same message I sent. For example, if the user types "I want to make a reservation" my $server_output is now "I want to make a reservation" after all that chunk of code above.

Still, I know it reaches wit successfully because I can see it in the logs there and I know the bot says (from wit.ai):

{
"confidence": null
"action": null
"type": "action" 
}

On top of this, if I just do a curl with the same query:

curl -XPOST 'https://api.wit.ai/converse?v=20170822&session_id=123abc&q=I%20want%20to%20make%20a%20reservation' \
>       -H "Content-Type: application/json" \
>       -H "Accept: application/json" \
>       -H 'Authorization: Bearer xxxxxxxx'

I get the following output:

{
  "confidence" : null,
  "type" : "action",
  "action" : null,
  "entities" : {
    "contact" : [ {
      "suggested" : true,
      "value" : "reservation",
      "type" : "value",
      "confidence" : 0.95062723294726
    } ],
    "intent" : [ {
      "confidence" : 0.98638622681962,
      "value" : "make_reservation"
    } ]
  }
}

I'm not sure where my error is or what I'm missing to properly handle use of the value like I need.

I've been googling non-stop but I can't find anything after they (wit.ai) deprecated "stories" and there's seldom anything about handling the response.

1 Answers1

0

You're using 2 different end points: /message and /converse. The log you pasted is from /converse so I'm not even sure your first call went through. Can you try a curl to /message like this

curl -XGET 'https://api.wit.ai/message?v=20170307&q=I%20want%20to%20make%20a%20reservation' \
  -H 'Authorization: Bearer $TOKEN'
l5t
  • 578
  • 2
  • 5
  • You're right, I can't believe I missed that. However even with this my PHP code should work, right? I'm making a call to /message and have the URL formed just like you're doing here. However while when I run either XGET or XPOST in a curl I get the json response, when I try it over php I just get back an echo of sorts (i.e. if I type: "Heya" I get back "Heya") Am I missing something? I have no idea why the curl works but the php doesn't. –  Aug 24 '17 at 05:24
  • Hard to say. The Wit outcome is a JSON so maybe your PHP doesn't parse it correctly. Examples from our community: https://github.com/search?l=PHP&q=wit.ai&type=Repositories – l5t Aug 24 '17 at 15:37