-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigest.go
155 lines (134 loc) · 4.39 KB
/
digest.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"bytes"
"fmt"
"log"
"regexp"
"strings"
"time"
"crypto/tls"
"gopkg.in/gomail.v2"
)
import "github.com/slack-go/slack"
var re = regexp.MustCompile(`(?m)<@(\w+)>`)
const initialNumBufSize = 24
func DigestTitle() string {
loc, _ := time.LoadLocation("America/Los_Angeles")
t := time.Now()
t = t.In(loc)
return fmt.Sprintf("Apache Iceberg Daily Slack Digest (%s)", t.Format("2006-01-02"))
}
func RunDailyDigest(c *Config) string {
if c.From == "" || c.To == "" {
fmt.Println("Some config is missing. Please double check `FROM/TO`.")
return "Some config is missing. Please double check `FROM/TO`."
}
// Initialize slack api
api := slack.New(c.SlackBotUserToken, slack.OptionDebug(true))
// Fetch user list
userList, err := Users(api)
if err != nil {
fmt.Println("Failed to fetch the user list: " + err.Error())
return "Failed to fetch the user list: " + err.Error()
}
// Fetch channels
pm := &slack.GetConversationsParameters{
ExcludeArchived: true,
Limit: 1000,
}
channels, _, err := api.GetConversations(pm)
if err != nil {
fmt.Println("Failed to fetch the channels list: " + err.Error())
return "Failed to fetch the channels list: " + err.Error()
}
// Construct conversation history html content
buffer := bytes.NewBuffer(make([]byte, 0, initialNumBufSize))
for _, channel := range channels {
if channel.Name == "daily-digest" {
continue
}
ch := &slack.GetConversationHistoryParameters{
ChannelID: channel.ID,
Oldest: fmt.Sprintf("%f", float64(time.Now().Add(-24 * time.Hour).Unix())),
Latest: fmt.Sprintf("%f", float64(time.Now().Unix())),
Limit: 10000,
}
history, err := api.GetConversationHistory(ch)
if err != nil {
log.Println("Failed to get conversation history: ", channel.Name)
}
if len(history.Messages) > 0 {
buffer.WriteString(fmt.Sprintf("<h3><u>#%s</u></h3>", channel.Name))
buffer.WriteString("<br>")
for i := len(history.Messages) -1; i >=0; i-- {
m := history.Messages[i]
buffer.WriteString(fmt.Sprintf("<strong>%s: </strong>", userList[fmt.Sprintf("<@%s>", m.User)]))
buffer.WriteString(ReplaceMentionUser(userList, m.Text))
buffer.WriteString("<br>")
repliesParam := &slack.GetConversationRepliesParameters{
ChannelID: channel.ID,
Timestamp: m.Timestamp,
Limit: 1000,
}
replies, _, _, err := api.GetConversationReplies(repliesParam)
if err != nil {
log.Println("Failed to get conversation replies: ", channel.Name)
}
for j := 1; j < len(replies); j++ {
r := replies[j].Msg
buffer.WriteString(fmt.Sprintf("  <strong>%s: </strong>", userList[fmt.Sprintf("<@%s>", r.User)]))
buffer.WriteString(ReplaceMentionUser(userList, r.Text))
buffer.WriteString("<br>")
}
}
}
}
htmlContent := string(buffer.Bytes())
if len(htmlContent) <= 0 {
fmt.Println("Not sending a mail because the content size is zero")
return "Not sending a mail because the content size is zero"
}
if strings.ToUpper(c.MailClientType) == "GMAIL" {
return SendEmailViaGmail(c, DigestTitle(), htmlContent)
} else {
log.Println("Invalid mail client type: ", c.MailClientType)
return "Invalid mail client type: " + c.MailClientType
}
}
func Users(api *slack.Client) (map[string]string, error) {
users, err := api.GetUsers()
if err != nil {
return nil, err
}
var userList = make(map[string]string)
for _, user := range users {
userList[fmt.Sprintf("<@%s>", user.ID)] = fmt.Sprintf("@%s", user.Name)
}
return userList, nil
}
func ReplaceMentionUser(userList map[string]string, text string) string {
var msg = text
for _, match := range re.FindAllString(msg, -1) {
msg = strings.ReplaceAll(msg, match, userList[match])
}
return msg
}
func SendEmailViaGmail(c *Config, subject string, htmlContent string) string {
d := gomail.NewDialer("smtp.gmail.com", 587, c.GmailAccount, c.GmailAppPassword)
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
// Send emails using d.
m := gomail.NewMessage()
m.SetAddressHeader("From", c.From, "Iceberg Slack Email Digest")
m.SetHeader("To", c.To)
m.SetHeader("Subject", subject)
m.SetBody("text/html", htmlContent)
err := d.DialAndSend(m)
if err != nil {
fmt.Println("Failed to send the mail via Gmail: " + err.Error())
return "Failed to send mail via Gmail: " + err.Error()
} else {
msg := fmt.Sprintf("Daily digest successfully sent with the title: `%s`\n", DigestTitle())
fmt.Println(msg)
return msg
}
}