Skip to content

Instantly share code, notes, and snippets.

@ceuk
Last active April 15, 2019 14:13
Show Gist options
  • Save ceuk/0067290f0512adcc23324a7b7fe0d42f to your computer and use it in GitHub Desktop.
Save ceuk/0067290f0512adcc23324a7b7fe0d42f to your computer and use it in GitHub Desktop.
import dedent from 'dedent'
import moment from 'moment'
import { twiml } from 'twilio'
const { VoiceResponse } = twiml
const voiceOptions = getVoiceOptionsSomehow()
/**
* an example of voice options could be something like
* {
* voice: 'alice',
* language:'en-GB'
* }
**/
export const welcome = function(request) {
const response = new VoiceResponse()
const practiceName = getPracticeNameSomehow() // get the tenant details somehow
const patient = getPatientSomehow(request.body.From) // lookup patient by their phonenumber somehow
const nextAppointment = patient && getNextAppointmentSomehow(patient) // lookup the patients next appointment
const primaryClinician = patient && getPrimaryClinicianSomehow(patient) // lookup the patient's primary clinician if set
response.say(
dedent`Hi ${patient.name},
${nextAppointment ? giveNextAppointmentDetails(appointment) : ''}`
, voiceOptions
)
response.say(
dedent`
Thanks for calling ${practiceName},
We're just putting you through.
`,
voiceOptions
)
// refer the call to EITHER the primary clinician (if known) or a referrer
// see https://www.twilio.com/docs/voice/twiml/dial and https://www.twilio.com/docs/voice/client/twiml
// for more info
if (primaryClinician) {
response.dial().client('Dr Banks')
} else {
response.dial('0161 408 1276')
}
return voiceResponse.toString();
}
/**
* @return {String}
*/
function giveNextAppointmentDetails(appointment) {
const appointmentStart = moment(appointment.start)
const toNow = appointmentStart.toNow() // today, in 2 days, in a month etc
const time = appointmentStart.format('h A') // 4 PM, 9:30 AM etc
const day = moment(appoitnment.start).format('dddd') // Monday, Tuesday etc
const dayOfMonth = moment(appoitnment.start).format('Do') // 1st, 2nd, 5th etc
const month = moment(appoitnment.start).format('MMMM') // January, February etc
const fullDate = appointmentStart.isSame(new Date(), 'day')
? `at ${time}`
: `on ${day} the ${dayOfMonth} of ${month}`
return `Your next appointment is ${toNow} ${fullDate}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment