-
Notifications
You must be signed in to change notification settings - Fork 12
/
chat.go
104 lines (98 loc) · 3.4 KB
/
chat.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
package slack
import (
"encoding/json"
"net/url"
"strconv"
"strings"
)
// AttachmentField holds information about an attachment field
type AttachmentField struct {
Title string `json:"title"`
Value string `json:"value"`
Short bool `json:"short"`
}
// Attachment holds information about an attachment
type Attachment struct {
ServiceName string `json:"service_name"`
Fallback string `json:"fallback"`
Color string `json:"color,omitempty"`
Pretext string `json:"pretext,omitempty"`
AuthorName string `json:"author_name,omitempty"`
AuthorLink string `json:"author_link,omitempty"`
AuthorIcon string `json:"author_icon,omitempty"`
Title string `json:"title,omitempty"`
TitleLink string `json:"title_link,omitempty"`
Text string `json:"text"`
ImageURL string `json:"image_url,omitempty"`
ThumbURL string `json:"thumb_url,omitempty"`
ThumbWidth int `json:"thumb_width"`
ThumbHeight int `json:"thumb_height"`
FromURL string `json:"from_url"`
Fields []AttachmentField `json:"fields,omitempty"`
MarkdownIn []string `json:"mrkdwn_in,omitempty"`
}
// PostMessageRequest includes all the fields in the post message request - see https://api.slack.com/methods/chat.postMessage
type PostMessageRequest struct {
Channel string `json:"channel"`
Text string `json:"text"`
Username string `json:"username"`
AsUser bool `json:"as_user"`
Parse string `json:"parse"`
LinkNames int `json:"link_names"`
Attachments []Attachment `json:"attachments"`
UnfurlLinks bool `json:"unfurl_links"`
UnfurlMedia bool `json:"unfurl_media"`
IconURL string `json:"icon_url"`
IconEmoji string `json:"icon_emoji"`
ThreadID string `json:"thread_ts"`
}
// PostMessageReply is the reply to the post message request - see https://api.slack.com/methods/chat.postMessage
type PostMessageReply struct {
slackResponse
Channel string `json:"channel"`
Timestamp string `json:"ts"`
Message PostMessageRequest `json:"message"`
}
// PostMessage posts a message to a channel
func (s *Slack) PostMessage(m *PostMessageRequest, escape bool) (*PostMessageReply, error) {
// Escape the special chars
text := ""
if escape {
replacer := strings.NewReplacer("&", "&", "<", "<", ">", ">")
text = replacer.Replace(m.Text)
} else {
text = m.Text
}
params := url.Values{
"channel": {m.Channel},
"text": {text},
}
if m.Username != "" {
params.Set("username", m.Username)
}
params.Set("as_user", strconv.FormatBool(m.AsUser))
params.Set("parse", m.Parse)
params.Set("link_names", strconv.Itoa(m.LinkNames))
params.Set("thread_ts", m.ThreadID)
if len(m.Attachments) > 0 {
attachments, err := json.Marshal(m.Attachments)
if err != nil {
return nil, err
}
params.Set("attachments", string(attachments))
}
params.Set("unfurl_links", strconv.FormatBool(m.UnfurlLinks))
params.Set("unfurl_media", strconv.FormatBool(m.UnfurlMedia))
if m.IconURL != "" {
params.Set("icon_url", m.IconURL)
}
if m.IconEmoji != "" {
params.Set("icon_emoji", m.IconEmoji)
}
r := &PostMessageReply{}
err := s.do("chat.postMessage", params, r)
if err != nil {
return nil, err
}
return r, nil
}