Skip to content

Instantly share code, notes, and snippets.

@luisleao
Created April 5, 2017 02:56
Show Gist options
  • Save luisleao/5276c35813ebc38101449930b606e416 to your computer and use it in GitHub Desktop.
Save luisleao/5276c35813ebc38101449930b606e416 to your computer and use it in GitHub Desktop.
Google Assistant Sample
/**
* Endpoint which handles requests for a Google Assistant action which asks users to say a number
* and read out the ordinal of that number.
* e.g. If the user says "Twelve" the action will say "The ordinal of twelve is twelfth".
*/
exports.raffleUser = functions.https.onRequest((req, res) => {
const assistant = new ActionsSdkAssistant({request: req, response: res});
// List of re-prompts that are used when we did not understand a number from the user.
const reprompts = [
'You can say <break time="3"/>Yes, sure or go'
];
const actions = [
'yes',
'sure',
'let\s do this',
'go'
];
const ending = [
'no',
'bye'
];
const WELCOME_MESSAGE = `<speak>
Hi Capivaras! <break time="1"/>
Do you like to raffle a new swag?.
</speak>`;
const actionMap = new Map();
actionMap.set(assistant.StandardIntents.MAIN, assistant => {
const inputPrompt = assistant.buildInputPrompt(true, WELCOME_MESSAGE, reprompts
);
assistant.ask(inputPrompt);
});
actionMap.set(assistant.StandardIntents.TEXT, assistant => {
const rawInput = assistant.getRawInput();
if (actions.indexOf(rawInput.toLowerCase()) >= 0) {
//https://www.myinstants.com/media/sounds/piao-do-bau-com-musica.mp3
//https://estudenaudacity.firebaseapp.com/piao-do-bau-com-musica.wav
//https://actions.google.com/sounds/v1/cartoon/magic_chime.ogg
var raffled_user = raffle();
console.log("SORTEEI ", raffled_user);
if(!raffled_user) {
assistant.tell('Sorry. I can\'t find any user to raffle!');
} else {
var user_displayName = raffled_user.displayName || "NAME NOT DEFINED";
const message = `<speak><audio src="https://estudenaudacity.firebaseapp.com/piao-do-bau-com-musica.wav"></audio><break time="3"/>
The user is <break time="3"/>` +
user_displayName +
` <break time="3"/>Whould you like to receive another one?</speak>`;
// sortear usuario e avisar
const inputPrompt = assistant.buildInputPrompt(true, message, reprompts);
// avisar que não entendeu
assistant.ask(inputPrompt);
}
} else if (ending.indexOf(rawInput.toLowerCase()) >= 0) {
assistant.tell('Goodbye!');
} else {
const inputPrompt = assistant.buildInputPrompt(true, WELCOME_MESSAGE, reprompts);
//TODO: avisar que não entendeu
assistant.ask(inputPrompt);
}
});
assistant.handleRequest(actionMap);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment