-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (60 loc) · 1.9 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
package main
import (
"fmt"
"strconv"
"time"
"github.com/BurntSushi/toml"
"github.com/robfig/cron/v3"
"github.com/slack-go/slack"
)
type tomlConfig struct {
SlackToken string `toml:"slack_token"`
TargetChannels map[string]targetChannel `toml:"target_channels"`
}
type targetChannel struct {
ChannelID string `toml:"channel_id"`
Timer int64 `toml:"timer"`
}
func cleanChats(slackClient *slack.Client, config tomlConfig) {
// apiCallCounter := 0
currentTime := time.Now().Unix()
for _, targetChannelConf := range config.TargetChannels {
fmt.Println(targetChannelConf.ChannelID)
fmt.Println(targetChannelConf.Timer)
var getConversasionHistoryParameters slack.GetConversationHistoryParameters
getConversasionHistoryParameters.ChannelID = targetChannelConf.ChannelID
getConversasionHistoryParameters.Latest = strconv.FormatInt(currentTime-targetChannelConf.Timer, 10)
getConversasionHistoryParameters.Cursor = ""
getConversasionHistoryParameters.Inclusive = false
getConversasionHistoryParameters.Limit = 100
getConversasionHistoryParameters.Oldest = "0"
conversationHistory, err := slackClient.GetConversationHistory(
&getConversasionHistoryParameters,
)
// apiCallCounter++
if err != nil {
fmt.Println("[ERROR] getConversationHistory failed.")
fmt.Println(err)
continue
}
for _, message := range conversationHistory.Messages {
slackClient.DeleteMessage(targetChannelConf.ChannelID, message.Timestamp)
}
}
}
func main() {
// load config
var config tomlConfig
if _, err := toml.DecodeFile("config.toml", &config); err != nil {
fmt.Println("configuration file load failed.")
return
}
slackClient := slack.New(config.SlackToken)
cronInstance := cron.New()
cronInstance.AddFunc("@every 1m", func() { cleanChats(slackClient, config) })
cronInstance.Start()
cleanChats(slackClient, config)
for {
time.Sleep(86400 * time.Second)
}
}