talent/api_line_products.go

110 lines
3.5 KiB
Go

package main
import (
"strconv"
"strings"
"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=")
var prods []Product
DB.Find(&prods, "id in (select product_id from product_categories where category_name = ?)", data)
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)
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
panic(err)
}
}
func ApiPurchase(event *linebot.Event) {
data := event.Postback.Data
data = strings.TrimPrefix(data, "purchase=")
prod := &Product{}
if err := DB.Preload("Categories").First(prod, "name = ?", data).Error; err != nil {
panic(err)
}
// show amount and finish purchase
msg := linebot.NewTextMessage(`好的,
保費試算為: XXX元
請選擇您附近的專業業務員。`)
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,
linebot.NewPostbackAction("讓我幫你", "sales="+salse.Name+"&"+event.Postback.Data, "讓我幫你", ""),
)
}
template := linebot.NewCarouselTemplate(columns...)
selectSales := linebot.NewTemplateMessage("選擇專業業務員", template)
SendMessage(event.ReplyToken, event.Source.UserID, msg, selectSales)
}
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
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)
SendMessage(event.ReplyToken, event.Source.UserID, msg)
}