Codebase list golang-github-nlopes-slack / d3abe60
simplify as we look forward to other methods Ryan Carver 8 years ago
2 changed file(s) with 41 addition(s) and 51 deletion(s). Raw diff Collapse all Expand all
44 "net/url"
55 )
66
7 const (
8 DEFAULT_REACTION_NAME = ""
9 DEFAULT_REACTION_FILE = ""
10 DEFAULT_REACTION_FILE_COMMENT = ""
11 DEFAULT_REACTION_CHANNEL = ""
12 DEFAULT_REACTION_TIMESTAMP = ""
13 )
14
15 type reactionResponseFull struct {
16 SlackResponse
17 }
18
19 type ReactionParameters struct {
7 // Reaction describes the reaction and the item reacted to. One of file,
8 // file_comment, or the combination of channel and timestamp must be specified.
9 type Reaction struct {
2010 Name string `json:"name"`
2111 File string `json:"file"`
2212 FileComment string `json:"file_comment"`
2414 Timestamp string `json:"timestamp"`
2515 }
2616
27 // NewReactioneParameters provides an instance of
28 // ReactionParameters with all of the sane default value set.
29 func NewReactionParameters() ReactionParameters {
30 return ReactionParameters{
31 Name: DEFAULT_REACTION_NAME,
32 File: DEFAULT_REACTION_FILE,
33 FileComment: DEFAULT_REACTION_FILE_COMMENT,
34 Channel: DEFAULT_REACTION_CHANNEL,
35 Timestamp: DEFAULT_REACTION_TIMESTAMP,
36 }
17 // NewMessageReaction initializes a reaction to a message.
18 func NewMessageReaction(name, channel, timestamp string) Reaction {
19 return Reaction{Channel: channel, Timestamp: timestamp}
3720 }
3821
39 func reactionRequest(path string, values url.Values, debug bool) (*reactionResponseFull, error) {
40 response := &reactionResponseFull{}
22 // NewFileReaction initializes a reaction to a file.
23 func NewFileReaction(name, file string) Reaction {
24 return Reaction{Name: name, File: file}
25 }
26
27 // NewFileCommentReaction initializes a reaction to a file comment.
28 func NewFileCommentReaction(name, fileComment string) Reaction {
29 return Reaction{Name: name, FileComment: fileComment}
30 }
31
32 func addReactionRequest(path string, values url.Values, debug bool) (*SlackResponse, error) {
33 response := &SlackResponse{}
4134 err := parseResponse(path, values, response, debug)
4235 if err != nil {
4336 return nil, err
4942 }
5043
5144 // AddReaction adds a reaction emoji to a message, file or file comment.
52 // One of file, file_comment, or the combination of channel and timestamp
53 // must be specified.
54 func (api *Slack) AddReaction(params ReactionParameters) error {
45 func (api *Slack) AddReaction(reaction Reaction) error {
5546 values := url.Values{
5647 "token": {api.config.token},
5748 }
58 if params.Name != DEFAULT_REACTION_NAME {
59 values.Set("name", string(params.Name))
49 if reaction.Name != "" {
50 values.Set("name", reaction.Name)
6051 }
61 if params.File != DEFAULT_REACTION_FILE {
62 values.Set("file", string(params.File))
52 if reaction.File != "" {
53 values.Set("file", string(reaction.File))
6354 }
64 if params.FileComment != DEFAULT_REACTION_FILE_COMMENT {
65 values.Set("file_comment", string(params.FileComment))
55 if reaction.FileComment != "" {
56 values.Set("file_comment", string(reaction.FileComment))
6657 }
67 if params.Channel != DEFAULT_REACTION_CHANNEL {
68 values.Set("channel", string(params.Channel))
58 if reaction.Channel != "" {
59 values.Set("channel", string(reaction.Channel))
6960 }
70 if params.Timestamp != DEFAULT_REACTION_TIMESTAMP {
71 values.Set("timestamp", string(params.Timestamp))
61 if reaction.Timestamp != "" {
62 values.Set("timestamp", string(reaction.Timestamp))
7263 }
73 _, err := reactionRequest("reactions.add", values, api.debug)
64 _, err := addReactionRequest("reactions.add", values, api.debug)
7465 if err != nil {
7566 return err
7667 }
66 )
77
88 var (
9 addedReaction ReactionParameters
9 addedReaction Reaction
1010 )
1111
1212 func addReactionHandler(w http.ResponseWriter, r *http.Request) {
13 w.Header().Set("Content-Type", "application/json")
1413 addedReaction.Name = r.FormValue("name")
1514 addedReaction.File = r.FormValue("file")
1615 addedReaction.FileComment = r.FormValue("file_comment")
1716 addedReaction.Channel = r.FormValue("channel")
1817 addedReaction.Timestamp = r.FormValue("timestamp")
18 w.Header().Set("Content-Type", "application/json")
1919 w.Write([]byte(`{ "ok": true }`))
2020 }
2121
2424 once.Do(startServer)
2525 SLACK_API = "http://" + serverAddr + "/"
2626 api := New("testing-token")
27 params := NewReactionParameters()
28 params.Name = "thumbsup"
29 params.File = "FileID"
30 params.FileComment = "FileCommentID"
31 params.Channel = "ChannelID"
32 params.Timestamp = "123"
33 addedReaction = ReactionParameters{}
34 err := api.AddReaction(params)
27 r := Reaction{}
28 r.File = "FileID"
29 r.FileComment = "FileCommentID"
30 r.Channel = "ChannelID"
31 r.Timestamp = "123"
32 addedReaction = Reaction{}
33 err := api.AddReaction(r)
3534 if err != nil {
3635 t.Fatalf("Unexpected error: %s", err)
3736 }
38 if !reflect.DeepEqual(params, addedReaction) {
39 t.Fatalf("Got reaction %#v, want %#v", addedReaction, params)
37 if got := addedReaction; !reflect.DeepEqual(got, r) {
38 t.Errorf("Got reaction %#v, want %#v", got, r)
4039 }
4140 }