add pre group one bot setting
This commit is contained in:
+12
-14
@@ -13,7 +13,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.trj.tw/golang/mtfosbot/module/apis"
|
||||
"git.trj.tw/golang/mtfosbot/module/config"
|
||||
)
|
||||
|
||||
// TextMessage - line text message object
|
||||
@@ -66,11 +65,10 @@ func getURL(p string) (string, bool) {
|
||||
return str, true
|
||||
}
|
||||
|
||||
func getHeaders() map[string]string {
|
||||
func getHeaders(token string) map[string]string {
|
||||
m := make(map[string]string)
|
||||
conf := config.GetConf()
|
||||
m["Content-Type"] = "application/json"
|
||||
m["Authorization"] = fmt.Sprintf("Bearer %s", conf.Line.Access)
|
||||
m["Authorization"] = fmt.Sprintf("Bearer %s", token)
|
||||
return m
|
||||
}
|
||||
|
||||
@@ -104,7 +102,7 @@ func checkMessageObject(m interface{}) interface{} {
|
||||
}
|
||||
|
||||
// PushMessage -
|
||||
func PushMessage(target string, message ...interface{}) {
|
||||
func PushMessage(accessToken, target string, message ...interface{}) {
|
||||
log.Println("push target :::: ", target)
|
||||
if len(target) == 0 || len(message) == 0 {
|
||||
return
|
||||
@@ -145,7 +143,7 @@ func PushMessage(target string, message ...interface{}) {
|
||||
reqObj := apis.RequestObj{
|
||||
Method: "POST",
|
||||
URL: apiURL,
|
||||
Headers: getHeaders(),
|
||||
Headers: getHeaders(accessToken),
|
||||
Body: byteReader,
|
||||
}
|
||||
|
||||
@@ -163,7 +161,7 @@ func PushMessage(target string, message ...interface{}) {
|
||||
}
|
||||
|
||||
// ReplyMessage -
|
||||
func ReplyMessage(replyToken string, message ...interface{}) {
|
||||
func ReplyMessage(accessToken, replyToken string, message ...interface{}) {
|
||||
if len(replyToken) == 0 || len(message) == 0 {
|
||||
return
|
||||
}
|
||||
@@ -201,7 +199,7 @@ func ReplyMessage(replyToken string, message ...interface{}) {
|
||||
reqObj := apis.RequestObj{
|
||||
Method: "POST",
|
||||
URL: apiURL,
|
||||
Headers: getHeaders(),
|
||||
Headers: getHeaders(accessToken),
|
||||
Body: byteReader,
|
||||
}
|
||||
|
||||
@@ -217,9 +215,9 @@ func ReplyMessage(replyToken string, message ...interface{}) {
|
||||
}
|
||||
|
||||
// GetUserInfo -
|
||||
func GetUserInfo(u, g string) (user *LineUserInfo, err error) {
|
||||
func GetUserInfo(accessToken, u, g string) (user *LineUserInfo, err error) {
|
||||
urlPath := fmt.Sprintf("/v2/bot/group/%s/member/%s", g, u)
|
||||
header := getHeaders()
|
||||
header := getHeaders(accessToken)
|
||||
apiURL, ok := getURL(urlPath)
|
||||
if !ok {
|
||||
return nil, errors.New("url parser fail")
|
||||
@@ -263,9 +261,9 @@ func GetUserInfo(u, g string) (user *LineUserInfo, err error) {
|
||||
}
|
||||
|
||||
// GetContentHead -
|
||||
func GetContentHead(id string) (mime string, err error) {
|
||||
func GetContentHead(accessToken, id string) (mime string, err error) {
|
||||
urlPath := fmt.Sprintf("/v2/bot/message/%s/content", id)
|
||||
header := getHeaders()
|
||||
header := getHeaders(accessToken)
|
||||
u, ok := getURL(urlPath)
|
||||
if !ok {
|
||||
return "", errors.New("get url fail")
|
||||
@@ -294,9 +292,9 @@ func GetContentHead(id string) (mime string, err error) {
|
||||
}
|
||||
|
||||
// DownloadContent -
|
||||
func DownloadContent(id string, w io.Writer) (err error) {
|
||||
func DownloadContent(accessToken, id string, w io.Writer) (err error) {
|
||||
urlPath := fmt.Sprintf("/v2/bot/message/%s/content", id)
|
||||
header := getHeaders()
|
||||
header := getHeaders(accessToken)
|
||||
u, ok := getURL(urlPath)
|
||||
if !ok {
|
||||
return errors.New("get url fail")
|
||||
|
||||
@@ -2,6 +2,7 @@ package background
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"sort"
|
||||
@@ -138,6 +139,11 @@ func getPageHTML(page *model.FacebookPage) {
|
||||
|
||||
for _, v := range page.Groups {
|
||||
if v.Notify {
|
||||
bot, err := v.GetBot()
|
||||
if err != nil || bot == nil {
|
||||
log.Println("get group binding bot fail :: ", err)
|
||||
continue
|
||||
}
|
||||
tmpl := v.Tmpl
|
||||
if len(tmpl) > 0 {
|
||||
tmpl = strings.Replace(tmpl, "{link}", lastData.Link, -1)
|
||||
@@ -148,7 +154,7 @@ func getPageHTML(page *model.FacebookPage) {
|
||||
msg := line.TextMessage{
|
||||
Text: tmpl,
|
||||
}
|
||||
line.PushMessage(v.ID, msg)
|
||||
line.PushMessage(bot.AccessToken, v.ID, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package background
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -61,6 +62,11 @@ func checkStream(ch *model.TwitchChannel, info *twitch.StreamInfo) {
|
||||
link := fmt.Sprintf("https://twitch.tv/%s", ch.Name)
|
||||
for _, v := range ch.Groups {
|
||||
if v.Notify {
|
||||
bot, err := v.GetBot()
|
||||
if err != nil || bot == nil {
|
||||
log.Println("get group binding bot fail :: ", err)
|
||||
continue
|
||||
}
|
||||
tmpl := v.Tmpl
|
||||
if len(tmpl) > 0 {
|
||||
tmpl = strings.Replace(tmpl, "{txt}", info.Title, -1)
|
||||
@@ -71,7 +77,7 @@ func checkStream(ch *model.TwitchChannel, info *twitch.StreamInfo) {
|
||||
msg := line.TextMessage{
|
||||
Text: tmpl,
|
||||
}
|
||||
line.PushMessage(v.ID, msg)
|
||||
line.PushMessage(bot.AccessToken, v.ID, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,11 +7,13 @@ import (
|
||||
)
|
||||
|
||||
// MessageEvent -
|
||||
func MessageEvent(e *lineobj.EventObject) {
|
||||
|
||||
func MessageEvent(botid string, e *lineobj.EventObject) {
|
||||
if len(botid) == 0 {
|
||||
return
|
||||
}
|
||||
switch e.Type {
|
||||
case "message":
|
||||
messageType(e)
|
||||
messageType(botid, e)
|
||||
break
|
||||
default:
|
||||
fmt.Println("line webhook type not match")
|
||||
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
msgcmd "git.trj.tw/golang/mtfosbot/module/message-command"
|
||||
)
|
||||
|
||||
func messageType(e *lineobj.EventObject) {
|
||||
func messageType(botid string, e *lineobj.EventObject) {
|
||||
msg := e.Message
|
||||
mtype, ok := msg["type"]
|
||||
if !ok {
|
||||
@@ -26,17 +26,17 @@ func messageType(e *lineobj.EventObject) {
|
||||
if t, ok := mtype.(string); ok {
|
||||
switch t {
|
||||
case "text":
|
||||
textMsg(e)
|
||||
textMsg(botid, e)
|
||||
break
|
||||
case "image":
|
||||
imageMsg(e)
|
||||
imageMsg(botid, e)
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func textMsg(e *lineobj.EventObject) {
|
||||
func textMsg(botid string, e *lineobj.EventObject) {
|
||||
msg := e.Message
|
||||
mtxt, ok := msg["text"]
|
||||
if !ok {
|
||||
@@ -46,14 +46,14 @@ func textMsg(e *lineobj.EventObject) {
|
||||
// group action
|
||||
if e.Source.Type == "group" {
|
||||
if txt, ok := mtxt.(string); ok {
|
||||
msgcmd.ParseLineMsg(txt, e.ReplyToken, e.Source)
|
||||
saveTextMsgToLog(txt, e.Source)
|
||||
msgcmd.ParseLineMsg(botid, txt, e.ReplyToken, e.Source)
|
||||
saveTextMsgToLog(botid, txt, e.Source)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func imageMsg(e *lineobj.EventObject) {
|
||||
func imageMsg(botid string, e *lineobj.EventObject) {
|
||||
msg := e.Message
|
||||
imgID, ok := msg["id"]
|
||||
if !ok {
|
||||
@@ -62,19 +62,19 @@ func imageMsg(e *lineobj.EventObject) {
|
||||
// group action
|
||||
if e.Source.Type == "group" {
|
||||
if id, ok := imgID.(string); ok {
|
||||
saveImageMsgToLog(id, e.Source)
|
||||
saveImageMsgToLog(botid, id, e.Source)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getSourceUser(uid, gid string) (u *model.LineUser, err error) {
|
||||
func getSourceUser(accessToken, uid, gid string) (u *model.LineUser, err error) {
|
||||
userData, err := model.GetLineUserByID(uid)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if userData == nil {
|
||||
tmpu, err := line.GetUserInfo(uid, gid)
|
||||
tmpu, err := line.GetUserInfo(accessToken, uid, gid)
|
||||
if err != nil || tmpu == nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -87,7 +87,7 @@ func getSourceUser(uid, gid string) (u *model.LineUser, err error) {
|
||||
}
|
||||
} else {
|
||||
if userData.Mtime.Unix() < (time.Now().Unix() - 86400) {
|
||||
tmpu, err := line.GetUserInfo(uid, gid)
|
||||
tmpu, err := line.GetUserInfo(accessToken, uid, gid)
|
||||
if err != nil || tmpu == nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -102,8 +102,13 @@ func getSourceUser(uid, gid string) (u *model.LineUser, err error) {
|
||||
return userData, nil
|
||||
}
|
||||
|
||||
func saveTextMsgToLog(txt string, s *lineobj.SourceObject) {
|
||||
u, err := getSourceUser(s.UserID, s.GroupID)
|
||||
func saveTextMsgToLog(botid, txt string, s *lineobj.SourceObject) {
|
||||
bot, err := model.GetBotInfo(botid)
|
||||
if err != nil || bot == nil {
|
||||
fmt.Println("get bot info fail :: ", err)
|
||||
return
|
||||
}
|
||||
u, err := getSourceUser(bot.AccessToken, s.UserID, s.GroupID)
|
||||
if err != nil || u == nil {
|
||||
return
|
||||
}
|
||||
@@ -112,13 +117,18 @@ func saveTextMsgToLog(txt string, s *lineobj.SourceObject) {
|
||||
model.AddLineMessageLog(s.GroupID, s.UserID, txt, "text")
|
||||
}
|
||||
|
||||
func saveImageMsgToLog(id string, s *lineobj.SourceObject) {
|
||||
u, err := getSourceUser(s.UserID, s.GroupID)
|
||||
func saveImageMsgToLog(botid, id string, s *lineobj.SourceObject) {
|
||||
bot, err := model.GetBotInfo(botid)
|
||||
if err != nil || bot == nil {
|
||||
fmt.Println("get bot info fail :: ", err)
|
||||
return
|
||||
}
|
||||
u, err := getSourceUser(bot.AccessToken, s.UserID, s.GroupID)
|
||||
if err != nil || u == nil {
|
||||
return
|
||||
}
|
||||
|
||||
mime, err := line.GetContentHead(id)
|
||||
mime, err := line.GetContentHead(bot.AccessToken, id)
|
||||
if err != nil || len(mime) == 0 {
|
||||
return
|
||||
}
|
||||
@@ -150,7 +160,7 @@ func saveImageMsgToLog(id string, s *lineobj.SourceObject) {
|
||||
}
|
||||
defer w.Close()
|
||||
|
||||
err = line.DownloadContent(id, w)
|
||||
err = line.DownloadContent(bot.AccessToken, id, w)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package msgcmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
@@ -22,10 +23,15 @@ func parseCMD(in string) (c [][]string) {
|
||||
}
|
||||
|
||||
// ParseLineMsg -
|
||||
func ParseLineMsg(txt, replyToken string, source *lineobj.SourceObject) {
|
||||
func ParseLineMsg(botid, txt, replyToken string, source *lineobj.SourceObject) {
|
||||
if source.Type != "group" {
|
||||
return
|
||||
}
|
||||
bot, err := model.GetBotInfo(botid)
|
||||
if err != nil || bot == nil {
|
||||
fmt.Println("get bot err or no bot :: ", err)
|
||||
return
|
||||
}
|
||||
strs := strings.Split(strings.Trim(txt, " "), " ")
|
||||
|
||||
// skip empty string
|
||||
@@ -57,7 +63,7 @@ func ParseLineMsg(txt, replyToken string, source *lineobj.SourceObject) {
|
||||
m := parseResult(v)
|
||||
msgs = append(msgs, m)
|
||||
}
|
||||
line.ReplyMessage(replyToken, msgs...)
|
||||
line.ReplyMessage(bot.AccessToken, replyToken, msgs...)
|
||||
|
||||
} else {
|
||||
// key cmd
|
||||
@@ -72,7 +78,8 @@ func ParseLineMsg(txt, replyToken string, source *lineobj.SourceObject) {
|
||||
m := parseResult(v)
|
||||
msgs = append(msgs, m)
|
||||
}
|
||||
line.ReplyMessage(replyToken, msgs...)
|
||||
|
||||
line.ReplyMessage(bot.AccessToken, replyToken, msgs...)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user