I try to get events from a Google Calendar and pass them to my chatbot in DialogFlow.
'use strict';
const functions = require('firebase-functions');
const {google} = require('googleapis');
const {WebhookClient} = require('dialogflow-fulfillment');
const {moment} = require('moment');
const calendarId = "some_id";
const serviceAccount = {some_account};
const serviceAccountAuth = new google.auth.JWT({
email: serviceAccount.client_email,
key: serviceAccount.private_key,
scopes: 'https://www.googleapis.com/auth/calendar'
});
const calendar = google.calendar('v3');
process.env.DEBUG = 'dialogflow:*'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log("Parameters", agent.parameters);
function checkAppointment (agent) {
return check().then((value) => {
agent.add(`yep ${value.data}`);
}).catch(() => {
agent.add(`I'm sorry.`);
});
}
let intentMap = new Map();
intentMap.set('CheckAppointment', checkAppointment);
agent.handleRequest(intentMap);
});
function check(agent){
var beginning = moment().format("YYYY-MM-DDT00:01:00Z");
var end = moment().format("YYYY-MM-DDT23:59:00Z");
return new Promise((resolve, reject) => {
calendar.events.list({
auth: serviceAccountAuth,
calendarId: calendarId,
timeMin: beginning,
timeMax: end,
}, (err, calendarResponse) => {
console.log(calendarResponse.data);
if (err || calendarResponse.data.items.length <= 0) {
reject(err || new Error('theres nothing here'));
} else {
resolve(calendarResponse);
}
});
});
}
Here's the log after trigerring the intent:
It just says "crash" and therefore I have no idea what the bug could possibly be. Any ideas what to do to find out?