I am building an android app in which I am calling api.ai. I want to parse the response and display it to users. Earlier I had written code in node.js which is as below:
function sendMessageToApiAi(options,botcontext) {
var message = options.message; // Mandatory
var sessionId = options.sessionId || ""; // optinal
var callback = options.callback;
if (!(callback && typeof callback == 'function')) {
return botcontext.sendResponse("ERROR : type of options.callback should be function and its Mandatory");
}
var nlpToken = options.nlpToken;
if (!nlpToken) {
if (!botcontext.simpledb.botleveldata.config || !botcontext.simpledb.botleveldata.config.nlpToken) {
return botcontext.sendResponse("ERROR : token not set. Please set Api.ai Token to options.nlpToken or context.simpledb.botleveldata.config.nlpToken");
} else {
nlpToken = botcontext.simpledb.botleveldata.config.nlpToken;
}
}
var query = '?v=20150910&query='+ encodeURIComponent(message) +'&sessionId='+context.simpledb.roomleveldata.c1+'&timezone=Asia/Calcutta&lang=en '
var apiurl = "https://api.api.ai/api/query"+query;
var headers = { "Authorization": "Bearer " + nlpToken};
botcontext.simplehttp.makeGet(apiurl, headers, function(context, event) {
if (event.getresp) {
callback(event.getresp);
} else {
callback({})
}
});
}
My Android code is as below:
package com.example.pramod.apidev;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity{
private Button listenButton;
private TextView resultTextView;
private EditText inputText;
private static String API_URL = "https://api.api.ai/api/query";
private static String API_KEY = "d05b02dfe52f4b5f969ba1257cffac37";
private static String query;
private static String s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listenButton = (Button) findViewById(R.id.listenButton);
resultTextView = (TextView) findViewById(R.id.resultTextView);
inputText = (EditText)findViewById(R.id.inputText);
s = inputText.getText().toString();
query = "?v=20150910&query=hi" +"&sessionId=1480181847573api&timezone=Asia/Calcutta&lang=en";
listenButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
try {
URL url = new URL(API_URL + query + "&apiKey=" + API_KEY);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
}
catch(Exception e) {
Log.e("ERROR", e.getMessage(), e);
}
}
});
}
}
I need help in following:
(i) How can call the Api.ai since I am getting an error 401? Can some one tell how to exactly call Api.ai using the Node.js code?
(ii) How can parse the response and display it to users?
Thanks in advance
I'm connecting with api.ai from android app.
The steps to do are the following
1. Add the dependencies:
2. Create an activity implementing AIListener.
3. Declare AIService,and AIDataService:
4. Initialize config, services and add listener:
5. Execute async task to make request:
//request--any string you want to send to chat bot to get there corresponding response.
6. onResult method
If the result is a string, the string with the response will be in:
Regards. Nuria
You can refer the official documentation of integrating API.AI here -
https://github.com/api-ai/apiai-android-client
Happy coding.