forked from RocketChat/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendMessageBySMS.js
69 lines (55 loc) · 1.87 KB
/
sendMessageBySMS.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { callbacks } from '../../../lib/callbacks';
import { settings } from '../../settings';
import { SMS } from '../../sms';
import { LivechatVisitors } from '../../models';
import { normalizeMessageFileUpload } from '../../utils/server/functions/normalizeMessageFileUpload';
callbacks.add(
'afterSaveMessage',
function (message, room) {
// skips this callback if the message was edited
if (message.editedAt) {
return message;
}
if (!SMS.enabled) {
return message;
}
// only send the sms by SMS if it is a livechat room with SMS set to true
if (!(typeof room.t !== 'undefined' && room.t === 'l' && room.sms && room.v && room.v.token)) {
return message;
}
// if the message has a token, it was sent from the visitor, so ignore it
if (message.token) {
return message;
}
// if the message has a type means it is a special message (like the closing comment), so skips
if (message.t) {
return message;
}
let extraData;
if (message.file) {
message = Promise.await(normalizeMessageFileUpload(message));
const { fileUpload, rid, u: { _id: userId } = {} } = message;
extraData = Object.assign({}, { rid, userId, fileUpload });
}
if (message.customFields && message.customFields.mediaCardURL) {
const mediaUrl = message.customFields.mediaCardURL;
extraData = Object.assign({}, { mediaUrl });
}
if (message.location) {
const { location } = message;
extraData = Object.assign({}, extraData, { location });
}
const SMSService = SMS.getService(settings.get('SMS_Service'));
if (!SMSService) {
return message;
}
const visitor = LivechatVisitors.getVisitorByToken(room.v.token);
if (!visitor || !visitor.phone || visitor.phone.length === 0) {
return message;
}
SMSService.send(room.sms.from, visitor.phone[0].phoneNumber, message.msg, extraData);
return message;
},
callbacks.priority.LOW,
'sendMessageBySms',
);