Skip to content

Commit 6e23b6a

Browse files
committed
speed depandant on length
1 parent f42b5d6 commit 6e23b6a

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

niceday-broker/index.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ require('dotenv').config();
1414
const { THERAPIST_PASSWORD, THERAPIST_EMAIL_ADDRESS, ENVIRONMENT } = process.env;
1515
let { RASA_AGENT_URL } = process.env;
1616
RASA_AGENT_URL = (RASA_AGENT_URL === undefined) ? 'http://rasa_server:5005/webhooks/rest/webhook' : RASA_AGENT_URL;
17-
const MESSAGE_DELAY = 3000; // Delay in between messages in ms
17+
// proportional factor between the number of words in a message and the time to wait before the
18+
// newt message is delivered
19+
WORDS_PER_SECOND = 5
20+
// maximum delay between a message and the next one
21+
MAX_DELAY = 10
1822

1923
const chatSdk = new Chat();
2024
let selectedServer;
@@ -73,22 +77,31 @@ function sleep(ms) {
7377
* Handle the response from rasa, send each message to the Niceday user.
7478
* We insert a delay in between messages, so the user has some time to read each message.
7579
* */
76-
function onRasaResponse() {
80+
async function onRasaResponse() {
7781
if (this.readyState === 4 && this.status === 200) {
7882
const responseJson = JSON.parse(this.responseText);
79-
responseJson.forEach(async (message, i) => {
80-
if (ENVIRONMENT === 'prod') {
81-
await sleep(i * MESSAGE_DELAY);
82-
}
83-
const attachment = {
83+
for (message of responseJson){
84+
const attachment = {
8485
replyOfId: null,
8586
attachmentIds: [],
8687
};
8788
if ('metadata' in message) {
8889
attachment.attachmentIds = message.metadata;
8990
}
90-
sendMessage(message.text, parseInt(message.recipient_id, 10), attachment);
91-
});
91+
92+
sendMessage(message.text, parseInt(message.recipient_id, 10), attachment)
93+
94+
if (ENVIRONMENT === 'prod') {
95+
const delay = (message.text.split(" ").length)/WORDS_PER_SECOND;
96+
97+
if (delay > MAX_DELAY) {
98+
delay = MAX_DELAY;
99+
}
100+
101+
await sleep(delay*1000);
102+
}
103+
}
104+
92105
} else if (this.readyState === 4) {
93106
console.log('Something went wrong, status:', this.status, this.responseText);
94107
}

0 commit comments

Comments
 (0)