/
webhook_in_message.go
66 lines (56 loc) · 1.83 KB
/
webhook_in_message.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
package slack
import (
"encoding/json"
"net/url"
"strings"
"github.com/grokify/mogo/encoding/jsonutil"
)
const (
ParamNamePayload = "payload"
)
type Message struct {
Attachments []Attachment `json:"attachments,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
IconURL string `json:"icon_url,omitempty"`
Mrkdwn bool `json:"mrkdwn,omitempty"`
Text string `json:"text,omitempty"`
Username string `json:"username,omitempty"`
}
func ParseMessageJSON(bytes []byte) (Message, error) {
msg := Message{}
return msg, json.Unmarshal(bytes, &msg)
}
func ParseMessageURLEncoded(data []byte) (Message, error) {
qry, err := url.ParseQuery(string(data))
if err != nil {
return Message{}, err
}
return ParseMessageURLValues(qry)
}
func ParseMessageURLValues(qry url.Values) (Message, error) {
return ParseMessageJSON([]byte(qry.Get(ParamNamePayload)))
}
func ParseMessageAny(data []byte) (Message, error) {
if strings.Index(strings.TrimSpace(string(data)), "{") == 0 {
return ParseMessageJSON(data)
}
return ParseMessageURLEncoded(data)
}
type Attachment struct {
AuthorIcon string `json:"author_icon,omitempty"`
AuthorLink string `json:"author_link,omitempty"`
AuthorName string `json:"author_name,omitempty"`
Color string `json:"color,omitempty"`
Fallback string `json:"fallback,omitempty"`
Fields []Field `json:"fields,omitempty"`
MarkdownIn []string `json:"mrkdwn_in,omitempty"`
Pretext string `json:"pretext,omitempty"`
Text string `json:"text,omitempty"`
ThumbnailURL string `json:"thumbnail_url,omitempty"`
Title string `json:"title,omitempty"`
}
type Field struct {
Title string `json:"title,omitempty"`
Value jsonutil.String `json:"value,omitempty"`
Short bool `json:"short,omitempty"`
}