master
Evan Chen 2022-01-03 00:47:25 +08:00
parent bf27136a4d
commit 6d9d87c00b
3 changed files with 66 additions and 47 deletions

55
api_join.go Normal file
View File

@ -0,0 +1,55 @@
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
}

View File

@ -26,7 +26,7 @@ for PLATFORM in $PLATFORMS; do
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 -db-name talentdev > talent.log" ssh root@bot.ework.tw screen -d -m bash -c './talent -dev -db-name talentdev | tee talent.log'
done done
if [[ "${FAILURES}" != "" ]]; then if [[ "${FAILURES}" != "" ]]; then

View File

@ -34,56 +34,20 @@ func webhook(c *gin.Context) {
if event.Type == linebot.EventTypeFollow { if event.Type == linebot.EventTypeFollow {
Follow(event) Follow(event)
} }
if event.Type == linebot.EventTypeMessage {
controller(event)
}
} }
OK(c, "ok") OK(c, "ok")
} }
func Follow(event *linebot.Event) { func controller(event *linebot.Event) {
// add user switch event.Source.Ty {
uid := event.Source.UserID case "":
usr := &User{
ID: uid,
}
Hit("加入", uid)
reply := event.ReplyToken default:
// route to watson
row := DB.Exec("select id from users where id = ?", uid).RowsAffected log.Error().Caller().Msg("NOT IMPLEMENTED")
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
}