This repository was archived by the owner on Dec 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcommon.js
52 lines (44 loc) · 1.6 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"use strict";
const postbackActions = require("./messageTypes/postback");
const messageActions = require("./messageTypes/message");
const Bluebird = require("bluebird");
const Users = require("../models/users.model");
const { TextMessage } = require("../platforms/generics");
const translator = require("../utils/translator");
const logger = require("../providers/logging/logger");
module.exports = () => ({
ask (type, senderId, message, intent, entities, res, locale) {
Users.where({ senderId }).update({ $inc: { messageNumber: 1 } }).exec();
switch (type) {
case "postback": {
let promise;
postbackActions.some((postback) => {
if (message.match(new RegExp(postback.regx))) {
promise = postback.action(senderId, message, postback.regx, entities, res, locale);
return true;
}
return false;
});
return promise.catch((err) => isDisconnected(err, locale));
}
case "message": {
if (!messageActions[intent] || !messageActions[intent].action) {
return Bluebird.resolve({ responses: [new TextMessage(translator("noAnswer", locale))], feedback: false });
}
return messageActions[intent].action(senderId, message, entities, res, locale).catch((err) => isDisconnected(err, locale));
}
default:
return null;
}
}
});
function isDisconnected (err, locale) {
let error;
if (err.statusCode === 403 || err.statusCode === 401) {
error = Object.assign({}, err, { message: translator("disconnected", locale) });
} else {
logger.error(err);
error = err;
}
return Bluebird.reject(error);
}