|
| 1 | +package slack |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "net/url" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + DEFAULT_MESSAGE_USERNAME = "" |
| 12 | + DEFAULT_MESSAGE_THREAD_TIMESTAMP = "" |
| 13 | + DEFAULT_MESSAGE_ASUSER = false |
| 14 | + DEFAULT_MESSAGE_PARSE = "" |
| 15 | + DEFAULT_MESSAGE_LINK_NAMES = 0 |
| 16 | + DEFAULT_MESSAGE_UNFURL_LINKS = false |
| 17 | + DEFAULT_MESSAGE_UNFURL_MEDIA = true |
| 18 | + DEFAULT_MESSAGE_ICON_URL = "" |
| 19 | + DEFAULT_MESSAGE_ICON_EMOJI = "" |
| 20 | + DEFAULT_MESSAGE_MARKDOWN = true |
| 21 | + DEFAULT_MESSAGE_ESCAPE_TEXT = true |
| 22 | +) |
| 23 | + |
| 24 | +// PostMessageParameters contains all the parameters necessary (including the optional ones) for a PostMessage() request |
| 25 | +type PostMessageParameters struct { |
| 26 | + Text string `json:"text"` |
| 27 | + Username string `json:"user_name"` |
| 28 | + AsUser bool `json:"as_user"` |
| 29 | + Parse string `json:"parse"` |
| 30 | + ThreadTimestamp string `json:"thread_ts"` |
| 31 | + LinkNames int `json:"link_names"` |
| 32 | + Attachments []Attachment `json:"attachments"` |
| 33 | + UnfurlLinks bool `json:"unfurl_links"` |
| 34 | + UnfurlMedia bool `json:"unfurl_media"` |
| 35 | + IconURL string `json:"icon_url"` |
| 36 | + IconEmoji string `json:"icon_emoji"` |
| 37 | + Markdown bool `json:"mrkdwn,omitempty"` |
| 38 | + EscapeText bool `json:"escape_text"` |
| 39 | +} |
| 40 | + |
| 41 | +func escapeMessage(message string) string { |
| 42 | + replacer := strings.NewReplacer("&", "&", "<", "<", ">", ">") |
| 43 | + return replacer.Replace(message) |
| 44 | +} |
| 45 | + |
| 46 | +type chatResponseFull struct { |
| 47 | + Channel string `json:"channel"` |
| 48 | + Timestamp string `json:"ts"` |
| 49 | + Text string `json:"text"` |
| 50 | + APIresponse |
| 51 | +} |
| 52 | + |
| 53 | +func chatRequest(path string, values url.Values) (*chatResponseFull, error) { |
| 54 | + response := &chatResponseFull{} |
| 55 | + err := post(path, values, response) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + if !response.Ok { |
| 60 | + return nil, errors.New(response.Error) |
| 61 | + } |
| 62 | + return response, nil |
| 63 | +} |
| 64 | + |
| 65 | +// PostMessage sends a message to a channel. |
| 66 | +// Message is escaped by default according to https://api.slack.com/docs/formatting |
| 67 | +// Use http://davestevens.github.io/slack-message-builder/ to help crafting your message. |
| 68 | +func (api *Client) PostMessage(channel, text string, params PostMessageParameters) (string, string, error) { |
| 69 | + if params.EscapeText { |
| 70 | + text = escapeMessage(text) |
| 71 | + } |
| 72 | + values := url.Values{ |
| 73 | + "token": {api.APIToken}, |
| 74 | + "channel": {channel}, |
| 75 | + "text": {text}, |
| 76 | + } |
| 77 | + if params.Username != DEFAULT_MESSAGE_USERNAME { |
| 78 | + values.Set("username", string(params.Username)) |
| 79 | + } |
| 80 | + if params.AsUser != DEFAULT_MESSAGE_ASUSER { |
| 81 | + values.Set("as_user", "true") |
| 82 | + } |
| 83 | + if params.Parse != DEFAULT_MESSAGE_PARSE { |
| 84 | + values.Set("parse", string(params.Parse)) |
| 85 | + } |
| 86 | + if params.LinkNames != DEFAULT_MESSAGE_LINK_NAMES { |
| 87 | + values.Set("link_names", "1") |
| 88 | + } |
| 89 | + if params.Attachments != nil { |
| 90 | + attachments, err := json.Marshal(params.Attachments) |
| 91 | + if err != nil { |
| 92 | + return "", "", err |
| 93 | + } |
| 94 | + values.Set("attachments", string(attachments)) |
| 95 | + } |
| 96 | + if params.UnfurlLinks != DEFAULT_MESSAGE_UNFURL_LINKS { |
| 97 | + values.Set("unfurl_links", "true") |
| 98 | + } |
| 99 | + // I want to send a message with explicit `as_user` `true` and `unfurl_links` `false` in request. |
| 100 | + // Because setting `as_user` to `true` will change the default value for `unfurl_links` to `true` on Slack API side. |
| 101 | + if params.AsUser != DEFAULT_MESSAGE_ASUSER && params.UnfurlLinks == DEFAULT_MESSAGE_UNFURL_LINKS { |
| 102 | + values.Set("unfurl_links", "false") |
| 103 | + } |
| 104 | + if params.UnfurlMedia != DEFAULT_MESSAGE_UNFURL_MEDIA { |
| 105 | + values.Set("unfurl_media", "false") |
| 106 | + } |
| 107 | + if params.IconURL != DEFAULT_MESSAGE_ICON_URL { |
| 108 | + values.Set("icon_url", params.IconURL) |
| 109 | + } |
| 110 | + if params.IconEmoji != DEFAULT_MESSAGE_ICON_EMOJI { |
| 111 | + values.Set("icon_emoji", params.IconEmoji) |
| 112 | + } |
| 113 | + if params.Markdown != DEFAULT_MESSAGE_MARKDOWN { |
| 114 | + values.Set("mrkdwn", "false") |
| 115 | + } |
| 116 | + if params.ThreadTimestamp != DEFAULT_MESSAGE_THREAD_TIMESTAMP { |
| 117 | + values.Set("thread_ts", params.ThreadTimestamp) |
| 118 | + } |
| 119 | + |
| 120 | + response, err := chatRequest("chat.postMessage", values) |
| 121 | + if err != nil { |
| 122 | + return "", "", err |
| 123 | + } |
| 124 | + return response.Channel, response.Timestamp, nil |
| 125 | +} |
| 126 | + |
| 127 | +// UpdateMessageParameters contains all the parameters necessary (including the optional ones) for a UpdateMessage() request |
| 128 | +type UpdateMessageParameters struct { |
| 129 | + Timestamp string `json:"ts"` |
| 130 | + Text string `json:"text"` |
| 131 | + Attachments []Attachment `json:"attachments"` |
| 132 | + Parse string `json:"parse"` |
| 133 | + LinkNames int `json:"link_names"` |
| 134 | + AsUser bool `json:"as_user"` |
| 135 | +} |
| 136 | + |
| 137 | +// UpdateMessageWithAttachments updates a message in a channel with attachments |
| 138 | +func (api *Client) UpdateMessageWithAttachments(channel string, params UpdateMessageParameters) (string, string, string, error) { |
| 139 | + values := url.Values{ |
| 140 | + "token": {api.APIToken}, |
| 141 | + "channel": {channel}, |
| 142 | + "text": {escapeMessage(params.Text)}, |
| 143 | + "ts": {params.Timestamp}, |
| 144 | + } |
| 145 | + if params.AsUser != DEFAULT_MESSAGE_ASUSER { |
| 146 | + values.Set("as_user", "true") |
| 147 | + } |
| 148 | + if params.Parse != DEFAULT_MESSAGE_PARSE { |
| 149 | + values.Set("parse", string(params.Parse)) |
| 150 | + } |
| 151 | + if params.LinkNames != DEFAULT_MESSAGE_LINK_NAMES { |
| 152 | + values.Set("link_names", "1") |
| 153 | + } |
| 154 | + if params.Attachments != nil { |
| 155 | + attachments, err := json.Marshal(params.Attachments) |
| 156 | + if err != nil { |
| 157 | + return "", "", "", err |
| 158 | + } |
| 159 | + values.Set("attachments", string(attachments)) |
| 160 | + } |
| 161 | + response, err := chatRequest("chat.update", values) |
| 162 | + if err != nil { |
| 163 | + return "", "", "", err |
| 164 | + } |
| 165 | + return response.Channel, response.Timestamp, response.Text, nil |
| 166 | +} |
| 167 | + |
| 168 | +// NewPostMessageParameters provides an instance of PostMessageParameters with all the sane default values set |
| 169 | +func NewPostMessageParameters() PostMessageParameters { |
| 170 | + return PostMessageParameters{ |
| 171 | + Username: DEFAULT_MESSAGE_USERNAME, |
| 172 | + AsUser: DEFAULT_MESSAGE_ASUSER, |
| 173 | + Parse: DEFAULT_MESSAGE_PARSE, |
| 174 | + LinkNames: DEFAULT_MESSAGE_LINK_NAMES, |
| 175 | + Attachments: nil, |
| 176 | + UnfurlLinks: DEFAULT_MESSAGE_UNFURL_LINKS, |
| 177 | + UnfurlMedia: DEFAULT_MESSAGE_UNFURL_MEDIA, |
| 178 | + IconURL: DEFAULT_MESSAGE_ICON_URL, |
| 179 | + IconEmoji: DEFAULT_MESSAGE_ICON_EMOJI, |
| 180 | + Markdown: DEFAULT_MESSAGE_MARKDOWN, |
| 181 | + EscapeText: DEFAULT_MESSAGE_ESCAPE_TEXT, |
| 182 | + } |
| 183 | +} |
0 commit comments