Skip to content

Commit

Permalink
refactor: create handler package
Browse files Browse the repository at this point in the history
  • Loading branch information
claustra01 committed Mar 30, 2024
1 parent 2690823 commit 68c226f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 42 deletions.
53 changes: 53 additions & 0 deletions bot/handler/callback.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
44 changes: 2 additions & 42 deletions bot/server.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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.
Expand Down

0 comments on commit 68c226f

Please sign in to comment.