Inzamam Malik
Inzamam Malik

Reputation: 3425

how to send events to api.ai using nodejs sdk

I'm developing chatbots with Api.ai from last few months

I want to connect it with nodejs client and I successfully sent text queries from nodejs to api.ai and get response back(as described in doc: https://github.com/api-ai/apiai-nodejs-client),

but I'm still unable to send Events using nodejs sdk since no documentation is available in doc: https://github.com/api-ai/apiai-nodejs-client

please refer me to any helpful sample code or article

Upvotes: 1

Views: 1654

Answers (1)

Kevin Alwell
Kevin Alwell

Reputation: 131

If you are using Node try the following:

var apiai = require('apiai');

var app = apiai(process.env.APIAI_TOKEN);

const sendEventToApiAi = (event, sessionId) => {
    return new Promise(function(resolve, reject) {

        let eventArg = {
            "name": event.type
            "data": event.data
        };

        var request = app.eventRequest(eventArg, {sessionId: sessionId});

        request.on('response', function(response) {
            console.log("sendEventToApiAi: response=" + JSON.stringify(response));
            return resolve(response);
        });

        request.on('error', function(error) {
            return reject(error);
        });

        request.end();
    });
}

  let event = { type: "CALCULATED_RESULTS" };

 //Send Event to apiai Intent.        
  sendEventToApiAi(event, sessionId);

Upvotes: 1

Related Questions