master
Evan Chen 2022-01-04 13:01:42 +08:00
parent b17b595948
commit 8c697dc874
15 changed files with 178 additions and 35 deletions

View File

@ -13,8 +13,8 @@ import (
func ApiUpdateEmail(c *gin.Context) {
data := struct {
ID string `json:"id"`
Email string `json:"email"`
ID string `json:"id" binding:"required"`
Email string `json:"email" binding:"required,email"`
}{}
if err := c.ShouldBindJSON(&data); err != nil {
panic(err)

View File

@ -30,22 +30,21 @@ func Follow(event *linebot.Event) {
log.Debug().Str("user", uid).Msg("user unblocked.")
}
// Welcome and ask for
// Welcome and ask for profile
msg := linebot.NewTextMessage("歡迎使用台新人壽智慧聊天服務! 請您協助核對您的身分。")
template := linebot.NewCarouselTemplate(
linebot.NewCarouselColumn(
"https://imgs.gotrip.hk/wp-content/uploads/2017/08/brown-innocent_19545495235b69335e17060.jpg",
"Profile A", "A 很帥 A 很棒",
linebot.NewPostbackAction("選擇", "select=a", "我是A", ""),
),
linebot.NewCarouselColumn(
"https://imgs.gotrip.hk/wp-content/uploads/2017/08/brown-innocent_19545495235b69335e17060.jpg",
"Profile B", "B 很帥 B 很棒",
linebot.NewPostbackAction("選擇", "select=b", "我是B", ""),
),
)
profiles := linebot.NewTemplateMessage("請使用手機讀取", template)
// get profiles
var profs []Profile
DB.Limit(10).Find(&profs)
columns := make([]*linebot.CarouselColumn, len(profs))
for i, prof := range profs {
columns[i] = linebot.NewCarouselColumn(
prof.Img, prof.Name, prof.Brief,
linebot.NewPostbackAction("我是"+prof.Name, "select="+prof.Name, "我是"+prof.Name, ""),
)
}
template := linebot.NewCarouselTemplate(columns...)
profiles := linebot.NewTemplateMessage("請核對您的身分", template)
if _, err := SendMessage(event.ReplyToken, uid, msg, profiles); err != nil {
panic(err)
@ -72,7 +71,7 @@ func ApiSelectProfile(event *linebot.Event) {
}
go func(uid string) {
time.Sleep(time.Second * 10)
time.Sleep(time.Second * 5)
if _, err := SendMessage("", uid, DefaultMsg()); err != nil {
panic(err)
}

View File

@ -45,6 +45,8 @@ func postback(event *linebot.Event) {
switch {
case strings.HasPrefix(data, "select="):
ApiSelectProfile(event)
case strings.HasPrefix(data, "claim="):
ApiPrepareClaim(event)
}
}
@ -67,6 +69,7 @@ func controller(event *linebot.Event) {
case msg.Text == "我的保單":
ApiMyOrders(event)
case strings.HasPrefix(msg.Text, "我是"):
case strings.HasPrefix(msg.Text, "我要理賠"):
case strings.HasPrefix(msg.Text, "幫我介紹"):

43
api_line_claim.go Normal file
View File

@ -0,0 +1,43 @@
package main
import (
"fmt"
"strings"
"github.com/line/line-bot-sdk-go/v7/linebot"
)
func ApiPrepareClaim(event *linebot.Event) {
data := event.Postback.Data
data = strings.TrimPrefix(data, "claim=")
prod := &Product{}
if err := DB.First(prod, "name = ?", data).Error; err != nil {
panic(err)
}
order := &Order{}
if err := DB.First(order, "product_id = ? and user_id = ?", prod.ID, event.Source.UserID); err != nil {
panic(err)
}
DB.Model(order).Update("status", OrderClaiming)
for _, cat := range prod.Categories {
if cat.Name == "旅平" {
url := "https://bot.ework.tw/claim"
button := linebot.NewButtonsTemplate("", "", "請上傳收據",
linebot.NewURIAction("開啟上傳頁面", url))
msg := linebot.NewTemplateMessage(fmt.Sprintf("請前往 %v 上傳收據", url), button)
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
panic(err)
}
return
}
}
msg := linebot.NewTextMessage("我們收到您的申請囉! 稍後將由專人致電為您服務!")
sticker := linebot.NewStickerMessage("6632", "11825397")
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg, sticker); err != nil {
panic(err)
}
}

View File

@ -6,7 +6,7 @@ func ApiListProduct(event *linebot.Event) {
leftBtn := linebot.NewMessageAction("修改資料", "修改資料")
rightBtn := linebot.NewMessageAction("我的保單", "我的保單")
template := linebot.NewConfirmTemplate("請問想辦理甚麼服務?", leftBtn, rightBtn)
msg := linebot.NewTemplateMessage("電腦版不支援此訊息, 請至手機上讀取", template)
msg := linebot.NewTemplateMessage("請問想辦理甚麼服務?", template)
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
panic(err)
}

View File

@ -3,10 +3,10 @@ package main
import "github.com/line/line-bot-sdk-go/v7/linebot"
func ApiMe(event *linebot.Event) {
leftBtn := linebot.NewMessageAction("修改資料", "修改資料")
leftBtn := linebot.NewURIAction("修改資料", "https://bot.ework.tw/profile")
rightBtn := linebot.NewMessageAction("我的保單", "我的保單")
template := linebot.NewConfirmTemplate("請問想辦理甚麼服務?", leftBtn, rightBtn)
msg := linebot.NewTemplateMessage("電腦版不支援此訊息, 請至手機上讀取", template)
msg := linebot.NewTemplateMessage("請問想辦理甚麼服務?", template)
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
panic(err)
}
@ -14,11 +14,33 @@ func ApiMe(event *linebot.Event) {
func ApiMyOrders(event *linebot.Event) {
var orders []Order
DB.Find(&orders, "user_id = ?", event.Source.UserID)
DB.Order("product_id").Find(&orders, "user_id = ?", event.Source.UserID)
if len(orders) == 0 {
msg := linebot.NewTextMessage("您目前沒有保單, 歡迎前往「我要投保」專區選取保單。")
msg := linebot.NewTextMessage("您目前沒有保單, 歡迎前往「我想投保」專區選取保單。")
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
panic(err)
}
} else {
pids := make([]uint, len(orders))
for i := range orders {
pids[i] = orders[i].ProductID
}
var products []Product
if err := DB.Preload("Categories").Order("id").Find(&products, "id in ?", pids).Error; err != nil {
panic(err)
}
columns := make([]*linebot.CarouselColumn, len(products))
for i, prod := range products {
columns[i] = linebot.NewCarouselColumn(
prod.Img, prod.Name, prod.Brief,
linebot.NewURIAction("保單詳細", "https://bot.ework.tw/product"),
linebot.NewPostbackAction("我要理賠", "claim="+prod.Name, "我要理賠", ""),
// linebot.NewPostbackAction("我是"+prof.Name, "select="+prof.Name, "我是"+prof.Name, ""),
)
}
template := linebot.NewCarouselTemplate(columns...)
msg := linebot.NewTemplateMessage("您的保單資訊", template)
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
panic(err)
}

11
assets/check.html Normal file
View File

@ -0,0 +1,11 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>核身</title>
</head>
<body>
<h1>HI</h1>
</body>
</html>

11
assets/claim.html Normal file
View File

@ -0,0 +1,11 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>上傳收據</title>
</head>
<body>
<h1>上傳收據</h1>
</body>
</html>

11
assets/product.html Normal file
View File

@ -0,0 +1,11 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>保單詳細</title>
</head>
<body>
<h1>保單細項</h1>
</body>
</html>

11
assets/profile.html Normal file
View File

@ -0,0 +1,11 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>修改基本資料</title>
</head>
<body>
<h1>基本資料</h1>
</body>
</html>

15
files.go Normal file
View File

@ -0,0 +1,15 @@
package main
import _ "embed"
//go:embed assets/check.html
var check_web []byte
//go:embed assets/profile.html
var profile_web []byte
//go:embed assets/claim.html
var claim_web []byte
//go:embed assets/product.html
var product_web []byte

13
main.go
View File

@ -70,6 +70,19 @@ func main() {
// setup route
server.GET("/", func(c *gin.Context) { OK(c, "ok") })
server.GET("/check", func(c *gin.Context) {
c.Writer.Write(check_web)
})
server.GET("/profile", func(c *gin.Context) {
c.Writer.Write(profile_web)
})
server.GET("/claim", func(c *gin.Context) {
c.Writer.Write(claim_web)
})
server.GET("/product", func(c *gin.Context) {
c.Writer.Write(product_web)
})
// setup api
api := server.Group("api")
api.POST("/email", ApiUpdateEmail)

View File

@ -29,6 +29,7 @@ type Profile struct {
Name string
Brief string
Img string
CreatedAt time.Time
UpdatedAt time.Time
@ -38,6 +39,7 @@ type Product struct {
ID uint `gorm:"primaryKey"`
Categories []Category `gorm:"many2many:product_categories;"`
Img string
Name string
Brief string
Description string
@ -50,6 +52,15 @@ type Category struct {
Name string `gorm:"primaryKey"`
}
const (
OrderReceived = "RECEIVED"
OrderPrepared = "PREPARED"
OrderClaiming = "CLAIMING"
OrderClaimAccept = "CLAIM_ACCEPT"
OrderClaimReject = "CLAIM_REJECT"
OrderClaimPaid = "CLAIM_PAID"
)
type Order struct {
ID string
@ -59,8 +70,8 @@ type Order struct {
Start time.Time
End time.Time
Attachment string
Price float32
Price float32
Status string
CreatedAt time.Time
UpdatedAt time.Time
@ -100,5 +111,6 @@ func (o *Order) BeforeCreate(tx *gorm.DB) (err error) {
if o.ID == "" {
o.ID = xid.New().String()
}
o.Status = OrderReceived
return
}

View File

@ -70,15 +70,7 @@ func middleware(c *gin.Context) {
func skip(c *gin.Context) bool {
switch filepath.Ext(c.Request.URL.Path) {
case ".css":
fallthrough
case ".woff2":
fallthrough
case ".map":
fallthrough
case ".ico":
fallthrough
case ".js":
case ".css", ".woff2", ".map", ".ico", ".js":
return true
default:
return false

View File

@ -57,5 +57,5 @@ func DefaultMsg() *linebot.TemplateMessage {
linebot.NewMessageAction("我的資料", "我的資料"),
linebot.NewMessageAction("辦理理賠", "辦理理賠"),
)
return linebot.NewTemplateMessage(NO_COMPAT, msg)
return linebot.NewTemplateMessage("有甚麼我能為你服務的嗎?", msg)
}