summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/nlopes/slack/chat.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2019-09-07 22:46:58 +0200
committerGitHub <noreply@github.com>2019-09-07 22:46:58 +0200
commita3bee01e0af3394c19360b98fd2db1b647f49299 (patch)
treeffc5778361d55d592a718354a37c9251e75fc7f6 /vendor/github.com/nlopes/slack/chat.go
parent1dc93ec4f001edd01daccbe408767d4878be25a3 (diff)
downloadmatterbridge-msglm-a3bee01e0af3394c19360b98fd2db1b647f49299.tar.gz
matterbridge-msglm-a3bee01e0af3394c19360b98fd2db1b647f49299.tar.bz2
matterbridge-msglm-a3bee01e0af3394c19360b98fd2db1b647f49299.zip
Update dependencies (#886)
Diffstat (limited to 'vendor/github.com/nlopes/slack/chat.go')
-rw-r--r--vendor/github.com/nlopes/slack/chat.go154
1 files changed, 134 insertions, 20 deletions
diff --git a/vendor/github.com/nlopes/slack/chat.go b/vendor/github.com/nlopes/slack/chat.go
index c08c1808..a480e5a7 100644
--- a/vendor/github.com/nlopes/slack/chat.go
+++ b/vendor/github.com/nlopes/slack/chat.go
@@ -3,6 +3,7 @@ package slack
import (
"context"
"encoding/json"
+ "net/http"
"net/url"
"github.com/nlopes/slack/slackutilsx"
@@ -25,7 +26,7 @@ const (
type chatResponseFull struct {
Channel string `json:"channel"`
- Timestamp string `json:"ts"` //Regualr message timestamp
+ Timestamp string `json:"ts"` //Regular message timestamp
MessageTimeStamp string `json:"message_ts"` //Ephemeral message timestamp
Text string `json:"text"`
SlackResponse
@@ -156,17 +157,18 @@ func (api *Client) SendMessage(channel string, options ...MsgOption) (string, st
}
// SendMessageContext more flexible method for configuring messages with a custom context.
-func (api *Client) SendMessageContext(ctx context.Context, channelID string, options ...MsgOption) (channel string, timestamp string, text string, err error) {
+func (api *Client) SendMessageContext(ctx context.Context, channelID string, options ...MsgOption) (_channel string, _timestamp string, _text string, err error) {
var (
- config sendConfig
+ req *http.Request
+ parser func(*chatResponseFull) responseParser
response chatResponseFull
)
- if config, err = applyMsgOptions(api.token, channelID, options...); err != nil {
+ if req, parser, err = buildSender(api.endpoint, options...).BuildRequest(api.token, channelID); err != nil {
return "", "", "", err
}
- if err = postForm(ctx, api.httpclient, config.endpoint, config.values, &response, api); err != nil {
+ if err = doPost(ctx, api.httpclient, req, parser(&response), api); err != nil {
return "", "", "", err
}
@@ -176,14 +178,15 @@ func (api *Client) SendMessageContext(ctx context.Context, channelID string, opt
// UnsafeApplyMsgOptions utility function for debugging/testing chat requests.
// NOTE: USE AT YOUR OWN RISK: No issues relating to the use of this function
// will be supported by the library.
-func UnsafeApplyMsgOptions(token, channel string, options ...MsgOption) (string, url.Values, error) {
- config, err := applyMsgOptions(token, channel, options...)
+func UnsafeApplyMsgOptions(token, channel, apiurl string, options ...MsgOption) (string, url.Values, error) {
+ config, err := applyMsgOptions(token, channel, apiurl, options...)
return config.endpoint, config.values, err
}
-func applyMsgOptions(token, channel string, options ...MsgOption) (sendConfig, error) {
+func applyMsgOptions(token, channel, apiurl string, options ...MsgOption) (sendConfig, error) {
config := sendConfig{
- endpoint: APIURL + string(chatPostMessage),
+ apiurl: apiurl,
+ endpoint: apiurl + string(chatPostMessage),
values: url.Values{
"token": {token},
"channel": {channel},
@@ -199,6 +202,13 @@ func applyMsgOptions(token, channel string, options ...MsgOption) (sendConfig, e
return config, nil
}
+func buildSender(apiurl string, options ...MsgOption) sendConfig {
+ return sendConfig{
+ apiurl: apiurl,
+ options: options,
+ }
+}
+
type sendMode string
const (
@@ -206,22 +216,77 @@ const (
chatPostMessage sendMode = "chat.postMessage"
chatDelete sendMode = "chat.delete"
chatPostEphemeral sendMode = "chat.postEphemeral"
+ chatResponse sendMode = "chat.responseURL"
chatMeMessage sendMode = "chat.meMessage"
chatUnfurl sendMode = "chat.unfurl"
)
type sendConfig struct {
+ apiurl string
+ options []MsgOption
+ mode sendMode
+ endpoint string
+ values url.Values
+ attachments []Attachment
+ responseType string
+}
+
+func (t sendConfig) BuildRequest(token, channelID string) (req *http.Request, _ func(*chatResponseFull) responseParser, err error) {
+ if t, err = applyMsgOptions(token, channelID, t.apiurl, t.options...); err != nil {
+ return nil, nil, err
+ }
+
+ switch t.mode {
+ case chatResponse:
+ return responseURLSender{
+ endpoint: t.endpoint,
+ values: t.values,
+ attachments: t.attachments,
+ responseType: t.responseType,
+ }.BuildRequest()
+ default:
+ return formSender{endpoint: t.endpoint, values: t.values}.BuildRequest()
+ }
+}
+
+type formSender struct {
endpoint string
values url.Values
}
+func (t formSender) BuildRequest() (*http.Request, func(*chatResponseFull) responseParser, error) {
+ req, err := formReq(t.endpoint, t.values)
+ return req, func(resp *chatResponseFull) responseParser {
+ return newJSONParser(resp)
+ }, err
+}
+
+type responseURLSender struct {
+ endpoint string
+ values url.Values
+ attachments []Attachment
+ responseType string
+}
+
+func (t responseURLSender) BuildRequest() (*http.Request, func(*chatResponseFull) responseParser, error) {
+ req, err := jsonReq(t.endpoint, Msg{
+ Text: t.values.Get("text"),
+ Timestamp: t.values.Get("ts"),
+ Attachments: t.attachments,
+ ResponseType: t.responseType,
+ })
+ return req, func(resp *chatResponseFull) responseParser {
+ return newContentTypeParser(resp)
+ }, err
+}
+
// MsgOption option provided when sending a message.
type MsgOption func(*sendConfig) error
// MsgOptionPost posts a messages, this is the default.
func MsgOptionPost() MsgOption {
return func(config *sendConfig) error {
- config.endpoint = APIURL + string(chatPostMessage)
+ config.endpoint = config.apiurl + string(chatPostMessage)
config.values.Del("ts")
return nil
}
@@ -230,7 +295,7 @@ func MsgOptionPost() MsgOption {
// MsgOptionPostEphemeral - posts an ephemeral message to the provided user.
func MsgOptionPostEphemeral(userID string) MsgOption {
return func(config *sendConfig) error {
- config.endpoint = APIURL + string(chatPostEphemeral)
+ config.endpoint = config.apiurl + string(chatPostEphemeral)
MsgOptionUser(userID)(config)
config.values.Del("ts")
@@ -241,7 +306,7 @@ func MsgOptionPostEphemeral(userID string) MsgOption {
// MsgOptionMeMessage posts a "me message" type from the calling user
func MsgOptionMeMessage() MsgOption {
return func(config *sendConfig) error {
- config.endpoint = APIURL + string(chatMeMessage)
+ config.endpoint = config.apiurl + string(chatMeMessage)
return nil
}
}
@@ -249,7 +314,7 @@ func MsgOptionMeMessage() MsgOption {
// MsgOptionUpdate updates a message based on the timestamp.
func MsgOptionUpdate(timestamp string) MsgOption {
return func(config *sendConfig) error {
- config.endpoint = APIURL + string(chatUpdate)
+ config.endpoint = config.apiurl + string(chatUpdate)
config.values.Add("ts", timestamp)
return nil
}
@@ -258,7 +323,7 @@ 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 = APIURL + string(chatDelete)
+ config.endpoint = config.apiurl + string(chatDelete)
config.values.Add("ts", timestamp)
return nil
}
@@ -267,7 +332,7 @@ func MsgOptionDelete(timestamp string) MsgOption {
// 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.endpoint = config.apiurl + string(chatUnfurl)
config.values.Add("ts", timestamp)
unfurlsStr, err := json.Marshal(unfurls)
if err == nil {
@@ -277,6 +342,17 @@ func MsgOptionUnfurl(timestamp string, unfurls map[string]Attachment) MsgOption
}
}
+// MsgOptionResponseURL supplies a url to use as the endpoint.
+func MsgOptionResponseURL(url string, rt string) MsgOption {
+ return func(config *sendConfig) error {
+ config.mode = chatResponse
+ config.endpoint = url
+ config.responseType = rt
+ config.values.Del("ts")
+ return nil
+ }
+}
+
// MsgOptionAsUser whether or not to send the message as the user.
func MsgOptionAsUser(b bool) MsgOption {
return func(config *sendConfig) error {
@@ -322,9 +398,31 @@ func MsgOptionAttachments(attachments ...Attachment) MsgOption {
return nil
}
- attachments, err := json.Marshal(attachments)
+ config.attachments = attachments
+
+ // FIXME: We are setting the attachments on the message twice: above for
+ // the json version, and below for the html version. The marshalled bytes
+ // we put into config.values below don't work directly in the Msg version.
+
+ attachmentBytes, err := json.Marshal(attachments)
if err == nil {
- config.values.Set("attachments", string(attachments))
+ config.values.Set("attachments", string(attachmentBytes))
+ }
+
+ return err
+ }
+}
+
+// MsgOptionBlocks sets blocks for the message
+func MsgOptionBlocks(blocks ...Block) MsgOption {
+ return func(config *sendConfig) error {
+ if blocks == nil {
+ return nil
+ }
+
+ blocks, err := json.Marshal(blocks)
+ if err == nil {
+ config.values.Set("blocks", string(blocks))
}
return err
}
@@ -395,15 +493,31 @@ func MsgOptionParse(b bool) MsgOption {
return func(c *sendConfig) error {
var v string
if b {
- v = "1"
+ v = "full"
} else {
- v = "0"
+ v = "none"
}
c.values.Set("parse", v)
return nil
}
}
+// MsgOptionIconURL sets an icon URL
+func MsgOptionIconURL(iconURL string) MsgOption {
+ return func(c *sendConfig) error {
+ c.values.Set("icon_url", iconURL)
+ return nil
+ }
+}
+
+// MsgOptionIconEmoji sets an icon emoji
+func MsgOptionIconEmoji(iconEmoji string) MsgOption {
+ return func(c *sendConfig) error {
+ c.values.Set("icon_emoji", iconEmoji)
+ return nil
+ }
+}
+
// UnsafeMsgOptionEndpoint deliver the message to the specified endpoint.
// NOTE: USE AT YOUR OWN RISK: No issues relating to the use of this Option
// will be supported by the library, it is subject to change without notice that
@@ -499,7 +613,7 @@ func (api *Client) GetPermalinkContext(ctx context.Context, params *PermalinkPar
Permalink string `json:"permalink"`
SlackResponse
}{}
- err := getSlackMethod(ctx, api.httpclient, "chat.getPermalink", values, &response, api)
+ err := api.getMethod(ctx, "chat.getPermalink", values, &response)
if err != nil {
return "", err
}