From 4d711bbed194807ed7335ddfdb592fedb72e6678 Mon Sep 17 00:00:00 2001 From: Gil Raphaelli Date: Mon, 15 Feb 2021 14:17:55 -0500 Subject: [PATCH] update slack utility and instructions, fix config loading (#32) --- README.md | 9 ++++++--- cmd/slack/listchannels/listchannels.go | 22 ++++++++++++++++------ main.go | 2 +- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 02093f5..f513181 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ Once tokens have been obtained, `zat -no-server` will perform only archival duti * [Optional] Obtain Slack credentials * [Create an App](https://api.slack.com/apps?new_app=1) * Add Permissions > Scopes > Bot Token Scopes > Add An Oauth Scope granting: `channels:read`, `chat:write`, `chat:write.public` - * Save the Bot User OAuth Access Token to `slack.config.json` with content: + * Save the Bot User OAuth Access Token (under OAuth & Permissions) to `slack.config.json` with content: ```json { "token": "your-token" @@ -133,8 +133,11 @@ The slack configuration is the ID of the channel where the message should be sen ``` $ go build ./cmd/slack/listchannels $ ./listchannels -0: {GroupConversation:{Conversation:{ID:CAAAAAAAA Created:"Sat Mar 10" IsOpen:false LastRead: Latest: UnreadCount:0 UnreadCountDisplay:0 IsGroup:false IsShared:false IsIM:false IsExtShared:false IsOrgShared:false IsPendingExtShared:false IsPrivate:false IsMpIM:false Unlinked:0 NameNormalized:zat NumMembers:1 Priority:0 User:} Name:zat Creator:U99999999 IsArchived:false Members:[] Topic:{Value: Creator: LastSet:"Wed Dec 31"} Purpose:{Value:Zat Discussion Creator:U99999999 LastSet:"Sat Mar 10"}} IsChannel:true IsGeneral:false IsMember:false Locale:} -1: {GroupConversation:{Conversation:{ID:CAAAAAAAA Created:"Sat Mar 10" IsOpen:false LastRead: Latest: UnreadCount:0 UnreadCountDisplay:0 IsGroup:false IsShared:false IsIM:false IsExtShared:false IsOrgShared:false IsPendingExtShared:false IsPrivate:false IsMpIM:false Unlinked:0 NameNormalized:welcome NumMembers:1 Priority:0 User:} Name:welcome Creator:U99999999 IsArchived:false Members:[] Topic:{Value: Creator:U99999999 LastSet:"Sat Mar 10"} Purpose:{Value:This channel is for workspace-wide communication and announcements. All members are in this channel. Creator:U99999999 LastSet:"Sat Mar 10"}} IsChannel:true IsGeneral:true IsMember:false Locale:} +CAAAAAAAA general +CAAAAAAAB zat + +One method for finding private channel IDs is to open Slack in a web browser and look at `$$('.p-channel_sidebar__static_list__item')` elements. +The application's bot user will need to be invited to the private channel to post messages there. ``` `cmd/slack/chat` can assist in verifying permissions are correct. diff --git a/cmd/slack/listchannels/listchannels.go b/cmd/slack/listchannels/listchannels.go index ef2f13c..d679467 100644 --- a/cmd/slack/listchannels/listchannels.go +++ b/cmd/slack/listchannels/listchannels.go @@ -23,11 +23,21 @@ func main() { logger.Fatal("failed to create slack api client") } - channels, _, err := api.GetConversations(&slackapi.GetConversationsParameters{}) - if err != nil { - panic(err) - } - for i, channel := range channels { - fmt.Printf("%d: %+v\n", i, channel) + next := "" + for i := 0; i == 0 || next != ""; i++ { + channels, nextCursor, err := api.GetConversations( + &slackapi.GetConversationsParameters{ + Cursor: next, + ExcludeArchived: "true", + Limit: 100, + }, + ) + if err != nil { + panic(err) + } + for _, channel := range channels { + fmt.Printf("%s %s\n", channel.ID, channel.NameNormalized) + } + next = nextCursor } } diff --git a/main.go b/main.go index fa1cf15..d754b14 100644 --- a/main.go +++ b/main.go @@ -575,7 +575,7 @@ func main() { if err != nil { logger.Fatal(err) } - slackClient, _ := slack.NewClientFromEnvOrFile(logger, *cfgDir, slackapi.OptionHTTPClient(http.DefaultClient)) + slackClient, _ := slack.NewClientFromEnvOrFile(logger, path.Join(*cfgDir, cmd.SlackConfigPath), slackapi.OptionHTTPClient(http.DefaultClient)) rp := runParams{ minDuration: *minDuration, since: *since,