master
Evan Chen 2022-01-03 22:50:49 +08:00
parent d217229aba
commit b17b595948
8 changed files with 127 additions and 29 deletions

View File

@ -1,6 +1,10 @@
package main
import (
"fmt"
"strings"
"time"
"github.com/line/line-bot-sdk-go/v7/linebot"
"github.com/rs/zerolog/log"
)
@ -20,21 +24,57 @@ func Follow(event *linebot.Event) {
log.Error().Err(err).Msg("Follow error.")
return
}
log.Debug().Str("user", uid).Msg("New user.")
msg := linebot.NewTextMessage("你好!")
if _, err := SendMessage(event.ReplyToken, uid, msg); err != nil {
panic(err)
}
} else {
// 舊用戶
log.Debug().Str("user", uid).Msg("user unblocked.")
msg := linebot.NewTextMessage("歡迎回來!")
if _, err := SendMessage(event.ReplyToken, uid, msg); err != nil {
panic(err)
}
}
// Welcome and ask for
msg := linebot.NewTextMessage("歡迎使用台新人壽智慧聊天服務! 請您協助核對您的身分。")
template := linebot.NewCarouselTemplate(
linebot.NewCarouselColumn(
"https://imgs.gotrip.hk/wp-content/uploads/2017/08/brown-innocent_19545495235b69335e17060.jpg",
"Profile A", "A 很帥 A 很棒",
linebot.NewPostbackAction("選擇", "select=a", "我是A", ""),
),
linebot.NewCarouselColumn(
"https://imgs.gotrip.hk/wp-content/uploads/2017/08/brown-innocent_19545495235b69335e17060.jpg",
"Profile B", "B 很帥 B 很棒",
linebot.NewPostbackAction("選擇", "select=b", "我是B", ""),
),
)
profiles := linebot.NewTemplateMessage("請使用手機讀取", template)
if _, err := SendMessage(event.ReplyToken, uid, msg, profiles); err != nil {
panic(err)
}
}
func ApiSelectProfile(event *linebot.Event) {
uid := event.Source.UserID
data := event.Postback.Data
data = strings.TrimPrefix(data, "select=")
prof := &Profile{}
if err := DB.First(prof, "name = ?", data).Error; err != nil {
panic(err)
}
DB.Exec("update users set profile_id = ? where id = ?",
prof.ID, uid)
greet := linebot.NewTextMessage(fmt.Sprintf("歡迎回來! %v", data))
msg := linebot.NewStickerMessage("6325", "10979904")
if _, err := SendMessage(event.ReplyToken, event.Source.UserID,
greet, msg); err != nil {
panic(err)
}
go func(uid string) {
time.Sleep(time.Second * 10)
if _, err := SendMessage("", uid, DefaultMsg()); err != nil {
panic(err)
}
}(event.Source.UserID)
}

View File

@ -32,11 +32,22 @@ func webhook(c *gin.Context) {
if event.Type == linebot.EventTypeMessage {
controller(event)
}
if event.Type == linebot.EventTypePostback {
postback(event)
}
}
OK(c, "ok")
}
func postback(event *linebot.Event) {
data := event.Postback.Data
switch {
case strings.HasPrefix(data, "select="):
ApiSelectProfile(event)
}
}
func controller(event *linebot.Event) {
msg, ok := event.Message.(*linebot.TextMessage)
@ -47,8 +58,16 @@ func controller(event *linebot.Event) {
Hit(msg.Text, event.Source.UserID)
switch {
case msg.Text == "我想投保":
ApiListProduct(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, "幫我介紹"):
default:
@ -84,6 +103,7 @@ func watson(event *linebot.Event) error {
if err != nil {
return err
}
log.Debug().RawJSON("watson", data).Msg("")
resData := struct {
Output struct {
Generic []struct {
@ -93,13 +113,18 @@ func watson(event *linebot.Event) error {
}{}
json.Unmarshal(data, &resData)
var msg *linebot.TextMessage
if len(resData.Output.Generic) > 0 {
msg = linebot.NewTextMessage(resData.Output.Generic[0].Text)
} else {
msg = DefaultMsg()
if len(resData.Output.Generic) > 0 &&
resData.Output.Generic[0].Text != "我想我不明白。請重新描述您的問題。" {
log.Debug().Str("message", resData.Output.Generic[0].Text).
Msg("redirect")
_, err = SendMessage(event.ReplyToken, event.Source.UserID,
linebot.NewTextMessage(resData.Output.Generic[0].Text))
if err == nil {
return nil
}
}
_, err = SendMessage(event.ReplyToken, event.Source.UserID, msg)
_, err = SendMessage("", event.Source.UserID,
DefaultMsg())
return err
}

13
api_line_products.go Normal file
View File

@ -0,0 +1,13 @@
package main
import "github.com/line/line-bot-sdk-go/v7/linebot"
func ApiListProduct(event *linebot.Event) {
leftBtn := linebot.NewMessageAction("修改資料", "修改資料")
rightBtn := linebot.NewMessageAction("我的保單", "我的保單")
template := linebot.NewConfirmTemplate("請問想辦理甚麼服務?", leftBtn, rightBtn)
msg := linebot.NewTemplateMessage("電腦版不支援此訊息, 請至手機上讀取", template)
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
panic(err)
}
}

View File

@ -2,16 +2,22 @@ package main
import "github.com/line/line-bot-sdk-go/v7/linebot"
func ApiMe(event *linebot.Event) {
leftBtn := linebot.NewMessageAction("修改資料", "修改資料")
rightBtn := linebot.NewMessageAction("我的保單", "我的保單")
template := linebot.NewConfirmTemplate("請問想辦理甚麼服務?", leftBtn, rightBtn)
msg := linebot.NewTemplateMessage("電腦版不支援此訊息, 請至手機上讀取", template)
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
panic(err)
}
}
func ApiMyOrders(event *linebot.Event) {
var orders []Order
DB.Find(&orders, "user_id = ?", event.Source.UserID)
if len(orders) == 0 {
leftBtn := linebot.NewMessageAction("left", "left clicked")
rightBtn := linebot.NewMessageAction("right", "right clicked")
template := linebot.NewConfirmTemplate("Hello World", leftBtn, rightBtn)
msg := linebot.NewTemplateMessage("Sorry :(, please update your app.", template)
msg := linebot.NewTextMessage("您目前沒有保單, 歡迎前往「我要投保」專區選取保單。")
if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil {
panic(err)

View File

@ -25,8 +25,8 @@ for PLATFORM in $PLATFORMS; do
ssh root@bot.ework.tw rm talent
scp ${DIST}/${BIN_FILENAME} root@bot.ework.tw:~
ssh root@bot.ework.tw chmod +x talent
ssh root@bot.ework.tw screen -X quit
ssh root@bot.ework.tw screen -d -m ./talent -dev
# ssh root@bot.ework.tw screen -X quit
# ssh root@bot.ework.tw screen -d -m ./talent -dev
done
if [[ "${FAILURES}" != "" ]]; then

View File

@ -13,6 +13,7 @@ import (
var (
flagAddr string
flagDev bool
flagMigrate bool
DB_HOST string
DB_USER string
@ -34,6 +35,7 @@ var (
func init() {
flag.StringVar(&flagAddr, "addr", "0.0.0.0:8000", "address to listen on.")
flag.BoolVar(&flagDev, "dev", false, "is dev mode")
flag.BoolVar(&flagMigrate, "migrate", false, "migrate database")
flag.StringVar(&DB_HOST, "db-host", "postgres.ework.tw", "postgress db host.")
flag.StringVar(&DB_USER, "db-user", "talent", "postgress db user.")
@ -58,7 +60,9 @@ func main() {
setupLog()
setupServer()
connectToDB()
if flagMigrate {
migrateDB()
}
setupLine()
IBM_API = fmt.Sprintf("Basic %v", base64.URLEncoding.EncodeToString([]byte(fmt.Sprintf("apikey:%v", IBM_API))))

View File

@ -28,6 +28,7 @@ type Profile struct {
ID uint `gorm:"primaryKey"`
Name string
Brief string
CreatedAt time.Time
UpdatedAt time.Time

13
util.go
View File

@ -8,6 +8,8 @@ import (
"github.com/rs/zerolog/log"
)
const NO_COMPAT = "電腦版不支援此訊息, 請至手機上讀取"
func Mkdir(args ...interface{}) error {
var path string
var mode os.FileMode
@ -47,6 +49,13 @@ func SendMessage(replyToken string, to string, messages ...linebot.SendingMessag
}
}
func DefaultMsg() *linebot.TextMessage {
return linebot.NewTextMessage("NO RESULT")
func DefaultMsg() *linebot.TemplateMessage {
msg := linebot.NewButtonsTemplate(
"https://www.taishinlife.com.tw/event/LuckyForNew/assets/img/banner-1.jpg",
"有甚麼我能為你服務的嗎?", "您可以試著選擇:",
linebot.NewMessageAction("我想投保", "我想投保"),
linebot.NewMessageAction("我的資料", "我的資料"),
linebot.NewMessageAction("辦理理賠", "辦理理賠"),
)
return linebot.NewTemplateMessage(NO_COMPAT, msg)
}