2022-01-03 14:50:49 +00:00
|
|
|
package main
|
|
|
|
|
2022-01-04 07:09:46 +00:00
|
|
|
import (
|
2022-01-04 12:39:28 +00:00
|
|
|
"strconv"
|
2022-01-04 07:09:46 +00:00
|
|
|
"strings"
|
2022-01-03 14:50:49 +00:00
|
|
|
|
2022-01-04 07:09:46 +00:00
|
|
|
"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=")
|
|
|
|
|
2022-01-04 12:39:28 +00:00
|
|
|
var prods []Product
|
2022-01-04 12:42:18 +00:00
|
|
|
DB.Find(&prods, "id in (select product_id from product_categories where category_name = ?)", data)
|
2022-01-04 12:39:28 +00:00
|
|
|
|
|
|
|
columns := make([]*linebot.CarouselColumn, len(prods))
|
|
|
|
for i, prod := range prods {
|
|
|
|
columns[i] = linebot.NewCarouselColumn(
|
|
|
|
prod.Img, prod.Name, prod.Brief,
|
|
|
|
linebot.NewURIAction("保單詳細", flagBase+"/product/"+strconv.Itoa(int(prod.ID))),
|
|
|
|
linebot.NewPostbackAction("我要投保", "purchase="+prod.Name, "我要投保", ""),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
template := linebot.NewCarouselTemplate(columns...)
|
|
|
|
|
|
|
|
msg := linebot.NewTemplateMessage("我要投保", template)
|
2022-01-04 07:09:46 +00:00
|
|
|
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 12:39:28 +00:00
|
|
|
func ApiPurchase(event *linebot.Event) {
|
|
|
|
data := event.Postback.Data
|
|
|
|
data = strings.TrimPrefix(data, "purchase=")
|
|
|
|
prod := &Product{}
|
2022-01-04 14:27:59 +00:00
|
|
|
if err := DB.Preload("Categories").First(prod, "name = ?", data).Error; err != nil {
|
2022-01-04 12:39:28 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// show amount and finish purchase
|
|
|
|
msg := linebot.NewTextMessage(`好的,
|
|
|
|
保費試算為: XXX元
|
|
|
|
|
2022-01-04 14:27:59 +00:00
|
|
|
請選擇您附近的專業業務員。`)
|
|
|
|
|
|
|
|
var salses []Sales
|
|
|
|
DB.Find(&salses)
|
|
|
|
columns := make([]*linebot.CarouselColumn, len(salses))
|
|
|
|
for i, salse := range salses {
|
|
|
|
columns[i] = linebot.NewCarouselColumn(
|
|
|
|
salse.Img, salse.Name, salse.Brief,
|
2022-01-04 15:45:55 +00:00
|
|
|
linebot.NewPostbackAction("讓我幫你", "sales="+salse.Name+"&"+event.Postback.Data, "讓我幫你", ""),
|
2022-01-04 14:27:59 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
template := linebot.NewCarouselTemplate(columns...)
|
|
|
|
|
|
|
|
selectSales := linebot.NewTemplateMessage("選擇專業業務員", template)
|
|
|
|
SendMessage(event.ReplyToken, event.Source.UserID, msg, selectSales)
|
2022-01-04 12:39:28 +00:00
|
|
|
}
|
2022-01-04 07:09:46 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-01-04 14:27:59 +00:00
|
|
|
columns := make([]*linebot.CarouselColumn, len(prods))
|
|
|
|
for i, prod := range prods {
|
|
|
|
columns[i] = linebot.NewCarouselColumn(
|
|
|
|
prod.Img, prod.Name, prod.Brief,
|
|
|
|
linebot.NewURIAction("保單詳細", flagBase+"/product/"+strconv.Itoa(int(prod.ID))),
|
|
|
|
linebot.NewPostbackAction("我要投保", "purchase="+prod.Name, "我要投保", ""),
|
|
|
|
)
|
2022-01-03 14:50:49 +00:00
|
|
|
}
|
2022-01-04 14:27:59 +00:00
|
|
|
template := linebot.NewCarouselTemplate(columns...)
|
|
|
|
|
|
|
|
msg := linebot.NewTemplateMessage("我要投保", template)
|
|
|
|
SendMessage(event.ReplyToken, event.Source.UserID, msg)
|
2022-01-03 14:50:49 +00:00
|
|
|
}
|