Add attachment type as Go struct (not []byte)
Make Id consistent as Id instead of ID throughout the code
Norberto Lopes
9 years ago
0 | 0 | package slack |
1 | 1 | |
2 | 2 | import ( |
3 | "encoding/json" | |
3 | 4 | "errors" |
4 | 5 | "net/url" |
5 | 6 | "strings" |
16 | 17 | ) |
17 | 18 | |
18 | 19 | type chatResponseFull struct { |
19 | ChannelID string `json:"channel"` | |
20 | ChannelId string `json:"channel"` | |
20 | 21 | Timestamp string `json:"ts"` |
21 | 22 | Text string `json:"text"` |
22 | 23 | SlackResponse |
24 | } | |
25 | ||
26 | type AttachmentField struct { | |
27 | Title string `json:"title"` | |
28 | Value string `json:"value"` | |
29 | Short string `json:"short"` | |
30 | } | |
31 | ||
32 | type Attachment struct { | |
33 | Fallback string `json:"fallback"` | |
34 | ||
35 | Color string `json:"color,omitempty"` | |
36 | ||
37 | Pretext string `json:"pretext,omitempty"` | |
38 | ||
39 | AuthorName string `json:"author_name,omitempty"` | |
40 | AuthorLink string `json:"author_link,omitempty"` | |
41 | AuthorIcon string `json:"author_icon,omitempty"` | |
42 | ||
43 | Title string `json:"title,omitempty"` | |
44 | TitleLink string `json:"title_link,omitempty"` | |
45 | ||
46 | Text string `json:"text"` | |
47 | ||
48 | Fields []AttachmentField `json:"fields,omitempty"` | |
23 | 49 | } |
24 | 50 | |
25 | 51 | type PostMessageParameters struct { |
27 | 53 | Username string |
28 | 54 | Parse string |
29 | 55 | LinkNames int |
30 | Attachments []byte | |
56 | Attachments []Attachment | |
31 | 57 | UnfurlLinks bool |
32 | 58 | UnfurlMedia bool |
33 | 59 | IconURL string |
69 | 95 | if err != nil { |
70 | 96 | return "", "", err |
71 | 97 | } |
72 | return response.ChannelID, response.Timestamp, nil | |
98 | return response.ChannelId, response.Timestamp, nil | |
73 | 99 | } |
74 | 100 | |
75 | 101 | func EscapeMessage(message string) string { |
82 | 108 | return replacer.Replace(message) |
83 | 109 | } |
84 | 110 | |
85 | func (api *Slack) PostMessage(channelId string, text string, params PostMessageParameters) (string, string, error) { | |
111 | func (api *Slack) PostMessage(channelId string, text string, params PostMessageParameters) (channel string, timestamp string, err error) { | |
86 | 112 | values := url.Values{ |
87 | 113 | "token": {api.config.token}, |
88 | 114 | "channel": {channelId}, |
98 | 124 | values.Set("link_names", "1") |
99 | 125 | } |
100 | 126 | if params.Attachments != nil { |
101 | values.Set("attachments", string(params.Attachments)) | |
127 | attachments, err := json.Marshal(params.Attachments) | |
128 | if err != nil { | |
129 | return "", "", err | |
130 | } | |
131 | values.Set("attachments", string(attachments)) | |
102 | 132 | } |
103 | 133 | if params.UnfurlLinks == DEFAULT_MESSAGE_UNFURL_LINKS { |
104 | 134 | values.Set("unfurl_links", "false") |
117 | 147 | if err != nil { |
118 | 148 | return "", "", err |
119 | 149 | } |
120 | return response.ChannelID, response.Timestamp, nil | |
150 | return response.ChannelId, response.Timestamp, nil | |
121 | 151 | } |
122 | 152 | |
123 | 153 | func (api *Slack) UpdateMessage(channelId, timestamp, text string) (string, string, string, error) { |
131 | 161 | if err != nil { |
132 | 162 | return "", "", "", err |
133 | 163 | } |
134 | return response.ChannelID, response.Timestamp, response.Text, nil | |
164 | return response.ChannelId, response.Timestamp, response.Text, nil | |
135 | 165 | |
136 | 166 | } |
0 | package main | |
1 | ||
2 | import ( | |
3 | "fmt" | |
4 | ||
5 | "github.com/nlopes/slack" | |
6 | ) | |
7 | ||
8 | func main() { | |
9 | api := slack.New("YOUR_TOKEN_HERE") | |
10 | params := slack.PostMessageParameters{} | |
11 | attachment := slack.Attachment{ | |
12 | Pretext: "some pretext", | |
13 | Text: "some text", | |
14 | // Uncomment the following part to send a field too | |
15 | /* | |
16 | Fields: []slack.AttachmentField{ | |
17 | slack.AttachmentField{ | |
18 | Title: "a", | |
19 | Value: "no", | |
20 | }, | |
21 | }, | |
22 | */ | |
23 | } | |
24 | params.Attachments = []slack.Attachment{attachment} | |
25 | channelId, timestamp, err := api.PostMessage("CHANNEL_ID", "Some text", params) | |
26 | if err != nil { | |
27 | fmt.Printf("%s\n", err) | |
28 | return | |
29 | } | |
30 | fmt.Printf("Message successfully sent to channel %s at %s", channelId, timestamp) | |
31 | } |
19 | 19 | type Comment struct { |
20 | 20 | Id string `json:"id"` |
21 | 21 | Timestamp JSONTime `json:"timestamp"` |
22 | UserID string `json:"user"` | |
22 | UserId string `json:"user"` | |
23 | 23 | Comment string `json:"comment"` |
24 | 24 | } |
25 | 25 | |
33 | 33 | Mimetype string `json:"mimetype"` |
34 | 34 | Filetype string `json:"filetype"` |
35 | 35 | PrettyType string `json:"pretty_type"` |
36 | UserID string `json:"user"` | |
36 | UserId string `json:"user"` | |
37 | 37 | |
38 | 38 | Mode string `json:"mode"` |
39 | 39 | Editable bool `json:"editable"` |
1 | 1 | |
2 | 2 | type OutgoingMessage struct { |
3 | 3 | Id int `json:"id"` |
4 | ChannelID string `json:"channel,omitempty"` | |
4 | ChannelId string `json:"channel,omitempty"` | |
5 | 5 | Text string `json:"text,omitempty"` |
6 | 6 | Type string `json:"type,omitempty"` |
7 | 7 | } |
13 | 13 | |
14 | 14 | type Msg struct { |
15 | 15 | Id string `json:"id"` |
16 | UserID string `json:"user,omitempty"` | |
17 | ChannelID string `json:"channel,omitempty"` | |
16 | UserId string `json:"user,omitempty"` | |
17 | ChannelId string `json:"channel,omitempty"` | |
18 | 18 | Timestamp string `json:"ts,omitempty"` |
19 | 19 | Text string `json:"text,omitempty"` |
20 | 20 | Team string `json:"team,omitempty"` |
30 | 30 | |
31 | 31 | type Presence struct { |
32 | 32 | Presence string `json:"presence"` |
33 | UserID string `json:"user"` | |
33 | UserId string `json:"user"` | |
34 | 34 | } |
35 | 35 | |
36 | 36 | type Event struct { |
56 | 56 | return &OutgoingMessage{ |
57 | 57 | Id: api.messageId, |
58 | 58 | Type: "message", |
59 | ChannelID: channel, | |
59 | ChannelId: channel, | |
60 | 60 | Text: text, |
61 | 61 | } |
62 | 62 | } |
8 | 8 | |
9 | 9 | type UserTyping struct { |
10 | 10 | Type string `json:"type"` |
11 | UserID string `json:"user"` | |
12 | ChannelID string `json:"channel"` | |
11 | UserId string `json:"user"` | |
12 | ChannelId string `json:"channel"` | |
13 | 13 | } |
14 | 14 | |
15 | 15 | type SlackEvent struct { |
152 | 152 | // Allows us to have stats about latency and what not |
153 | 153 | return |
154 | 154 | case "presence_change": |
155 | //log.Printf("`%s is %s`\n", info.GetUserById(event.PUserID).Name, event.Presence) | |
155 | //log.Printf("`%s is %s`\n", info.GetUserById(event.PUserId).Name, event.Presence) | |
156 | 156 | case "message": |
157 | 157 | handleMessage(ch, event) |
158 | 158 | case "channel_marked": |