-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack_util.go
176 lines (154 loc) · 5.18 KB
/
slack_util.go
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
172
173
174
175
176
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/slack-go/slack"
)
// Slack utility functions. Mostly just for data parsing. Don't actually reqire Slack
// client, but operate on Slack resources.
func parseSlackMrkdwnLinks(message string) string {
// Regular expression to match links with optional labels
linkRegex := regexp.MustCompile(`<([^|>]+)\|([^>]+)>|<([^>]+)>`)
// Replace links with HTML-formatted links
result := linkRegex.ReplaceAllStringFunc(message, func(match string) string {
// If the link has a label, use it as the anchor text, otherwise use the URL
parts := strings.Split(match[1:len(match)-1], "|")
if len(parts) == 2 {
return fmt.Sprintf(`<a target="_blank" href="%s">%s</a>`, parts[0], parts[1])
} else {
return fmt.Sprintf(`<a target="_blank" href="%s">%s</a>`, parts[0], parts[0])
}
})
return result
}
// REALLY SHITTY parser from ChatGPT. I spent some time fucking around with the
// Blocks and have concluded that writing a parser for that shit is a whole other
// project in and of itself. Maybe someday. For now, my shit will probably be
// vulnerable to regex-based attacks.
func mrkdwnToMarkdown(input string) string {
// Handle bold text
boldRegex := regexp.MustCompile(`\*(.*?)\*`)
input = boldRegex.ReplaceAllString(input, "**$1**")
// Handle italic text
italicRegex := regexp.MustCompile(`_(.*?)_`)
input = italicRegex.ReplaceAllString(input, "*$1*")
// Handle strikethrough text
strikeRegex := regexp.MustCompile(`~(.*?)~`)
input = strikeRegex.ReplaceAllString(input, "~~$1~~")
// Handle code blocks
codeRegex := regexp.MustCompile("`([^`]+)`")
input = codeRegex.ReplaceAllString(input, "`$1`")
// Handle links with labels
linkWithLabelRegex := regexp.MustCompile(`<([^|]+)\|([^>]+)>`)
input = linkWithLabelRegex.ReplaceAllString(input, "[$2]($1)")
// Handle links without labels
linkWithoutLabelRegex := regexp.MustCompile(`<([^>]+)>`)
input = linkWithoutLabelRegex.ReplaceAllString(input, "[$1]($1)")
return input
}
func slackTSToTime(slackTimestamp string) (slackTime time.Time) {
// Convert the Slack timestamp to a Unix timestamp (float64)
slackUnixTimestamp, err := strconv.ParseFloat(strings.Split(slackTimestamp, ".")[0], 64)
if err != nil {
fmt.Println("Error parsing Slack timestamp:", err)
return
}
// Create a time.Time object from the Unix timestamp (assuming UTC time zone)
slackTime = time.Unix(int64(slackUnixTimestamp), 0)
return slackTime
}
// Converts the timestamp from a message into a human-readable format.
func slackTSToHumanTime(slackTimestamp string) (hrt string) {
slackTime := slackTSToTime(slackTimestamp)
// Convert to a specific time zone (e.g., "America/New_York")
location, err := time.LoadLocation("America/New_York")
if err != nil {
fmt.Println("Error loading location:", err)
return
}
slackTimeInLocation := slackTime.In(location)
// Format the time as a human-readable string
humanReadableTimestamp := slackTimeInLocation.Format("2006-01-02 15:04:05 MST")
return humanReadableTimestamp
}
// Function to build the message the bot sends in response to being pinged with
// a new status update.
func CreateUpdateResponseMsg(channelName string, user string) (blocks []slack.Block) {
blocks = []slack.Block{
slack.NewSectionBlock(
slack.NewTextBlockObject(slack.MarkdownType, fmt.Sprintf("<@%s> I see you have posted a new message to the support page. What kind of alert is this? *Warning: this alert is live immediately!*", user), false, false),
nil,
nil,
),
slack.NewInputBlock("options", slack.NewTextBlockObject(slack.PlainTextType, " ", false, false), nil,
slack.NewCheckboxGroupsBlockElement(
"options",
slack.NewOptionBlockObject(
CSPPin,
slack.NewTextBlockObject(
"plain_text",
"Pin this message to the status page",
false,
false,
),
nil,
),
slack.NewOptionBlockObject(
CSPForward,
slack.NewTextBlockObject(
"plain_text",
fmt.Sprintf("Forward message to the #%s channel", channelName),
false,
false,
),
nil,
),
),
),
slack.NewActionBlock(
"",
slack.NewButtonBlockElement(
CSPSetError,
CSPSetError,
slack.NewTextBlockObject("plain_text", "🔥 Critical", true, false),
),
slack.NewButtonBlockElement(
CSPSetWarn,
CSPSetWarn,
slack.NewTextBlockObject("plain_text", "⚠️ Warning", true, false),
),
slack.NewButtonBlockElement(
CSPSetOK,
CSPSetOK,
slack.NewTextBlockObject("plain_text", "✅ OK/Info", true, false),
),
slack.NewButtonBlockElement(
CSPCancel,
CSPCancel,
slack.NewTextBlockObject("plain_text", "❌Close", true, false),
),
),
}
return blocks
}
func GetPinnedMessageStatus(reactions []slack.ItemReaction) string {
for _, reaction := range reactions {
// Only take action on our reactions
if botReaction := stringInSlice(reaction.Users, config.SlackBotID); !botReaction {
continue
}
// Use the first reaction sent by the bot that we find
switch reaction.Name {
case config.StatusOKEmoji:
return config.StatusOKEmoji
case config.StatusWarnEmoji:
return config.StatusWarnEmoji
case config.StatusErrorEmoji:
return config.StatusErrorEmoji
}
}
return ""
}