diff --git a/api_join.go b/api_join.go index f04766b..e5350df 100644 --- a/api_join.go +++ b/api_join.go @@ -71,7 +71,7 @@ func ApiSelectProfile(event *linebot.Event) { } go func(uid string) { - time.Sleep(time.Second * 5) + time.Sleep(time.Second * 3) if _, err := SendMessage("", uid, DefaultMsg()); err != nil { panic(err) } diff --git a/api_line.go b/api_line.go index 73a559f..befa77a 100644 --- a/api_line.go +++ b/api_line.go @@ -47,6 +47,10 @@ func postback(event *linebot.Event) { ApiSelectProfile(event) case strings.HasPrefix(data, "claim="): ApiPrepareClaim(event) + case strings.HasPrefix(data, "cat="): + ApiListProductOfCat(event) + case data == "list-tags": + ApiListCategories(event) } } @@ -61,17 +65,20 @@ func controller(event *linebot.Event) { switch { case msg.Text == "我想投保": - ApiListProduct(event) + ApiPreparePurchase(event) case msg.Text == "辦理理賠": ApiMyOrders(event) case msg.Text == "我的資料": ApiMe(event) case msg.Text == "我的保單": ApiMyOrders(event) - case strings.HasPrefix(msg.Text, "我是"): - case strings.HasPrefix(msg.Text, "我要理賠"): + case msg.Text == "為我推薦": + ApiListRecommanded(event) - case strings.HasPrefix(msg.Text, "幫我介紹"): + // discard + case strings.HasPrefix(msg.Text, "我是"): + case msg.Text == "我知道": + case msg.Text == "我要理賠": default: // route to watson diff --git a/api_line_claim.go b/api_line_claim.go index df075e7..10b8169 100644 --- a/api_line_claim.go +++ b/api_line_claim.go @@ -22,7 +22,7 @@ func ApiPrepareClaim(event *linebot.Event) { for _, cat := range prod.Categories { if cat.Name == "旅平" { - url := "https://bot.ework.tw/claim" + url := flagBase + "/claim" button := linebot.NewButtonsTemplate("", "", "請上傳收據", linebot.NewURIAction("開啟上傳頁面", url)) msg := linebot.NewTemplateMessage(fmt.Sprintf("請前往 %v 上傳收據", url), button) diff --git a/api_line_products.go b/api_line_products.go index 6355322..b05f21c 100644 --- a/api_line_products.go +++ b/api_line_products.go @@ -1,12 +1,63 @@ package main -import "github.com/line/line-bot-sdk-go/v7/linebot" +import ( + "strings" -func ApiListProduct(event *linebot.Event) { - leftBtn := linebot.NewMessageAction("修改資料", "修改資料") - rightBtn := linebot.NewMessageAction("我的保單", "我的保單") - template := linebot.NewConfirmTemplate("請問想辦理甚麼服務?", leftBtn, rightBtn) - msg := linebot.NewTemplateMessage("請問想辦理甚麼服務?", template) + "github.com/line/line-bot-sdk-go/v7/linebot" +) + +func ApiPreparePurchase(event *linebot.Event) { + text := "沒問題!您知道想投保甚麼險種嗎?我們很樂意為您介紹" + template := linebot.NewConfirmTemplate(text, + linebot.NewPostbackAction("我知道", "list-tags", "我知道", ""), + linebot.NewMessageAction("為我推薦", "為我推薦")) + msg := linebot.NewTemplateMessage(text, template) + if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil { + panic(err) + } +} + +func ApiListCategories(event *linebot.Event) { + var cats []Category + DB.Find(&cats) + buttons := make([]*linebot.QuickReplyButton, len(cats)) + + for i, cat := range cats { + buttons[i] = linebot.NewQuickReplyButton("", + linebot.NewPostbackAction(cat.Name, "cat="+cat.Name, "", "險種: "+cat.Name)) + } + msg := linebot.NewTextMessage("請選擇險種。").WithQuickReplies( + linebot.NewQuickReplyItems(buttons...), + ) + if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil { + panic(err) + } +} +func ApiListProductOfCat(event *linebot.Event) { + data := event.Postback.Data + data = strings.TrimPrefix(data, "cat=") + + msg := linebot.NewTextMessage(data + ":") + if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil { + panic(err) + } +} + +func ApiGetProduct(event *linebot.Event) {} + +func ApiListRecommanded(event *linebot.Event) { + var prods []Product + DB.Limit(10).Find(&prods, "id not in (select product_id from orders where user_id = ?)", event.Source.UserID) + + // sort for target user + + // send slide + + text := "您的推薦" + leftBtn := linebot.NewMessageAction("修改資料", "修改資料") + rightBtn := linebot.NewMessageAction("我的保單", "我的保單") + template := linebot.NewConfirmTemplate(text, leftBtn, rightBtn) + msg := linebot.NewTemplateMessage(text, 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 ea08986..9df46d2 100644 --- a/api_line_user.go +++ b/api_line_user.go @@ -3,7 +3,7 @@ package main import "github.com/line/line-bot-sdk-go/v7/linebot" func ApiMe(event *linebot.Event) { - leftBtn := linebot.NewURIAction("修改資料", "https://bot.ework.tw/profile") + leftBtn := linebot.NewURIAction("修改資料", flagBase+"/profile") rightBtn := linebot.NewMessageAction("我的保單", "我的保單") template := linebot.NewConfirmTemplate("請問想辦理甚麼服務?", leftBtn, rightBtn) msg := linebot.NewTemplateMessage("請問想辦理甚麼服務?", template) @@ -17,7 +17,9 @@ func ApiMyOrders(event *linebot.Event) { DB.Order("product_id").Find(&orders, "user_id = ?", event.Source.UserID) if len(orders) == 0 { - msg := linebot.NewTextMessage("您目前沒有保單, 歡迎前往「我想投保」專區選取保單。") + text := "您目前沒有保單喲, 歡迎前往投保專區選取保單。" + tmp := linebot.NewButtonsTemplate("", "", text, linebot.NewMessageAction("帶我前往", "我想投保")) + msg := linebot.NewTemplateMessage(text, tmp) if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil { panic(err) } @@ -34,7 +36,7 @@ func ApiMyOrders(event *linebot.Event) { for i, prod := range products { columns[i] = linebot.NewCarouselColumn( prod.Img, prod.Name, prod.Brief, - linebot.NewURIAction("保單詳細", "https://bot.ework.tw/product"), + linebot.NewURIAction("保單詳細", flagBase+"/product"), linebot.NewPostbackAction("我要理賠", "claim="+prod.Name, "我要理賠", ""), // linebot.NewPostbackAction("我是"+prof.Name, "select="+prof.Name, "我是"+prof.Name, ""), ) diff --git a/assets/asset.go b/assets/asset.go new file mode 100644 index 0000000..6db2b46 --- /dev/null +++ b/assets/asset.go @@ -0,0 +1,6 @@ +package asset + +import "embed" + +//go:embed * +var FS embed.FS diff --git a/assets/brown.jpg b/assets/brown.jpg new file mode 100644 index 0000000..c13d853 Binary files /dev/null and b/assets/brown.jpg differ diff --git a/files.go b/files.go index 087be10..fa4fef4 100644 --- a/files.go +++ b/files.go @@ -1,15 +1,17 @@ package main -import _ "embed" +import ( + _ "embed" +) -//go:embed assets/check.html +//go:embed tmpl/check.html var check_web []byte -//go:embed assets/profile.html +//go:embed tmpl/profile.html var profile_web []byte -//go:embed assets/claim.html +//go:embed tmpl/claim.html var claim_web []byte -//go:embed assets/product.html +//go:embed tmpl/product.html var product_web []byte diff --git a/main.go b/main.go index c856999..0d9dc63 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,8 @@ import ( "encoding/base64" "flag" "fmt" + "net/http" + asset "talent/assets" "time" "github.com/gin-gonic/gin" @@ -14,6 +16,7 @@ var ( flagAddr string flagDev bool flagMigrate bool + flagBase string DB_HOST string DB_USER string @@ -34,6 +37,7 @@ var ( func init() { flag.StringVar(&flagAddr, "addr", "0.0.0.0:8000", "address to listen on.") + flag.StringVar(&flagBase, "base", "https://bot.ework.tw", "base url.") flag.BoolVar(&flagDev, "dev", false, "is dev mode") flag.BoolVar(&flagMigrate, "migrate", false, "migrate database") @@ -92,6 +96,10 @@ func main() { hook := server.Group("bot") hook.POST("/", webhook) + // setup static files + server.StaticFS("static", http.FS(asset.FS)) + server.Static("attachment", "attachment") + // start server log.Info().Msgf("serving on %v", flagAddr) if err := server.Run(flagAddr); err != nil { diff --git a/build.sh b/scripts/build.sh similarity index 100% rename from build.sh rename to scripts/build.sh diff --git a/deploy.sh b/scripts/deploy.sh similarity index 100% rename from deploy.sh rename to scripts/deploy.sh diff --git a/run.sh b/scripts/run.sh similarity index 100% rename from run.sh rename to scripts/run.sh diff --git a/assets/check.html b/tmpl/check.html similarity index 100% rename from assets/check.html rename to tmpl/check.html diff --git a/assets/claim.html b/tmpl/claim.html similarity index 100% rename from assets/claim.html rename to tmpl/claim.html diff --git a/assets/product.html b/tmpl/product.html similarity index 100% rename from assets/product.html rename to tmpl/product.html diff --git a/assets/profile.html b/tmpl/profile.html similarity index 100% rename from assets/profile.html rename to tmpl/profile.html