update
parent
d217229aba
commit
b17b595948
58
api_join.go
58
api_join.go
|
@ -1,6 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/line/line-bot-sdk-go/v7/linebot"
|
"github.com/line/line-bot-sdk-go/v7/linebot"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
@ -20,21 +24,57 @@ func Follow(event *linebot.Event) {
|
||||||
log.Error().Err(err).Msg("Follow error.")
|
log.Error().Err(err).Msg("Follow error.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug().Str("user", uid).Msg("New user.")
|
log.Debug().Str("user", uid).Msg("New user.")
|
||||||
msg := linebot.NewTextMessage("你好!")
|
|
||||||
if _, err := SendMessage(event.ReplyToken, uid, msg); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// 舊用戶
|
// 舊用戶
|
||||||
log.Debug().Str("user", uid).Msg("user unblocked.")
|
log.Debug().Str("user", uid).Msg("user unblocked.")
|
||||||
msg := linebot.NewTextMessage("歡迎回來!")
|
}
|
||||||
if _, err := SendMessage(event.ReplyToken, uid, msg); err != nil {
|
|
||||||
|
// 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)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Welcome and ask for
|
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)
|
||||||
}
|
}
|
||||||
|
|
37
api_line.go
37
api_line.go
|
@ -32,11 +32,22 @@ func webhook(c *gin.Context) {
|
||||||
if event.Type == linebot.EventTypeMessage {
|
if event.Type == linebot.EventTypeMessage {
|
||||||
controller(event)
|
controller(event)
|
||||||
}
|
}
|
||||||
|
if event.Type == linebot.EventTypePostback {
|
||||||
|
postback(event)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
OK(c, "ok")
|
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) {
|
func controller(event *linebot.Event) {
|
||||||
|
|
||||||
msg, ok := event.Message.(*linebot.TextMessage)
|
msg, ok := event.Message.(*linebot.TextMessage)
|
||||||
|
@ -47,8 +58,16 @@ func controller(event *linebot.Event) {
|
||||||
Hit(msg.Text, event.Source.UserID)
|
Hit(msg.Text, event.Source.UserID)
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
|
case msg.Text == "我想投保":
|
||||||
|
ApiListProduct(event)
|
||||||
|
case msg.Text == "辦理理賠":
|
||||||
|
ApiMyOrders(event)
|
||||||
|
case msg.Text == "我的資料":
|
||||||
|
ApiMe(event)
|
||||||
case msg.Text == "我的保單":
|
case msg.Text == "我的保單":
|
||||||
ApiMyOrders(event)
|
ApiMyOrders(event)
|
||||||
|
case strings.HasPrefix(msg.Text, "我是"):
|
||||||
|
|
||||||
case strings.HasPrefix(msg.Text, "幫我介紹"):
|
case strings.HasPrefix(msg.Text, "幫我介紹"):
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -84,6 +103,7 @@ func watson(event *linebot.Event) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
log.Debug().RawJSON("watson", data).Msg("")
|
||||||
resData := struct {
|
resData := struct {
|
||||||
Output struct {
|
Output struct {
|
||||||
Generic []struct {
|
Generic []struct {
|
||||||
|
@ -93,13 +113,18 @@ func watson(event *linebot.Event) error {
|
||||||
}{}
|
}{}
|
||||||
json.Unmarshal(data, &resData)
|
json.Unmarshal(data, &resData)
|
||||||
|
|
||||||
var msg *linebot.TextMessage
|
if len(resData.Output.Generic) > 0 &&
|
||||||
if len(resData.Output.Generic) > 0 {
|
resData.Output.Generic[0].Text != "我想我不明白。請重新描述您的問題。" {
|
||||||
msg = linebot.NewTextMessage(resData.Output.Generic[0].Text)
|
log.Debug().Str("message", resData.Output.Generic[0].Text).
|
||||||
} else {
|
Msg("redirect")
|
||||||
msg = DefaultMsg()
|
_, 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
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,16 +2,22 @@ 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) {
|
||||||
|
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) {
|
func ApiMyOrders(event *linebot.Event) {
|
||||||
var orders []Order
|
var orders []Order
|
||||||
DB.Find(&orders, "user_id = ?", event.Source.UserID)
|
DB.Find(&orders, "user_id = ?", event.Source.UserID)
|
||||||
|
|
||||||
if len(orders) == 0 {
|
if len(orders) == 0 {
|
||||||
leftBtn := linebot.NewMessageAction("left", "left clicked")
|
msg := linebot.NewTextMessage("您目前沒有保單, 歡迎前往「我要投保」專區選取保單。")
|
||||||
rightBtn := linebot.NewMessageAction("right", "right clicked")
|
|
||||||
template := linebot.NewConfirmTemplate("Hello World", leftBtn, rightBtn)
|
|
||||||
|
|
||||||
msg := linebot.NewTemplateMessage("Sorry :(, please update your app.", 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)
|
||||||
|
|
|
@ -25,8 +25,8 @@ for PLATFORM in $PLATFORMS; do
|
||||||
ssh root@bot.ework.tw rm talent
|
ssh root@bot.ework.tw rm talent
|
||||||
scp ${DIST}/${BIN_FILENAME} root@bot.ework.tw:~
|
scp ${DIST}/${BIN_FILENAME} root@bot.ework.tw:~
|
||||||
ssh root@bot.ework.tw chmod +x talent
|
ssh root@bot.ework.tw chmod +x talent
|
||||||
ssh root@bot.ework.tw screen -X quit
|
# ssh root@bot.ework.tw screen -X quit
|
||||||
ssh root@bot.ework.tw screen -d -m ./talent -dev
|
# ssh root@bot.ework.tw screen -d -m ./talent -dev
|
||||||
done
|
done
|
||||||
|
|
||||||
if [[ "${FAILURES}" != "" ]]; then
|
if [[ "${FAILURES}" != "" ]]; then
|
||||||
|
|
4
main.go
4
main.go
|
@ -13,6 +13,7 @@ import (
|
||||||
var (
|
var (
|
||||||
flagAddr string
|
flagAddr string
|
||||||
flagDev bool
|
flagDev bool
|
||||||
|
flagMigrate bool
|
||||||
|
|
||||||
DB_HOST string
|
DB_HOST string
|
||||||
DB_USER string
|
DB_USER string
|
||||||
|
@ -34,6 +35,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.BoolVar(&flagDev, "dev", false, "is dev mode")
|
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_HOST, "db-host", "postgres.ework.tw", "postgress db host.")
|
||||||
flag.StringVar(&DB_USER, "db-user", "talent", "postgress db user.")
|
flag.StringVar(&DB_USER, "db-user", "talent", "postgress db user.")
|
||||||
|
@ -58,7 +60,9 @@ func main() {
|
||||||
setupLog()
|
setupLog()
|
||||||
setupServer()
|
setupServer()
|
||||||
connectToDB()
|
connectToDB()
|
||||||
|
if flagMigrate {
|
||||||
migrateDB()
|
migrateDB()
|
||||||
|
}
|
||||||
setupLine()
|
setupLine()
|
||||||
|
|
||||||
IBM_API = fmt.Sprintf("Basic %v", base64.URLEncoding.EncodeToString([]byte(fmt.Sprintf("apikey:%v", IBM_API))))
|
IBM_API = fmt.Sprintf("Basic %v", base64.URLEncoding.EncodeToString([]byte(fmt.Sprintf("apikey:%v", IBM_API))))
|
||||||
|
|
1
model.go
1
model.go
|
@ -28,6 +28,7 @@ type Profile struct {
|
||||||
ID uint `gorm:"primaryKey"`
|
ID uint `gorm:"primaryKey"`
|
||||||
|
|
||||||
Name string
|
Name string
|
||||||
|
Brief string
|
||||||
|
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
|
|
13
util.go
13
util.go
|
@ -8,6 +8,8 @@ import (
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const NO_COMPAT = "電腦版不支援此訊息, 請至手機上讀取"
|
||||||
|
|
||||||
func Mkdir(args ...interface{}) error {
|
func Mkdir(args ...interface{}) error {
|
||||||
var path string
|
var path string
|
||||||
var mode os.FileMode
|
var mode os.FileMode
|
||||||
|
@ -47,6 +49,13 @@ func SendMessage(replyToken string, to string, messages ...linebot.SendingMessag
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func DefaultMsg() *linebot.TextMessage {
|
func DefaultMsg() *linebot.TemplateMessage {
|
||||||
return linebot.NewTextMessage("NO RESULT")
|
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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue