From d8a923d69e124becd0de1def20da0a9bab802708 Mon Sep 17 00:00:00 2001 From: Shion Ichikawa Date: Sat, 12 Oct 2024 11:16:38 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=E2=9C=A8=20attach=20LINEBotService=20Vonag?= =?UTF-8?q?eWebhook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/handler/vonage.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pkg/handler/vonage.go b/pkg/handler/vonage.go index dfc5c98..61fc4f8 100644 --- a/pkg/handler/vonage.go +++ b/pkg/handler/vonage.go @@ -1,6 +1,9 @@ package handler -import "github.com/gin-gonic/gin" +import ( + "github.com/a-company/yoriai-backend/pkg/service/line" + "github.com/gin-gonic/gin" +) type VonageWebhookRequest struct { AgentID string `json:"agent_id"` @@ -13,10 +16,13 @@ type VonageWebhookRequest struct { } type VonageWebhook struct { + line *line.LINEBotService } -func NewVonageWebhook() *VonageWebhook { - return &VonageWebhook{} +func NewVonageWebhook(svc line.LINEBotService) *VonageWebhook { + return &VonageWebhook{ + line: &svc, + } } func (v *VonageWebhook) Handle(c *gin.Context) { @@ -26,6 +32,8 @@ func (v *VonageWebhook) Handle(c *gin.Context) { return } // TODO: forward the request to LINE + v.line.ReplyTextMessage(req.ConversationID, req.Message) + c.JSON(200, gin.H{ "message": "success", }) From 5ab3b0d7b9dcf802592a89905ec63559c13b34b2 Mon Sep 17 00:00:00 2001 From: Shion Ichikawa Date: Sat, 12 Oct 2024 11:20:12 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=90=9B=20give=20lineBotSvc=20in=20mai?= =?UTF-8?q?n.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 54f969c..1f2898c 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) e.Any("/vonage/webhook", vonageWHService.Handle) port := 8080 if config.Config.General.Port != "" { From ee315d8aa7a1f4823bfd07865adb6d3bc18798da Mon Sep 17 00:00:00 2001 From: Shion Ichikawa Date: Sat, 12 Oct 2024 11:23:08 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=E2=9C=A8=20PushTextMessage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/service/line/line_bot.go | 8 ++++++++ 1 file changed, 8 insertions(+) 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, From 42728e6f55466b4622167eb0574fc22e2563219a Mon Sep 17 00:00:00 2001 From: Shion Ichikawa Date: Sat, 12 Oct 2024 11:39:07 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=E2=9C=A8=20NotifyText=20generation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 2 +- pkg/handler/vonage.go | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 1f2898c..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(*lineBotSvc) + 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 61fc4f8..8415ece 100644 --- a/pkg/handler/vonage.go +++ b/pkg/handler/vonage.go @@ -1,8 +1,12 @@ package handler 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 { @@ -17,11 +21,16 @@ type VonageWebhookRequest struct { type VonageWebhook struct { line *line.LINEBotService + fs *firestore.Client } -func NewVonageWebhook(svc line.LINEBotService) *VonageWebhook { +func NewVonageWebhook( + svc *line.LINEBotService, + fs *firestore.Client, +) *VonageWebhook { return &VonageWebhook{ - line: &svc, + line: svc, + fs: fs, } } @@ -31,9 +40,32 @@ func (v *VonageWebhook) Handle(c *gin.Context) { c.JSON(400, gin.H{"error": err.Error()}) return } - // TODO: forward the request to LINE - v.line.ReplyTextMessage(req.ConversationID, req.Message) + 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", })