talent/api_join.go

56 lines
1.2 KiB
Go

package main
import (
"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)
reply := event.ReplyToken
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.")
msg := linebot.NewTextMessage("你好!")
if reply != "" {
if _, err := bot.ReplyMessage(reply, msg).Do(); err != nil {
log.Error().Err(err).Caller().Msg("reply error")
}
} else {
if _, err := bot.PushMessage(uid, msg).Do(); err != nil {
log.Error().Err(err).Caller().Msg("push error")
}
}
} else {
// 舊用戶
log.Debug().Str("user", uid).Msg("user unblocked.")
msg := linebot.NewTextMessage("歡迎回來!")
if reply != "" {
if _, err := bot.ReplyMessage(reply, msg).Do(); err != nil {
log.Error().Err(err).Caller().Msg("reply error")
}
} else {
if _, err := bot.PushMessage(uid, msg).Do(); err != nil {
log.Error().Err(err).Caller().Msg("push error")
}
}
}
// Welcome and ask for
}