2021-12-30 09:16:20 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-01-03 10:28:00 +00:00
|
|
|
"bytes"
|
2021-12-30 09:16:20 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-01-03 10:28:00 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2021-12-30 09:16:20 +00:00
|
|
|
|
|
|
|
"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, "", " ")
|
|
|
|
fmt.Println(string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, event := range events {
|
|
|
|
if event.Type == linebot.EventTypeFollow {
|
|
|
|
Follow(event)
|
|
|
|
}
|
2022-01-02 16:47:25 +00:00
|
|
|
if event.Type == linebot.EventTypeMessage {
|
|
|
|
controller(event)
|
|
|
|
}
|
2022-01-03 14:50:49 +00:00
|
|
|
if event.Type == linebot.EventTypePostback {
|
|
|
|
postback(event)
|
|
|
|
}
|
2021-12-30 09:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OK(c, "ok")
|
|
|
|
}
|
|
|
|
|
2022-01-03 14:50:49 +00:00
|
|
|
func postback(event *linebot.Event) {
|
|
|
|
data := event.Postback.Data
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(data, "select="):
|
|
|
|
ApiSelectProfile(event)
|
2022-01-04 05:01:42 +00:00
|
|
|
case strings.HasPrefix(data, "claim="):
|
|
|
|
ApiPrepareClaim(event)
|
2022-01-03 14:50:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 16:47:25 +00:00
|
|
|
func controller(event *linebot.Event) {
|
2022-01-03 05:06:26 +00:00
|
|
|
|
|
|
|
msg, ok := event.Message.(*linebot.TextMessage)
|
|
|
|
if !ok {
|
|
|
|
log.Error().Msg("controller not impemented")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
Hit(msg.Text, event.Source.UserID)
|
|
|
|
|
2022-01-03 10:28:00 +00:00
|
|
|
switch {
|
2022-01-03 14:50:49 +00:00
|
|
|
case msg.Text == "我想投保":
|
|
|
|
ApiListProduct(event)
|
|
|
|
case msg.Text == "辦理理賠":
|
|
|
|
ApiMyOrders(event)
|
|
|
|
case msg.Text == "我的資料":
|
|
|
|
ApiMe(event)
|
2022-01-03 10:28:00 +00:00
|
|
|
case msg.Text == "我的保單":
|
|
|
|
ApiMyOrders(event)
|
2022-01-03 14:50:49 +00:00
|
|
|
case strings.HasPrefix(msg.Text, "我是"):
|
2022-01-04 05:01:42 +00:00
|
|
|
case strings.HasPrefix(msg.Text, "我要理賠"):
|
2022-01-03 14:50:49 +00:00
|
|
|
|
2022-01-03 10:28:00 +00:00
|
|
|
case strings.HasPrefix(msg.Text, "幫我介紹"):
|
2021-12-30 13:39:00 +00:00
|
|
|
|
2022-01-02 16:47:25 +00:00
|
|
|
default:
|
|
|
|
// route to watson
|
2022-01-03 10:28:00 +00:00
|
|
|
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,
|
2021-12-30 09:16:20 +00:00
|
|
|
}
|
2022-01-03 10:28:00 +00:00
|
|
|
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
|
|
|
|
}
|
2022-01-03 14:50:49 +00:00
|
|
|
log.Debug().RawJSON("watson", data).Msg("")
|
2022-01-03 10:28:00 +00:00
|
|
|
resData := struct {
|
|
|
|
Output struct {
|
|
|
|
Generic []struct {
|
|
|
|
Text string `json:"text"`
|
|
|
|
} `json:"generic"`
|
|
|
|
} `json:"output"`
|
|
|
|
}{}
|
|
|
|
json.Unmarshal(data, &resData)
|
|
|
|
|
2022-01-03 14:50:49 +00:00
|
|
|
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
|
|
|
|
}
|
2022-01-03 10:28:00 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 14:50:49 +00:00
|
|
|
_, err = SendMessage("", event.Source.UserID,
|
|
|
|
DefaultMsg())
|
2022-01-03 10:28:00 +00:00
|
|
|
return err
|
2021-12-30 09:16:20 +00:00
|
|
|
}
|