Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metadata Support #1083

Merged
merged 4 commits into from Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions chat.go
Expand Up @@ -64,6 +64,9 @@ type PostMessageParameters struct {
// chat.postEphemeral support
Channel string `json:"channel"`
User string `json:"user"`

// chat metadata support
MetaData SlackMetadata `json:"metadata"`
}

// NewPostMessageParameters provides an instance of PostMessageParameters with all the sane default values set
Expand Down Expand Up @@ -285,6 +288,7 @@ type sendConfig struct {
endpoint string
values url.Values
attachments []Attachment
metadata SlackMetadata
blocks Blocks
responseType string
replaceOriginal bool
Expand All @@ -306,6 +310,7 @@ func (t sendConfig) BuildRequestContext(ctx context.Context, token, channelID st
endpoint: t.endpoint,
values: t.values,
attachments: t.attachments,
metadata: t.metadata,
blocks: t.blocks,
responseType: t.responseType,
replaceOriginal: t.replaceOriginal,
Expand Down Expand Up @@ -336,6 +341,7 @@ type responseURLSender struct {
endpoint string
values url.Values
attachments []Attachment
metadata SlackMetadata
blocks Blocks
responseType string
replaceOriginal bool
Expand All @@ -352,6 +358,7 @@ func (t responseURLSender) BuildRequestContext(ctx context.Context) (*http.Reque
Timestamp: t.values.Get("ts"),
Attachments: t.attachments,
Blocks: t.blocks,
Metadata: t.metadata,
ResponseType: t.responseType,
ReplaceOriginal: t.replaceOriginal,
DeleteOriginal: t.deleteOriginal,
Expand Down Expand Up @@ -662,6 +669,18 @@ func MsgOptionIconEmoji(iconEmoji string) MsgOption {
}
}

// MsgOptionMetadata sets message metadata
func MsgOptionMetadata(metadata SlackMetadata) MsgOption {
return func(config *sendConfig) error {
config.metadata = metadata
meta, err := json.Marshal(metadata)
if err == nil {
config.values.Set("metadata", string(meta))
}
return err
}
}

// 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
Expand Down
26 changes: 26 additions & 0 deletions chat_test.go
Expand Up @@ -82,6 +82,14 @@ func TestPostMessage(t *testing.T) {
blockStr := `[{"type":"context","block_id":"context","elements":[{"type":"plain_text","text":"hello"}]}]`

tests := map[string]messageTest{
"OnlyBasicProperties": {
endpoint: "/chat.postMessage",
opt: []MsgOption{},
expected: url.Values{
"channel": []string{"CXXX"},
"token": []string{"testing-token"},
},
},
"Blocks": {
endpoint: "/chat.postMessage",
opt: []MsgOption{
Expand Down Expand Up @@ -109,6 +117,24 @@ func TestPostMessage(t *testing.T) {
"token": []string{"testing-token"},
},
},
"Metadata": {
endpoint: "/chat.postMessage",
opt: []MsgOption{
MsgOptionMetadata(
SlackMetadata{
EventType: "testing-event",
EventPayload: map[string]interface{}{
"id": 13,
"name": "testing-name",
},
}),
},
expected: url.Values{
"metadata": []string{`{"event_type":"testing-event","event_payload":{"id":13,"name":"testing-name"}}`},
"channel": []string{"CXXX"},
"token": []string{"testing-token"},
},
},
"Unfurl": {
endpoint: "/chat.unfurl",
opt: []MsgOption{
Expand Down
18 changes: 12 additions & 6 deletions conversation.go
Expand Up @@ -571,12 +571,13 @@ func (api *Client) JoinConversationContext(ctx context.Context, channelID string
}

type GetConversationHistoryParameters struct {
ChannelID string
Cursor string
Inclusive bool
Latest string
Limit int
Oldest string
ChannelID string
Cursor string
Inclusive bool
Latest string
Limit int
Oldest string
IncludeAllMetadata bool
}

type GetConversationHistoryResponse struct {
Expand Down Expand Up @@ -615,6 +616,11 @@ func (api *Client) GetConversationHistoryContext(ctx context.Context, params *Ge
if params.Oldest != "" {
values.Add("oldest", params.Oldest)
}
if params.IncludeAllMetadata {
values.Add("include_all_metadata", "1")
} else {
values.Add("include_all_metadata", "0")
}

response := GetConversationHistoryResponse{}

Expand Down
3 changes: 3 additions & 0 deletions messages.go
Expand Up @@ -129,6 +129,9 @@ type Msg struct {
ReplaceOriginal bool `json:"replace_original"`
DeleteOriginal bool `json:"delete_original"`

// metadata
Metadata SlackMetadata `json:"metadata,omitempty"`

// Block type Message
Blocks Blocks `json:"blocks,omitempty"`
// permalink
Expand Down
7 changes: 7 additions & 0 deletions metadata.go
@@ -0,0 +1,7 @@
package slack

// SlackMetadata https://api.slack.com/reference/metadata
type SlackMetadata struct {
EventType string `json:"event_type"`
EventPayload map[string]interface{} `json:"event_payload"`
}