Debian Codegraph @

examples/pins/pins.go

raw symbols SCIP
1 package main
2
3 import (
4 "flag"
5 "fmt"
6
7 "github.com/slack-go/slack"
8 )
9
10 /*
11 WARNING: This example is destructive in the sense that it create a channel called testpinning
12 */
13 func main() {
14 var (
15 apiToken string
16 debug bool
17 )
18
19 flag.StringVar(&apiToken, "token", "YOUR_TOKEN_HERE", "Your Slack API Token")
20 flag.BoolVar(&debug, "debug", false, "Show JSON output")
21 flag.Parse()
22
23 api := slack.New(apiToken, slack.OptionDebug(debug))
24
25 var (
26 postAsUserName string
27 postAsUserID string
28 postToChannelID string
29 channels []slack.Channel
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 channelName := "testpinning"
40
41 // Post as the authenticated user.
42 postAsUserName = authTest.User
43 postAsUserID = authTest.UserID
44
45 // Create a temporary channel
46 channel, err := api.CreateConversation(channelName, false)
47
48 if err != nil {
49 // If the channel exists, that means we just need to unarchive it
50 if err.Error() == "name_taken" {
51 err = nil
52 params := &slack.GetConversationsParameters{ExcludeArchived: false}
53 if channels, _, err = api.GetConversations(params); err != nil {
54 fmt.Println("Could not retrieve channels")
55 return
56 }
57 for _, archivedChannel := range channels {
58 if archivedChannel.Name == channelName {
59 if archivedChannel.IsArchived {
60 err = api.UnArchiveConversation(archivedChannel.ID)
61 if err != nil {
62 fmt.Printf("Could not unarchive %s: %s\n", archivedChannel.ID, err)
63 return
64 }
65 }
66 channel = &archivedChannel
67 break
68 }
69 }
70 }
71 if err != nil {
72 fmt.Printf("Error setting test channel for pinning: %s\n", err)
73 return
74 }
75 }
76 postToChannelID = channel.ID
77
78 fmt.Printf("Posting as %s (%s) in channel %s\n", postAsUserName, postAsUserID, postToChannelID)
79
80 // Post a message.
81 channelID, timestamp, err := api.PostMessage(postToChannelID, slack.MsgOptionText("Is this any good?", false))
82 if err != nil {
83 fmt.Printf("Error posting message: %s\n", err)
84 return
85 }
86
87 // Grab a reference to the message.
88 msgRef := slack.NewRefToMessage(channelID, timestamp)
89
90 // Add message pin to channel
91 if err = api.AddPin(channelID, msgRef); err != nil {
92 fmt.Printf("Error adding pin: %s\n", err)
93 return
94 }
95
96 // List all of the users pins.
97 listPins, _, err := api.ListPins(channelID)
98 if err != nil {
99 fmt.Printf("Error listing pins: %s\n", err)
100 return
101 }
102 fmt.Printf("\n")
103 fmt.Printf("All pins by %s...\n", authTest.User)
104 for _, item := range listPins {
105 fmt.Printf(" > Item type: %s\n", item.Type)
106 }
107
108 // Remove the pin.
109 err = api.RemovePin(channelID, msgRef)
110 if err != nil {
111 fmt.Printf("Error remove pin: %s\n", err)
112 return
113 }
114
115 if err = api.UnArchiveConversation(channelID); err != nil {
116 fmt.Printf("Error archiving channel: %s\n", err)
117 return
118 }
119
120 }