krilor / wp-chatbot

Simple Wordpress Plugin to add any chatbot to your web page
GNU General Public License v3.0
19 stars 17 forks source link

Integration with own backend + Api.ai #49

Open agustina-biscayart-movile opened 7 years ago

agustina-biscayart-movile commented 7 years ago

Hi Krilor,

Opening this issue just to ask you a question. I have a Facebook messenger bot developed by myself integrated with api.ai. Now I'm trying to use the bot from fb messenger and from a WordPress site as well.

I'm trying with your plugin and it works pretty good if I connect it to api.ai endpoint but if I connect it to my endpoint it doesn't: I send a message from the chat but my backend does not receives anything. I'm not sure how do I have to configure it.

Here is my configuration:

image

image

What I'm doing wrong? Also, if I wanted to do some debugging, what is the starting point in the plugin core I should look at?

Many thanks!

krilor commented 7 years ago

Hey Augustina Thanks for your interest in the plugin :)

I guess the readme can get much better on this point, but basically what you need to do is to kind of build the request (params and headers) and then interpret the response (the jsonpath).

Check this ticket for an example: https://github.com/krilor/wp-chatbot/issues/1

I can be of more specific help if you share a bit of information on what the endpoint you are trying reach looks like, and what it returns. Like what does a request have to look like? And what json structure does it return?

The wp chatbot request class is a good place to start in the code. Also, when trying to do this, fire up your browsers dev console. The plugin should print some helpful error messages there.

Cheers!

agustina-biscayart-movile commented 7 years ago

Hi Krilor, Thanks for your response! :blush:

Finally I was able to send a message to my backend but now I'd need a little more help with my response.

I'll try to be more specific on what I need... I have a nodejs app integrated with api.ai, but I need to point the plugin to my app backend instead of api.ai directly because the app does a lot of more things (like DB connections, interaction with others APIs, some business logic and so on..)

The thing is that I do need the api.ai response to set it in my own response and then display it through the bot, but I have a problem here. I'm using the api.ai client and I don't know how to use the api.ai response since the call is async.

Suppose that I have something like this:

app.get('/', function (req, res) {
   var message = req.query.message;
   var responseMessage = callApiAi(message):
   return res.status(200).json({
        status: "ok",
        message: responseMessage
    });    
});

function callApiAi(msg) {
   //some code... blah blah

    apiaiRequest.on('response', (response) => {
        // I want here to use the response.result in my own response.
        return response.result...
    });

    apiaiRequest.on('error', (error) => console.error(error));
    apiaiRequest.end();
}

The "responseMessage" always is undefined and I think it is because the calll to api.ai is async.

To summarize, I want to integrate this plugin with my nodejs app that is integrated with api.ai.

Any help?

Thanks!!

Maniae commented 7 years ago

Hi, You're right, the call to api.ai is async, so you have to use it asynchronously. Also you should use the POST method instead of th GET method. Try something like this:

app.post('/', (req, res) => {
    var message = req.query.message;
        callApiAi(message, (responseMessage) => {
        return res.status(200).json({
                status: "ok",
                message: responseMessage
    });
  })
});

function callApiAi(msg, callback) {
   //some code... blah blah

    apiaiRequest.on('response', (response) => {
        callback(response.result...)
    });

    apiaiRequest.on('error', (error) => console.error(error));
    apiaiRequest.end();
}

Now, the responseMessage should be given the value of the response of the request to api.ai.

agustina-biscayart-movile commented 7 years ago

Hi again!

Now I'm trying with POST method but the request body is always empty, there isn't any parameter. Have you tested it?

Thanks!

Maniae commented 7 years ago

Woops, I didn't look right.

Replace app.post('/', ...) with app.post('/webhook', ...) since it's the right path.

agustina-biscayart-movile commented 7 years ago

No that was fine. I mean I did replace to the correct path and the correct method is called. But if I print the body like console.log(req.body) I always get an empty json '{}'.