Skip to content

Commit 4de4114

Browse files
committed
Create main.go
1 parent cb2b9a6 commit 4de4114

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

cmd/slackMessageParser/main.go

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* Copyright (c) 2024 Michael Plunkett (https://github.com/michplunkett)
3+
* All rights reserved.
4+
* Used to parse Slack messages.
5+
*/
6+
7+
package main
8+
9+
import (
10+
"encoding/json"
11+
"fmt"
12+
"os"
13+
"path/filepath"
14+
"strconv"
15+
"strings"
16+
"time"
17+
18+
"github.com/gocarina/gocsv"
19+
)
20+
21+
type Message struct {
22+
TimeStamp string
23+
UserID string `json:"user"`
24+
TS string `json:"ts"`
25+
Type string `json:"type"`
26+
ClientMessageID string `json:"client_msg_id"`
27+
Text string `json:"text,omitempty"`
28+
UserProfile *UserProfile `json:"user_profile,omitempty"`
29+
Attachments *[]*Attachment `json:"attachments,omitempty"`
30+
Files *[]File `json:"files,omitempty"`
31+
IsUpload bool `json:"upload,omitempty"`
32+
}
33+
34+
type UserProfile struct {
35+
ProfileImage string `json:"image_72,omitempty"`
36+
FirstName string `json:"first_name,omitempty"`
37+
RealName string `json:"real_name,omitempty"`
38+
DisplayName string `json:"display_name,omitempty"`
39+
Name string `json:"name,omitempty"`
40+
}
41+
42+
type Attachment struct {
43+
Text string `json:"text"`
44+
Fallback string `json:"fallback"`
45+
FromURL string `json:"from_url"`
46+
ServiceName string `json:"service_name"`
47+
ID int `json:"id"`
48+
OriginalURL string `json:"original_url"`
49+
}
50+
51+
type File struct {
52+
ID string `json:"id"`
53+
Name string `json:"name"`
54+
Title string `json:"title"`
55+
MimeType string `json:"mimetype"`
56+
FileType string `json:"pretty_type"`
57+
IsExternal bool `json:"is_external"`
58+
IsPublic bool `json:"is_public"`
59+
DownloadLink string `json:"url_private_download"`
60+
Height int `json:"original_w"`
61+
Width int `json:"original_h"`
62+
}
63+
64+
type CSVRecord struct {
65+
TimeStamp string
66+
UserID string
67+
UserName string
68+
RealName string
69+
MessageType string
70+
Text string
71+
Attachments []string
72+
Files []string
73+
}
74+
75+
func main() {
76+
files, err := filepath.Glob("../../SlackMessages/*.json")
77+
if err != nil {
78+
fmt.Println(err)
79+
}
80+
81+
messages := make([]*Message, 0)
82+
83+
for _, file := range files {
84+
bytes, err := os.ReadFile(file)
85+
if err != nil {
86+
fmt.Printf("Error occured while opening %s: %+v\n", file, err)
87+
continue
88+
}
89+
90+
fileMessages := make([]*Message, 0)
91+
err = json.Unmarshal(bytes, &fileMessages)
92+
if err != nil {
93+
fmt.Printf("Error occured while parsing JSON from %s: %+v\n", file, err)
94+
continue
95+
}
96+
97+
messages = append(messages, fileMessages...)
98+
}
99+
100+
csvRecords := make([]*CSVRecord, 0)
101+
for _, msg := range messages {
102+
record := &CSVRecord{
103+
UserID: msg.UserID,
104+
UserName: "",
105+
RealName: "",
106+
MessageType: msg.Type,
107+
Attachments: make([]string, 0),
108+
Files: make([]string, 0),
109+
}
110+
111+
timeStampSplit := strings.Split(msg.TS, ".")
112+
seconds, err := strconv.ParseInt(timeStampSplit[0], 10, 64)
113+
if err != nil {
114+
fmt.Printf("Error occured while parsing seconds: %+v\n", err)
115+
continue
116+
}
117+
118+
nanoseconds, err := strconv.ParseInt(timeStampSplit[1], 10, 64)
119+
if err != nil {
120+
fmt.Printf("Error occured while parsing nanoseconds: %+v\n", err)
121+
continue
122+
}
123+
124+
record.TimeStamp = time.Unix(seconds, nanoseconds).Format(time.RFC3339)
125+
126+
if msg.UserProfile != nil {
127+
record.UserName = msg.UserProfile.Name
128+
record.RealName = msg.UserProfile.RealName
129+
}
130+
131+
if msg.Text != "" {
132+
record.Text = msg.Text
133+
}
134+
135+
if msg.Attachments != nil {
136+
for _, attachment := range *msg.Attachments {
137+
record.Attachments = append(record.Attachments, attachment.OriginalURL)
138+
}
139+
}
140+
141+
if msg.Files != nil {
142+
for _, file := range *msg.Files {
143+
record.Files = append(record.Files, file.DownloadLink)
144+
}
145+
}
146+
147+
csvRecords = append(csvRecords, record)
148+
}
149+
150+
csvFile, err := os.Create("../../slack_records.csv")
151+
if err != nil {
152+
fmt.Println(err)
153+
}
154+
defer csvFile.Close()
155+
156+
_ = gocsv.MarshalFile(csvRecords, csvFile)
157+
}

0 commit comments

Comments
 (0)