mirror of
https://github.com/seigler/slack-cleanup
synced 2025-07-27 01:36:12 +00:00
Added the ability to delete lists of files from Slack
This commit is contained in:
parent
443298cf40
commit
edf99b52c0
2 changed files with 69 additions and 16 deletions
|
@ -2,4 +2,11 @@
|
||||||
|
|
||||||
[](https://circleci.com/gh/madecomfy/slack-cleanup/tree/master)
|
[](https://circleci.com/gh/madecomfy/slack-cleanup/tree/master)
|
||||||
|
|
||||||
A program for cleaning up slack files to free up space
|
A program for cleaning up slack files to free up space.
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
You will need to generate an API token that has permission to delete files. If you do not have administrator
|
||||||
|
privileges you will only be able to delete your own files. Generate an API token at the following link:
|
||||||
|
|
||||||
|
[Legacy Tokens](https://api.slack.com/custom-integrations/legacy-tokens)
|
||||||
|
|
66
main.go
66
main.go
|
@ -1,33 +1,79 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const apiURL string = "https://slack.com/api/"
|
// Files contains the list of files we want to clean
|
||||||
|
type Files struct {
|
||||||
|
FileList []File `json:"files"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// File is an individual file we wish to remove from Slack
|
||||||
|
type File struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
token := os.Getenv("SLACK_API_TOKEN")
|
|
||||||
|
|
||||||
fmt.Println(token)
|
req, err := http.NewRequest("GET", "https://slack.com/api/files.list", nil)
|
||||||
|
|
||||||
response, err := http.Get(apiURL + "files.list?token=" + token)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
|
||||||
|
|
||||||
fmt.Println(response.Body)
|
tsNow := time.Now()
|
||||||
|
tsTo := tsNow.AddDate(0, -14, 0)
|
||||||
|
tsUnix := tsTo.Unix()
|
||||||
|
|
||||||
|
q := req.URL.Query()
|
||||||
|
q.Add("token", os.Getenv("SLACK_API_TOKEN"))
|
||||||
|
q.Add("ts_to", fmt.Sprint(tsUnix))
|
||||||
|
q.Add("count", "1000")
|
||||||
|
req.URL.RawQuery = q.Encode()
|
||||||
|
|
||||||
|
httpClient := &http.Client{}
|
||||||
|
resp, err := httpClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
content, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func listFiles() {
|
var files Files
|
||||||
|
if err := json.Unmarshal(content, &files); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteFiles() {
|
for _, file := range files.FileList {
|
||||||
|
req, err := http.NewRequest("GET", "https://slack.com/api/files.delete", nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
q := req.URL.Query()
|
||||||
|
q.Add("token", os.Getenv("SLACK_API_TOKEN"))
|
||||||
|
q.Add("file", file.ID)
|
||||||
|
req.URL.RawQuery = q.Encode()
|
||||||
|
|
||||||
|
httpClient := &http.Client{}
|
||||||
|
resp, err := httpClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
_, err = ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("File", file.ID, "successfully deleted")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue