talent/api_join.go

81 lines
2.1 KiB
Go
Raw Normal View History

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.")
}
// Welcome and ask for
2022-01-03 14:50:49 +00:00
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)
2022-01-02 16:47:25 +00:00
}