Skip to content

Commit

Permalink
Merge pull request #4 from a-company-jp/new-webhook-line
Browse files Browse the repository at this point in the history
LINE Webhookの追加
  • Loading branch information
Shion1305 authored Oct 11, 2024
2 parents e6fd59d + 9d4a95a commit 58cf40a
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 5 deletions.
16 changes: 15 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package main

import "github.com/gin-gonic/gin"
import (
"github.com/a-company/yoriai-backend/pkg/handler"
"github.com/a-company/yoriai-backend/pkg/service/line"
"github.com/gin-gonic/gin"
"log/slog"
)

func main() {
e := gin.Default()
Expand All @@ -10,5 +15,14 @@ func main() {
"message": "Hello World",
})
})

lineBotSvc, err := line.NewLINEBotService()
if err != nil {
slog.Error("failed to initialize line bot service", err)
return
}

lineWHandler := handler.NewLINEWebhookHandler(lineBotSvc)
e.Any("/line/webhook", lineWHandler.Handle)
e.Run(":8080")
}
6 changes: 3 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package config

import (
"fmt"
"gopkg.in/yaml.v3"
"log/slog"
"os"
)

Expand All @@ -17,11 +17,11 @@ func init() {
Config = config{}
loc := os.Getenv("ENV_LOCATION")
if loc == "" {
slog.Error("No ENV_LOCATION found")
panic("No ENV_LOCATION found")
return
}
if err := yaml.Unmarshal([]byte(loc), &Config); err != nil {
slog.Error("Failed to unmarshal yaml", err)
panic(fmt.Errorf("failed to unmarshal yaml: %w", err))
return
}
}
2 changes: 1 addition & 1 deletion pkg/config/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package config
type LineConfig struct {
ClientID string `yaml:"channel_id"`
ChannelSecret string `yaml:"channel_secret"`
ChannelToken string `yaml:"channel_access_token"`
AccessToken string `yaml:"channel_access_token"`
}
77 changes: 77 additions & 0 deletions pkg/handler/line.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package handler

import (
"github.com/a-company/yoriai-backend/pkg/config"
"github.com/a-company/yoriai-backend/pkg/service/line"
"github.com/gin-gonic/gin"
"github.com/line/line-bot-sdk-go/v8/linebot/webhook"
)

type LINEWebhookHandler struct {
lineBotSvc *line.LINEBotService
}

func NewLINEWebhookHandler(lineBotSvc *line.LINEBotService) *LINEWebhookHandler {
return &LINEWebhookHandler{
lineBotSvc: lineBotSvc,
}
}

func (l *LINEWebhookHandler) Handle(c *gin.Context) {
req, err := webhook.ParseRequest(config.Config.LineConfig.ChannelSecret, c.Request)
if err != nil {
return
}

for _, event := range req.Events {
switch event.GetType() {
case "join":
// join: When your LINE Official Account joins a group chat or multi-person chat. You can reply to this event.
case "accountLink":
// accountLink: When a user has linked their LINE account with a provider's service. You can reply to this event.
case "activated":
// activated: When a user has linked their LINE account with a provider's service. You can reply to this event.
case "beacon":
// beacon: When a user enters the range of a LINE Beacon. You can reply to this event.
case "botResumed":
// botResumed: When a LINE Official Account that was suspended is resumed. You can reply to this event.
case "botSuspended":
// botSuspended: When a LINE Official Account is suspended. You can reply to this event.
case "deactivated":
// deactivated: When a user has unlinked their LINE account from a provider's service. You can reply to this event.
case "delivery":
// delivery: When a message is successfully delivered to a user. You can't reply to this event.
case "follow":
// follow: When a user adds your LINE Official Account as a friend, or unblocks your LINE Official Account. You can reply to this event.
case "leave":
// leave: When a user deletes your LINE Official Account or your LINE Official Account leaves, from a group chat or multi-person chat.
case "memberJoined":
// memberJoined: When a user joins a group chat or multi-person chat that your LINE Official Account is a member of. You can reply to this event.
case "memberLeft":
// memberLeft: When a user leaves a group chat or multi-person chat that your LINE Official Account is a member of.
case "message":
// message: When a user sends a message. You can reply to this event.
case "module":
// module: When a user interacts with a module. You can reply to this event.
case "postback":
// postback: When a user triggers a postback action. You can reply to this event.
case "things":
// things: When a user interacts with a LINE Things-compatible device. You can reply to this event.
case "unfollow":
// unfollow: When a user blocks your LINE Official Account.
case "unsend":
// unsend: When a user unsends a message. For more information on handling this event, see Processing on receipt of unsend event.
case "videoPlayComplete":
// videoPlayComplete: When a user finishes watching a video message that has a trackingId specified sent from the LINE Official Account. You can reply to this event.
}
}
}

func (l *LINEWebhookHandler) HandleJoinEvent(event *webhook.FollowEvent) {
}

func (l *LINEWebhookHandler) HandleMessageEvent(event *webhook.MessageEvent) {
}

func (l *LINEWebhookHandler) HandleLeaveEvent(event *webhook.UnfollowEvent) {
}
30 changes: 30 additions & 0 deletions pkg/service/line/line_bot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package line

import (
"fmt"
"github.com/a-company/yoriai-backend/pkg/config"
"github.com/line/line-bot-sdk-go/v7/linebot"
"log/slog"
)

type LINEBotService struct {
client *linebot.Client
}

func NewLINEBotService() (*LINEBotService, error) {
secret := config.Config.LineConfig.ChannelSecret
tkn := config.Config.LineConfig.AccessToken
bot, err := linebot.New(secret, tkn)
if err != nil {
return nil, fmt.Errorf("failed to create line bot: %w", err)
}
return &LINEBotService{
client: bot,
}, nil
}

func (l *LINEBotService) ReplyTextMessage(replyToken, message string) {
if _, err := l.client.ReplyMessage(replyToken, linebot.NewTextMessage(message)).Do(); err != nil {
slog.Error("failed to reply message", err)
}
}

0 comments on commit 58cf40a

Please sign in to comment.