@@ -50,9 +50,19 @@ function requestRasa(text, userId, attachmentIds, callback) {
5050 * Send a message to a niceday recipient
5151 * */
5252function sendMessage ( text , recipientId , additionalContents ) {
53- chatSdk . sendTextMessage ( recipientId , text , additionalContents ) . then ( ( response ) => {
54- console . log ( 'Successfully sent the message' , response ) ;
55- } ) ;
53+ chatSdk . sendTextMessage ( recipientId , text , additionalContents )
54+ . then ( ( response ) => {
55+ console . log ( 'Successfully sent the message' , response ) ;
56+ } )
57+ . catch ( ( ) => {
58+ chatSdk . sendTextMessage ( recipientId , text , additionalContents )
59+ . then ( ( newResponse ) => {
60+ console . log ( 'Successfully sent the message' , newResponse ) ;
61+ } )
62+ . catch ( ( error ) => {
63+ throw Error ( `Send message failed: ${ error } ` ) ;
64+ } ) ;
65+ } ) ;
5666}
5767
5868function sleep ( ms ) {
@@ -133,6 +143,17 @@ function setup(therapistId, token) {
133143 chatSdk . subscribeToConnectionStatusChanges ( ( connectionStatus ) => {
134144 if ( connectionStatus === ConnectionStatus . Connected ) {
135145 chatSdk . sendInitialPresence ( ) ;
146+ } else if ( connectionStatus === ConnectionStatus . Disconnected ) {
147+ authSdk . login ( THERAPIST_EMAIL_ADDRESS , THERAPIST_PASSWORD )
148+ . then ( ( response ) => {
149+ chatSdk . connect ( response . user . id , response . token )
150+ . catch ( ( connectionError ) => {
151+ throw Error ( `Error during connection: ${ connectionError } ` ) ;
152+ } ) ;
153+ } )
154+ . catch ( ( error ) => {
155+ throw Error ( `Error during relogin: ${ error } ` ) ;
156+ } ) ;
136157 }
137158 } ) ;
138159
@@ -144,9 +165,13 @@ function setup(therapistId, token) {
144165module . exports . setup = setup ;
145166
146167if ( require . main === module ) {
147- authSdk . login ( THERAPIST_EMAIL_ADDRESS , THERAPIST_PASSWORD ) . then ( ( response ) => {
148- setup ( response . user . id , response . token ) ;
149- } ) ;
168+ authSdk . login ( THERAPIST_EMAIL_ADDRESS , THERAPIST_PASSWORD )
169+ . then ( ( response ) => {
170+ setup ( response . user . id , response . token ) ;
171+ } )
172+ . catch ( ( error ) => {
173+ throw Error ( `Error during authentication: ${ error } ` ) ;
174+ } ) ;
150175
151176 // schedule a tasks to regenerate the token every 9 hours
152177 setupTokenRegeneration ( ) ;
0 commit comments