Skip to content

Commit f42b5d6

Browse files
authored
Merge pull request #16 from PerfectFit-project/355-reconnect-broker
Fixing disconnected broker
2 parents 0461569 + 57a2a66 commit f42b5d6

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

niceday-broker/index.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,19 @@ function requestRasa(text, userId, attachmentIds, callback) {
5050
* Send a message to a niceday recipient
5151
* */
5252
function 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

5868
function 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) {
144165
module.exports.setup = setup;
145166

146167
if (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

Comments
 (0)