Skip to content

Instantly share code, notes, and snippets.

@zhiyao
Created June 19, 2018 15:34
Show Gist options
  • Save zhiyao/18351b4f768440176c0250e7603e7542 to your computer and use it in GitHub Desktop.
Save zhiyao/18351b4f768440176c0250e7603e7542 to your computer and use it in GitHub Desktop.
Dialogflow Pluralsight - 19/6/2018
//package.json
{
"name": "dialogflowFirebaseFulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "~6.0"
},
"scripts": {
"start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
"deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
},
"dependencies": {
"actions-on-google": "2.0.0-alpha.3",
"firebase-admin": "^4.2.1",
"firebase-functions": "^0.5.7",
"dialogflow": "^0.1.0",
"dialogflow-fulfillment": "0.3.0-beta.2",
"moment": "2.4.0",
"request": "2.85.0",
"request-promise-native": "1.0.5"
}
}
//index.js
'use strict';
const functions = require('firebase-functions');
const { WebhookClient } = require('dialogflow-fulfillment');
const { Card, Suggestion } = require('dialogflow-fulfillment');
const rp = require('request-promise-native');
const moment = require('moment');
const username = "<intrinio username>";
const password = "<intrinio password>";
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function welcome(agent) {
agent.add(`Welcome to my agent!`);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
function getstockprice(agent) {
const stockprice = agent.parameters;
const companyName = stockprice.company_name;
const priceType = stockprice.price_type;
const date = moment(stockprice.date).format('YYYY-MM-DD');
const tickerMap = {
"apple": "AAPL",
"microsoft": "MSFT",
"ibm": "IBM",
"google": "GOOG",
"facebook": "FB",
"amazon": "AMZN"
};
const priceMap = {
"opening": "open_price",
"closing": "close_price",
"maximum": "high_price",
"high": "high_price",
"low": "low_price",
"minimum": "low_price"
};
const stockTicker = tickerMap[companyName.toLowerCase()];
const priceTypeCode = priceMap[priceType.toLowerCase()];
var pathString = `/historical_data?identifier=${stockTicker}&item=${priceTypeCode}&start_date=${date}&end_date=${date}`;
console.log(`pricetype: ${stockprice.price_type}, stockTicker: ${stockprice.company_name}, date: ${stockprice.date}`);
console.log(`priceTypeCode: ${priceTypeCode}, stockTicker: ${stockTicker}, date: ${date}`);
console.log('Path string: ' + pathString);
var auth = "Basic " + new Buffer(username + ":" + password).toString('base64');
const options = {
method: 'GET',
uri: `https://api.intrinio.com${pathString}`,
headers: {
"Authorization": auth
}
};
return rp(options).then(
function (jsonBody) {
var jsonData = JSON.parse(jsonBody);
var stockPrice = jsonData.data[0].value;
agent.add(`On ${date}, ${stockprice.company_name}, ${stockprice.price_type} was ${stockPrice}`);
return Promise.resolve(agent);
}, function(error) {
console.log(`error: #{error}`);
}
);
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('GetStockPrice', getstockprice);
agent.handleRequest(intentMap);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment