Google Cloud Function Fulfilment for Alexa

amazon-alexa

#1

I’ve seen this post and the linked article, Validating signatures however i’m still having trouble with the Alexa signature when deploying on Google Cloud Functions. I’ve install the alexa-verifier-middleware package.

I feel like I should be doing something with the Express server instead of adding:

exports.handler = async (req, res) => {
    await app.handle(new GoogleCloudFunction(req, res));
}

Here’s my index.js

// Use this
const { WebhookVerified: Webhook, ExpressJS, GoogleCloudFunction } = require('jovo-framework');

const {app} = require('./app.js');

// ------------------------------------------------------------------
// HOST CONFIGURATION
// ------------------------------------------------------------------

// ExpressJS (Jovo Webhook)
if (process.argv.indexOf('--webhook') > -1) {
    const port = process.env.JOVO_PORT || 3000;
    Webhook.jovoApp = app;

    Webhook.listen(port, () => {
        console.info(`Local server listening on port ${port}.`);
    });

    Webhook.post(['/webhook','/webhook_alexa'], async (req, res) => {
        await app.handle(new ExpressJS(req, res));
    });
}

exports.handler = async (req, res) => {
    await app.handle(new GoogleCloudFunction(req, res));
};

#2

The alexa-verifier-middleware works only with ExpressJS. It is more complex if you want to use Google Cloud Functions. You have to verify the requests yourself. (https://github.com/mreinstein/alexa-verifier)

I might have not enough knowledge of the the Google Cloud Functions, but I try to help :slight_smile:

How do you want to access the Google Function from Alexa? via something that is similar to the AWS API Gateway?


#3

Yes, I was just planning on using an HTTP trigger for the Google Cloud Function. And provide that as an HTTP endpoint in the Alexa console.

However I ran into the certificate issue when trying to pass the skill through the certification process. The http google cloud endpoint works fine when testing on alexa.

I wonder if there has ever been anyone else whose deployed a Google Cloud function to power an Alexa skill, without using the express server solution.


#4

I built something where you may pass certification. We will add it deeper into the framework (soon).

Add this to your index.js:

const verifier = require('alexa-verifier');


// Google Cloud function
exports.gcloud = async (req, res) => {
   
    try {
        await verify(req)
        await app.handle(new GoogleCloudFunction(req, res));
    } catch(e) {        
        console.log(e);
        res.status(400).json({ status: 'failure', reason: e.message })
    }
};

function verify(req) {

    const certUrl = req.headers.signaturecertchainurl;
    const signature = req.headers.signature;
    const rawBody = JSON.stringify(req.body);

    return new Promise((resolve, reject) => {
        verifier(certUrl, signature, rawBody, function(er) {
            if (er) {
              return reject({ status: 'failure', reason: er })
            }
            resolve();
          })
    })
}

But I haven’t tested it with a Google Action yet. Maybe you have to add an additional check, if the request is an Alexa request.


Errors with deploying to Alexa
#5

Thank you @AlexSwe! That worked great :grinning:. FYI, you are correct about how it doesn’t work for Google Actions and you need to add an additional check to make sure it works for both platforms.

I am looking forward to seeing this incorporated in a future version of JOVO!


#6

Hey Marko how did you end up solving this - I seem to be getting an error when trying to run it on Google Assistant.