diff --git a/bot/handler/callback.go b/bot/handler/callback.go new file mode 100644 index 0000000..d2619fc --- /dev/null +++ b/bot/handler/callback.go @@ -0,0 +1,53 @@ +package handler + +import ( + "errors" + "log" + "net/http" + + "github.com/claustra01/calendeye/linebot" + "github.com/claustra01/calendeye/webhook" +) + +func Callback(w http.ResponseWriter, req *http.Request, bot *linebot.LineBot, channelSecret string) { + log.Println("/callback called...") + + cb, err := webhook.ParseRequest(channelSecret, req) + if err != nil { + log.Printf("Cannot parse request: %+v\n", err) + if errors.Is(err, webhook.ErrInvalidSignature) { + w.WriteHeader(http.StatusBadRequest) + } else { + w.WriteHeader(http.StatusInternalServerError) + } + return + } + + log.Println("Handling events...") + for _, event := range cb.Events { + log.Printf("/callback called%+v...\n", event) + + switch e := event.(type) { + case webhook.MessageEvent: + switch message := e.Message.(type) { + case webhook.TextMessageContent: + if _, err = bot.ReplyMessage( + &linebot.ReplyMessageRequest{ + ReplyToken: e.ReplyToken, + Messages: []linebot.MessageInterface{ + linebot.NewTextMessage(message.Text), + }, + }, + ); err != nil { + log.Print(err) + } else { + log.Println("Sent text reply.") + } + default: + log.Printf("Unsupported message content: %T\n", e.Message) + } + default: + log.Printf("Unsupported message: %T\n", event) + } + } +} diff --git a/bot/server.go b/bot/server.go index 90f08c5..ecec89b 100644 --- a/bot/server.go +++ b/bot/server.go @@ -1,13 +1,12 @@ package main import ( - "errors" "log" "net/http" "os" + "github.com/claustra01/calendeye/handler" "github.com/claustra01/calendeye/linebot" - "github.com/claustra01/calendeye/webhook" "github.com/joho/godotenv" ) @@ -32,46 +31,7 @@ func main() { // Setup HTTP Server for receiving requests from LINE platform http.HandleFunc("/callback", func(w http.ResponseWriter, req *http.Request) { - log.Println("/callback called...") - - cb, err := webhook.ParseRequest(channelSecret, req) - if err != nil { - log.Printf("Cannot parse request: %+v\n", err) - if errors.Is(err, webhook.ErrInvalidSignature) { - w.WriteHeader(http.StatusBadRequest) - } else { - w.WriteHeader(http.StatusInternalServerError) - } - return - } - - log.Println("Handling events...") - for _, event := range cb.Events { - log.Printf("/callback called%+v...\n", event) - - switch e := event.(type) { - case webhook.MessageEvent: - switch message := e.Message.(type) { - case webhook.TextMessageContent: - if _, err = bot.ReplyMessage( - &linebot.ReplyMessageRequest{ - ReplyToken: e.ReplyToken, - Messages: []linebot.MessageInterface{ - linebot.NewTextMessage(message.Text), - }, - }, - ); err != nil { - log.Print(err) - } else { - log.Println("Sent text reply.") - } - default: - log.Printf("Unsupported message content: %T\n", e.Message) - } - default: - log.Printf("Unsupported message: %T\n", event) - } - } + handler.Callback(w, req, bot, channelSecret) }) // This is just sample code.