-
Notifications
You must be signed in to change notification settings - Fork 0
/
iceberg-bot.go
125 lines (110 loc) · 3.08 KB
/
iceberg-bot.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
package main
import (
"context"
"fmt"
"github.com/go-joe/cron"
"github.com/go-joe/http-server"
"github.com/go-joe/joe"
"github.com/go-joe/slack-adapter"
"log"
"os"
"time"
)
type IcebergBot struct {
*joe.Bot
Config *Config
}
type DailyDigestEvent struct {
}
type Config struct {
SlackBotUserToken string
From string
To string
Port string
GmailAccount string
GmailAppPassword string
MailClientType string
}
func NewConfig() (*Config, error) {
config := &Config{
SlackBotUserToken: os.Getenv("SLACK_BOT_USER_TOKEN"),
From: os.Getenv("FROM"),
To: os.Getenv("TO"),
Port: os.Getenv("PORT"),
GmailAccount: os.Getenv("GMAIL_ACCOUNT"),
GmailAppPassword: os.Getenv("GMAIL_APP_PASSWORD"),
MailClientType: os.Getenv("MAIL_CLIENT_TYPE"),
}
fmt.Println(config)
if config.Port == "" {
config.Port = "80"
}
return config, nil
}
func main() {
config, err := NewConfig()
if err != nil {
log.Fatal("Failed to load config: ", err)
}
modules := []joe.Module {
joehttp.Server(":" + config.Port),
// Schedule the daily digest cron job at 2:00:00 AM (UTC)
//cron.ScheduleEvent("0 0 2 * * *", DailyDigestEvent{}),
cron.ScheduleEvent("0 * * * * *", DailyDigestEvent{}),
}
if config.SlackBotUserToken != "" {
modules = append(modules, slack.Adapter(config.SlackBotUserToken))
}
b := &IcebergBot{
Bot: joe.New("iceberg-bot", modules...),
Config: config,
}
// Register event handlers
b.Brain.RegisterHandler(b.HandleDailyDigestEvent)
b.Brain.RegisterHandler(b.HandleHTTP)
b.Respond("daily-digest", b.DailyDigest)
b.Respond("config", b.PrintConfig)
b.Respond("ping", Pong)
b.Respond("time", Time)
b.Say("daily-digest", "Iceberg archive bot is starting..")
err = b.Run()
if err != nil {
b.Logger.Fatal(err.Error())
}
}
func (b *IcebergBot) HandleDailyDigestEvent(evt DailyDigestEvent) {
responseMsg := RunDailyDigest(b.Config)
b.Say("daily-digest", responseMsg)
}
func (b *IcebergBot) DailyDigest(msg joe.Message) error {
responseMsg := RunDailyDigest(b.Config)
msg.Respond(responseMsg)
return nil
}
func (b *IcebergBot) PrintConfig(msg joe.Message) error {
fmt.Println("printconfig")
configMsg := fmt.Sprintf("From: `%s`\n", b.Config.From)
configMsg += fmt.Sprintf("To: `%s`\n", b.Config.To)
configMsg += fmt.Sprintf("SlackBotUserToken: `%s`\n", b.Config.SlackBotUserToken)
configMsg += fmt.Sprintf("MailClientType: `%s`\n", b.Config.MailClientType)
configMsg += fmt.Sprintf("GmailAccount: `%s`", b.Config.GmailAccount)
msg.Respond(configMsg)
return nil
}
func (b *IcebergBot) HandleHTTP(c context.Context, r joehttp.RequestEvent) {
if r.URL.Path == "/" {
fmt.Println("Iceberg archive bot is running..")
}
}
func Time(msg joe.Message) error {
loc, _ := time.LoadLocation("America/Los_Angeles")
t := time.Now()
timeMsg := fmt.Sprintf("Machine local time: `%s`\n", fmt.Sprint(t))
timeMsg += fmt.Sprintf("Machine local time (in PDT): `%s`", fmt.Sprint(t.In(loc)))
msg.Respond(timeMsg)
return nil
}
func Pong(msg joe.Message) error {
msg.Respond("PONG")
return nil
}