1

My service is sending back an asynchronous response back to api.ai after a request, which is being displayed on the user interface (Slack, Skype etc).

But I am not able to send a response to the interface after the requested operation is completed (after 5 seconds).

const apiai = require('apiai');

module.exports = (server, config) => {
  const app = apiai('<Client access token>');
  server.post('/', (req, resp) => {

    let body = '';
    req.on('data', function(data) {
      body += data;
    });
    req.on('end', function() {
      body = JSON.parse(body);
      resp.send({
        speech: 'Please wait...',
      });

      const sessionId = body.sessionId;

      setTimeout(() => {
        const evt = app.eventRequest({
          name: 'testevent', data: { },
        }, { sessionId });

        evt.on('response', resp2 => {
          debugger;
        });
        evt.on('error', err => {
          debugger;
        });
        evt.end();

      }, 10 * 1000);
    });
  });
};

In the object resp2, I see the result.fulfillment.speech has the response text which should have been sent to the interface. But it is not.

Is there a way to achieve this?

Philip John
  • 5,275
  • 10
  • 43
  • 68

1 Answers1

0

The code in your question uses Api.ai's API which is meant to be used to embed your Api.ai agent into your own application. The intention behind this is for you to implement/use your own chat/messaging system and then send events and text from the conversation with your users to Api.ai via this API which will provide you with responses (as you've seen) and then you can use those responses from the API and to respond to your users in your own chat/messaging system: https://docs.api.ai/docs/query

It sounds like you are interested in having your Api.ai agent integrate with chat platforms like Slack and Skype directly. If that is the case you should look at Api.ai's integration documentation: https://docs.api.ai/docs/integrations

mattcarrollcode
  • 3,429
  • 16
  • 16