-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (105 loc) · 2.72 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
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
package main
import (
"log"
"net/http"
"os"
"wolfbot/domain/model"
"wolfbot/handler"
"wolfbot/initializer"
"github.com/joho/godotenv"
"github.com/line/line-bot-sdk-go/linebot"
)
func main() {
env := os.Getenv("APP_ENV")
if env == "local" {
godotenv.Load(".env.local")
}
channelSecret := mustGetenv("CHANNEL_SECRET")
channelAccessToken := mustGetenv("CHANNEL_ACCESS_TOKEN")
port := mustGetenv("PORT")
dbURL := mustGetenv("DATABASE_URL")
_, service := initializer.Initialize(dbURL)
messageHandler := handler.NewMessageHandler(
service.VillageService,
service.PlayerService,
service.UserPlayerRelationService,
)
bot, err := linebot.New(channelSecret, channelAccessToken)
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/callback", func(w http.ResponseWriter, req *http.Request) {
events, err := bot.ParseRequest(req)
if err != nil {
if err == linebot.ErrInvalidSignature {
w.WriteHeader(400)
} else {
w.WriteHeader(500)
}
return
}
for _, event := range events {
if event.Type == linebot.EventTypeMessage {
switch message := event.Message.(type) {
case *linebot.TextMessage:
var output Replyer
var err error
switch event.Source.Type {
case linebot.EventSourceTypeGroup:
output, err = messageHandler.HandleGroupMessage(
message.Text,
model.UserID(event.Source.UserID),
event.Source.GroupID,
)
case linebot.EventSourceTypeUser:
output, err = messageHandler.HandleUserMessage(
message.Text,
model.UserID(event.Source.UserID),
)
}
if err != nil {
log.Println(err)
// 特別なエラーメッセージが設定されている場合にはそれを返して終了
if e, ok := err.(Replyer); ok {
if _, err := bot.ReplyMessage(
event.ReplyToken,
linebot.NewTextMessage(e.Reply()),
).Do(); err != nil {
log.Println(err)
}
continue
}
// 特別なエラーメッセージが設定されていない場合にはエラー発生の旨のみ通知して終了
if _, err := bot.ReplyMessage(
event.ReplyToken,
linebot.NewTextMessage("エラーが発生しました"),
).Do(); err != nil {
log.Println(err)
}
continue
}
if _, err = bot.ReplyMessage(
event.ReplyToken,
linebot.NewTextMessage(output.Reply()),
).
Do(); err != nil {
log.Println(err)
}
}
}
}
})
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}
func mustGetenv(key string) string {
val := os.Getenv(key)
if val == "" {
log.Fatal("failed to read environment variable: " + key)
}
return val
}
type Replyer interface {
Reply() string
}