Skip to content

Commit

Permalink
Merge pull request #19 from a-company-jp/vonage-endpoint
Browse files Browse the repository at this point in the history
✨ attach LINEBotService VonageWebhook
  • Loading branch information
Shion1305 authored Oct 12, 2024
2 parents 63274d9 + 42728e6 commit 3de8d76
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
48 changes: 44 additions & 4 deletions pkg/handler/vonage.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand All @@ -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) {
Expand All @@ -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",
})
Expand Down
8 changes: 8 additions & 0 deletions pkg/service/line/line_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3de8d76

Please sign in to comment.