-
Notifications
You must be signed in to change notification settings - Fork 0
/
clicksendText.js
65 lines (52 loc) · 1.54 KB
/
clicksendText.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
export default (request) => {
// require console module for logging to console
const console = require('console');
const pubnub = require('pubnub');
// require xhr
const xhr = require('xhr');
const auth = require('codec/auth');
// your clicksend api key
const apiUsername = '';
const apiKey = '';
// api endpoint
const apiUrl = 'https://rest.clicksend.com/v3/sms/send';
// destination phone number
const phoneNumber = '';
const channel = request.channels[0];
// require for unit tests only
const responseChannel = channel + '-response';
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: auth.basic(apiUsername, apiKey)
},
body: JSON.stringify({messages: [{
source: "blocks",
body: "Alert received from " + request.message.clientId,
from: "clicksend",
to: phoneNumber
}]})
};
// create a HTTP POST request to the sendgrid API
return xhr.fetch(apiUrl, options).then((r) => {
console.log(r);
let testResponse = 0;
if (r.status == 200) {
testResponse = 1;
}
pubnub.publish({
channel: responseChannel,
message: testResponse
});
return request.ok()
})
.catch(e => {
console.error(e);
pubnub.publish({
channel: responseChannel,
message: 0
});
return request.ok();
});
};