@@ -296,6 +296,14 @@ async function checkForNewSubmissions() {
296296 nextPage ( ) ;
297297 } ) ;
298298
299+ // Check if Twilio credentials are present, in order to send followup text
300+ const accountSid = config . TWILIO_ACCOUNT_SID ;
301+ const authToken = config . TWILIO_AUTH_TOKEN ;
302+ const client = require ( "twilio" ) ( accountSid , authToken ) ;
303+ const twilioPhoneNumber = config . TWILIO_PHONE_NUMBER ;
304+ const hasTwilioCredentials =
305+ ( accountSid && authToken && twilioPhoneNumber ) || false ;
306+
299307 // Check Airtable for tasks completed in the last day, then send volunteer
300308 // a followup text
301309 base ( config . AIRTABLE_REQUESTS_TABLE_NAME )
@@ -322,48 +330,53 @@ async function checkForNewSubmissions() {
322330 logger . error ( err ) ;
323331 return ;
324332 }
325- const accountSid = config . TWILIO_ACCOUNT_SID ;
326- const authToken = config . TWILIO_AUTH_TOKEN ;
327- const client = require ( "twilio" ) ( accountSid , authToken ) ;
328333 const phoneNumber = rec . get (
329334 "Please provide your contact phone number:"
330335 ) ;
331336 const formattedPhoneNumber = formatPhoneNumber ( phoneNumber ) ;
332337
333- logger . info ( `Sending followup text to: ${ formattedPhoneNumber } ` ) ;
334- client . messages
335- . create ( {
336- body : "Thank you for being a great neighbor!" ,
337- from : config . TWILIO_PHONE_NUMBER ,
338- to : formattedPhoneNumber ,
339- } )
340- . then ( ( message ) => {
341- logger . info ( `Message SID: ${ message . sid } ` ) ;
342- base ( config . AIRTABLE_REQUESTS_TABLE_NAME ) . update (
343- [
344- {
345- id : record . id ,
346- fields : {
347- "Followup SMS Sent?" : "Yes" ,
338+ if ( hasTwilioCredentials ) {
339+ logger . info ( `Sending followup text to: ${ formattedPhoneNumber } ` ) ;
340+ client . messages
341+ . create ( {
342+ body : "Thank you for being a great neighbor!" ,
343+ from : config . TWILIO_PHONE_NUMBER ,
344+ to : formattedPhoneNumber ,
345+ } )
346+ . then ( ( message ) => {
347+ logger . info ( `Message SID: ${ message . sid } ` ) ;
348+ base ( config . AIRTABLE_REQUESTS_TABLE_NAME ) . update (
349+ [
350+ {
351+ id : record . id ,
352+ fields : {
353+ "Followup SMS Sent?" : "Yes" ,
354+ } ,
348355 } ,
349- } ,
350- ] ,
351- function ( err , records ) {
352- if ( err ) {
353- logger . error ( err ) ;
354- return ;
356+ ] ,
357+ function ( err , records ) {
358+ if ( err ) {
359+ logger . error ( err ) ;
360+ return ;
361+ }
362+ records . forEach ( function ( record ) {
363+ logger . info (
364+ `Followup text sent?: ${ record . get (
365+ "Followup SMS Sent?"
366+ ) } `
367+ ) ;
368+ } ) ;
355369 }
356- records . forEach ( function ( record ) {
357- logger . info (
358- `Followup text sent?: ${ record . get ( "Followup SMS Sent?" ) } `
359- ) ;
360- } ) ;
361- }
362- ) ;
363- } )
364- . catch ( ( error ) => {
365- logger . error ( `onRejected function called: ${ error . message } ` ) ;
366- } ) ;
370+ ) ;
371+ } )
372+ . catch ( ( error ) => {
373+ logger . error ( `onRejected function called: ${ error . message } ` ) ;
374+ } ) ;
375+ } else {
376+ logger . error (
377+ "Twilio credentials missing -- Followup text not sent"
378+ ) ;
379+ }
367380 } ) ;
368381 }
369382 nextPage ( ) ;
0 commit comments