update
parent
c6038a8f0c
commit
d217229aba
60
api_line.go
60
api_line.go
|
@ -1,8 +1,13 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/line/line-bot-sdk-go/v7/linebot"
|
"github.com/line/line-bot-sdk-go/v7/linebot"
|
||||||
|
@ -41,11 +46,60 @@ func controller(event *linebot.Event) {
|
||||||
}
|
}
|
||||||
Hit(msg.Text, event.Source.UserID)
|
Hit(msg.Text, event.Source.UserID)
|
||||||
|
|
||||||
switch msg.Text {
|
switch {
|
||||||
case "":
|
case msg.Text == "我的保單":
|
||||||
|
ApiMyOrders(event)
|
||||||
|
case strings.HasPrefix(msg.Text, "幫我介紹"):
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// route to watson
|
// 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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
12
main.go
|
@ -1,7 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -21,6 +23,10 @@ var (
|
||||||
LINE_SECRET string
|
LINE_SECRET string
|
||||||
LINE_TOKEN string
|
LINE_TOKEN string
|
||||||
|
|
||||||
|
IBM_INSTANCE string
|
||||||
|
IBM_ASSISTANT string
|
||||||
|
IBM_API string
|
||||||
|
|
||||||
Version string
|
Version string
|
||||||
Build string
|
Build string
|
||||||
)
|
)
|
||||||
|
@ -37,6 +43,10 @@ func init() {
|
||||||
|
|
||||||
flag.StringVar(&LINE_SECRET, "line-secret", "cf406aa7577569ba2b211bc04f51630e", "line channel secret.")
|
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(&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() {
|
func main() {
|
||||||
|
@ -51,6 +61,8 @@ func main() {
|
||||||
migrateDB()
|
migrateDB()
|
||||||
setupLine()
|
setupLine()
|
||||||
|
|
||||||
|
IBM_API = fmt.Sprintf("Basic %v", base64.URLEncoding.EncodeToString([]byte(fmt.Sprintf("apikey:%v", IBM_API))))
|
||||||
|
|
||||||
// setup route
|
// setup route
|
||||||
server.GET("/", func(c *gin.Context) { OK(c, "ok") })
|
server.GET("/", func(c *gin.Context) { OK(c, "ok") })
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue