Package list golang-github-nlopes-slack / 565ec8a
simplify calling all reactions methods Ryan Carver 6 years ago
3 changed file(s) with 100 addition(s) and 115 deletion(s). Raw diff Collapse all Expand all
6666 msgRef := slack.NewRefToMessage(channelID, timestamp)
6767
6868 // React with :+1:
69 reactionParams := slack.NewAddReactionParameters("+1", msgRef)
70 if err := api.AddReaction(reactionParams); err != nil {
69 if err := api.AddReaction("+1", msgRef); err != nil {
7170 fmt.Printf("Error adding reaction: %s\n", err)
7271 return
7372 }
7473
7574 // React with :-1:
76 reactionParams = slack.NewAddReactionParameters("cry", msgRef)
77 if err := api.AddReaction(reactionParams); err != nil {
75 if err := api.AddReaction("cry", msgRef); err != nil {
7876 fmt.Printf("Error adding reaction: %s\n", err)
7977 return
8078 }
8179
8280 // Get all reactions on the message.
83 getReactionsParams := slack.NewGetReactionsParameters(msgRef)
84 msgReactions, err := api.GetReactions(getReactionsParams)
81 msgReactions, err := api.GetReactions(msgRef, slack.NewGetReactionsParameters())
8582 if err != nil {
8683 fmt.Printf("Error getting reactions: %s\n", err)
8784 return
9390 }
9491
9592 // List all of the users reactions.
96 listReactionsParams := slack.NewListReactionsParameters(postAsUserID)
97 listReactions, _, err := api.ListReactions(listReactionsParams)
93 listReactions, _, err := api.ListReactions(slack.NewListReactionsParameters())
9894 if err != nil {
9995 fmt.Printf("Error listing reactions: %s\n", err)
10096 return
109105 }
110106
111107 // Remove the :cry: reaction.
112 removeReactionParams := slack.NewRemoveReactionParameters("cry", msgRef)
113 err = api.RemoveReaction(removeReactionParams)
108 err = api.RemoveReaction("cry", msgRef)
114109 if err != nil {
115110 fmt.Printf("Error remove reaction: %s\n", err)
116111 return
117112 }
118113
119114 // Get all reactions on the message.
120 getReactionsParams = slack.NewGetReactionsParameters(msgRef)
121 msgReactions, err = api.GetReactions(getReactionsParams)
115 msgReactions, err = api.GetReactions(msgRef, slack.NewGetReactionsParameters())
122116 if err != nil {
123117 fmt.Printf("Error getting reactions: %s\n", err)
124118 return
2222 Reactions []ItemReaction
2323 }
2424
25 // AddReactionParameters is the inputs to create a new reaction.
26 type AddReactionParameters struct {
27 Name string
28 ItemRef
29 }
30
31 // NewAddReactionParameters initialies the inputs to react to an item.
32 func NewAddReactionParameters(name string, ref ItemRef) AddReactionParameters {
33 return AddReactionParameters{Name: name, ItemRef: ref}
34 }
35
36 // RemoveReactionParameters is the inputs to remove an existing reaction.
37 type RemoveReactionParameters struct {
38 Name string
39 ItemRef
40 }
41
42 // NewAddReactionParameters initialies the inputs to react to an item.
43 func NewRemoveReactionParameters(name string, ref ItemRef) RemoveReactionParameters {
44 return RemoveReactionParameters{Name: name, ItemRef: ref}
45 }
46
4725 // GetReactionsParameters is the inputs to get reactions to an item.
4826 type GetReactionsParameters struct {
4927 Full bool
50 ItemRef
5128 }
5229
5330 // NewGetReactionsParameters initializes the inputs to get reactions to an item.
54 func NewGetReactionsParameters(ref ItemRef) GetReactionsParameters {
55 return GetReactionsParameters{ItemRef: ref}
31 func NewGetReactionsParameters() GetReactionsParameters {
32 return GetReactionsParameters{
33 Full: false,
34 }
5635 }
5736
5837 type getReactionsResponseFull struct {
9069
9170 // ListReactionsParameters is the inputs to find all reactions by a user.
9271 type ListReactionsParameters struct {
93 User string
94 Count int
95 Page int
96 Full bool
72 UserId string
73 Count int
74 Page int
75 Full bool
9776 }
9877
9978 // NewListReactionsParameters initializes the inputs to find all reactions
10079 // performed by a user.
101 func NewListReactionsParameters(userID string) ListReactionsParameters {
80 func NewListReactionsParameters() ListReactionsParameters {
10281 return ListReactionsParameters{
103 User: userID,
104 Count: DEFAULT_REACTIONS_COUNT,
105 Page: DEFAULT_REACTIONS_PAGE,
106 Full: DEFAULT_REACTIONS_FULL,
82 UserId: DEFAULT_REACTIONS_USERID,
83 Count: DEFAULT_REACTIONS_COUNT,
84 Page: DEFAULT_REACTIONS_PAGE,
85 Full: DEFAULT_REACTIONS_FULL,
10786 }
10887 }
10988
150129 }
151130
152131 // AddReaction adds a reaction emoji to a message, file or file comment.
153 func (api *Slack) AddReaction(params AddReactionParameters) error {
154 values := url.Values{
155 "token": {api.config.token},
156 }
157 if params.Name != "" {
158 values.Set("name", params.Name)
159 }
160 if params.ChannelId != "" {
161 values.Set("channel", string(params.ChannelId))
162 }
163 if params.Timestamp != "" {
164 values.Set("timestamp", string(params.Timestamp))
165 }
166 if params.FileId != "" {
167 values.Set("file", string(params.FileId))
168 }
169 if params.FileCommentId != "" {
170 values.Set("file_comment", string(params.FileCommentId))
132 func (api *Slack) AddReaction(name string, item ItemRef) error {
133 values := url.Values{
134 "token": {api.config.token},
135 }
136 if name != "" {
137 values.Set("name", name)
138 }
139 if item.ChannelId != "" {
140 values.Set("channel", string(item.ChannelId))
141 }
142 if item.Timestamp != "" {
143 values.Set("timestamp", string(item.Timestamp))
144 }
145 if item.FileId != "" {
146 values.Set("file", string(item.FileId))
147 }
148 if item.FileCommentId != "" {
149 values.Set("file_comment", string(item.FileCommentId))
171150 }
172151 response := &SlackResponse{}
173152 if err := parseResponse("reactions.add", values, response, api.debug); err != nil {
180159 }
181160
182161 // RemoveReaction removes a reaction emoji from a message, file or file comment.
183 func (api *Slack) RemoveReaction(params RemoveReactionParameters) error {
184 values := url.Values{
185 "token": {api.config.token},
186 }
187 if params.Name != "" {
188 values.Set("name", params.Name)
189 }
190 if params.ChannelId != "" {
191 values.Set("channel", string(params.ChannelId))
192 }
193 if params.Timestamp != "" {
194 values.Set("timestamp", string(params.Timestamp))
195 }
196 if params.FileId != "" {
197 values.Set("file", string(params.FileId))
198 }
199 if params.FileCommentId != "" {
200 values.Set("file_comment", string(params.FileCommentId))
162 func (api *Slack) RemoveReaction(name string, item ItemRef) error {
163 values := url.Values{
164 "token": {api.config.token},
165 }
166 if name != "" {
167 values.Set("name", name)
168 }
169 if item.ChannelId != "" {
170 values.Set("channel", string(item.ChannelId))
171 }
172 if item.Timestamp != "" {
173 values.Set("timestamp", string(item.Timestamp))
174 }
175 if item.FileId != "" {
176 values.Set("file", string(item.FileId))
177 }
178 if item.FileCommentId != "" {
179 values.Set("file_comment", string(item.FileCommentId))
201180 }
202181 response := &SlackResponse{}
203182 if err := parseResponse("reactions.remove", values, response, api.debug); err != nil {
210189 }
211190
212191 // GetReactions returns details about the reactions on an item.
213 func (api *Slack) GetReactions(params GetReactionsParameters) ([]ItemReaction, error) {
214 values := url.Values{
215 "token": {api.config.token},
216 }
217 if params.ChannelId != "" {
218 values.Set("channel", string(params.ChannelId))
219 }
220 if params.Timestamp != "" {
221 values.Set("timestamp", string(params.Timestamp))
222 }
223 if params.FileId != "" {
224 values.Set("file", string(params.FileId))
225 }
226 if params.FileCommentId != "" {
227 values.Set("file_comment", string(params.FileCommentId))
192 func (api *Slack) GetReactions(item ItemRef, params GetReactionsParameters) ([]ItemReaction, error) {
193 values := url.Values{
194 "token": {api.config.token},
195 }
196 if item.ChannelId != "" {
197 values.Set("channel", string(item.ChannelId))
198 }
199 if item.Timestamp != "" {
200 values.Set("timestamp", string(item.Timestamp))
201 }
202 if item.FileId != "" {
203 values.Set("file", string(item.FileId))
204 }
205 if item.FileCommentId != "" {
206 values.Set("file_comment", string(item.FileCommentId))
228207 }
229208 if params.Full != DEFAULT_REACTIONS_FULL {
230209 values.Set("full", fmt.Sprintf("%t", params.Full))
244223 values := url.Values{
245224 "token": {api.config.token},
246225 }
247 if params.User != DEFAULT_REACTIONS_USERID {
248 values.Add("user", params.User)
226 if params.UserId != DEFAULT_REACTIONS_USERID {
227 values.Add("user", params.UserId)
249228 }
250229 if params.Count != DEFAULT_REACTIONS_COUNT {
251230 values.Add("count", fmt.Sprintf("%d", params.Count))
4343 SLACK_API = "http://" + serverAddr + "/"
4444 api := New("testing-token")
4545 tests := []struct {
46 params AddReactionParameters
46 name string
47 ref ItemRef
4748 wantParams map[string]string
4849 }{
4950 {
50 NewAddReactionParameters("thumbsup", NewRefToMessage("ChannelID", "123")),
51 "thumbsup",
52 NewRefToMessage("ChannelID", "123"),
5153 map[string]string{
5254 "name": "thumbsup",
5355 "channel": "ChannelID",
5557 },
5658 },
5759 {
58 NewAddReactionParameters("thumbsup", NewRefToFile("FileID")),
60 "thumbsup",
61 NewRefToFile("FileID"),
5962 map[string]string{
6063 "name": "thumbsup",
6164 "file": "FileID",
6265 },
6366 },
6467 {
65 NewAddReactionParameters("thumbsup", NewRefToFileComment("FileCommentID")),
68 "thumbsup",
69 NewRefToFileComment("FileCommentID"),
6670 map[string]string{
6771 "name": "thumbsup",
6872 "file_comment": "FileCommentID",
7377 http.HandleFunc("/reactions.add", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
7478 for i, test := range tests {
7579 rh = newReactionsHandler()
76 err := api.AddReaction(test.params)
80 err := api.AddReaction(test.name, test.ref)
7781 if err != nil {
7882 t.Fatalf("%d: Unexpected error: %s", i, err)
7983 }
8892 SLACK_API = "http://" + serverAddr + "/"
8993 api := New("testing-token")
9094 tests := []struct {
91 params RemoveReactionParameters
95 name string
96 ref ItemRef
9297 wantParams map[string]string
9398 }{
9499 {
95 NewRemoveReactionParameters("thumbsup", NewRefToMessage("ChannelID", "123")),
100 "thumbsup",
101 NewRefToMessage("ChannelID", "123"),
96102 map[string]string{
97103 "name": "thumbsup",
98104 "channel": "ChannelID",
100106 },
101107 },
102108 {
103 NewRemoveReactionParameters("thumbsup", NewRefToFile("FileID")),
109 "thumbsup",
110 NewRefToFile("FileID"),
104111 map[string]string{
105112 "name": "thumbsup",
106113 "file": "FileID",
107114 },
108115 },
109116 {
110 NewRemoveReactionParameters("thumbsup", NewRefToFileComment("FileCommentID")),
117 "thumbsup",
118 NewRefToFileComment("FileCommentID"),
111119 map[string]string{
112120 "name": "thumbsup",
113121 "file_comment": "FileCommentID",
118126 http.HandleFunc("/reactions.remove", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
119127 for i, test := range tests {
120128 rh = newReactionsHandler()
121 err := api.RemoveReaction(test.params)
129 err := api.RemoveReaction(test.name, test.ref)
122130 if err != nil {
123131 t.Fatalf("%d: Unexpected error: %s", i, err)
124132 }
133141 SLACK_API = "http://" + serverAddr + "/"
134142 api := New("testing-token")
135143 tests := []struct {
144 ref ItemRef
136145 params GetReactionsParameters
137146 wantParams map[string]string
138147 json string
139148 wantReactions []ItemReaction
140149 }{
141150 {
142
143 GetReactionsParameters{ItemRef: NewRefToMessage("ChannelID", "123")},
151 NewRefToMessage("ChannelID", "123"),
152 GetReactionsParameters{},
144153 map[string]string{
145154 "channel": "ChannelID",
146155 "timestamp": "123",
167176 },
168177 },
169178 {
170 GetReactionsParameters{ItemRef: NewRefToFile("FileID"), Full: true},
179 NewRefToFile("FileID"),
180 GetReactionsParameters{Full: true},
171181 map[string]string{
172182 "file": "FileID",
173183 "full": "true",
195205 },
196206 {
197207
198 GetReactionsParameters{ItemRef: NewRefToFileComment("FileCommentID")},
208 NewRefToFileComment("FileCommentID"),
209 GetReactionsParameters{},
199210 map[string]string{
200211 "file_comment": "FileCommentID",
201212 },
227238 for i, test := range tests {
228239 rh = newReactionsHandler()
229240 rh.response = test.json
230 got, err := api.GetReactions(test.params)
241 got, err := api.GetReactions(test.ref, test.params)
231242 if err != nil {
232243 t.Fatalf("%d: Unexpected error: %s", i, err)
233244 }
330341 "page": "2",
331342 "full": "true",
332343 }
333 params := NewListReactionsParameters("UserID")
344 params := NewListReactionsParameters()
345 params.UserId = "UserID"
334346 params.Count = 200
335347 params.Page = 2
336348 params.Full = true