1

I'm trying to create a simple google home actions. I was able to successfully try out some basic functions in the google firebase function. But when I tried to do the same with Hapi framework, I couldnt make it work? When I tried to search I could only see with frameworks for Express and lambda?

I tried to integrate with hapi, but only got 500 as response. I am also pasting the snippet of it

const { dialogflow } = require('actions-on-google')
const app = dialogflow({
    debug: true
})
const welcomeIntent = (conv) => {
    conv.contexts.set('previousintent', 1, { 'previousintent': 'welcomeIntent' })
    if (!conv.surface.capabilities.has('actions.capability.MEDIA_RESPONSE_AUDIO')) {
        conv.close('Sorry, this device does not support audio playback.')
        return
    }
    console.log('welcome intent')
    conv.ask('Welcome')
};
app.intent('Default Welcome Intent', welcomeIntent)

fulfillmentRoute.push({
    path: `${constants.V1_API_PATH}/vee/fulfillment`,
    method: 'POST',
    handler: app
})

1 Answers1

0

Here is an Example with Hapi,

const Hapi = require('@hapi/hapi');
const dialogflow = require('dialogflow');
const credentials = {
    client_email: process.env.GOOGLE_CLIENT_EMAIL,
    private_key: process.env.GOOGLE_PRIVATE_KEY,
};

const sessionClient = new dialogflow.SessionsClient(
    {
        projectId: process.env.GOOGLE_PROJECT_ID,
        credentials
    }
);
const init = async () => {

    const server = Hapi.server({
        port: 3000,
        host: 'localhost'
    });

    server.route({
        method: 'POST',
        path: '/',
        handler: async (request, h) => {
            const sessionID = "unique ID" // unique id
            const response = await sendTextQueryToDialogFlow(sessionID,request.payload.query)
            return response[0].queryResult;
        }
    });

    await server.start();
    console.log('Server running on %s', server.info.uri);
};


process.on('unhandledRejection', (err) => {
    console.log(err);
    process.exit(1);
});

init();

async function sendTextQueryToDialogFlow(sessionId, text, params = {}) {
    const sessionPath = sessionClient.sessionPath(config.GOOGLE_PROJECT_ID, sessionId);

    const request = {
        session: sessionPath,
        queryInput: {
            text: {
                text: text,
                languageCode: process.env.DF_LANGUAGE_CODE,
            },
        },
        queryParams: {
            payload: {
                data: params
            }
        }
    };
    return await sessionClient.detectIntent(request);
}

Send POST request with your server with JSON Data like,

{ query:"hello" }

You will Get response.

Nikhil Savaliya
  • 2,138
  • 4
  • 24
  • 45
  • Hi @Nikhil, thanks for your response. However am looking to integrate with the dialogflow from actions-on-google library. When I looked into dialogflow in npm, found that its still in beta phase. So could you help in integrating with actions-on-google? – Mohana Sudhan Jul 26 '19 at 10:55
  • you can use same way around – Nikhil Savaliya Jul 26 '19 at 10:58
  • But the dialogflow from actions-on-google doesnt have SessionsClient. Could you please elaborate!! Thanks! – Mohana Sudhan Jul 26 '19 at 13:43