-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (57 loc) · 1.48 KB
/
main.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
package main
import (
"github.com/joho/godotenv"
"github.com/line/line-bot-sdk-go/linebot"
"github.com/robfig/cron"
"log"
"net/http"
"os"
"time"
"todoreminder/controller"
"todoreminder/job"
)
var EventListeners = map[linebot.EventType] controller.BaseController {
linebot.EventTypeMessage: controller.EventTypeMessageController{},
linebot.EventTypeFollow: controller.EventTypeFollowController{},
}
func loadEnv() {
err := godotenv.Load()
if err != nil {
log.Fatal(err.Error())
}
}
func initBot() *linebot.Client {
bot, err := linebot.New(os.Getenv("CHANNEL_SECRET"), os.Getenv("CHANNEL_ACCESS_TOKEN"))
if err != nil {
log.Fatal(err.Error())
}
return bot
}
func setListeners(bot *linebot.Client, r *http.Request) {
events, err := bot.ParseRequest(r)
if err != nil {
log.Fatal(err.Error())
}
for _, event := range events {
EventListeners[event.Type].Execute(bot, event)
}
}
func loadJobs(bot *linebot.Client, cronInstance *cron.Cron) {
job.Bot = bot
cronInstance.AddFunc("CRON_TZ=Asia/Jakarta 0 7 * * *", job.ReminderToDoListJob{}.Execute)
}
func main() {
loadEnv()
bot := initBot()
location, _ := time.LoadLocation("Asia/Jakarta")
cronInstance := cron.New(cron.WithLocation(location))
loadJobs(bot, cronInstance)
cronInstance.Start()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
setListeners(bot, r)
})
err := http.ListenAndServe(os.Getenv("APP_HOST") + ":" + os.Getenv("APP_PORT"), nil)
if err != nil {
log.Fatal(err.Error())
}
}