-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (47 loc) · 1.21 KB
/
index.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
/**
* Triggered from a message on a Cloud Pub/Sub topic.
*
* @param {!Object} event Event payload.
* @param {!Object} context Metadata for the event.
*/
exports.helloPubSub = (event, context) => {
const message = event.data
? Buffer.from(event.data, 'base64').toString()
: 'Hello, World';
console.log(message);
sendMessage(message);
};
const dotenv = require('dotenv');
const https = require('https');
dotenv.config();
const slackToken = process.env.SLACK_TOKEN;
const slackChannel = process.env.SLACK_CHANNEL_ID;
console.log(slackChannel);
function sendMessage(message) {
var postData = JSON.stringify({
'channel' : slackChannel,
'text' : message,
});
var options = {
hostname: 'slack.com',
path: '/api/chat.postMessage',
method: 'POST',
port: 443,
headers: {
'Content-Type': 'application/json',
'Content-Length': postData.length,
'Authorization': 'Bearer ' + slackToken,
}
};
var req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
res.on('data', (d) => {
process.stdout.write(d);
});
});
req.on('error', (e) => {
console.error(e);
});
req.write(postData);
req.end();
}