Created
December 26, 2017 15:20
-
-
Save jinqian/b07b4d5021c45da9ad4626dd47822b14 to your computer and use it in GitHub Desktop.
Example google assistant for LaLiga
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
// Copyright 2016, Google, Inc. | |
// | |
// 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. | |
'use strict'; | |
process.env.DEBUG = 'actions-on-google:*'; | |
const { DialogflowApp } = require('actions-on-google'); | |
const functions = require('firebase-functions'); | |
/** Dialogflow Actions {@link https://dialogflow.com/docs/actions-and-parameters#actions} */ | |
const Actions = { | |
MATCH_RESULT: 'match.result', | |
MATCH_VIDEO: 'match.video' | |
}; | |
/** Dialogflow Parameters {@link https://dialogflow.com/docs/actions-and-parameters#parameters} */ | |
const Parameters = { | |
HOME_TEAM: 'HomeTeam', | |
AWAY_TEAM: 'AwayTeam' | |
}; | |
class LaLiga { | |
/** | |
* Create a new instance of the app handler | |
* @param {AoG.ExpressRequest} req | |
* @param {AoG.ExpressResponse} res | |
*/ | |
constructor (req, res) { | |
console.log(`Headers: ${JSON.stringify(req.headers)}`); | |
console.log(`Body: ${JSON.stringify(req.body)}`); | |
/** @type {DialogflowApp} */ | |
this.app = new DialogflowApp({ request: req, response: res }); | |
} | |
/** | |
* Get the Dialogflow intent and handle it using the appropriate method | |
*/ | |
run () { | |
/** @type {*} */ | |
const map = this; | |
const action = this.app.getIntent(); | |
console.log(action); | |
map[action](); | |
} | |
// Below are Dialogflow intent handlers | |
[Actions.MATCH_RESULT] () { | |
console.log("MATCH_RESULT intent") | |
const homeTeam = this.app.getArgument(Parameters.HOME_TEAM); | |
const awayTeam = this.app.getArgument(Parameters.AWAY_TEAM); | |
console.log(homeTeam) | |
console.log(awayTeam) | |
if (homeTeam == "Real Madrid" || homeTeam == "Sevilla") { | |
this.app.ask("El resultado del Real Madrid contra el Sevilla es 5-0 a favor del Madrid. ¿Te gustaria ver todos los goles?"); | |
} else if (homeTeam == "Villarreal" || homeTeam == "Barcelona") { | |
this.app.ask("El resultado del Villarreal contra el Barcelona es 0-2 a favor del Barcelona ¿Te gustaria ver el resumen del partido?"); | |
} else if (homeTeam == "Betis" || homeTeam == "Atletico de Madrid") { | |
this.app.ask("El resultado del Betis contra el Atletico de Madrid es 0-1 a favor de los colchoneros. ¿Te gustaría ver las mejores jugadas?"); | |
} else if (homeTeam == "Real Sociedad" || homeTeam == "Málaga") { | |
this.app.ask("El resultado del Real Sociedad contra el Málaga es 0-2 a favor del Málaga. ¿Quieres ver el video del resumen?"); | |
} else { | |
this.app.ask("Lo siento, no tengo información de este equipo. ¿Quieres saber algo de otro equipo?"); | |
} | |
} | |
[Actions.MATCH_VIDEO] () { | |
console.log("MATCH_VIDEO intent") | |
const context = this.app.getContext("match_result"); | |
const homeTeam = context["parameters"][Parameters.HOME_TEAM] | |
if (homeTeam == "Real Madrid" || homeTeam == "Sevilla") { | |
this.app.tell(this.app.buildRichResponse() | |
.addSimpleResponse('Aqui está el video') | |
.addBasicCard(this.app.buildBasicCard('Video') | |
.setTitle('Real Madrid v.s. Sevilla') | |
.addButton('Ver', 'https://www.youtube.com/watch?v=x6LKJjIhUEs') | |
.setImage('https://elpais.com/deportes/imagenes/2017/12/09/actualidad/1512842297_440229_1512847403_noticia_fotograma.jpg', 'Video') | |
.setImageDisplay('CROPPED') | |
) | |
); | |
} else if (homeTeam == "Villarreal" || homeTeam == "Barcelona") { | |
this.app.tell(this.app.buildRichResponse() | |
.addSimpleResponse('Aqui está el video') | |
.addBasicCard(this.app.buildBasicCard('Video') | |
.setTitle('Villarreal v.s. Barcelona') | |
.addButton('Ver', 'https://www.youtube.com/watch?v=M8stuApKzjI') | |
.setImage('http://www.armsport.am/news_images/283/847203_3/25323720_1542032332579230_1485384922_n.jpg', 'Video') | |
.setImageDisplay('CROPPED') | |
) | |
); | |
} else if (homeTeam == "Betis" || homeTeam == "Atletico de Madrid") { | |
this.app.tell(this.app.buildRichResponse() | |
.addSimpleResponse('Aqui está el video') | |
.addBasicCard(this.app.buildBasicCard('Video') | |
.setTitle('Betis v.s. Atlético Madrid') | |
.addButton('Ver', 'https://www.youtube.com/watch?v=hWcMTK_fhnI') | |
.setImage('http://files.laliga.es/201705/855x481_14201215bet-atco004.jpg', 'Video') | |
.setImageDisplay('CROPPED') | |
) | |
); | |
} else if (homeTeam == "Real Sociedad" || homeTeam == "Málaga") { | |
this.app.tell(this.app.buildRichResponse() | |
.addSimpleResponse('Aqui está el video') | |
.addBasicCard(this.app.buildBasicCard('Video') | |
.setTitle('R. Sociedad v.s. Malaga') | |
.addButton('Ver', 'https://www.youtube.com/watch?v=J0mhfU_b8wY') | |
.setImage('http://files.laliga.es/201510/855x481_04002011malaga-real-sociedad021.jpg', 'Video') | |
.setImageDisplay('CROPPED') | |
) | |
); | |
} else { | |
this.app.ask("Lo siento, no tengo información de este equipo. ¿Quieres saber algo de otro equipo?"); | |
} | |
} | |
} | |
// HTTP Cloud Function for Firebase handler | |
exports.laliga = functions.https.onRequest( | |
/** @param {*} res */ (req, res) => new LaLiga(req, res).run() | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment