r/GoogleAssistantDev • GDE • Jul 15 '19

Multivocal 0.12.0 now available

Multivocal is an open source library that tries to bring a different approach to helping you write webhooks for the Google Assistant. It takes a configuration and template driven approach - letting you focus on what you want to do and say, rather than the boilerplate. It also provides some best practices as standard features - such as localization, repeating responses, and varying responses to users.

Version 0.12.0 introduces postprocessing, which lets you run code after you've sent the reply to the user. This is a great way to do things such as analytics, logging, or saving database information without taking the time to do so before replying.

More information, documentation, examples, and links to the repository are available at https://multivocal.info/

I've started using this for some of the games I'm developing, so the user can get a fast response, and other things that the user doesn't need to see get handled next. This also may be useful for handling operations that will take a while. Would love to chat with people about how to use this, and other, Multivocal features.

3 Upvotes

2 comments sorted by

1

u/fleker2 Googler Jul 15 '19

How are you able to do work after the response is sent? If you're using standard REST calls, you don't seem to be able to do that typically.

2

u/afirstenberg GDE Jul 16 '19

Using express-like platforms (including Firebase Cloud Functions), there is the response object that has a send() method. So we can call that method to send the JSON we've built, and it returns to us after the JSON is sent.

With something like AWS Lambda, you have the callback function that you're passed, which works similarly - calling it with the JSON and other attributes necessary sends it - but then returns control to us.

In both cases, we can then call the various postprocessing functions that are either part of multivocal, or that have been registered. Depending on the platform, these may get cut off if they run too long, but they will still run at least until the forced termination time (or we end).

In terms of what it looks like for a developer, fairly straightforward and something like this:

const Multivocal = require('multivocal');

const config = {
  Local: {
    und: {
      Response: {
        "Action.input.welcome": [
          "Hi there!"
        ]
      }
    }
  }
};
new Multivocal.Config.Simple( config );

function delay( usec ){
  return new Promise((resolve) => setTimeout(resolve,usec));
}

function thinkAboutIt( env ){
  console.log('Thinking',new Date());
  return delay( 30000 )
    .then( () => {
      console.log('Done thinking',new Date());
      return Promise.resolve( env );
    })
}
Multivocal.addPostprocessor( thinkAboutIt );

exports.webhook = Multivocal.processFirebaseWebhook;

Running this on Firebase Cloud Functions will give us a voice reply immediately, but we will see something like this in the logs:

6:34:58.030 PM  webhook  Thinking 2019-07-16T22:34:58.030Z
6:35:28.032 PM  webhook  Done thinking 2019-07-16T22:35:28.031Z

We see this same sort of thing using the actions-on-google library when people write code that has async operations, but fail to return the Promise from the Intent Handler. Just in this case, we're explicitly using Promises and taking advantage of how the runtime works to get work done without the timeout that AoG/Dialogflow impose.