-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclickatell.js
More file actions
68 lines (60 loc) · 1.78 KB
/
Copy pathclickatell.js
File metadata and controls
68 lines (60 loc) · 1.78 KB
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
import clickatell from 'clickatell-node';
let count = 0;
const config = Meteor.settings.clickatell || {
username: null,
password: null,
api_id: null,
};
const clhttp = clickatell.http(config.username, config.password, config.api_id);
const clsend = Meteor.wrapAsync(clhttp.sendMessage, clhttp);
const clbal = Meteor.wrapAsync(clhttp.getBalance, clhttp);
const sendOrFake = (to, message, extra) => {
if (config.api_id) {
clsend(to, message, extra).forEach(result => {
if (result.error) {
console.error('Error sending SMS:', result);
throw new Meteor.Error(result.code, result.error);
}
});
} else {
console.log(`====== BEGIN SMS #${count} ======`);
console.log('(SMS not sent; to enable sending, configure the ' +
'\'clickatell\' section in settings.json.)');
console.log('To:', to);
console.log('Extra:', extra, '\n');
console.log(message);
console.log(`====== END SMS #${count} ======`);
}
};
SMS = {
send: (options) => {
let to = [];
[options.to, options.cc, options.bcc].forEach(param => {
if (Array.isArray(param)) {
to = to.concat(param);
} else if (param) {
to.push(param);
}
});
const extra = {
callback: 0,
concat: 3,
};
if (options.from) {
extra.from = options.from;
extra.mo = 1;
}
if (options.messageId) {
extra.cliMsgId = options.messageId;
}
if (options.headers) {
Object.assign(extra, options.headers);
}
sendOrFake(to, options.text, extra);
count++;
},
balance: () => (config.api_id ? clbal().balance : 0),
};
// Variables exported by this module can be imported by other packages and
// applications. See clickatell-tests.js for an example of importing.
export const name = 'clickatell';