From 02978ce4533249738f13fe8a0174b1bbba395122 Mon Sep 17 00:00:00 2001 From: Alexander Sheiko Date: Tue, 9 Mar 2021 13:02:21 +0200 Subject: [PATCH] Use working Slack library --- api.go | 64 ++++++++++++++++++++++++++++++++++++++++------------- go.mod | 9 ++++---- go.sum | 24 ++++++++++++++++++-- slackcat.go | 18 ++++++++------- 4 files changed, 86 insertions(+), 29 deletions(-) diff --git a/api.go b/api.go index d5cda2f..e8bcd1f 100644 --- a/api.go +++ b/api.go @@ -3,14 +3,15 @@ package main import ( "fmt" - "github.com/bluele/slack" + "github.com/slack-go/slack" ) var ( - api *slack.Slack - msgOpts = &slack.ChatPostMessageOpt{AsUser: true} + api *slack.Client + msgOpts = &slack.PostMessageParameters{AsUser: true} ) +// InitAPI for Slack func InitAPI(token string) { api = slack.New(token) res, err := api.AuthTest() @@ -20,7 +21,7 @@ func InitAPI(token string) { // Return list of all channels by name func listChannels() (names []string) { - list, err := api.ChannelsList() + list, err := getConversations("public_channel", "private_channel") failOnError(err) for _, c := range list { names = append(names, c.Name) @@ -30,7 +31,7 @@ func listChannels() (names []string) { // Return list of all groups by name func listGroups() (names []string) { - list, err := api.GroupsList() + list, err := getConversations("mpim") failOnError(err) for _, c := range list { names = append(names, c.Name) @@ -40,14 +41,14 @@ func listGroups() (names []string) { // Return list of all ims by name func listIms() (names []string) { - users, err := api.UsersList() + users, err := api.GetUsers() failOnError(err) - list, err := api.ImList() + list, err := getConversations("im") failOnError(err) for _, c := range list { for _, u := range users { - if u.Id == c.User { + if u.ID == c.User { names = append(names, u.Name) continue } @@ -58,15 +59,48 @@ func listIms() (names []string) { // Lookup Slack id for channel, group, or im by name func lookupSlackID(name string) string { - if channel, err := api.FindChannelByName(name); err == nil { - return channel.Id - } - if group, err := api.FindGroupByName(name); err == nil { - return group.Id + list, err := getConversations("public_channel", "private_channel", "mpim") + if err == nil { + for _, c := range list { + if c.Name == name { + return c.ID + } + } } - if im, err := api.FindImByName(name); err == nil { - return im.Id + users, err := api.GetUsers() + if err == nil { + list, err := getConversations("im") + if err == nil { + for _, c := range list { + for _, u := range users { + if u.ID == c.User { + return c.ID + } + } + } + } } exitErr(fmt.Errorf("No such channel, group, or im")) return "" } + +func getConversations(types ...string) (list []slack.Channel, err error) { + cursor := "" + for { + param := &slack.GetConversationsParameters{ + Cursor: cursor, + ExcludeArchived: "true", + Types: types, + } + channels, cur, err := api.GetConversations(param) + if err != nil { + return list, err + } + list = append(list, channels...) + if cur == "" { + break + } + cursor = cur + } + return list, nil +} diff --git a/go.mod b/go.mod index a392341..9b5b914 100644 --- a/go.mod +++ b/go.mod @@ -1,12 +1,13 @@ module github.com/bcicen/slackcat +go 1.16 + require ( github.com/BurntSushi/toml v0.3.0 - github.com/bluele/slack v0.0.0-20170929071952-fb5cadcf9ed2 github.com/fatih/color v1.5.0 - github.com/mattn/go-colorable v0.0.9 - github.com/mattn/go-isatty v0.0.3 + github.com/mattn/go-colorable v0.1.8 // indirect + github.com/mattn/go-isatty v0.0.12 // indirect github.com/skratchdot/open-golang v0.0.0-20160302144031-75fb7ed4208c + github.com/slack-go/slack v0.8.1 github.com/urfave/cli v1.20.0 - golang.org/x/sys v0.0.0-20171123182949-a13efeb2fd21 ) diff --git a/go.sum b/go.sum index ae6bc88..a3f0fb3 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,35 @@ github.com/BurntSushi/toml v0.3.0 h1:e1/Ivsx3Z0FVTV0NSOv/aVgbUWyQuzj7DDnFblkRvsY= github.com/BurntSushi/toml v0.3.0/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/bluele/slack v0.0.0-20170929071952-fb5cadcf9ed2 h1:AZMvNzZTAf0VWlW/4McKS9NhKMT/j/NqxfbdCzHBtpM= -github.com/bluele/slack v0.0.0-20170929071952-fb5cadcf9ed2/go.mod h1:W679Ri2W93VLD8cVpEY/zLH1ow4zhJcCyjzrKxfM3QM= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/color v1.5.0 h1:vBh+kQp8lg9XPr56u1CPrWjFXtdphMoGWVHr9/1c+A0= github.com/fatih/color v1.5.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho= +github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8= +github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/skratchdot/open-golang v0.0.0-20160302144031-75fb7ed4208c h1:fyKiXKO1/I/B6Y2U8T7WdQGWzwehOuGIrljPtt7YTTI= github.com/skratchdot/open-golang v0.0.0-20160302144031-75fb7ed4208c/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog= +github.com/slack-go/slack v0.8.1 h1:NqGXuzni8Is3EJWmsuMuBiCCPbWOlBgTKPvdlwS3Huk= +github.com/slack-go/slack v0.8.1/go.mod h1:FGqNzJBmxIsZURAxh2a8D21AnOVvvXZvGligs4npPUM= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= +golang.org/x/sys v0.0.0-20171123182949-a13efeb2fd21 h1:i8c2841iGqFgiUiEQFmDkl5qGkfTS0axK9pPblMoTEU= golang.org/x/sys v0.0.0-20171123182949-a13efeb2fd21/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/slackcat.go b/slackcat.go index b169bde..5f5a62a 100644 --- a/slackcat.go +++ b/slackcat.go @@ -8,7 +8,7 @@ import ( "strings" "time" - "github.com/bluele/slack" + "github.com/slack-go/slack" ) //Slackcat client @@ -95,16 +95,18 @@ func (sc *Slackcat) postMsg(msglines []string) { msg = strings.Replace(msg, "<", "%26lt%3B", -1) msg = strings.Replace(msg, ">", "%26gt%3B", -1) - msgOpts := &slack.ChatPostMessageOpt{AsUser: true} + msgOpts := []slack.MsgOption{slack.MsgOptionText(msg, false)} if sc.username != "" { - msgOpts.AsUser = false - msgOpts.Username = sc.username + msgOpts = append(msgOpts, slack.MsgOptionAsUser(false)) + msgOpts = append(msgOpts, slack.MsgOptionUsername(sc.username)) + } else { + msgOpts = append(msgOpts, slack.MsgOptionAsUser(true)) } if sc.iconEmoji != "" { - msgOpts.IconEmoji = sc.iconEmoji + msgOpts = append(msgOpts, slack.MsgOptionIconEmoji(sc.iconEmoji)) } - err := api.ChatPostMessage(sc.channelID, msg, msgOpts) + _, _, err := api.PostMessage(sc.channelID, msgOpts...) failOnError(err) count := strconv.Itoa(len(msglines)) output(fmt.Sprintf("posted %s message lines to %s", count, sc.channelName)) @@ -122,8 +124,8 @@ func (sc *Slackcat) postFile(filePath, fileName, fileType, fileComment string) { } start := time.Now() - _, err := api.FilesUpload(&slack.FilesUploadOpt{ - Filepath: filePath, + _, err := api.UploadFile(slack.FileUploadParameters{ + File: filePath, Filename: fileName, Filetype: fileType, Title: fileName,