Skip to content

Commit 5a50289

Browse files
committed
feat(reactions): align GetReactions with Slack API response
I think the Slack API returns the channel field at the root level of the reactions.get response for message types, not inside the message object.
1 parent b4fa961 commit 5a50289

File tree

4 files changed

+37
-41
lines changed

4 files changed

+37
-41
lines changed

examples/reactions/reactions.go

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,21 @@ import (
1010

1111
func main() {
1212
debug := flag.Bool("debug", false, "Show JSON output")
13+
channelID := flag.String("channel", "", "Channel ID (required)")
1314
flag.Parse()
1415

1516
// Get token from environment variable
16-
apiToken := os.Getenv("SLACK_BOT_TOKEN")
17+
apiToken := os.Getenv("SLACK_USER_TOKEN")
1718
if apiToken == "" {
18-
fmt.Println("SLACK_BOT_TOKEN environment variable is required")
19+
fmt.Println("SLACK_USER_TOKEN environment variable is required")
1920
os.Exit(1)
2021
}
2122

2223
api := slack.New(apiToken, slack.OptionDebug(*debug))
2324

2425
var (
25-
postAsUserName string
26-
postAsUserID string
27-
postToUserName string
28-
postToUserID string
29-
postToChannelID string
26+
postAsUserName string
27+
postAsUserID string
3028
)
3129

3230
// Find the user to post as.
@@ -40,29 +38,19 @@ func main() {
4038
postAsUserName = authTest.User
4139
postAsUserID = authTest.UserID
4240

43-
// Posting to DM with self causes a conversation with slackbot.
44-
postToUserName = authTest.User
45-
postToUserID = authTest.UserID
46-
47-
// Find the channel.
48-
channel, _, _, err := api.OpenConversation(&slack.OpenConversationParameters{ChannelID: postToUserID})
49-
if err != nil {
50-
fmt.Printf("Error opening IM: %s\n", err)
51-
return
52-
}
53-
postToChannelID = channel.ID
54-
55-
fmt.Printf("Posting as %s (%s) in DM with %s (%s), channel %s\n", postAsUserName, postAsUserID, postToUserName, postToUserID, postToChannelID)
41+
fmt.Printf("Posting as %s (%s) in channel %s\n", postAsUserName, postAsUserID, *channelID)
5642

5743
// Post a message.
58-
channelID, timestamp, err := api.PostMessage(postToChannelID, slack.MsgOptionText("Is this any good?", false))
44+
_, timestamp, err := api.PostMessage(*channelID, slack.MsgOptionText("Is this any good?", false))
5945
if err != nil {
6046
fmt.Printf("Error posting message: %s\n", err)
6147
return
6248
}
6349

64-
// Grab a reference to the message.
65-
msgRef := slack.NewRefToMessage(channelID, timestamp)
50+
// // Grab a reference to the message.
51+
msgRef := slack.NewRefToMessage(*channelID, timestamp)
52+
53+
fmt.Printf("Adding reaction to message with reference %v\n", msgRef)
6654

6755
// React with :+1:
6856
if err = api.AddReaction("+1", msgRef); err != nil {
@@ -85,13 +73,19 @@ func main() {
8573
fmt.Printf("\n")
8674
fmt.Printf("%d reactions to message...\n", len(msgReactionsResp.Reactions))
8775
for _, r := range msgReactionsResp.Reactions {
88-
fmt.Printf(" %d users say %s\n", r.Count, r.Name)
76+
fmt.Printf(" %d users say %s in channel %s\n", r.Count, r.Name, msgReactionsResp.Item.Channel)
8977
}
9078

9179
// List all of the users reactions.
92-
listReactions, _, err := api.ListReactions(slack.NewListReactionsParameters())
80+
listParams := slack.NewListReactionsParameters()
81+
fmt.Printf("Listing reactions with params: User=%q, TeamID=%q, Count=%d, Page=%d, Full=%v\n",
82+
listParams.User, listParams.TeamID, listParams.Count, listParams.Page, listParams.Full)
83+
listReactions, _, err := api.ListReactions(listParams)
9384
if err != nil {
94-
fmt.Printf("Error listing reactions: %s\n", err)
85+
fmt.Printf("Error listing reactions: %v\n", err)
86+
if slackErr, ok := err.(slack.SlackErrorResponse); ok {
87+
fmt.Printf(" ResponseMetadata.Messages: %v\n", slackErr.ResponseMetadata.Messages)
88+
}
9589
return
9690
}
9791
fmt.Printf("\n")

reactions.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ func NewGetReactionsParameters() GetReactionsParameters {
3333
}
3434

3535
type getReactionsResponseFull struct {
36-
Type string
37-
M struct {
36+
Type string
37+
Channel string `json:"channel,omitempty"` // channel is at the root level for message types
38+
M struct {
3839
*Message // message structure already contains reactions
3940
} `json:"message"`
4041
F struct {
@@ -54,7 +55,7 @@ func (res getReactionsResponseFull) extractReactedItem() ReactedItem {
5455

5556
switch item.Type {
5657
case "message":
57-
item.Channel = res.M.Channel
58+
item.Channel = res.Channel
5859
item.Message = res.M.Message
5960
item.Reactions = res.M.Reactions
6061
case "file":
@@ -234,7 +235,7 @@ func (api *Client) GetReactionsContext(ctx context.Context, item ItemRef, params
234235
if item.Comment != "" {
235236
values.Set("file_comment", item.Comment)
236237
}
237-
if params.Full != DEFAULT_REACTIONS_FULL {
238+
if params.Full {
238239
values.Set("full", strconv.FormatBool(params.Full))
239240
}
240241

@@ -274,7 +275,7 @@ func (api *Client) ListReactionsContext(ctx context.Context, params ListReaction
274275
if params.Page != DEFAULT_REACTIONS_PAGE {
275276
values.Add("page", strconv.Itoa(params.Page))
276277
}
277-
if params.Full != DEFAULT_REACTIONS_FULL {
278+
if params.Full {
278279
values.Add("full", strconv.FormatBool(params.Full))
279280
}
280281

reactions_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ func TestSlack_GetReactions(t *testing.T) {
154154
},
155155
`{"ok": true,
156156
"type": "message",
157+
"channel": "ChannelID",
157158
"message": {
158-
"channel": "ChannelID",
159159
"text": "lorem ipsum dolor sit amet",
160160
"ts": "123",
161161
"user": "U2147483828",
@@ -174,10 +174,11 @@ func TestSlack_GetReactions(t *testing.T) {
174174
}}`,
175175
ReactedItem{
176176
Item: Item{
177-
Type: "message", Message: &Message{
177+
Type: "message",
178+
Channel: "ChannelID",
179+
Message: &Message{
178180
Msg: Msg{
179181
Text: "lorem ipsum dolor sit amet",
180-
Channel: "ChannelID",
181182
User: "U2147483828",
182183
Timestamp: "123",
183184
},
@@ -321,8 +322,8 @@ func TestSlack_GetReactions(t *testing.T) {
321322
if got.Message.Text != test.wantReactedItem.Message.Text {
322323
t.Errorf("%d: Got message text %#v, want %#v", i, got.Message.Text, test.wantReactedItem.Message.Text)
323324
}
324-
if got.Message.Channel != test.wantReactedItem.Message.Channel {
325-
t.Errorf("%d: Got message channel %#v, want %#v", i, got.Message.Channel, test.wantReactedItem.Message.Channel)
325+
if got.Channel != test.wantReactedItem.Channel {
326+
t.Errorf("%d: Got channel %#v, want %#v", i, got.Channel, test.wantReactedItem.Channel)
326327
}
327328
if got.Message.User != test.wantReactedItem.Message.User {
328329
t.Errorf("%d: Got message user %#v, want %#v", i, got.Message.User, test.wantReactedItem.Message.User)

slack.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ type authTestResponseFull struct {
5353
AuthTestResponse
5454
}
5555

56-
// Client for the slack api.
5756
type ParamOption func(*url.Values)
5857

58+
// Client for the slack api.
5959
type Client struct {
6060
token string
6161
appLevelToken string
@@ -145,14 +145,14 @@ func (api *Client) AuthTestContext(ctx context.Context) (response *AuthTestRespo
145145
}
146146

147147
// Debugf print a formatted debug line.
148-
func (api *Client) Debugf(format string, v ...interface{}) {
148+
func (api *Client) Debugf(format string, v ...any) {
149149
if api.debug {
150150
api.log.Output(2, fmt.Sprintf(format, v...))
151151
}
152152
}
153153

154154
// Debugln print a debug line.
155-
func (api *Client) Debugln(v ...interface{}) {
155+
func (api *Client) Debugln(v ...any) {
156156
if api.debug {
157157
api.log.Output(2, fmt.Sprintln(v...))
158158
}
@@ -164,11 +164,11 @@ func (api *Client) Debug() bool {
164164
}
165165

166166
// post to a slack web method.
167-
func (api *Client) postMethod(ctx context.Context, path string, values url.Values, intf interface{}) error {
167+
func (api *Client) postMethod(ctx context.Context, path string, values url.Values, intf any) error {
168168
return postForm(ctx, api.httpclient, api.endpoint+path, values, intf, api)
169169
}
170170

171171
// get a slack web method.
172-
func (api *Client) getMethod(ctx context.Context, path string, token string, values url.Values, intf interface{}) error {
172+
func (api *Client) getMethod(ctx context.Context, path string, token string, values url.Values, intf any) error {
173173
return getResource(ctx, api.httpclient, api.endpoint+path, token, values, intf, api)
174174
}

0 commit comments

Comments
 (0)