-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(slack): use attachments and add title and color fields (#119)
* fix(slack): use attachments and add title and color fields * fix(slack): simplify init * test(slack): add serialization test * fix(slack): remove arbitrary max message length
- Loading branch information
Showing
5 changed files
with
107 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,59 @@ | ||
package slack | ||
|
||
import "encoding/json" | ||
import ( | ||
"encoding/json" | ||
"strings" | ||
) | ||
|
||
// JSON used within the Slack service | ||
type JSON struct { | ||
Text string `json:"text"` | ||
BotName string `json:"username"` | ||
Text string `json:"text"` | ||
BotName string `json:"username,omitempty"` | ||
Blocks []block `json:"blocks,omitempty"` | ||
Attachments []attachment `json:"attachments,omitempty"` | ||
} | ||
|
||
type block struct { | ||
Type string `json:"type"` | ||
Text blockText `json:"text"` | ||
} | ||
|
||
type blockText struct { | ||
Type string `json:"type"` | ||
Text string `json:"text"` | ||
} | ||
|
||
type attachment struct { | ||
Title string `json:"title,omitempty"` | ||
Fallback string `json:"fallback,omitempty"` | ||
Text string `json:"text"` | ||
Color string `json:"color,omitempty"` | ||
Fields []legacyField `json:"fields,omitempty"` | ||
Footer string `json:"footer,omitempty"` | ||
Time int `json:"ts,omitempty"` | ||
} | ||
|
||
type legacyField struct { | ||
Title string `json:"title"` | ||
Value string `json:"value"` | ||
Short bool `json:"short,omitempty"` | ||
} | ||
|
||
// CreateJSONPayload compatible with the slack webhook api | ||
func CreateJSONPayload(config *Config, message string) ([]byte, error) { | ||
|
||
var atts []attachment | ||
for _, line := range strings.Split(message, "\n") { | ||
atts = append(atts, attachment{ | ||
Text: line, | ||
Color: config.Color, | ||
}) | ||
} | ||
|
||
return json.Marshal( | ||
JSON{ | ||
Text: message, | ||
BotName: config.BotName, | ||
Text: config.Title, | ||
BotName: config.BotName, | ||
Attachments: atts, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters