2022-01-02 16:47:25 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-01-03 14:50:49 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-01-02 16:47:25 +00:00
|
|
|
"github.com/line/line-bot-sdk-go/v7/linebot"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Follow(event *linebot.Event) {
|
|
|
|
// add user
|
|
|
|
uid := event.Source.UserID
|
|
|
|
usr := &User{
|
|
|
|
ID: uid,
|
|
|
|
}
|
|
|
|
Hit("加入", uid)
|
|
|
|
|
|
|
|
row := DB.Exec("select id from users where id = ?", uid).RowsAffected
|
|
|
|
if row == 0 {
|
|
|
|
// 新用戶
|
|
|
|
if err := DB.Create(usr).Error; err != nil {
|
|
|
|
log.Error().Err(err).Msg("Follow error.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug().Str("user", uid).Msg("New user.")
|
|
|
|
} else {
|
|
|
|
// 舊用戶
|
|
|
|
log.Debug().Str("user", uid).Msg("user unblocked.")
|
|
|
|
}
|
|
|
|
|
2022-01-04 05:01:42 +00:00
|
|
|
// Welcome and ask for profile
|
2022-01-03 14:50:49 +00:00
|
|
|
msg := linebot.NewTextMessage("歡迎使用台新人壽智慧聊天服務! 請您協助核對您的身分。")
|
|
|
|
|
2022-01-04 05:01:42 +00:00
|
|
|
// get profiles
|
|
|
|
var profs []Profile
|
|
|
|
DB.Limit(10).Find(&profs)
|
|
|
|
columns := make([]*linebot.CarouselColumn, len(profs))
|
|
|
|
for i, prof := range profs {
|
|
|
|
columns[i] = linebot.NewCarouselColumn(
|
|
|
|
prof.Img, prof.Name, prof.Brief,
|
|
|
|
linebot.NewPostbackAction("我是"+prof.Name, "select="+prof.Name, "我是"+prof.Name, ""),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
template := linebot.NewCarouselTemplate(columns...)
|
|
|
|
profiles := linebot.NewTemplateMessage("請核對您的身分", template)
|
2022-01-03 14:50:49 +00:00
|
|
|
|
|
|
|
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) {
|
2022-01-04 07:09:46 +00:00
|
|
|
time.Sleep(time.Second * 3)
|
2022-01-03 14:50:49 +00:00
|
|
|
if _, err := SendMessage("", uid, DefaultMsg()); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}(event.Source.UserID)
|
2022-01-02 16:47:25 +00:00
|
|
|
}
|