example of working with reactions
Ryan Carver
8 years ago
0 | package main | |
1 | ||
2 | import ( | |
3 | "flag" | |
4 | "fmt" | |
5 | ||
6 | "github.com/nlopes/slack" | |
7 | ) | |
8 | ||
9 | func main() { | |
10 | var ( | |
11 | apiToken string | |
12 | debug bool | |
13 | ) | |
14 | ||
15 | flag.StringVar(&apiToken, "token", "YOUR_TOKEN_HERE", "Your Slack API Token") | |
16 | flag.BoolVar(&debug, "debug", false, "Show JSON output") | |
17 | flag.Parse() | |
18 | ||
19 | api := slack.New(apiToken) | |
20 | if debug { | |
21 | api.SetDebug(true) | |
22 | } | |
23 | ||
24 | var ( | |
25 | postAsUserName string | |
26 | postAsUserID string | |
27 | postToUserName string | |
28 | postToUserID string | |
29 | postToChannelID string | |
30 | ) | |
31 | ||
32 | // Find the user to post as. | |
33 | authTest, err := api.AuthTest() | |
34 | if err != nil { | |
35 | fmt.Printf("Error getting channels: %s\n", err) | |
36 | return | |
37 | } | |
38 | ||
39 | // Post as the authenticated user. | |
40 | postAsUserName = authTest.User | |
41 | postAsUserID = authTest.UserId | |
42 | ||
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 | _, _, chanID, err := api.OpenIMChannel(postToUserID) | |
49 | if err != nil { | |
50 | fmt.Printf("Error opening IM: %s\n", err) | |
51 | return | |
52 | } | |
53 | postToChannelID = chanID | |
54 | ||
55 | fmt.Printf("Posting as %s (%s) in DM with %s (%s), channel %s\n", postAsUserName, postAsUserID, postToUserName, postToUserID, postToChannelID) | |
56 | ||
57 | // Post a message. | |
58 | postParams := slack.PostMessageParameters{} | |
59 | channelID, timestamp, err := api.PostMessage(postToChannelID, "Is this any good?", postParams) | |
60 | if err != nil { | |
61 | fmt.Printf("Error posting message: %s\n", err) | |
62 | return | |
63 | } | |
64 | ||
65 | // Grab a reference to the message. | |
66 | msgRef := slack.NewRefToMessage(channelID, timestamp) | |
67 | ||
68 | // React with :+1: | |
69 | reactionParams := slack.NewAddReactionParameters("+1", msgRef) | |
70 | if err := api.AddReaction(reactionParams); err != nil { | |
71 | fmt.Printf("Error adding reaction: %s\n", err) | |
72 | return | |
73 | } | |
74 | ||
75 | // React with :-1: | |
76 | reactionParams = slack.NewAddReactionParameters("cry", msgRef) | |
77 | if err := api.AddReaction(reactionParams); err != nil { | |
78 | fmt.Printf("Error adding reaction: %s\n", err) | |
79 | return | |
80 | } | |
81 | ||
82 | // Get all reactions on the message. | |
83 | getReactionsParams := slack.NewGetReactionsParameters(msgRef) | |
84 | msgReactions, err := api.GetReactions(getReactionsParams) | |
85 | if err != nil { | |
86 | fmt.Printf("Error getting reactions: %s\n", err) | |
87 | return | |
88 | } | |
89 | fmt.Printf("\n") | |
90 | fmt.Printf("%d reactions to message...\n", len(msgReactions)) | |
91 | for _, r := range msgReactions { | |
92 | fmt.Printf(" %d users say %s\n", r.Count, r.Name) | |
93 | } | |
94 | ||
95 | // List all of the users reactions. | |
96 | listReactionsParams := slack.NewListReactionsParameters(postAsUserID) | |
97 | listReactions, _, err := api.ListReactions(listReactionsParams) | |
98 | if err != nil { | |
99 | fmt.Printf("Error listing reactions: %s\n", err) | |
100 | return | |
101 | } | |
102 | fmt.Printf("\n") | |
103 | fmt.Printf("All reactions by %s...\n", authTest.User) | |
104 | for _, item := range listReactions { | |
105 | fmt.Printf("%d on a %s...\n", len(item.Reactions), item.Type) | |
106 | for _, r := range item.Reactions { | |
107 | fmt.Printf(" %s (along with %d others)\n", r.Name, r.Count-1) | |
108 | } | |
109 | } | |
110 | ||
111 | // Remove the :cry: reaction. | |
112 | removeReactionParams := slack.NewRemoveReactionParameters("cry", msgRef) | |
113 | err = api.RemoveReaction(removeReactionParams) | |
114 | if err != nil { | |
115 | fmt.Printf("Error remove reaction: %s\n", err) | |
116 | return | |
117 | } | |
118 | ||
119 | // Get all reactions on the message. | |
120 | getReactionsParams = slack.NewGetReactionsParameters(msgRef) | |
121 | msgReactions, err = api.GetReactions(getReactionsParams) | |
122 | if err != nil { | |
123 | fmt.Printf("Error getting reactions: %s\n", err) | |
124 | return | |
125 | } | |
126 | fmt.Printf("\n") | |
127 | fmt.Printf("%d reactions to message after removing cry...\n", len(msgReactions)) | |
128 | for _, r := range msgReactions { | |
129 | fmt.Printf(" %d users say %s\n", r.Count, r.Name) | |
130 | } | |
131 | } |