Skip to content

Instantly share code, notes, and snippets.

@jirawatee
Last active October 5, 2019 07:48
Show Gist options
  • Save jirawatee/fd3807b7455843040b0b00a6c288c455 to your computer and use it in GitHub Desktop.
Save jirawatee/fd3807b7455843040b0b00a6c288c455 to your computer and use it in GitHub Desktop.
Appointment code in dialogflowFirebaseFulfillment function
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
function confirmAppointment(agent) {
// ...
}
function makeAppointment(agent) {
const appointment_type = agent.parameters.AppointmentType;
const dateTimeStart = new Date(
Date.parse(agent.parameters.date.split('T')[0] + 'T' + agent.parameters.time.split('T')[1])
);
// Calculate appointment start and end datetimes (end = +1hr from start)
const dateTimeEnd = new Date(new Date(dateTimeStart).setHours(dateTimeStart.getHours() + 1));
const appointmentTimeString = dateTimeStart.toLocaleString(
'en-US',
{ weekday: 'short', day: 'numeric', month: 'short', hour: 'numeric', minute: 'numeric', timeZone: 'Asia/Bangkok' }
);
// 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(`ลงตาราง ${appointmentTimeString} ให้เรียบร้อยแล้วจ้า`);
}).catch(() => {
agent.add(`ขออภัยค่ะ ช่วงเวลานี้ ${appointmentTimeString} ไม่ว่างนะคะ`);
});
}
let intentMap = new Map();
intentMap.set('Appointment', confirmAppointment);
intentMap.set('Appointment - yes', makeAppointment);
agent.handleRequest(intentMap);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment