This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (70 loc) · 1.63 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/nlopes/slack"
)
func must(err error, msg string) {
if err != nil {
log.Println(fmt.Errorf("%s: %v", msg, err.Error()))
os.Exit(1)
}
}
func deleteMessage(tries int, sleep time.Duration, f func() error) error {
var err error
for i := 0; ; i++ {
err = f()
if err == nil {
return nil
}
if i >= tries-1 {
break
}
time.Sleep(sleep)
}
if err != nil {
return err
}
return nil
}
func main() {
var token = flag.String("token", "", "https://api.slack.com/custom-integrations/legacy-tokens")
var channelName = flag.String("name", "", "channel name")
var latest = flag.String("latest", "", "timestamp")
flag.Parse()
api := slack.New(*token)
channels, err := api.GetChannels(true)
must(err, "channel list")
var purgeID string
for _, channel := range channels {
if channel.Name == *channelName {
purgeID = channel.ID
}
}
latestTimestamp := *latest
if latestTimestamp == "" {
latestTimestamp = strconv.FormatInt(time.Now().AddDate(0, 0, -1).Unix(), 10)
must(err, "latestTimestamp")
}
params := slack.NewHistoryParameters()
params.Latest = latestTimestamp
history, err := api.GetChannelHistory(purgeID, params)
must(err, "channel history")
for _, message := range history.Messages {
err := deleteMessage(3, 2*time.Second, func() (err error) {
for _, reply := range message.Replies {
_ = deleteMessage(3, 2*time.Second, func() (err error) {
_, _, err = api.DeleteMessage(purgeID, reply.Timestamp)
return
})
}
_, _, err = api.DeleteMessage(purgeID, message.Timestamp)
return
})
log.Print(err)
}
}