Last active
July 30, 2018 06:58
-
-
Save popigg/194e1bebfb49bc390872d6ee2880df2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {actionssdk} = require('actions-on-google'); | |
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const rp = require('request-promise'); | |
// get this url from publish section in luis.ai | |
const LUIS_URL = '<YOUR LUIS URL>'; | |
const app = actionssdk({debug: true}); | |
app.intent('actions.intent.MAIN', (conv) => { | |
conv.ask('Hi!'); | |
}); | |
app.intent('actions.intent.TEXT', (conv, input) => { | |
const url = `${LUIS_URL}${input}`; | |
return rp({ | |
url, | |
json: true, | |
}) | |
.then((res) => { | |
// create a function by intent detected by LUIS NLP | |
const actionMap = { | |
name: nameIntent, | |
help: helpIntent, | |
}; | |
// check that LUIS response with a valid intent | |
if (res.topScoringIntent && res.topScoringIntent.intent && res.topScoringIntent.intent !== 'None') { | |
// Map intents to functions for different responses | |
actionMap[res['topScoringIntent']['intent']](conv); | |
} else { | |
conv.ask('Sorry, I don\'t understand what you mean. Please try to ask for help or about my name.'); | |
} | |
}) | |
.catch((e) => { | |
console.log('Error =>', e); | |
}); | |
}); | |
function nameIntent(conv) { | |
conv.ask('My name is Lola test and I like talking.'); | |
} | |
function helpIntent(conv) { | |
conv.ask('I like when you can ask for my name.'); | |
} | |
express().use(bodyParser.json(), app).listen(8888) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment