-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
171 lines (140 loc) · 3.72 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const { App } = require("@slack/bolt");
const { WebClient, LogLevel } = require("@slack/web-api");
require("dotenv").config();
const app = new App({
signingSecret: process.env.SLACK_SIGNING_SECRET,
token: process.env.SLACK_BOT_TOKEN,
socketMode: true, // enable the following to use socket mode
appToken: process.env.APP_TOKEN,
});
const client = new WebClient(process.env.OWN_TOKEN, {
// LogLevel can be imported and used to make debugging simpler
logLevel: LogLevel.DEBUG,
});
/* Add functionality here */
app.event("app_home_opened", ({ event, say }) => {
// console.log(event);
say(`Hello, <@${event.user}>!`);
});
app.command("/delete", async ({ command, ack, say }) => {
// let conversationHistory, channel_id;
try {
// console.log(command);
await ack();
const result = await client.conversations.history({
channel: command.channel_id,
});
// console.log(result);
var timeStamp = getTs(result.messages);
// console.log(timeStamp, result.messages.length);
for (let msg of timeStamp) {
await client.chat.delete({
channel: command.channel_id,
ts: msg,
});
}
// say("Yaaay! that command works!");
} catch (error) {
console.log(error);
}
});
function getTs(arr) {
var res = [];
for (let msg of arr) {
res.push(msg.ts);
}
return res;
}
app.command("/charlie", async ({ command, ack, say }) => {
try {
await ack();
// console.log(command);
var myUser_id = command.user_id;
var thisChannel = command.channel_id;
var finalmsg = "";
var x = "";
x = command.text;
finalmsg = x.replace(/<[^,]*>/, "");
const result = await client.users.list();
// console.log(result);
var users = saveUsers(result.members);
// console.log(users);
var getting = x.match(/<[^>]*>/g);
if (getting != null) {
var excluders = filteringUsers(getting);
// console.log(excluders, "-----------");
users = users.filter((el) => {
return !excluders.includes(el);
});
}
var total_num = users.length;
for (const singleUser of users) {
const opening = await client.conversations.open({
users: singleUser,
return_im: true,
});
await client.chat.postMessage({
channel: singleUser,
text: finalmsg,
as_user: true,
});
}
// const sending =
// console.log("----------------------------------------");
// console.log(sending);
// console.log("----------------------------------------");
var attachments = [
{
color: "#f2c744",
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `Success! This message is being sent from you directly to the ${total_num} people in this channel. It may take a few minutes to reach everyone.`,
},
},
{
type: "divider",
},
],
},
];
// say(message);
await client.chat.postEphemeral({
channel: thisChannel,
user: myUser_id,
text: "Invisible",
attachments,
});
} catch (error) {
// console.log("err");
console.error(error);
}
});
function saveUsers(usersArray) {
var arr = [];
let userId = "";
usersArray.forEach(function (user) {
userId = user["id"];
arr.push(userId);
});
return arr;
}
function filteringUsers(users) {
var rem = [];
for (const single of users) {
var x = single.split("");
x.shift();
x.shift();
x.pop();
var ready = x.join("").split("|");
rem.push(ready[0]);
}
return rem;
}
(async () => {
// Start the app
await app.start(process.env.PORT || 3000);
console.log("⚡️ Bolt app is running!");
})();