I am using Dialogue flow inline editor. I have created cloud function to create an event but its not working and I am receiving following error in diagnostic info. Please help me to Resolve this issue.
Webhook call failed. Error: UNAVAILABLE, State: URL_UNREACHABLE, Reason: UNREACHABLE_5xx, HTTP status code: 500
My code in ndex.js file is as below:
- 'use strict';
-
- const functions = require('firebase-functions');
- const {google} = require('googleapis');
- const {WebhookClient} = require('dialogflow-fulfillment');
-
- // Enter your calendar ID below and service account JSON below
- const calendarId = "My CalenderId";
- const serviceAccount = {
- My account details
- }; // Starts with {"type": "service_account",...
-
- // Set up Google Calendar Service account credentials
- 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
-
- const timeZone = 'America/Los_Angeles';
- const timeZoneOffset = '-07:00';
-
- exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
- const agent = new WebhookClient({ request, response });
- console.log("Parameters", agent.parameters);
- const Person_name = agent.parameters.person;
- const phone = agent.parameters.phone;
- function makeAppointment (agent) {
- // Calculate appointment start and end datetimes (end = +1hr from start)
- //console.log("Parameters", agent.parameters.date);
- const dateTimeStart = new Date(Date.parse(agent.parameters.date.split('T')[0] + 'T' + agent.parameters.time.split('T')[1].split('-')[0] + timeZoneOffset));
- const dateTimeEnd = new Date(new Date(dateTimeStart).setHours(dateTimeStart.getHours() + 1));
- const appointmentTimeString = dateTimeStart.toLocaleString(
- 'en-US',
- { month: 'long', day: 'numeric', hour: 'numeric', timeZone: timeZone }
- );
-
-
- // Check the availibility of the time, and make an appointment if there is time on the calendar
- return createCalendarEvent(dateTimeStart, dateTimeEnd, Person_name, phone).then(() => {
- function confirmAppointment() {
- agent.add(`Ok, let me see if we can fit you in. ${appointmentTimeString} is fine!`);
- }
- confirmAppointment();
- }).catch(() => {
- agent.add(`I'm sorry, there are no slots available for ${appointmentTimeString}.`);
- });
- }
-
- let intentMap = new Map();
- intentMap.set('Schedule Appointment', makeAppointment);
- agent.handleRequest(intentMap);
- });
- function createCalendarEvent(dateTimeStart, dateTimeEnd, Person_name, phone) {
- const new_event = {
- summary: Person_name + ' Appointment',
- description: 'with ' + Person_name +' - '+ phone.toString(), // Convert phone to string
- start: { dateTime: dateTimeStart },
- end: { dateTime: dateTimeEnd }
- };
- return new Promise((resolve, reject) => {
- calendar.events.list({
- auth: serviceAccountAuth, // List events for time period
- calendarId: calendarId,
- timeMin: dateTimeStart.toISOString(),
- timeMax: dateTimeEnd.toISOString()
- }, (err, calendarResponse) => {
- // Check if there is an event already on the Calendar
- if (err || calendarResponse.data.items.length > 0) {
- reject(err || new Error('Requested time conflicts with another appointment'));
- } else {
- // Create event for the requested time period
- calendar.events.insert({
- auth: serviceAccountAuth,
- calendarId: calendarId,
- resource: new_event
- }, (err, new_event) => {
- if (err) {
- console.error("Error creating event:", err);
- } else {
- console.log("Event created successfully:", new_event.id);
- }
- });
- }
- });
- });
- }