4 | 4 |
"net/url"
|
5 | 5 |
)
|
6 | 6 |
|
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 {
|
20 | 10 |
Name string `json:"name"`
|
21 | 11 |
File string `json:"file"`
|
22 | 12 |
FileComment string `json:"file_comment"`
|
|
24 | 14 |
Timestamp string `json:"timestamp"`
|
25 | 15 |
}
|
26 | 16 |
|
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}
|
37 | 20 |
}
|
38 | 21 |
|
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{}
|
41 | 34 |
err := parseResponse(path, values, response, debug)
|
42 | 35 |
if err != nil {
|
43 | 36 |
return nil, err
|
|
49 | 42 |
}
|
50 | 43 |
|
51 | 44 |
// 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 {
|
55 | 46 |
values := url.Values{
|
56 | 47 |
"token": {api.config.token},
|
57 | 48 |
}
|
58 | |
if params.Name != DEFAULT_REACTION_NAME {
|
59 | |
values.Set("name", string(params.Name))
|
|
49 |
if reaction.Name != "" {
|
|
50 |
values.Set("name", reaction.Name)
|
60 | 51 |
}
|
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))
|
63 | 54 |
}
|
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))
|
66 | 57 |
}
|
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))
|
69 | 60 |
}
|
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))
|
72 | 63 |
}
|
73 | |
_, err := reactionRequest("reactions.add", values, api.debug)
|
|
64 |
_, err := addReactionRequest("reactions.add", values, api.debug)
|
74 | 65 |
if err != nil {
|
75 | 66 |
return err
|
76 | 67 |
}
|