| 1 | package slackscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/ |
| 2 | |
| 3 | import ( |
| 4 | "regexpscip-go gomod github.com/golang/go/src . regexp/" |
| 5 | "stringsscip-go gomod github.com/golang/go/src . strings/" |
| 6 | ) |
| 7 | |
| 8 | // MessagePayload used within the Slack service. |
| 9 | type MessagePayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload# struct { |
| 10 | Textscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#Text. string `json:"text"` |
| 11 | BotNamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#BotName. string `json:"username,omitempty"` |
| 12 | Blocksscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#Blocks. []blockscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/block# `json:"blocks,omitempty"` |
| 13 | Attachmentsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#Attachments. []attachmentscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment# `json:"attachments,omitempty"` |
| 14 | ThreadTSscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#ThreadTS. string `json:"thread_ts,omitempty"` |
| 15 | Channelscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#Channel. string `json:"channel,omitempty"` |
| 16 | IconEmojiscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#IconEmoji. string `json:"icon_emoji,omitempty"` |
| 17 | IconURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#IconURL. string `json:"icon_url,omitempty"` |
| 18 | } |
| 19 | |
| 20 | type blockscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/block# struct { |
| 21 | Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/block#Type. string `json:"type"` |
| 22 | Textscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/block#Text. blockTextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/blockText# `json:"text"` |
| 23 | } |
| 24 | |
| 25 | type blockTextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/blockText# struct { |
| 26 | Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/blockText#Type. string `json:"type"` |
| 27 | Textscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/blockText#Text. string `json:"text"` |
| 28 | } |
| 29 | |
| 30 | type attachmentscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment# struct { |
| 31 | Titlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment#Title. string `json:"title,omitempty"` |
| 32 | Fallbackscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment#Fallback. string `json:"fallback,omitempty"` |
| 33 | Textscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment#Text. string `json:"text"` |
| 34 | Colorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment#Color. string `json:"color,omitempty"` |
| 35 | Fieldsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment#Fields. []legacyFieldscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/legacyField# `json:"fields,omitempty"` |
| 36 | Footerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment#Footer. string `json:"footer,omitempty"` |
| 37 | Timescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment#Time. int `json:"ts,omitempty"` |
| 38 | } |
| 39 | |
| 40 | type legacyFieldscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/legacyField# struct { |
| 41 | Titlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/legacyField#Title. string `json:"title"` |
| 42 | Valuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/legacyField#Value. string `json:"value"` |
| 43 | Shortscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/legacyField#Short. bool `json:"short,omitempty"` |
| 44 | } |
| 45 | |
| 46 | // APIResponse is the default generic response message sent from the API. |
| 47 | type APIResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/APIResponse# struct { |
| 48 | Okscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/APIResponse#Ok. bool `json:"ok"` |
| 49 | Errorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/APIResponse#Error. string `json:"error"` |
| 50 | Warningscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/APIResponse#Warning. string `json:"warning"` |
| 51 | MetaDatascip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/APIResponse#MetaData. struct { |
| 52 | Warningsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/APIResponse#$anon_087240261f7db507#Warnings. []string `json:"warnings"` |
| 53 | } `json:"response_metadata"` |
| 54 | } |
| 55 | |
| 56 | // Constants for Slack API limits. |
| 57 | const ( |
| 58 | MaxAttachmentsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MaxAttachments. = 100 // Maximum number of attachments allowed by Slack API |
| 59 | ) |
| 60 | |
| 61 | var iconURLPatternscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/iconURLPattern. = regexpscip-go gomod github.com/golang/go/src . regexp/.MustCompilescip-go gomod github.com/golang/go/src . regexp/MustCompile().(`https?://`) |
| 62 | |
| 63 | // CreateJSONPayload compatible with the slack post message API. |
| 64 | func CreateJSONPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/CreateJSONPayload().(config *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/Config#, message string) any { |
| 65 | lines := stringsscip-go gomod github.com/golang/go/src . strings/.Splitscip-go gomod github.com/golang/go/src . strings/Split().(message, "\n") |
| 66 | // Pre-allocate atts with a capacity of min(len(lines), MaxAttachments) |
| 67 | atts := make([]attachmentscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment#, 0, minIntscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/minInt().(len(lines), MaxAttachmentsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/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 >= MaxAttachmentsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MaxAttachments. { |
| 72 | atts[MaxAttachmentsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MaxAttachments.-1].Textscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment#Text. += "\n" + line |
| 73 | |
| 74 | continue |
| 75 | } |
| 76 | |
| 77 | atts = append(atts, attachmentscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment#{ |
| 78 | Textscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment#Text.: line, |
| 79 | Colorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment#Color.: config.Colorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/Config#Color., |
| 80 | Titlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment#Title.: "", |
| 81 | Fallbackscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment#Fallback.: "", |
| 82 | Fieldsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment#Fields.: nil, |
| 83 | Footerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment#Footer.: "", |
| 84 | Timescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment#Time.: 0, |
| 85 | }) |
| 86 | } |
| 87 | |
| 88 | // Remove last attachment if empty |
| 89 | if len(atts) > 0 && atts[len(atts)-1].Textscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/attachment#Text. == "" { |
| 90 | atts = atts[:len(atts)-1] |
| 91 | } |
| 92 | |
| 93 | payload := MessagePayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#{ |
| 94 | ThreadTSscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#ThreadTS.: config.ThreadTSscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/Config#ThreadTS., |
| 95 | Textscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#Text.: config.Titlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/Config#Title., |
| 96 | BotNamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#BotName.: config.BotNamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/Config#BotName., |
| 97 | Attachmentsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#Attachments.: atts, |
| 98 | Blocksscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#Blocks.: nil, |
| 99 | Channelscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#Channel.: "", |
| 100 | IconEmojiscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#IconEmoji.: "", |
| 101 | IconURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#IconURL.: "", |
| 102 | } |
| 103 | |
| 104 | payload.SetIconscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#SetIcon().(config.Iconscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/Config#Icon.) |
| 105 | |
| 106 | if config.Channelscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/Config#Channel. != "webhook" { |
| 107 | payload.Channelscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#Channel. = config.Channelscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/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 *MessagePayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#) SetIconscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#SetIcon().(icon string) { |
| 115 | p.IconURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#IconURL. = "" |
| 116 | p.IconEmojiscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#IconEmoji. = "" |
| 117 | |
| 118 | if icon != "" { |
| 119 | if iconURLPatternscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/iconURLPattern..MatchStringscip-go gomod github.com/golang/go/src . regexp/Regexp#MatchString().(icon) { |
| 120 | p.IconURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#IconURL. = icon |
| 121 | } else { |
| 122 | p.IconEmojiscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/MessagePayload#IconEmoji. = icon |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // minInt returns the smaller of two integers. |
| 128 | func minIntscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/slack`/minInt().(a, b int) int { |
| 129 | if a < b { |
| 130 | return a |
| 131 | } |
| 132 | |
| 133 | return b |
| 134 | } |