diff --git a/main.go b/main.go index 54f969c..0039769 100644 --- a/main.go +++ b/main.go @@ -30,7 +30,7 @@ func main() { lineWHandler := handler.NewLINEWebhookHandler(lineBotSvc, fs) e.Any("/line/webhook", lineWHandler.Handle) - vonageWHService := handler.NewVonageWebhook() + vonageWHService := handler.NewVonageWebhook(lineBotSvc, fs) e.Any("/vonage/webhook", vonageWHService.Handle) port := 8080 if config.Config.General.Port != "" { diff --git a/pkg/handler/vonage.go b/pkg/handler/vonage.go index dfc5c98..8415ece 100644 --- a/pkg/handler/vonage.go +++ b/pkg/handler/vonage.go @@ -1,6 +1,13 @@ package handler -import "github.com/gin-gonic/gin" +import ( + "cloud.google.com/go/firestore" + "fmt" + "github.com/a-company/yoriai-backend/pkg/model" + "github.com/a-company/yoriai-backend/pkg/service/line" + "github.com/gin-gonic/gin" + "log/slog" +) type VonageWebhookRequest struct { AgentID string `json:"agent_id"` @@ -13,10 +20,18 @@ type VonageWebhookRequest struct { } type VonageWebhook struct { + line *line.LINEBotService + fs *firestore.Client } -func NewVonageWebhook() *VonageWebhook { - return &VonageWebhook{} +func NewVonageWebhook( + svc *line.LINEBotService, + fs *firestore.Client, +) *VonageWebhook { + return &VonageWebhook{ + line: svc, + fs: fs, + } } func (v *VonageWebhook) Handle(c *gin.Context) { @@ -25,7 +40,32 @@ func (v *VonageWebhook) Handle(c *gin.Context) { c.JSON(400, gin.H{"error": err.Error()}) return } - // TODO: forward the request to LINE + + userdata := model.User{} + doc, err := v.fs.Collection("users"). + Where("phone_number", "==", req.PhoneNumber). + Documents(c).Next() + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + doc.DataTo(&userdata) + + //気分: Feeling + //今日の通話のサマリー: TodayActivity + //伝言: Message + + notifyText := "本日の通話が終了しました\n\n" + notifyText += fmt.Sprintf("気分: %s\n", req.Feeling) + notifyText += fmt.Sprintf("今日の通話のサマリー: %s\n", req.TodayActivity) + notifyText += fmt.Sprintf("伝言: %s\n", req.Message) + + slog.Info("notifyText", slog.String("notifyText", notifyText)) + if err := v.line.PushTextMessage(userdata.LINEID, notifyText); err != nil { + slog.Error("failed to push message", err) + c.JSON(500, gin.H{"error": err.Error()}) + return + } c.JSON(200, gin.H{ "message": "success", }) diff --git a/pkg/service/line/line_bot.go b/pkg/service/line/line_bot.go index f0862bb..31f19a7 100644 --- a/pkg/service/line/line_bot.go +++ b/pkg/service/line/line_bot.go @@ -35,6 +35,14 @@ func (l *LINEBotService) ReplyMessage(replyToken string, flexMessage []linebot.S } } +func (l *LINEBotService) PushTextMessage(to, message string) error { + if _, err := l.client.PushMessage(to, linebot.NewTextMessage(message)).Do(); err != nil { + slog.Error("failed to push message", err) + return err + } + return nil +} + func CreateTimeSelectMessage() *linebot.FlexMessage { bubble := &linebot.BubbleContainer{ Type: linebot.FlexContainerTypeBubble,