-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
144 lines (138 loc) · 4.55 KB
/
index.ts
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
import { App } from "@slack/bolt";
import 'dotenv/config'
const app = new App({
token: process.env.OAuthToken,
signingSecret: process.env.signingSecret,
socketMode:true,
appToken: process.env.appToken
});
const disabledUserIds:string[] = []
const defaultSupportHero = {slackName: "", timezone: ""}
let supportHero = {...defaultSupportHero}
/*
params are username (ex @[email protected]) and timezone (ex. America/New_York)
@returns whispered message to user giving the name of the new support hero.
*/
app.command("/sethero", async ({ command, ack,client }) => {
try {
ack()
const id: string = command.user_id
const commandParam = command.text.trim().split(" ")
if(commandParam.length !== 2) {
const result = await client.chat.postEphemeral({
channel: command.channel_id,
user: id,
text: `This command needs two parameters, one of which is the tagged Support Hero and the other is their timezone.`,
});
return
}
const userInfo = await app.client.users.info({user: id})
if(!userInfo.user || !userInfo.user.is_admin) {
const result = await client.chat.postEphemeral({
channel: command.channel_id,
user: id,
text: "You do not have admin privileges ",
});
return
}
if(commandParam[0].charAt(0) !== "@") {
const result = await client.chat.postEphemeral({
channel: command.channel_id,
user: id,
text: "The first parameter needs to be a tagged member in this workspace",
});
return
}
supportHero = {slackName: `<${commandParam[0]}>`,timezone: commandParam[1] ? commandParam[1] : ""}
const result = await client.chat.postEphemeral({
channel: command.channel_id,
user: id,
text: `Support Hero is now ${supportHero.slackName} in ${supportHero.timezone}`,
});
} catch (error) {
console.log("err")
console.error(error);
}
});
/*
@returns whispered message telling user the bot is disabled for them or that it is already disabled.
*/
app.command("/disable", async ({ command, ack,client }) => {
try {
ack()
const id: string = command.user_id
let text = ""
if(disabledUserIds.includes(id)) {
text = "The bot is already disabled!"
}
else {
disabledUserIds.push(id)
text = "The bot is now disabled!"
}
const result = await client.chat.postEphemeral({
channel: command.channel_id,
user: id,
text
});
} catch (error) {
console.log("err")
console.error(error);
}
});
/*
@returns whispered message telling user the bot is enabled for them or that it is already enabled.
*/
app.command("/enable", async ({ command, ack,client}) => {
try {
ack()
const id: string = command.user_id
let text = ""
const userIdIndex: number = disabledUserIds.findIndex((value) => value == id)
if(userIdIndex !== -1) {
disabledUserIds.splice(userIdIndex,1)
text= "The bot is now enabled!"
}
else {
text = "The bot is already enabled!"
}
const result = await client.chat.postEphemeral({
channel: command.channel_id,
user: id,
text
});
} catch (error) {
console.log("err")
console.error(error);
}
});
/*
listener for events that occur in channels the bot is in
*/
app.event("message",async ({ message,client }) => {
// Getting weird type error
const finishedMessage = message as any
try {
if(JSON.stringify(supportHero) === JSON.stringify(defaultSupportHero)) {
const result = await client.chat.postEphemeral({
channel: message.channel,
user: finishedMessage.user,
text: "You're asking the community for help. We recommend checking <https://posthog.com/questions|our support site> to see if your question has already been asked. There's no Support Hero currently available.",
});
return
}
else if(!disabledUserIds.includes(finishedMessage.user)) {
const msg = `You're asking the community for help. We recommend checking <https://posthog.com/questions|our support site> to see if your question has already been asked - but, if not, we'll respond <https://posthog.com/handbook/engineering/support-hero#prioritizing-requests|as quickly as we can>. Our Support Hero this week is ${supportHero.slackName}, based in ${supportHero.timezone}.`
const result = await client.chat.postEphemeral({
channel: message.channel,
user: finishedMessage.user,
text: msg,
});
console.log(result)
}
}
catch (error) {
console.log("err")
console.error(error);
}
});
app.start(3000)