Skip to content

Instantly share code, notes, and snippets.

@runceel
Last active January 11, 2019 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save runceel/1b29f0e464130a612df5c47b639de317 to your computer and use it in GitHub Desktop.
Save runceel/1b29f0e464130a612df5c47b639de317 to your computer and use it in GitHub Desktop.
const express = require('express');
const bodyParser = require('body-parser');
const { dialogflow } = require('actions-on-google');
const graph = require('@microsoft/microsoft-graph-client');
const port = process.env.PORT || 4567;
const app = dialogflow({ debug: true });
app.intent('Default Welcome Intent', async conv => {
const client = graph.Client.init({
authProvider: (done) => {
done(null, conv.user.access.token);
}
});
var res = await client.api('/me/devices')
.version('beta')
.get();
const targetDevice = res.value[0];
conv.contexts.set('device', 5, targetDevice);
conv.ask(`Hello! I can access your device. Device name is ${targetDevice.Name}. What do you do? Please talk me like "Launch Twitter."`);
});
app.intent("Launch app", async conv => {
const client = graph.Client.init({
authProvider: (done) => {
done(null, conv.user.access.token);
}
});
let url = 'https://bing.com/';
switch(conv.parameters.appname) {
case 'Twitter':
url = 'http://twitter.com/'
break;
}
const targetDevice = conv.contexts.get('device').parameters;
await client.api(`/me/devices/${targetDevice.id}/commands`)
.version('beta')
.post({
type: 'LaunchUri',
payload: {
uri: url
}
});
conv.close('OK.');
});
const expressApp = express().use(bodyParser.json());
expressApp.post('/', app);
expressApp.listen(port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment