master
Evan Chen 2022-01-03 18:28:00 +08:00
parent c6038a8f0c
commit d217229aba
4 changed files with 93 additions and 3 deletions

View File

@ -1,8 +1,13 @@
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"
@ -41,11 +46,60 @@ func controller(event *linebot.Event) {
}
Hit(msg.Text, event.Source.UserID)
switch msg.Text {
case "":
switch {
case msg.Text == "我的保單":
ApiMyOrders(event)
case strings.HasPrefix(msg.Text, "幫我介紹"):
default:
// route to watson
log.Error().Caller().Msg("NOT IMPLEMENTED")
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
}
resData := struct {
Output struct {
Generic []struct {
Text string `json:"text"`
} `json:"generic"`
} `json:"output"`
}{}
json.Unmarshal(data, &resData)
var msg *linebot.TextMessage
if len(resData.Output.Generic) > 0 {
msg = linebot.NewTextMessage(resData.Output.Generic[0].Text)
} else {
msg = DefaultMsg()
}
_, err = SendMessage(event.ReplyToken, event.Source.UserID, msg)
return err
}

20
api_line_user.go Normal file
View File

@ -0,0 +1,20 @@
package main
import "github.com/line/line-bot-sdk-go/v7/linebot"
func ApiMyOrders(event *linebot.Event) {
var orders []Order
DB.Find(&orders, "user_id = ?", event.Source.UserID)
if len(orders) == 0 {
leftBtn := linebot.NewMessageAction("left", "left clicked")
rightBtn := linebot.NewMessageAction("right", "right clicked")
template := linebot.NewConfirmTemplate("Hello World", leftBtn, rightBtn)
msg := linebot.NewTemplateMessage("Sorry :(, please update your app.", template)
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
panic(err)
}
}
}

12
main.go
View File

@ -1,7 +1,9 @@
package main
import (
"encoding/base64"
"flag"
"fmt"
"time"
"github.com/gin-gonic/gin"
@ -21,6 +23,10 @@ var (
LINE_SECRET string
LINE_TOKEN string
IBM_INSTANCE string
IBM_ASSISTANT string
IBM_API string
Version string
Build string
)
@ -37,6 +43,10 @@ func init() {
flag.StringVar(&LINE_SECRET, "line-secret", "cf406aa7577569ba2b211bc04f51630e", "line channel secret.")
flag.StringVar(&LINE_TOKEN, "line-token", "IhP4dLM91boFQlRzHa/Iv9el2xXm2X6ByiDuWQXm6ndQL1LA+yA+O8x2OeuJkpSWlf5IE8cwkA+Mca18EjO42Q6vRm0T5cRdTLkO+42SX9HAx2GdJnhWu+S4IplOt38YktfmpodCyk6bXDBJp9YVwgdB04t89/1O/w1cDnyilFU=", "channel access token")
flag.StringVar(&IBM_INSTANCE, "ibm-instance", "d9329221-40fb-4911-ba9f-69098349b8ab", "watson instance id")
flag.StringVar(&IBM_ASSISTANT, "ibm-assistant", "13cb9242-722a-4644-ba9b-7a761ffe807e", "watson assistant id")
flag.StringVar(&IBM_API, "ibm-api", "tLHR-Fhob0UideN1HUQdQ5grtl338X_VrrT31yy8FbKB", "watson api token")
}
func main() {
@ -51,6 +61,8 @@ func main() {
migrateDB()
setupLine()
IBM_API = fmt.Sprintf("Basic %v", base64.URLEncoding.EncodeToString([]byte(fmt.Sprintf("apikey:%v", IBM_API))))
// setup route
server.GET("/", func(c *gin.Context) { OK(c, "ok") })

View File

@ -46,3 +46,7 @@ func SendMessage(replyToken string, to string, messages ...linebot.SendingMessag
return res, err
}
}
func DefaultMsg() *linebot.TextMessage {
return linebot.NewTextMessage("NO RESULT")
}