-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
122 lines (112 loc) · 2.68 KB
/
Copy pathmain.go
File metadata and controls
122 lines (112 loc) · 2.68 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"context"
"errors"
"fmt"
"os"
"regexp"
"strings"
"github.com/joho/godotenv"
"github.com/slack-go/slack"
"github.com/urfave/cli/v3"
)
var slackURLPattern = regexp.MustCompile(`/archives/([^/]+)/p(\d{10})(\d{6})`)
// parseMessageURL parses a Slack message URL and returns channelID and thread_ts.
func parseMessageURL(url string) (channelID, threadTS string, ok bool) {
m := slackURLPattern.FindStringSubmatch(url)
if m == nil {
return "", "", false
}
return m[1], m[2] + "." + m[3], true
}
func resolveUsers(api *slack.Client, names ...string) ([]string, error) {
ids := make([]string, 0, len(names))
namesMap := map[string]struct{}{}
for i := range names {
namesMap[strings.TrimPrefix(names[i], "@")] = struct{}{}
}
users, err := api.GetUsers()
if err != nil {
return nil, err
}
for _, u := range users {
if _, ok := namesMap[u.Name]; ok {
ids = append(ids, u.ID)
}
}
if len(ids) < len(names) {
return nil, errors.New("unable to find some users")
}
if len(ids) > len(names) {
return nil, errors.New("too many users were found")
}
return ids, nil
}
func resolveChannel(api *slack.Client, name string) (string, error) {
name = strings.TrimPrefix(name, "#")
// Accept channel IDs directly. C=public/private, G=mpim or legacy private, D=IM.
if strings.HasPrefix(name, "C") || strings.HasPrefix(name, "G") || strings.HasPrefix(name, "D") {
return name, nil
}
var cursor string
for {
params := &slack.GetConversationsParameters{
Cursor: cursor,
Limit: 1000,
ExcludeArchived: true,
Types: []string{"public_channel", "private_channel"},
}
channels, nextCursor, err := api.GetConversations(params)
if err != nil {
return "", err
}
for _, ch := range channels {
if ch.Name == name {
return ch.ID, nil
}
}
if nextCursor == "" {
break
}
cursor = nextCursor
}
return "", fmt.Errorf("channel %q not found", name)
}
func newAPI() *slack.Client {
botToken := os.Getenv("SLACK_BOT_TOKEN")
if botToken == "" {
fmt.Fprintln(os.Stderr, "SLACK_BOT_TOKEN required")
os.Exit(1)
}
appToken := os.Getenv("SLACK_APP_TOKEN")
if appToken == "" {
fmt.Fprintln(os.Stderr, "SLACK_APP_TOKEN required")
os.Exit(1)
}
return slack.New(
botToken,
slack.OptionAppLevelToken(appToken),
)
}
func main() {
_ = godotenv.Load()
cmd := &cli.Command{
Name: "slack-admin-cli",
Usage: "Slack管理用CLIツール",
Commands: []*cli.Command{
cmdAction(),
cmdListMembers(),
cmdPost(),
cmdReact(),
cmdDM(),
cmdEdit(),
cmdBot(),
cmdBackup(),
cmdEmoji(),
},
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}