Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
// Cloud Function serving as a webhook for Actions on Google, which ships
// a request to Cloud PubSub
//
// For more information, consult blog post:
// https://fixermark.blogspot.com/2017/08/publish-later-bridging-google-assistant.html
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const App = require('actions-on-google').ApiAiApp;
const pubsub = require('@google-cloud/pubsub')();
const projectId = '<add project ID>';
/**
* Responds to any HTTP request that can provide a "message" field in the body.
*
* @param {!Object} req Cloud Function request context.
* @param {!Object} res Cloud Function response context.
*/
exports.listOptions = function helloWorld(request, response) {
if (request.get('Authorization') !=
'Basic <The Base-64 encoding of the "username:password" string') {
console.log('Invalid auth; request rejected.');
response.sendStatus(401);
return;
}
console.log('Payload: ' + JSON.stringify(request.body));
const app = new App({request, response});
const actionMap = new Map();
actionMap.set('input.unknown', (app) => {
const cmd = app.getRawInput();
console.log('raw input: ' + cmd);
const topic = pubsub.topic('belphanior-commands');
topic.publish({data: Buffer.from(cmd)}, {raw: true}, (err) => console.log(err));
app.tell('Executing ' + app.getRawInput());
});
app.handleRequest(actionMap);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment