diff options
Diffstat (limited to 'vendor/github.com/nlopes/slack/chat.go')
-rw-r--r-- | vendor/github.com/nlopes/slack/chat.go | 123 |
1 files changed, 87 insertions, 36 deletions
diff --git a/vendor/github.com/nlopes/slack/chat.go b/vendor/github.com/nlopes/slack/chat.go index 8cc6bdef..eb8fd6c8 100644 --- a/vendor/github.com/nlopes/slack/chat.go +++ b/vendor/github.com/nlopes/slack/chat.go @@ -96,26 +96,24 @@ func (api *Client) DeleteMessageContext(ctx context.Context, channel, messageTim // PostMessage sends a message to a channel. // Message is escaped by default according to https://api.slack.com/docs/formatting // Use http://davestevens.github.io/slack-message-builder/ to help crafting your message. -func (api *Client) PostMessage(channel, text string, params PostMessageParameters) (string, string, error) { +func (api *Client) PostMessage(channelID string, options ...MsgOption) (string, string, error) { respChannel, respTimestamp, _, err := api.SendMessageContext( context.Background(), - channel, - MsgOptionText(text, params.EscapeText), - MsgOptionAttachments(params.Attachments...), - MsgOptionPostMessageParameters(params), + channelID, + MsgOptionPost(), + MsgOptionCompose(options...), ) return respChannel, respTimestamp, err } // PostMessageContext sends a message to a channel with a custom context -// For more details, see PostMessage documentation -func (api *Client) PostMessageContext(ctx context.Context, channel, text string, params PostMessageParameters) (string, string, error) { +// For more details, see PostMessage documentation. +func (api *Client) PostMessageContext(ctx context.Context, channelID string, options ...MsgOption) (string, string, error) { respChannel, respTimestamp, _, err := api.SendMessageContext( ctx, - channel, - MsgOptionText(text, params.EscapeText), - MsgOptionAttachments(params.Attachments...), - MsgOptionPostMessageParameters(params), + channelID, + MsgOptionPost(), + MsgOptionCompose(options...), ) return respChannel, respTimestamp, err } @@ -135,18 +133,23 @@ func (api *Client) PostEphemeral(channelID, userID string, options ...MsgOption) // PostEphemeralContext sends an ephemeal message to a user in a channel with a custom context // For more details, see PostEphemeral documentation func (api *Client) PostEphemeralContext(ctx context.Context, channelID, userID string, options ...MsgOption) (timestamp string, err error) { - _, timestamp, _, err = api.SendMessageContext(ctx, channelID, append(options, MsgOptionPostEphemeral2(userID))...) + _, timestamp, _, err = api.SendMessageContext(ctx, channelID, MsgOptionPostEphemeral(userID), MsgOptionCompose(options...)) return timestamp, err } // UpdateMessage updates a message in a channel -func (api *Client) UpdateMessage(channelID, timestamp, text string) (string, string, string, error) { - return api.UpdateMessageContext(context.Background(), channelID, timestamp, text) +func (api *Client) UpdateMessage(channelID, timestamp string, options ...MsgOption) (string, string, string, error) { + return api.SendMessageContext(context.Background(), channelID, MsgOptionUpdate(timestamp), MsgOptionCompose(options...)) } // UpdateMessageContext updates a message in a channel -func (api *Client) UpdateMessageContext(ctx context.Context, channelID, timestamp, text string) (string, string, string, error) { - return api.SendMessageContext(ctx, channelID, MsgOptionUpdate(timestamp), MsgOptionText(text, true)) +func (api *Client) UpdateMessageContext(ctx context.Context, channelID, timestamp string, options ...MsgOption) (string, string, string, error) { + return api.SendMessageContext(ctx, channelID, MsgOptionUpdate(timestamp), MsgOptionCompose(options...)) +} + +// UnfurlMessage unfurls a message in a channel +func (api *Client) UnfurlMessage(channelID, timestamp string, unfurls map[string]Attachment, options ...MsgOption) (string, string, string, error) { + return api.SendMessageContext(context.Background(), channelID, MsgOptionUnfurl(timestamp, unfurls), MsgOptionCompose(options...)) } // SendMessage more flexible method for configuring messages. @@ -165,7 +168,7 @@ func (api *Client) SendMessageContext(ctx context.Context, channelID string, opt return "", "", "", err } - if err = postForm(ctx, api.httpclient, config.endpoint, config.values, &response, api.debug); err != nil { + if err = postForm(ctx, api.httpclient, config.endpoint, config.values, &response, api); err != nil { return "", "", "", err } @@ -182,7 +185,7 @@ func UnsafeApplyMsgOptions(token, channel string, options ...MsgOption) (string, func applyMsgOptions(token, channel string, options ...MsgOption) (sendConfig, error) { config := sendConfig{ - endpoint: SLACK_API + string(chatPostMessage), + endpoint: APIURL + string(chatPostMessage), values: url.Values{ "token": {token}, "channel": {channel}, @@ -206,6 +209,7 @@ const ( chatDelete sendMode = "chat.delete" chatPostEphemeral sendMode = "chat.postEphemeral" chatMeMessage sendMode = "chat.meMessage" + chatUnfurl sendMode = "chat.unfurl" ) type sendConfig struct { @@ -219,26 +223,16 @@ type MsgOption func(*sendConfig) error // MsgOptionPost posts a messages, this is the default. func MsgOptionPost() MsgOption { return func(config *sendConfig) error { - config.endpoint = SLACK_API + string(chatPostMessage) - config.values.Del("ts") - return nil - } -} - -// MsgOptionPostEphemeral - DEPRECATED: use MsgOptionPostEphemeral2 -// posts an ephemeral message. -func MsgOptionPostEphemeral() MsgOption { - return func(config *sendConfig) error { - config.endpoint = SLACK_API + string(chatPostEphemeral) + config.endpoint = APIURL + string(chatPostMessage) config.values.Del("ts") return nil } } -// MsgOptionPostEphemeral2 - posts an ephemeral message to the provided user. -func MsgOptionPostEphemeral2(userID string) MsgOption { +// MsgOptionPostEphemeral - posts an ephemeral message to the provided user. +func MsgOptionPostEphemeral(userID string) MsgOption { return func(config *sendConfig) error { - config.endpoint = SLACK_API + string(chatPostEphemeral) + config.endpoint = APIURL + string(chatPostEphemeral) MsgOptionUser(userID)(config) config.values.Del("ts") @@ -249,7 +243,7 @@ func MsgOptionPostEphemeral2(userID string) MsgOption { // MsgOptionMeMessage posts a "me message" type from the calling user func MsgOptionMeMessage() MsgOption { return func(config *sendConfig) error { - config.endpoint = SLACK_API + string(chatMeMessage) + config.endpoint = APIURL + string(chatMeMessage) return nil } } @@ -257,7 +251,7 @@ func MsgOptionMeMessage() MsgOption { // MsgOptionUpdate updates a message based on the timestamp. func MsgOptionUpdate(timestamp string) MsgOption { return func(config *sendConfig) error { - config.endpoint = SLACK_API + string(chatUpdate) + config.endpoint = APIURL + string(chatUpdate) config.values.Add("ts", timestamp) return nil } @@ -266,12 +260,25 @@ func MsgOptionUpdate(timestamp string) MsgOption { // MsgOptionDelete deletes a message based on the timestamp. func MsgOptionDelete(timestamp string) MsgOption { return func(config *sendConfig) error { - config.endpoint = SLACK_API + string(chatDelete) + config.endpoint = APIURL + string(chatDelete) config.values.Add("ts", timestamp) return nil } } +// MsgOptionUnfurl unfurls a message based on the timestamp. +func MsgOptionUnfurl(timestamp string, unfurls map[string]Attachment) MsgOption { + return func(config *sendConfig) error { + config.endpoint = APIURL + string(chatUnfurl) + config.values.Add("ts", timestamp) + unfurlsStr, err := json.Marshal(unfurls) + if err == nil { + config.values.Add("unfurls", string(unfurlsStr)) + } + return err + } +} + // MsgOptionAsUser whether or not to send the message as the user. func MsgOptionAsUser(b bool) MsgOption { return func(config *sendConfig) error { @@ -290,6 +297,14 @@ func MsgOptionUser(userID string) MsgOption { } } +// MsgOptionUsername set the username for the message. +func MsgOptionUsername(username string) MsgOption { + return func(config *sendConfig) error { + config.values.Set("username", username) + return nil + } +} + // MsgOptionText provide the text for the message, optionally escape the provided // text. func MsgOptionText(text string, escape bool) MsgOption { @@ -365,7 +380,7 @@ func MsgOptionBroadcast() MsgOption { } } -// this function combines multiple options into a single option. +// MsgOptionCompose combines multiple options into a single option. func MsgOptionCompose(options ...MsgOption) MsgOption { return func(c *sendConfig) error { for _, opt := range options { @@ -377,6 +392,7 @@ func MsgOptionCompose(options ...MsgOption) MsgOption { } } +// MsgOptionParse set parse option. func MsgOptionParse(b bool) MsgOption { return func(c *sendConfig) error { var v string @@ -456,3 +472,38 @@ func MsgOptionPostMessageParameters(params PostMessageParameters) MsgOption { return nil } } + +// PermalinkParameters are the parameters required to get a permalink to a +// message. Slack documentation can be found here: +// https://api.slack.com/methods/chat.getPermalink +type PermalinkParameters struct { + Channel string + Ts string +} + +// GetPermalink returns the permalink for a message. It takes +// PermalinkParameters and returns a string containing the permalink. It +// returns an error if unable to retrieve the permalink. +func (api *Client) GetPermalink(params *PermalinkParameters) (string, error) { + return api.GetPermalinkContext(context.Background(), params) +} + +// GetPermalinkContext returns the permalink for a message using a custom context. +func (api *Client) GetPermalinkContext(ctx context.Context, params *PermalinkParameters) (string, error) { + values := url.Values{ + "token": {api.token}, + "channel": {params.Channel}, + "message_ts": {params.Ts}, + } + + response := struct { + Channel string `json:"channel"` + Permalink string `json:"permalink"` + SlackResponse + }{} + err := getSlackMethod(ctx, api.httpclient, "chat.getPermalink", values, &response, api) + if err != nil { + return "", err + } + return response.Permalink, response.Err() +} |