1+ // Import dependencies
2+ const express = require ( "express" )
3+ const mongoose = require ( "mongoose" ) ;
4+ const MessagingResponse = require ( "twilio" ) . twiml . MessagingResponse ;
5+
6+ // Setup the express server
7+ const app = express ( ) ;
8+ const port = 80 ;
9+
10+ // The mongoose appointment model
11+ let Appointment ;
12+
13+ // Import middlewares into express
14+ app . use ( express . json ( ) ) ;
15+
16+ // Connect to MongoDB
17+ ( async function connect ( ) {
18+
19+ try {
20+
21+ // Try to connect to MongoDB
22+ await mongoose . connect ( "Connection string goes here" , { useNewUrlParser : true } ) ;
23+
24+ // Log the success
25+ console . log ( "Mongoose connected successfully" ) ;
26+
27+ } catch ( error ) {
28+
29+ // Log the fail
30+ console . log ( "Mongoose failed to connect" ) ;
31+
32+ }
33+
34+ // Make the mongoose schema for appointments
35+ const schema = new mongoose . Schema ( {
36+ messageId : { type : String } ,
37+ accountId : { type : String } ,
38+ fullName : { type : String } ,
39+ datetime : { type : Date } ,
40+ message : { type : String } ,
41+ phoneNumber : { type : String } ,
42+ confirmed : {
43+ type : Boolean ,
44+ default : false
45+ }
46+ } ) ;
47+
48+ // Make the mongoose Appointment model
49+ Appointment = mongoose . model ( "Appointment" , schema ) ;
50+
51+ } ) ( ) ;
52+
53+ app . post ( "/api" , async ( req , res ) => {
54+
55+ // The Twilio message response generator
56+ const twiml = new MessagingResponse ( ) ;
57+
58+ // Wait for the Appointment to be initialized
59+ await Appointment . init ( ) ;
60+
61+ if ( req . body . Body . toLowerCase ( ) == "help" ) {
62+ twiml . message ( "book full name 2019-10-29 at 11:30 custom message" ) ;
63+ } else if ( req . body . Body . toLowerCase ( ) . startsWith ( "book" ) ) {
64+
65+ // Regular expression
66+ const command = / b o o k ( .* ?) \s ( \d .* ?) a t \s ( .* ?) \s ( .* ) / . exec ( req . body . Body ) ;
67+
68+ // Construct the appointment
69+ const appointment = {
70+ messageId : req . body . MessageSid ,
71+ accountId : req . body . AccountSid ,
72+ fullName : command [ 1 ] ,
73+ datetime : new Date ( `${ command [ 2 ] . trim ( ) } T${ command [ 3 ] . trim ( ) } Z` ) ,
74+ message : command [ 4 ] ,
75+ phoneNumber : req . body . From ,
76+ confirmed : false
77+ }
78+
79+ // Make a new appointment document and add it to the DB
80+ const appointmentDocument = new Appointment ( appointment ) ;
81+ await appointmentDocument . save ( ) ;
82+
83+ // Send a message back to the user asking for confirmation
84+ twiml . message ( "Write yes to confirm. If you don't write yes your appointment will not be booked." ) ;
85+
86+ } else if ( req . body . Body . toLowerCase ( ) == "yes" ) {
87+
88+ // Update the appointment in the DB
89+ const document = await Appointment . updateOne ( { accountId : req . body . AccountSid } , { confirmed : true } ) ;
90+
91+ // Show the user if the appointment was updated
92+ if ( document . nModified >= 1 ) {
93+ twiml . message ( "Your appointment is now booked." ) ;
94+ } else {
95+ twiml . message ( "It doesn't look like you have an appointment to accept, maybe you already accepted your appointment." ) ;
96+ }
97+
98+ } else {
99+ twiml . message ( "book full name 2019-10-29 at 11:30 custom message." ) ;
100+ }
101+
102+ // Return the response
103+ res . writeHead ( 200 , { "Content-Type" : "text/xml" } ) ;
104+ res . end ( twiml . toString ( ) ) ;
105+
106+ } ) ;
107+
108+ // Start the server
109+ app . listen ( port , ( ) => console . log ( `Messaging response listening on port ${ port } !` ) ) ;
0 commit comments