talent/api_line_user.go

77 lines
2.6 KiB
Go
Raw Normal View History

2022-01-03 10:28:00 +00:00
package main
2022-01-04 12:39:28 +00:00
import (
"strconv"
"github.com/line/line-bot-sdk-go/v7/linebot"
)
2022-01-03 10:28:00 +00:00
2022-01-03 14:50:49 +00:00
func ApiMe(event *linebot.Event) {
2022-01-04 12:39:28 +00:00
leftBtn := linebot.NewURIAction("修改資料", flagBase+"/profile/"+event.Source.UserID)
2022-01-03 14:50:49 +00:00
rightBtn := linebot.NewMessageAction("我的保單", "我的保單")
template := linebot.NewConfirmTemplate("請問想辦理甚麼服務?", leftBtn, rightBtn)
2022-01-04 05:01:42 +00:00
msg := linebot.NewTemplateMessage("請問想辦理甚麼服務?", template)
2022-01-03 14:50:49 +00:00
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
panic(err)
}
}
2022-01-04 12:39:28 +00:00
func valid(event *linebot.Event) bool {
msg, _ := event.Message.(*linebot.TextMessage)
if msg.Text == "我的資料" {
return true
}
usr := &User{}
if err := DB.First(usr, "id = ?", event.Source.UserID).Error; err != nil {
panic(err)
}
if usr.Email == "" {
text := "您的基本資料不完整, 請您協助填寫。"
tmp := linebot.NewButtonsTemplate("", "", text,
linebot.NewURIAction("點我填寫", flagBase+"/profile/"+event.Source.UserID))
msg := linebot.NewTemplateMessage(text, tmp)
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
panic(err)
}
return false
}
return true
}
2022-01-03 10:28:00 +00:00
func ApiMyOrders(event *linebot.Event) {
var orders []Order
2022-01-04 05:01:42 +00:00
DB.Order("product_id").Find(&orders, "user_id = ?", event.Source.UserID)
2022-01-03 10:28:00 +00:00
if len(orders) == 0 {
2022-01-04 07:09:46 +00:00
text := "您目前沒有保單喲, 歡迎前往投保專區選取保單。"
tmp := linebot.NewButtonsTemplate("", "", text, linebot.NewMessageAction("帶我前往", "我想投保"))
msg := linebot.NewTemplateMessage(text, tmp)
2022-01-04 05:01:42 +00:00
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
panic(err)
}
} else {
pids := make([]uint, len(orders))
for i := range orders {
pids[i] = orders[i].ProductID
}
var products []Product
if err := DB.Preload("Categories").Order("id").Find(&products, "id in ?", pids).Error; err != nil {
panic(err)
}
columns := make([]*linebot.CarouselColumn, len(products))
for i, prod := range products {
columns[i] = linebot.NewCarouselColumn(
prod.Img, prod.Name, prod.Brief,
2022-01-04 12:39:28 +00:00
linebot.NewURIAction("保單詳細", flagBase+"/product/"+strconv.Itoa(int(prod.ID))),
2022-01-04 05:01:42 +00:00
linebot.NewPostbackAction("我要理賠", "claim="+prod.Name, "我要理賠", ""),
// linebot.NewPostbackAction("我是"+prof.Name, "select="+prof.Name, "我是"+prof.Name, ""),
)
}
template := linebot.NewCarouselTemplate(columns...)
msg := linebot.NewTemplateMessage("您的保單資訊", template)
2022-01-03 10:28:00 +00:00
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
panic(err)
}
}
}