| 1 | package mainscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack/examples/pins`/ |
| 2 | |
| 3 | import ( |
| 4 | "flagscip-go gomod github.com/golang/go/src go1.16 flag/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/" |
| 6 | |
| 7 | "github.com/slack-go/slackscip-go gomod github.com/slack-go/slack . `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 mainscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack/examples/pins`/main().() { |
| 14 | var ( |
| 15 | apiToken string |
| 16 | debug bool |
| 17 | ) |
| 18 | |
| 19 | flagscip-go gomod github.com/golang/go/src go1.16 flag/.StringVarscip-go gomod github.com/golang/go/src go1.16 flag/StringVar().(&apiToken, "token", "YOUR_TOKEN_HERE", "Your Slack API Token") |
| 20 | flagscip-go gomod github.com/golang/go/src go1.16 flag/.BoolVarscip-go gomod github.com/golang/go/src go1.16 flag/BoolVar().(&debug, "debug", false, "Show JSON output") |
| 21 | flagscip-go gomod github.com/golang/go/src go1.16 flag/.Parsescip-go gomod github.com/golang/go/src go1.16 flag/Parse().() |
| 22 | |
| 23 | api := slackscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/.Newscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/New().(apiToken, slackscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/.OptionDebugscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/OptionDebug().(debug)) |
| 24 | |
| 25 | var ( |
| 26 | postAsUserName string |
| 27 | postAsUserID string |
| 28 | postToChannelID string |
| 29 | channels []slackscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/.Channelscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/Channel# |
| 30 | ) |
| 31 | |
| 32 | // Find the user to post as. |
| 33 | authTest, err := api.AuthTestscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/Client#AuthTest().() |
| 34 | if err != nil { |
| 35 | fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Printfscip-go gomod github.com/golang/go/src go1.16 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.Userscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/AuthTestResponse#User. |
| 43 | postAsUserID = authTest.UserIDscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/AuthTestResponse#UserID. |
| 44 | |
| 45 | // Create a temporary channel |
| 46 | channel, err := api.CreateConversationscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/Client#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 := &slackscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/.GetConversationsParametersscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/GetConversationsParameters#{ExcludeArchivedscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/GetConversationsParameters#ExcludeArchived.: false} |
| 53 | if channels, _, err = api.GetConversationsscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/Client#GetConversations().(params); err != nil { |
| 54 | fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.16 fmt/Println().("Could not retrieve channels") |
| 55 | return |
| 56 | } |
| 57 | for _, archivedChannel := range channels { |
| 58 | if archivedChannel.Namescip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/GroupConversation#Name. == channelName { |
| 59 | if archivedChannel.IsArchivedscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/GroupConversation#IsArchived. { |
| 60 | err = api.UnArchiveConversationscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/Client#UnArchiveConversation().(archivedChannel.IDscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/Conversation#ID.) |
| 61 | if err != nil { |
| 62 | fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Printfscip-go gomod github.com/golang/go/src go1.16 fmt/Printf().("Could not unarchive %s: %s\n", archivedChannel.IDscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/Conversation#ID., err) |
| 63 | return |
| 64 | } |
| 65 | } |
| 66 | channel = &archivedChannel |
| 67 | break |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | if err != nil { |
| 72 | fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Printfscip-go gomod github.com/golang/go/src go1.16 fmt/Printf().("Error setting test channel for pinning: %s\n", err) |
| 73 | return |
| 74 | } |
| 75 | } |
| 76 | postToChannelID = channel.IDscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/Conversation#ID. |
| 77 | |
| 78 | fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Printfscip-go gomod github.com/golang/go/src go1.16 fmt/Printf().("Posting as %s (%s) in channel %s\n", postAsUserName, postAsUserID, postToChannelID) |
| 79 | |
| 80 | // Post a message. |
| 81 | channelID, timestamp, err := api.PostMessagescip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/Client#PostMessage().(postToChannelID, slackscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/.MsgOptionTextscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/MsgOptionText().("Is this any good?", false)) |
| 82 | if err != nil { |
| 83 | fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Printfscip-go gomod github.com/golang/go/src go1.16 fmt/Printf().("Error posting message: %s\n", err) |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | // Grab a reference to the message. |
| 88 | msgRef := slackscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/.NewRefToMessagescip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/NewRefToMessage().(channelID, timestamp) |
| 89 | |
| 90 | // Add message pin to channel |
| 91 | if err = api.AddPinscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/Client#AddPin().(channelID, msgRef); err != nil { |
| 92 | fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Printfscip-go gomod github.com/golang/go/src go1.16 fmt/Printf().("Error adding pin: %s\n", err) |
| 93 | return |
| 94 | } |
| 95 | |
| 96 | // List all of the users pins. |
| 97 | listPins, _, err := api.ListPinsscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/Client#ListPins().(channelID) |
| 98 | if err != nil { |
| 99 | fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Printfscip-go gomod github.com/golang/go/src go1.16 fmt/Printf().("Error listing pins: %s\n", err) |
| 100 | return |
| 101 | } |
| 102 | fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Printfscip-go gomod github.com/golang/go/src go1.16 fmt/Printf().("\n") |
| 103 | fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Printfscip-go gomod github.com/golang/go/src go1.16 fmt/Printf().("All pins by %s...\n", authTest.Userscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/AuthTestResponse#User.) |
| 104 | for _, item := range listPins { |
| 105 | fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Printfscip-go gomod github.com/golang/go/src go1.16 fmt/Printf().(" > Item type: %s\n", item.Typescip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/Item#Type.) |
| 106 | } |
| 107 | |
| 108 | // Remove the pin. |
| 109 | err = api.RemovePinscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/Client#RemovePin().(channelID, msgRef) |
| 110 | if err != nil { |
| 111 | fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Printfscip-go gomod github.com/golang/go/src go1.16 fmt/Printf().("Error remove pin: %s\n", err) |
| 112 | return |
| 113 | } |
| 114 | |
| 115 | if err = api.UnArchiveConversationscip-go gomod github.com/slack-go/slack . `github.com/slack-go/slack`/Client#UnArchiveConversation().(channelID); err != nil { |
| 116 | fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Printfscip-go gomod github.com/golang/go/src go1.16 fmt/Printf().("Error archiving channel: %s\n", err) |
| 117 | return |
| 118 | } |
| 119 | |
| 120 | } |