master
Evan Chen 2022-01-04 15:09:46 +08:00
parent 8c697dc874
commit 748b5c9739
16 changed files with 96 additions and 20 deletions

View File

@ -71,7 +71,7 @@ func ApiSelectProfile(event *linebot.Event) {
}
go func(uid string) {
time.Sleep(time.Second * 5)
time.Sleep(time.Second * 3)
if _, err := SendMessage("", uid, DefaultMsg()); err != nil {
panic(err)
}

View File

@ -47,6 +47,10 @@ func postback(event *linebot.Event) {
ApiSelectProfile(event)
case strings.HasPrefix(data, "claim="):
ApiPrepareClaim(event)
case strings.HasPrefix(data, "cat="):
ApiListProductOfCat(event)
case data == "list-tags":
ApiListCategories(event)
}
}
@ -61,17 +65,20 @@ func controller(event *linebot.Event) {
switch {
case msg.Text == "我想投保":
ApiListProduct(event)
ApiPreparePurchase(event)
case msg.Text == "辦理理賠":
ApiMyOrders(event)
case msg.Text == "我的資料":
ApiMe(event)
case msg.Text == "我的保單":
ApiMyOrders(event)
case strings.HasPrefix(msg.Text, "我是"):
case strings.HasPrefix(msg.Text, "我要理賠"):
case msg.Text == "為我推薦":
ApiListRecommanded(event)
case strings.HasPrefix(msg.Text, "幫我介紹"):
// discard
case strings.HasPrefix(msg.Text, "我是"):
case msg.Text == "我知道":
case msg.Text == "我要理賠":
default:
// route to watson

View File

@ -22,7 +22,7 @@ func ApiPrepareClaim(event *linebot.Event) {
for _, cat := range prod.Categories {
if cat.Name == "旅平" {
url := "https://bot.ework.tw/claim"
url := flagBase + "/claim"
button := linebot.NewButtonsTemplate("", "", "請上傳收據",
linebot.NewURIAction("開啟上傳頁面", url))
msg := linebot.NewTemplateMessage(fmt.Sprintf("請前往 %v 上傳收據", url), button)

View File

@ -1,12 +1,63 @@
package main
import "github.com/line/line-bot-sdk-go/v7/linebot"
import (
"strings"
func ApiListProduct(event *linebot.Event) {
leftBtn := linebot.NewMessageAction("修改資料", "修改資料")
rightBtn := linebot.NewMessageAction("我的保單", "我的保單")
template := linebot.NewConfirmTemplate("請問想辦理甚麼服務?", leftBtn, rightBtn)
msg := linebot.NewTemplateMessage("請問想辦理甚麼服務?", template)
"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=")
msg := linebot.NewTextMessage(data + ":")
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
panic(err)
}
}
func ApiGetProduct(event *linebot.Event) {}
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)
}

View File

@ -3,7 +3,7 @@ package main
import "github.com/line/line-bot-sdk-go/v7/linebot"
func ApiMe(event *linebot.Event) {
leftBtn := linebot.NewURIAction("修改資料", "https://bot.ework.tw/profile")
leftBtn := linebot.NewURIAction("修改資料", flagBase+"/profile")
rightBtn := linebot.NewMessageAction("我的保單", "我的保單")
template := linebot.NewConfirmTemplate("請問想辦理甚麼服務?", leftBtn, rightBtn)
msg := linebot.NewTemplateMessage("請問想辦理甚麼服務?", template)
@ -17,7 +17,9 @@ func ApiMyOrders(event *linebot.Event) {
DB.Order("product_id").Find(&orders, "user_id = ?", event.Source.UserID)
if len(orders) == 0 {
msg := linebot.NewTextMessage("您目前沒有保單, 歡迎前往「我想投保」專區選取保單。")
text := "您目前沒有保單喲, 歡迎前往投保專區選取保單。"
tmp := linebot.NewButtonsTemplate("", "", text, linebot.NewMessageAction("帶我前往", "我想投保"))
msg := linebot.NewTemplateMessage(text, tmp)
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
panic(err)
}
@ -34,7 +36,7 @@ func ApiMyOrders(event *linebot.Event) {
for i, prod := range products {
columns[i] = linebot.NewCarouselColumn(
prod.Img, prod.Name, prod.Brief,
linebot.NewURIAction("保單詳細", "https://bot.ework.tw/product"),
linebot.NewURIAction("保單詳細", flagBase+"/product"),
linebot.NewPostbackAction("我要理賠", "claim="+prod.Name, "我要理賠", ""),
// linebot.NewPostbackAction("我是"+prof.Name, "select="+prof.Name, "我是"+prof.Name, ""),
)

6
assets/asset.go Normal file
View File

@ -0,0 +1,6 @@
package asset
import "embed"
//go:embed *
var FS embed.FS

BIN
assets/brown.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

View File

@ -1,15 +1,17 @@
package main
import _ "embed"
import (
_ "embed"
)
//go:embed assets/check.html
//go:embed tmpl/check.html
var check_web []byte
//go:embed assets/profile.html
//go:embed tmpl/profile.html
var profile_web []byte
//go:embed assets/claim.html
//go:embed tmpl/claim.html
var claim_web []byte
//go:embed assets/product.html
//go:embed tmpl/product.html
var product_web []byte

View File

@ -4,6 +4,8 @@ import (
"encoding/base64"
"flag"
"fmt"
"net/http"
asset "talent/assets"
"time"
"github.com/gin-gonic/gin"
@ -14,6 +16,7 @@ var (
flagAddr string
flagDev bool
flagMigrate bool
flagBase string
DB_HOST string
DB_USER string
@ -34,6 +37,7 @@ var (
func init() {
flag.StringVar(&flagAddr, "addr", "0.0.0.0:8000", "address to listen on.")
flag.StringVar(&flagBase, "base", "https://bot.ework.tw", "base url.")
flag.BoolVar(&flagDev, "dev", false, "is dev mode")
flag.BoolVar(&flagMigrate, "migrate", false, "migrate database")
@ -92,6 +96,10 @@ func main() {
hook := server.Group("bot")
hook.POST("/", webhook)
// setup static files
server.StaticFS("static", http.FS(asset.FS))
server.Static("attachment", "attachment")
// start server
log.Info().Msgf("serving on %v", flagAddr)
if err := server.Run(flagAddr); err != nil {