149 lines
3.3 KiB
Go
149 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/line/line-bot-sdk-go/v7/linebot"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func webhook(c *gin.Context) {
|
|
events, err := bot.ParseRequest(c.Request)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if flagDev {
|
|
data, _ := json.MarshalIndent(events, "", " ")
|
|
go fmt.Println(string(data))
|
|
}
|
|
|
|
for _, event := range events {
|
|
if event.Type == linebot.EventTypeFollow {
|
|
Follow(event)
|
|
}
|
|
if event.Type == linebot.EventTypeMessage {
|
|
controller(event)
|
|
}
|
|
if event.Type == linebot.EventTypePostback {
|
|
postback(event)
|
|
}
|
|
}
|
|
|
|
OK(c, "ok")
|
|
}
|
|
|
|
func postback(event *linebot.Event) {
|
|
data := event.Postback.Data
|
|
Hit(data, event.Source.UserID)
|
|
switch {
|
|
case strings.HasPrefix(data, "select="):
|
|
ApiSelectProfile(event)
|
|
case strings.HasPrefix(data, "claim="):
|
|
ApiPrepareClaim(event)
|
|
case strings.HasPrefix(data, "cat="):
|
|
ApiListProductOfCat(event)
|
|
case strings.HasPrefix(data, "purchase="):
|
|
ApiPurchase(event)
|
|
case data == "list-tags":
|
|
ApiListCategories(event)
|
|
}
|
|
}
|
|
|
|
func controller(event *linebot.Event) {
|
|
|
|
msg, ok := event.Message.(*linebot.TextMessage)
|
|
if !ok {
|
|
log.Error().Msg("controller not impemented")
|
|
return
|
|
}
|
|
if !valid(event) {
|
|
return
|
|
}
|
|
Hit(msg.Text, event.Source.UserID)
|
|
|
|
switch {
|
|
case msg.Text == "我想投保":
|
|
ApiPreparePurchase(event)
|
|
case msg.Text == "辦理理賠":
|
|
ApiMyOrders(event)
|
|
case msg.Text == "我的資料":
|
|
ApiMe(event)
|
|
case msg.Text == "我的保單":
|
|
ApiMyOrders(event)
|
|
case msg.Text == "為我推薦":
|
|
ApiListRecommanded(event)
|
|
|
|
// discard
|
|
case strings.HasPrefix(msg.Text, "我是"):
|
|
case msg.Text == "我知道":
|
|
case msg.Text == "我要理賠":
|
|
case msg.Text == "修改完成":
|
|
case msg.Text == "我要投保":
|
|
|
|
default:
|
|
// route to watson
|
|
if err := watson(event); err != nil {
|
|
log.Error().Caller().Err(err).Msg("watson error")
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func watson(event *linebot.Event) error {
|
|
input, _ := event.Message.(*linebot.TextMessage)
|
|
payload := fmt.Sprintf(`{"input":{"text": "%v"}}`, input.Text)
|
|
|
|
url := fmt.Sprintf("https://api.jp-tok.assistant.watson.cloud.ibm.com/instances/%v/v2/assistants/%v/message?version=2022-06-14",
|
|
IBM_INSTANCE, IBM_ASSISTANT)
|
|
req, err := http.NewRequest("POST", url, bytes.NewReader([]byte(payload)))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", IBM_API)
|
|
client := &http.Client{
|
|
Timeout: time.Second * 10,
|
|
}
|
|
res, err := client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer res.Body.Close()
|
|
data, err := ioutil.ReadAll(res.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log.Debug().RawJSON("watson", data).Msg("")
|
|
resData := struct {
|
|
Output struct {
|
|
Generic []struct {
|
|
Text string `json:"text"`
|
|
} `json:"generic"`
|
|
} `json:"output"`
|
|
}{}
|
|
json.Unmarshal(data, &resData)
|
|
|
|
if len(resData.Output.Generic) > 0 &&
|
|
resData.Output.Generic[0].Text != "我想我不明白。請重新描述您的問題。" {
|
|
log.Debug().Str("message", resData.Output.Generic[0].Text).
|
|
Msg("redirect")
|
|
_, err = SendMessage(event.ReplyToken, event.Source.UserID,
|
|
linebot.NewTextMessage(resData.Output.Generic[0].Text))
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
_, err = SendMessage("", event.Source.UserID,
|
|
DefaultMsg())
|
|
return err
|
|
}
|