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