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); err != nil { panic(err) } // show amount and finish purchase msg := linebot.NewTextMessage(`好的, 保費試算為: XXX元 稍後將由專人為您服務。`) SendMessage(event.ReplyToken, event.Source.UserID, msg) } 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) } }