Jahir
Jahir

Reputation: 778

Update clientId after initializing Google Actions SDK for NodeJS

I'm using the account linking feature for Actions SDK and following the guide here (https://developers.google.com/assistant/identity/google-sign-in#start_the_authentication_flow)

It shows the initialization like this

const app = actionssdk({
     // REPLACE THE PLACEHOLDER WITH THE CLIENT_ID OF YOUR ACTIONS PROJECT
    clientId: CLIENT_ID,
});

But for my use case, I'll read the clientId from DB which is stored against the projectId of the project. I can extract the projectId only after the MAIN intent is triggered.

My question is, how can I set the clientId after initializing actionssdk?

Upvotes: 0

Views: 54

Answers (2)

Jahir
Jahir

Reputation: 778

I found a simple solution to this. I am adding it here for future references.

// handler.js
async function handleRequest(req, res) {
    const clientId = // retrieve the clienId using your business logic
    const app = actionssdk({
        clientId: clientId
    })
}

module.exports = handleRequest;

Instead of directly creating an instance of actionssdk, wrap it inside a function like this.

// index.js
const handler = require('./path/to/hander.js');
app.post('/webhook', handler);

Then when defining the webhook, use the wrapper function to handle the webhook requests

Upvotes: 0

Thomas
Thomas

Reputation: 168

This solution uses the new Actions SDK, but the principal is the same for the legacy SDK as well:

const {
  conversation,
  Canvas,
} = require('@assistant/conversation');
const functions = require('firebase-functions');


const wrapper = async (req, res) => {
  // You can get any data you need here:
  const myAsyncBootstrapData = await getData();
  const app = conversation({debug: true, ...myAsyncBootstrapData});

  app.handle('welcome', (conv) => {
    conv.add('This is a demo.');
  });
  
  return app(req, res);
};

exports.ActionsOnGoogleFulfillment = functions.https.onRequest(wrapper);

functions.https.onRequest accepts any callable, including ones that return promises. If you need to block while loading configuration data asynchronously, you can do so by wrapping your definition in an async function.

Upvotes: 1

Related Questions