master
Evan Chen 2022-01-04 22:27:59 +08:00
parent 5ca8f38e40
commit 2f35f8c4b0
6 changed files with 92 additions and 12 deletions

6
api.go
View File

@ -29,7 +29,11 @@ func ApiUpdateEmail(c *gin.Context) {
OK(c, "ok") OK(c, "ok")
msg := linebot.NewTextMessage("更新成功!") msg := linebot.NewTextMessage("更新成功!")
sticker := linebot.NewStickerMessage("1070", "17840") sticker := linebot.NewStickerMessage("1070", "17840")
SendMessage("", data.ID, msg, sticker, DefaultMsg()) SendMessage("", data.ID, msg, sticker)
go func() {
time.Sleep(time.Second * 2)
SendMessage("", data.ID, DefaultMsg())
}()
} }
func ApiPostReceipt(c *gin.Context) { func ApiPostReceipt(c *gin.Context) {

View File

@ -52,6 +52,8 @@ func postback(event *linebot.Event) {
ApiListProductOfCat(event) ApiListProductOfCat(event)
case strings.HasPrefix(data, "purchase="): case strings.HasPrefix(data, "purchase="):
ApiPurchase(event) ApiPurchase(event)
case strings.HasPrefix(data, "sales="):
ApiSelectSales(event)
case data == "list-tags": case data == "list-tags":
ApiListCategories(event) ApiListCategories(event)
} }
@ -87,6 +89,7 @@ func controller(event *linebot.Event) {
case msg.Text == "我要理賠": case msg.Text == "我要理賠":
case msg.Text == "修改完成": case msg.Text == "修改完成":
case msg.Text == "我要投保": case msg.Text == "我要投保":
case msg.Text == "讓我幫你":
default: default:
// route to watson // route to watson

View File

@ -61,7 +61,7 @@ func ApiPurchase(event *linebot.Event) {
data := event.Postback.Data data := event.Postback.Data
data = strings.TrimPrefix(data, "purchase=") data = strings.TrimPrefix(data, "purchase=")
prod := &Product{} prod := &Product{}
if err := DB.Preload("Categories").First(prod, "name = ?", data); err != nil { if err := DB.Preload("Categories").First(prod, "name = ?", data).Error; err != nil {
panic(err) panic(err)
} }
@ -69,8 +69,21 @@ func ApiPurchase(event *linebot.Event) {
msg := linebot.NewTextMessage(`, msg := linebot.NewTextMessage(`,
: XXX : XXX
`) `)
SendMessage(event.ReplyToken, event.Source.UserID, msg)
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, "讓我幫你", ""),
)
}
template := linebot.NewCarouselTemplate(columns...)
selectSales := linebot.NewTemplateMessage("選擇專業業務員", template)
SendMessage(event.ReplyToken, event.Source.UserID, msg, selectSales)
} }
func ApiListRecommanded(event *linebot.Event) { func ApiListRecommanded(event *linebot.Event) {
@ -81,12 +94,16 @@ func ApiListRecommanded(event *linebot.Event) {
// send slide // send slide
text := "您的推薦" columns := make([]*linebot.CarouselColumn, len(prods))
leftBtn := linebot.NewMessageAction("修改資料", "修改資料") for i, prod := range prods {
rightBtn := linebot.NewMessageAction("我的保單", "我的保單") columns[i] = linebot.NewCarouselColumn(
template := linebot.NewConfirmTemplate(text, leftBtn, rightBtn) prod.Img, prod.Name, prod.Brief,
msg := linebot.NewTemplateMessage(text, template) linebot.NewURIAction("保單詳細", flagBase+"/product/"+strconv.Itoa(int(prod.ID))),
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil { linebot.NewPostbackAction("我要投保", "purchase="+prod.Name, "我要投保", ""),
panic(err) )
} }
template := linebot.NewCarouselTemplate(columns...)
msg := linebot.NewTemplateMessage("我要投保", template)
SendMessage(event.ReplyToken, event.Source.UserID, msg)
} }

50
api_line_sales.go Normal file
View File

@ -0,0 +1,50 @@
package main
import (
"fmt"
"strings"
"github.com/line/line-bot-sdk-go/v7/linebot"
)
func ApiSelectSales(event *linebot.Event) {
data := event.Postback.Data
data = strings.TrimPrefix(data, "sales=")
sales := &Sales{}
DB.First(sales, "name = ?", data)
tmp, _ := linebot.UnmarshalFlexMessageJSON([]byte(
fmt.Sprintf(greet, sales.Img, sales.Name, "很高興為您服務!"),
))
msg := linebot.NewFlexMessage("很高興為您服務!", tmp)
sticker := linebot.NewStickerMessage("1070", "17852")
SendMessage(event.ReplyToken, event.Source.UserID, msg, sticker)
}
var greet string = `
{
"type": "bubble",
"hero": {
"type": "image",
"url": "%v",
"size": "full"
},
"body": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "你好! 我是%v",
"weight": "bold",
"size": "xl"
},
{
"type": "text",
"text": "%v"
}
]
}
}
`

2
db.go
View File

@ -44,7 +44,7 @@ func migrateDB() {
if err := DB.SetupJoinTable(&User{}, "Products", &Order{}); err != nil { if err := DB.SetupJoinTable(&User{}, "Products", &Order{}); err != nil {
panic(err) panic(err)
} }
if err := DB.AutoMigrate(&Node{}, &User{}, &Profile{}, &Product{}, &Order{}, &Price{}); err != nil { if err := DB.AutoMigrate(&Node{}, &User{}, &Profile{}, &Product{}, &Order{}, &Price{}, &Sales{}); err != nil {
panic(err) panic(err)
} }
} }

View File

@ -63,6 +63,12 @@ type Product struct {
UpdatedAt time.Time UpdatedAt time.Time
} }
type Sales struct {
Img string
Name string
Brief string
}
type Category struct { type Category struct {
Name string `gorm:"primaryKey"` Name string `gorm:"primaryKey"`
} }