Debian Codegraph @

pkg/services/chat/slack/slack_json.go

raw
1 package slack
2
3 import (
4 "regexp"
5 "strings"
6 )
7
8 // MessagePayload used within the Slack service.
9 type MessagePayload struct {
10 Text string `json:"text"`
11 BotName string `json:"username,omitempty"`
12 Blocks []block `json:"blocks,omitempty"`
13 Attachments []attachment `json:"attachments,omitempty"`
14 ThreadTS string `json:"thread_ts,omitempty"`
15 Channel string `json:"channel,omitempty"`
16 IconEmoji string `json:"icon_emoji,omitempty"`
17 IconURL string `json:"icon_url,omitempty"`
18 }
19
20 type block struct {
21 Type string `json:"type"`
22 Text blockText `json:"text"`
23 }
24
25 type blockText struct {
26 Type string `json:"type"`
27 Text string `json:"text"`
28 }
29
30 type attachment struct {
31 Title string `json:"title,omitempty"`
32 Fallback string `json:"fallback,omitempty"`
33 Text string `json:"text"`
34 Color string `json:"color,omitempty"`
35 Fields []legacyField `json:"fields,omitempty"`
36 Footer string `json:"footer,omitempty"`
37 Time int `json:"ts,omitempty"`
38 }
39
40 type legacyField struct {
41 Title string `json:"title"`
42 Value string `json:"value"`
43 Short bool `json:"short,omitempty"`
44 }
45
46 // APIResponse is the default generic response message sent from the API.
47 type APIResponse struct {
48 Ok bool `json:"ok"`
49 Error string `json:"error"`
50 Warning string `json:"warning"`
51 MetaData struct {
52 Warnings []string `json:"warnings"`
53 } `json:"response_metadata"`
54 }
55
56 // Constants for Slack API limits.
57 const (
58 MaxAttachments = 100 // Maximum number of attachments allowed by Slack API
59 )
60
61 var iconURLPattern = regexp.MustCompile(`https?://`)
62
63 // CreateJSONPayload compatible with the slack post message API.
64 func CreateJSONPayload(config *Config, message string) any {
65 lines := strings.Split(message, "\n")
66 // Pre-allocate atts with a capacity of min(len(lines), MaxAttachments)
67 atts := make([]attachment, 0, minInt(len(lines), MaxAttachments))
68
69 for i, line := range lines {
70 // When MaxAttachments have been reached, append the remaining lines to the last attachment
71 if i >= MaxAttachments {
72 atts[MaxAttachments-1].Text += "\n" + line
73
74 continue
75 }
76
77 atts = append(atts, attachment{
78 Text: line,
79 Color: config.Color,
80 Title: "",
81 Fallback: "",
82 Fields: nil,
83 Footer: "",
84 Time: 0,
85 })
86 }
87
88 // Remove last attachment if empty
89 if len(atts) > 0 && atts[len(atts)-1].Text == "" {
90 atts = atts[:len(atts)-1]
91 }
92
93 payload := MessagePayload{
94 ThreadTS: config.ThreadTS,
95 Text: config.Title,
96 BotName: config.BotName,
97 Attachments: atts,
98 Blocks: nil,
99 Channel: "",
100 IconEmoji: "",
101 IconURL: "",
102 }
103
104 payload.SetIcon(config.Icon)
105
106 if config.Channel != "webhook" {
107 payload.Channel = config.Channel
108 }
109
110 return payload
111 }
112
113 // SetIcon sets the appropriate icon field in the payload based on whether the input is a URL or not.
114 func (p *MessagePayload) SetIcon(icon string) {
115 p.IconURL = ""
116 p.IconEmoji = ""
117
118 if icon != "" {
119 if iconURLPattern.MatchString(icon) {
120 p.IconURL = icon
121 } else {
122 p.IconEmoji = icon
123 }
124 }
125 }
126
127 // minInt returns the smaller of two integers.
128 func minInt(a, b int) int {
129 if a < b {
130 return a
131 }
132
133 return b
134 }