update
parent
8c697dc874
commit
748b5c9739
|
@ -71,7 +71,7 @@ func ApiSelectProfile(event *linebot.Event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
go func(uid string) {
|
go func(uid string) {
|
||||||
time.Sleep(time.Second * 5)
|
time.Sleep(time.Second * 3)
|
||||||
if _, err := SendMessage("", uid, DefaultMsg()); err != nil {
|
if _, err := SendMessage("", uid, DefaultMsg()); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
15
api_line.go
15
api_line.go
|
@ -47,6 +47,10 @@ func postback(event *linebot.Event) {
|
||||||
ApiSelectProfile(event)
|
ApiSelectProfile(event)
|
||||||
case strings.HasPrefix(data, "claim="):
|
case strings.HasPrefix(data, "claim="):
|
||||||
ApiPrepareClaim(event)
|
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 {
|
switch {
|
||||||
case msg.Text == "我想投保":
|
case msg.Text == "我想投保":
|
||||||
ApiListProduct(event)
|
ApiPreparePurchase(event)
|
||||||
case msg.Text == "辦理理賠":
|
case msg.Text == "辦理理賠":
|
||||||
ApiMyOrders(event)
|
ApiMyOrders(event)
|
||||||
case msg.Text == "我的資料":
|
case msg.Text == "我的資料":
|
||||||
ApiMe(event)
|
ApiMe(event)
|
||||||
case msg.Text == "我的保單":
|
case msg.Text == "我的保單":
|
||||||
ApiMyOrders(event)
|
ApiMyOrders(event)
|
||||||
case strings.HasPrefix(msg.Text, "我是"):
|
case msg.Text == "為我推薦":
|
||||||
case strings.HasPrefix(msg.Text, "我要理賠"):
|
ApiListRecommanded(event)
|
||||||
|
|
||||||
case strings.HasPrefix(msg.Text, "幫我介紹"):
|
// discard
|
||||||
|
case strings.HasPrefix(msg.Text, "我是"):
|
||||||
|
case msg.Text == "我知道":
|
||||||
|
case msg.Text == "我要理賠":
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// route to watson
|
// route to watson
|
||||||
|
|
|
@ -22,7 +22,7 @@ func ApiPrepareClaim(event *linebot.Event) {
|
||||||
|
|
||||||
for _, cat := range prod.Categories {
|
for _, cat := range prod.Categories {
|
||||||
if cat.Name == "旅平" {
|
if cat.Name == "旅平" {
|
||||||
url := "https://bot.ework.tw/claim"
|
url := flagBase + "/claim"
|
||||||
button := linebot.NewButtonsTemplate("", "", "請上傳收據",
|
button := linebot.NewButtonsTemplate("", "", "請上傳收據",
|
||||||
linebot.NewURIAction("開啟上傳頁面", url))
|
linebot.NewURIAction("開啟上傳頁面", url))
|
||||||
msg := linebot.NewTemplateMessage(fmt.Sprintf("請前往 %v 上傳收據", url), button)
|
msg := linebot.NewTemplateMessage(fmt.Sprintf("請前往 %v 上傳收據", url), button)
|
||||||
|
|
|
@ -1,12 +1,63 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "github.com/line/line-bot-sdk-go/v7/linebot"
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
func ApiListProduct(event *linebot.Event) {
|
"github.com/line/line-bot-sdk-go/v7/linebot"
|
||||||
leftBtn := linebot.NewMessageAction("修改資料", "修改資料")
|
)
|
||||||
rightBtn := linebot.NewMessageAction("我的保單", "我的保單")
|
|
||||||
template := linebot.NewConfirmTemplate("請問想辦理甚麼服務?", leftBtn, rightBtn)
|
func ApiPreparePurchase(event *linebot.Event) {
|
||||||
msg := linebot.NewTemplateMessage("請問想辦理甚麼服務?", template)
|
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 {
|
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ package main
|
||||||
import "github.com/line/line-bot-sdk-go/v7/linebot"
|
import "github.com/line/line-bot-sdk-go/v7/linebot"
|
||||||
|
|
||||||
func ApiMe(event *linebot.Event) {
|
func ApiMe(event *linebot.Event) {
|
||||||
leftBtn := linebot.NewURIAction("修改資料", "https://bot.ework.tw/profile")
|
leftBtn := linebot.NewURIAction("修改資料", flagBase+"/profile")
|
||||||
rightBtn := linebot.NewMessageAction("我的保單", "我的保單")
|
rightBtn := linebot.NewMessageAction("我的保單", "我的保單")
|
||||||
template := linebot.NewConfirmTemplate("請問想辦理甚麼服務?", leftBtn, rightBtn)
|
template := linebot.NewConfirmTemplate("請問想辦理甚麼服務?", leftBtn, rightBtn)
|
||||||
msg := linebot.NewTemplateMessage("請問想辦理甚麼服務?", template)
|
msg := linebot.NewTemplateMessage("請問想辦理甚麼服務?", template)
|
||||||
|
@ -17,7 +17,9 @@ func ApiMyOrders(event *linebot.Event) {
|
||||||
DB.Order("product_id").Find(&orders, "user_id = ?", event.Source.UserID)
|
DB.Order("product_id").Find(&orders, "user_id = ?", event.Source.UserID)
|
||||||
|
|
||||||
if len(orders) == 0 {
|
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 {
|
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -34,7 +36,7 @@ func ApiMyOrders(event *linebot.Event) {
|
||||||
for i, prod := range products {
|
for i, prod := range products {
|
||||||
columns[i] = linebot.NewCarouselColumn(
|
columns[i] = linebot.NewCarouselColumn(
|
||||||
prod.Img, prod.Name, prod.Brief,
|
prod.Img, prod.Name, prod.Brief,
|
||||||
linebot.NewURIAction("保單詳細", "https://bot.ework.tw/product"),
|
linebot.NewURIAction("保單詳細", flagBase+"/product"),
|
||||||
linebot.NewPostbackAction("我要理賠", "claim="+prod.Name, "我要理賠", ""),
|
linebot.NewPostbackAction("我要理賠", "claim="+prod.Name, "我要理賠", ""),
|
||||||
// linebot.NewPostbackAction("我是"+prof.Name, "select="+prof.Name, "我是"+prof.Name, ""),
|
// linebot.NewPostbackAction("我是"+prof.Name, "select="+prof.Name, "我是"+prof.Name, ""),
|
||||||
)
|
)
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
package asset
|
||||||
|
|
||||||
|
import "embed"
|
||||||
|
|
||||||
|
//go:embed *
|
||||||
|
var FS embed.FS
|
Binary file not shown.
After Width: | Height: | Size: 23 KiB |
12
files.go
12
files.go
|
@ -1,15 +1,17 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import _ "embed"
|
import (
|
||||||
|
_ "embed"
|
||||||
|
)
|
||||||
|
|
||||||
//go:embed assets/check.html
|
//go:embed tmpl/check.html
|
||||||
var check_web []byte
|
var check_web []byte
|
||||||
|
|
||||||
//go:embed assets/profile.html
|
//go:embed tmpl/profile.html
|
||||||
var profile_web []byte
|
var profile_web []byte
|
||||||
|
|
||||||
//go:embed assets/claim.html
|
//go:embed tmpl/claim.html
|
||||||
var claim_web []byte
|
var claim_web []byte
|
||||||
|
|
||||||
//go:embed assets/product.html
|
//go:embed tmpl/product.html
|
||||||
var product_web []byte
|
var product_web []byte
|
||||||
|
|
8
main.go
8
main.go
|
@ -4,6 +4,8 @@ import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
asset "talent/assets"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -14,6 +16,7 @@ var (
|
||||||
flagAddr string
|
flagAddr string
|
||||||
flagDev bool
|
flagDev bool
|
||||||
flagMigrate bool
|
flagMigrate bool
|
||||||
|
flagBase string
|
||||||
|
|
||||||
DB_HOST string
|
DB_HOST string
|
||||||
DB_USER string
|
DB_USER string
|
||||||
|
@ -34,6 +37,7 @@ var (
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.StringVar(&flagAddr, "addr", "0.0.0.0:8000", "address to listen on.")
|
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(&flagDev, "dev", false, "is dev mode")
|
||||||
flag.BoolVar(&flagMigrate, "migrate", false, "migrate database")
|
flag.BoolVar(&flagMigrate, "migrate", false, "migrate database")
|
||||||
|
|
||||||
|
@ -92,6 +96,10 @@ func main() {
|
||||||
hook := server.Group("bot")
|
hook := server.Group("bot")
|
||||||
hook.POST("/", webhook)
|
hook.POST("/", webhook)
|
||||||
|
|
||||||
|
// setup static files
|
||||||
|
server.StaticFS("static", http.FS(asset.FS))
|
||||||
|
server.Static("attachment", "attachment")
|
||||||
|
|
||||||
// start server
|
// start server
|
||||||
log.Info().Msgf("serving on %v", flagAddr)
|
log.Info().Msgf("serving on %v", flagAddr)
|
||||||
if err := server.Run(flagAddr); err != nil {
|
if err := server.Run(flagAddr); err != nil {
|
||||||
|
|
Loading…
Reference in New Issue