-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
100 lines (81 loc) · 3 KB
/
app.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const fetch = require('node-fetch');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 8080;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
// token generated for workspace
let token = "xoxb-460185489317-459921751539-aBOTAgojhVOxggZNfXc0NGgt";
// const revoke = 'https://slack.com/api/auth.revoke?token=' + token;
// fetch(revoke, {
// method : "GET",
// headers: { 'Content-Type': 'application/x-www-form-urlencoded'},
// }).then(
// response => response.json() // .json(), etc.
// // same as function(response) {return response.text();}
// ).then(
// html => console.log(html)
// );
function response(payload) {
const url = 'https://slack.com/api/chat.postMessage';
fetch(url, {
method : "POST",
body : JSON.stringify({
text: "Hello <@"+ payload.event.user + ">! Knock, knock.", // response of bot <@userID> tag user
channel: payload.event.channel // channel to post text
}),
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer '+ token},
}).then(
response => response.text() // .json(), etc.
// same as function(response) {return response.text();}
).then(
html => console.log(html)
);
}
function response2(payload) {
const url = 'https://slack.com/api/chat.postMessage';
fetch(url, {
method : "POST",
body : JSON.stringify({
text: "channel rename", // response of bot <@userID> tag user
channel: payload.event.channel.id // channel to post text
}),
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer '+ token},
}).then(
response => response.text() // .json(), etc.
// same as function(response) {return response.text();}
).then(
html => console.log(html)
);
}
// slash comand on create set endpoint for new comand
app.post('/slash', (req, res) => {
res.send("test"); // response text on chat
console.log(req.body);
});
// // for verify url for bot only need in configuration
// app.post('/bot', (req, res) => {
// const challenge = req.body.challenge;
// token = req.body.token;
// res.send(challenge);
// });
// endpoint for bot
app.post("/bot", (req, res, next) => {
// Get event payload
let payload = req.body;
console.log(req.body);
res.sendStatus(200);
// Respond to this event with HTTP 200 status
if (payload.event.type === "app_mention") { // if bot is mention in chat
if (payload.event.text.includes("test")) { // key words in message
response(payload);
}
} else if (payload.event.type === "channel_rename") {
response2(payload);
console.log("channel rename");
}
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));