-
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Never answer in POST /message. Gladys now call user back
- Loading branch information
1 parent
99a56f7
commit 0843ec9
Showing
12 changed files
with
146 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const queries = require('./brain.queries.js'); | ||
const Promise = require('bluebird'); | ||
const injector = require('./injector/injector.js'); | ||
|
||
module.exports = function answer(result, user) { | ||
return gladys.utils.sql(queries.getAnswers, [user.language, result.response.label]) | ||
.then((answers) => { | ||
|
||
// pick one answer randomly | ||
var randomRow = Math.floor(Math.random() * (answers.length - 1)) + 0; | ||
|
||
// test if answer exist, if yes pick the text | ||
if(randomRow != -1 && answers[randomRow] != undefined) result.response.text = answers[randomRow].text; | ||
else result.response.text = null; | ||
|
||
// replace variables by values | ||
result.response.text = injector.inject(result.response.text, result.response.scope); | ||
|
||
return gladys.utils.sql(queries.getNotificationTypes, [user.id]); | ||
}) | ||
.then((notificationTypes) => { | ||
|
||
// test each notification system | ||
return Promise.mapSeries(notificationTypes, function(notificationType) { | ||
return trySendingMessage(result.response, notificationType, user); | ||
}) | ||
.catch(function(err) { | ||
if (err.message !== 'ok') { | ||
sails.log.warn(err); | ||
} | ||
}); | ||
}) | ||
.then(() => result); | ||
}; | ||
|
||
/** | ||
* Call the service related to the notification | ||
*/ | ||
function trySendingMessage(response, type, user) { | ||
|
||
if (!gladys.modules[type.service] || typeof gladys.modules[type.service].notify !== "function") { | ||
return Promise.reject(new Error(`${type.service} is not a valid service`)); | ||
} | ||
|
||
sails.log.info(`Brain : answer : Trying to contact ${type.service}`); | ||
|
||
var notification = { | ||
title: user.assistantName, | ||
text: response.text | ||
}; | ||
|
||
return gladys.modules[type.service].notify(notification, user) | ||
.then(function(result) { | ||
|
||
sails.log.info(`Message sent with success with ${type.service}. Aborting the chain.`); | ||
|
||
// if module resolved, we stop the promise chain | ||
// it means one notification worked! | ||
return Promise.reject(new Error('ok')); | ||
}) | ||
.catch(function(e){ | ||
|
||
// if the error is because we want to exist the promise chain, | ||
// we need to propagate the error | ||
if(e.message === 'ok') return Promise.reject(e); | ||
|
||
// if notification does not work, we resolve | ||
// it means that we need to continue the flow | ||
return Promise.resolve(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = { | ||
|
||
getNotificationTypes: ` | ||
SELECT * FROM notificationtype nt | ||
JOIN notificationuser nu ON (nt.id = nu.notificationtype) | ||
WHERE nu.user = ? | ||
ORDER BY nu.priority; | ||
`, | ||
|
||
getAnswers: ` | ||
SELECT text FROM answer WHERE language = ? AND label = ?; | ||
` | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,11 @@ | ||
const uuid = require('uuid'); | ||
const Promise = require('bluebird'); | ||
|
||
module.exports = function send(user, message) { | ||
message.sender = user.id; | ||
|
||
// if receiver is null, it's a message for gladys | ||
if(!message.receiver) return sendMessageGladys(user, message); | ||
else sendMessageUser(message); | ||
}; | ||
// if receiver is null, it's a message for gladys, so we send message to the brain | ||
if(!message.receiver) gladys.brain.classify(user, message); | ||
|
||
function sendMessageUser(message) { | ||
return gladys.message.create(message) | ||
.then((newMessage) => { | ||
return { | ||
message: newMessage | ||
}; | ||
}); | ||
} | ||
|
||
function sendMessageGladys(user, message) { | ||
|
||
// first, classify the message | ||
return gladys.brain.classify(message.text, user.language) | ||
.then((results) => { | ||
|
||
var responses = []; | ||
|
||
results.forEach(function(result) { | ||
responses.push(result.response); | ||
}); | ||
|
||
// save the message in DB | ||
return gladys.message.create(message) | ||
.then((message) => { | ||
return {message, responses}; | ||
}); | ||
}); | ||
} | ||
return gladys.message.create(message); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ module.exports = { | |
type: 'integer', | ||
min: -2, | ||
max: 2, | ||
required: true | ||
defaultsTo: 0 | ||
}, | ||
|
||
isRead: { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters