gpt4 book ai didi

javascript - 如何验证dialogflow中的参数输入

转载 作者:行者123 更新时间:2023-12-02 22:07:51 24 4
gpt4 key购买 nike

我想在继续预订之前验证用户输入的参数。

这是我的代码:

function makeAppointment (agent) {
// Use the Dialogflow's date and time parameters to create Javascript Date instances, 'dateTimeStart' and 'dateTimeEnd',
// which are used to specify the appointment's time.
const appointmentDuration = 1;// Define the length of the appointment to be one hour.

const dateTimeStart = convertParametersDate(agent.parameters.date, agent.parameters.time,timeZoneOffset);
const dateTimeEnd = addHours(dateTimeStart, appointmentDuration);
const appointmentTimeString = getLocaleTimeString(dateTimeStart);
const appointmentDateString = getLocaleDateString(dateTimeStart);
// Check the availability of the time slot and set up an appointment if the time slot is available on the calendar
return createCalendarEvent(dateTimeStart, dateTimeEnd).then(() => {
agent.add(`Done 😊`);
}).catch(() => {
agent.add(` 😔Sorry check other date`);
});
}
function createCalendarEvent (dateTimeStart, dateTimeEnd) {
return new Promise((resolve, reject) => {
calendar.events.list({ // List all events in the specified time period
auth: serviceAccountAuth,
calendarId: calendarId,
timeMin: dateTimeStart.toISOString(),
timeMax: dateTimeEnd.toISOString()
}, (err, calendarResponse) => {
// Check if there exists any event on the calendar given the specified the time period
if (err || calendarResponse.data.items.length > 0) {
reject(err || new Error('Die angeforderte Zeit kollidiert mit einem anderen Termin'));
} else {
// Create an event for the requested time period
calendar.events.insert({ auth: serviceAccountAuth,
calendarId: calendarId,
resource: {summary: 'Appointment',
start: {dateTime: dateTimeStart},
end: {dateTime: dateTimeEnd}}
}, (err, event) => {
err ? reject(err) : resolve(event);
}
);
}
});
});
}

我的验证应该是:如果 dateTimeStart < 现在,那么代理将响应您无法预订已过的日期或者是 dateTimeStart < openhourtime(可以提供服务的时间)和 dateTimeEnd > closehourtime

营业时间 = '09:00'关闭时间 = '16:00'

你能帮我如何制作这个内部代码吗?

最佳答案

假设您使用 Google sample code其中辅助函数在函数 convertParametersDate 中实现 if 语句会返回一个日期对象,您可以按如下方式修改函数 makeAppointment 来实现日期验证:

function makeAppointment (agent) {
// Use the Dialogflow's date and time parameters to create Javascript Date instances, 'dateTimeStart' and 'dateTimeEnd',
// which are used to specify the appointment's time.
const appointmentDuration = 1;// Define the length of the appointment to be one hour.

const dateTimeStart = convertParametersDate(agent.parameters.date, agent.parameters.time,timeZoneOffset);
const dateTimeEnd = addHours(dateTimeStart, appointmentDuration);
const appointmentTimeString = getLocaleTimeString(dateTimeStart);
const appointmentDateString = getLocaleDateString(dateTimeStart);
// Check the availability of the time slot and set up an appointment if the time slot is available on the calendar

//here the new part:
var now = new Date();
var openhour = 9;
var closehour = 16;
if (dateTimeStart.getTime() < now.getTime() || dateTimeStart.getHours() < openhour || dateTimeEnd.getHours()> closehour){
agent.add(` 😔Sorry check other date`);
} else {
return createCalendarEvent(dateTimeStart, dateTimeEnd).then(() => {
agent.add(`Done 😊`);
}).catch(() => {
agent.add(` 😔Sorry check other date`);
});
}
}

用于实现验证的方法是Javascript日期对象方法,具体为getTime() , getHours()new Date() .

关于javascript - 如何验证dialogflow中的参数输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59660098/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com