Skip to content

Commit a1f3b12

Browse files
committed
Add fetch-messages subcmd
1 parent e179831 commit a1f3b12

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

subcmd/fetch_messages.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package subcmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strconv"
8+
"time"
9+
10+
"github.com/vim-jp/slacklog-generator/internal/slacklog"
11+
)
12+
13+
func FetchMessages(args []string) error {
14+
slackToken := os.Getenv("SLACK_TOKEN")
15+
if slackToken == "" {
16+
return fmt.Errorf("$SLACK_TOKEN required")
17+
}
18+
19+
if len(args) < 2 {
20+
fmt.Println("Usage: go run scripts/main.go update-messages {data-dir} {yyyy-mm-dd}")
21+
return nil
22+
}
23+
24+
dataDir := filepath.Clean(args[0])
25+
26+
loc, err := time.LoadLocation("Asia/Tokyo")
27+
if err != nil {
28+
return err
29+
}
30+
31+
date, err := time.ParseInLocation("2006-01-02", args[1], loc)
32+
if err != nil {
33+
return err
34+
}
35+
dateEndTime := date.AddDate(0, 0, 1)
36+
37+
channelsJSONPath := filepath.Join(dataDir, "channels.json")
38+
channelTable, err := slacklog.NewChannelTable(channelsJSONPath, []string{"*"})
39+
if err != nil {
40+
return err
41+
}
42+
43+
for _, channel := range channelTable.Channels {
44+
channelDir := filepath.Join(dataDir, channel.ID)
45+
46+
request := slacklog.APIRequest{
47+
SlackToken: slackToken,
48+
APIMethod: "conversations.history",
49+
ExtraParams: map[string]string{
50+
"channel": channel.ID,
51+
"oldest": strconv.FormatInt(date.Unix(), 10) + ".000000",
52+
"latest": strconv.FormatInt(dateEndTime.Unix(), 10) + ".000000",
53+
"limit": "200",
54+
},
55+
JSONKey: "messages",
56+
Reverse: true,
57+
}
58+
outFile := filepath.Join(channelDir, date.Format("2006-01-02")+".json")
59+
err := slacklog.DownloadEntitiesToFile(request, outFile)
60+
if err != nil {
61+
return err
62+
}
63+
}
64+
65+
return nil
66+
}

subcmd/subcmd.go

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func Run() error {
1313
convert-exported-logs
1414
download-emoji
1515
download-files
16+
fetch-messages
1617
generate-html
1718
update-channel-list
1819
update-user-list`)
@@ -28,6 +29,8 @@ func Run() error {
2829
return DownloadEmoji(args)
2930
case "download-files":
3031
return DownloadFiles(args)
32+
case "fetch-messages":
33+
return FetchMessages(args)
3134
case "generate-html":
3235
return GenerateHTML(args)
3336
case "update-channel-list":

0 commit comments

Comments
 (0)