node.js - 如何使 Dialog Fulfillment Google Calendar 集成工作?
问题描述
我是 node.js 的新手,我正在尝试使用 Dialogflow 构建一个聊天机器人,让您计划约会并将约会插入 Google 日历。无论我尝试什么,我似乎总是遇到同样的错误(没有请求意图的处理程序)。有没有人看到可能出了什么问题或者已经尝试过本教程并且可以正常工作?
我尝试过的事情:
- 更新了 package.json 中的依赖项
- 更正了时区(我住在比利时/欧洲)和 timeZoneOffset
- 将结算帐号关联到 Google Cloud Platform 中的项目
这是我遵循的教程:
这是特定意图“安排约会”的屏幕截图。它会自动转到默认响应,但应该转到:
Ok, let me see if we can fit you in. ${appointmentTimeString} is fine!.
此意图中有一个自定义实体:@AppointmentType 有 2 个选项:- 车辆登记 - 驾驶执照
在意图屏幕截图中,您还可以看到时间给出了今天的日期(在本例中为 2020-02-28),而它应该给出所请求的约会日期(在本例中为 2020-03-10)。这可能会导致错误,但我不知道如何在内联编辑器中修复它。
我来自内联编辑器的 index.js(日历 ID 和服务帐户数据是正确的,只是从这里取出):
'use strict';
const functions = require('firebase-functions');
const {google} = require('googleapis');
const {WebhookClient} = require('dialogflow-fulfillment');
// const nodemailer = require('nodemailer');
// const xoauth2 = require('xoauth2');
const axios = require('axios');
// Enter your calendar ID below and service account JSON below
const calendarId = "CALENDAR ID";
const serviceAccount = {
"type": "service_account",
"project_id": "PROJECT ID",
"private_key_id": "PRIVATE KEY ID",
"private_key": "PRIVATE KEY",
"client_email": "CLIENT EMAIL",
"client_id": "CLIENT ID",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/XXXXXXXXXXXXXXX.iam.gserviceaccount.com"
}; // 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 = 'Europe/Madrid';
const timeZoneOffset = '+01:00';
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
const appointment_type = agent.parameters.AppointmentType;
function makeAppointment (agent) {
// Calculate appointment start and end datetimes (end = +1hr from start)
const dateTimeStart = new Date(Date.parse(agent.parameters.date.split('T')[0] + 'T' + agent.parameters.time + timeZoneOffset));
// const dateTimeStart = new Date(Date.parse(agent.parameters.date.split('T')[0] + 'T' + agent.parameters.time.split('T')[1].split('+')[0]));
const dateTimeEnd = new Date(new Date(dateTimeStart).setHours(dateTimeStart.getHours() + 1));
const appointmentTimeString = dateTimeStart.toLocaleString(
'en-GB',
{ 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, appointment_type).then(() => {
agent.add(`Ok, let me see if we can fit you in. ${appointmentTimeString} is fine!.`);
}).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, appointment_type) {
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 a 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: {summary: appointment_type +' Appointment', description: appointment_type,
start: {dateTime: dateTimeStart},
end: {dateTime: dateTimeEnd}}
}, (err, event) => {
err ? reject(err) : resolve(event);
}
);
}
});
});
}
我的 package.json:
{
"name": "dialogflowFirebaseFulfillment",
"description": "Dialogflow fulfillment for the bike shop sample",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "6"
},
"scripts": {
"lint": "semistandard --fix \"**/*.js\"",
"start": "firebase deploy --only functions",
"deploy": "firebase deploy --only functions"
},
"dependencies": {
"firebase-functions": "^2.0.2",
"firebase-admin": "^5.13.1",
"actions-on-google": "^2.2.0",
"googleapis": "^27.0.0",
"dialogflow": "^0.6.0",
"dialogflow-fulfillment": "^0.5.0",
"nodemailer": "^4.4.2",
"apiai": "^4.0.3",
"xoauth2": "^1.2.0",
"axios": "^0.19.2"
}
}
这是我在 Google Cloud Platform Logs Viewer 中遇到的错误: Google Cloud Platform error
这是我在 Firebase 控制台中遇到的错误: Firebase 错误
解决方案
在代理将您的请求路由到正确的函数之前,您似乎正在读取参数。尝试在函数内部删除函数 makeAppointment 上方的那条线,如下所示。
function makeAppointment (agent) {
// Calculate appointment start and end datetimes (end = +1hr from start)
const appointment_type = agent.parameters.AppointmentType;
const dateTimeStart = new Date(Date.parse(agent.parameters.date.split('T')[0] + 'T' + agent.parameters.time + timeZoneOffset));
希望这对您有用,因为其他一切似乎都已正确设置。
推荐阅读
- fortran - 如何显示不连续函数
- javascript - 在 SVG 中动态插入图像不显示
- microsoft-graph-api - 如何以编程方式触发 OneDrive/OneNote 同步?
- java - ALPN 回调被丢弃:SPDY 和 HTTP/2 被禁用。alpn-boot 在引导类路径上吗?
- java - java中有没有像setw()这样的内置函数?如果不是,我该如何格式化?
- java - 在成功的 FormData POST 请求上获取“404 Not Found”
- javascript - 从 API 搜索中获取随机结果
- c# - C# - Visual Studio 中从 SQL 导出数据到 TreeView 的问题
- python - 加快 PostgreSQL/SQLAlchemy 中的 JSONB 全文搜索
- android - 在 ExoPlayer 播放之前预缓冲视频