From 8c697dc874b01a09c65a5e302e7208cef4c212ba Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Tue, 4 Jan 2022 13:01:42 +0800 Subject: [PATCH] update --- api_user.go => api.go | 4 ++-- api_join.go | 29 ++++++++++++++--------------- api_line.go | 3 +++ api_line_claim.go | 43 +++++++++++++++++++++++++++++++++++++++++++ api_line_products.go | 2 +- api_line_user.go | 32 +++++++++++++++++++++++++++----- assets/check.html | 11 +++++++++++ assets/claim.html | 11 +++++++++++ assets/product.html | 11 +++++++++++ assets/profile.html | 11 +++++++++++ files.go | 15 +++++++++++++++ main.go | 13 +++++++++++++ model.go | 16 ++++++++++++++-- setup_server.go | 10 +--------- util.go | 2 +- 15 files changed, 178 insertions(+), 35 deletions(-) rename api_user.go => api.go (93%) create mode 100644 api_line_claim.go create mode 100644 assets/check.html create mode 100644 assets/claim.html create mode 100644 assets/product.html create mode 100644 assets/profile.html create mode 100644 files.go diff --git a/api_user.go b/api.go similarity index 93% rename from api_user.go rename to api.go index 996077b..1a504cd 100644 --- a/api_user.go +++ b/api.go @@ -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) diff --git a/api_join.go b/api_join.go index 3fff173..f04766b 100644 --- a/api_join.go +++ b/api_join.go @@ -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) } diff --git a/api_line.go b/api_line.go index ab09b08..73a559f 100644 --- a/api_line.go +++ b/api_line.go @@ -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, "幫我介紹"): diff --git a/api_line_claim.go b/api_line_claim.go new file mode 100644 index 0000000..df075e7 --- /dev/null +++ b/api_line_claim.go @@ -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) + } + +} diff --git a/api_line_products.go b/api_line_products.go index 71a88f7..6355322 100644 --- a/api_line_products.go +++ b/api_line_products.go @@ -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) } diff --git a/api_line_user.go b/api_line_user.go index 184ba62..ea08986 100644 --- a/api_line_user.go +++ b/api_line_user.go @@ -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) } diff --git a/assets/check.html b/assets/check.html new file mode 100644 index 0000000..9a0c012 --- /dev/null +++ b/assets/check.html @@ -0,0 +1,11 @@ + + + + + + 核身 + + +

HI

+ + \ No newline at end of file diff --git a/assets/claim.html b/assets/claim.html new file mode 100644 index 0000000..89f2e52 --- /dev/null +++ b/assets/claim.html @@ -0,0 +1,11 @@ + + + + + + 上傳收據 + + +

上傳收據

+ + \ No newline at end of file diff --git a/assets/product.html b/assets/product.html new file mode 100644 index 0000000..a327440 --- /dev/null +++ b/assets/product.html @@ -0,0 +1,11 @@ + + + + + + 保單詳細 + + +

保單細項

+ + \ No newline at end of file diff --git a/assets/profile.html b/assets/profile.html new file mode 100644 index 0000000..29e012e --- /dev/null +++ b/assets/profile.html @@ -0,0 +1,11 @@ + + + + + + 修改基本資料 + + +

基本資料

+ + \ No newline at end of file diff --git a/files.go b/files.go new file mode 100644 index 0000000..087be10 --- /dev/null +++ b/files.go @@ -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 diff --git a/main.go b/main.go index 9f54f6a..c856999 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/model.go b/model.go index 394569f..aa59c6f 100644 --- a/model.go +++ b/model.go @@ -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 } diff --git a/setup_server.go b/setup_server.go index ff05da6..77d1a47 100644 --- a/setup_server.go +++ b/setup_server.go @@ -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 diff --git a/util.go b/util.go index b483f4b..7425575 100644 --- a/util.go +++ b/util.go @@ -57,5 +57,5 @@ func DefaultMsg() *linebot.TemplateMessage { linebot.NewMessageAction("我的資料", "我的資料"), linebot.NewMessageAction("辦理理賠", "辦理理賠"), ) - return linebot.NewTemplateMessage(NO_COMPAT, msg) + return linebot.NewTemplateMessage("有甚麼我能為你服務的嗎?", msg) }