diff --git a/model/facebook_page.go b/model/facebook_page.go index 030f10c..0cece42 100644 --- a/model/facebook_page.go +++ b/model/facebook_page.go @@ -26,6 +26,22 @@ func GetAllFacebookPage() (pages []*FacebookPage, err error) { return } +// GetFacebookPage - +func GetFacebookPage(id string) (page *FacebookPage, err error) { + err = x.Get(&page, `select * from "public"."facebook_page" where "id" = $1`, id) + return +} + +// AddPage - +func (p *FacebookPage) AddPage() (err error) { + rows, err := x.NamedQuery(`insert into "public"."facebook_page" ("id", "lastpost") values (:id, :lastpost) returning *`, p) + if err != nil { + return err + } + err = rows.StructScan(&p) + return +} + // UpdatePost - func (p *FacebookPage) UpdatePost(postID string) (err error) { query := `update "public"."facebook_page" set "lastpost" = $1 where id = $2` diff --git a/model/key_commands.go b/model/key_commands.go index 1064114..c2fdadd 100644 --- a/model/key_commands.go +++ b/model/key_commands.go @@ -15,8 +15,8 @@ type KeyCommands struct { Mtime time.Time `db:"mtime" cc:"ctime"` } -// GetKeyCommand - -func GetKeyCommand(c, g string) (cmd *KeyCommands, err error) { +// GetGroupKeyCommand - +func GetGroupKeyCommand(c, g string) (cmd *KeyCommands, err error) { if len(c) == 0 { return nil, errors.New("command is empty") } diff --git a/model/line_group.go b/model/line_group.go index 1cdf240..67e3d55 100644 --- a/model/line_group.go +++ b/model/line_group.go @@ -24,3 +24,37 @@ func CheckGroup(g string) (exists bool, err error) { } return ss.C > 0, nil } + +// CheckGroupOwner - +func CheckGroupOwner(user, g string) (exists bool, err error) { + ss := struct { + C int `db:"c"` + }{} + + err = x.Get(&ss, `select count(*) as c from "public"."line_group" where "id" = $1 and "owner" = $2`, g, user) + if err != nil { + return false, err + } + return ss.C > 0, nil +} + +// GetLineGroup - +func GetLineGroup(id string) (g *LineGroup, err error) { + err = x.Get(&g, `select * from "public"."line_group" where "id" = $1`, id) + return +} + +// AddLineGroup - +func AddLineGroup(name, owner string, notify bool) (g *LineGroup, err error) { + err = x.Get(&g, `insert into "public"."line_group" ("name", "owner", "notify") values ($1, $2, $3)`, name, owner, notify) + if err != nil { + return nil, err + } + return +} + +// DeleteGroup - +func (p *LineGroup) DeleteGroup() (err error) { + _, err = x.Exec(`delete from "public"."line_group" where "id" = $1`, p.ID) + return +} diff --git a/model/rt_table.go b/model/rt_table.go index b1f1b22..d41f857 100644 --- a/model/rt_table.go +++ b/model/rt_table.go @@ -18,3 +18,21 @@ type LineYoutubeRT struct { Youtube string `db:"youtube" cc:"youtube"` Tmpl string `db:"tmpl" cc:"tmpl"` } + +// AddRT - add facebook line rt +func (p *LineFacebookRT) AddRT() (err error) { + _, err = x.NamedExec(`insert into "public"."line_fb_rt" ("line", "facebook", "tmpl") values (:line, :facebook, :tmpl)`, p) + return +} + +// AddRT - add twitch line rt +func (p *LineTwitchRT) AddRT() (err error) { + _, err = x.NamedExec(`insert into "public"."line_twitch_rt" ("line", "twitch", "type", "tmpl") values (:line, :twitch, :type, :tmpl)`, p) + return +} + +// AddRT - add youtube line rt +func (p *LineYoutubeRT) AddRT() (err error) { + _, err = x.NamedExec(`insert into "public"."line_youtube_rt" ("line", "youtube", "tmpl") values (:line, :youtube, :tmpl)`, p) + return +} diff --git a/model/twitch_channel.go b/model/twitch_channel.go index 81a1bb2..2fc14f5 100644 --- a/model/twitch_channel.go +++ b/model/twitch_channel.go @@ -32,6 +32,16 @@ func GetJoinChatChannel() (channels []*TwitchChannel, err error) { return } +// Add - +func (p *TwitchChannel) Add() (err error) { + rows, err := x.NamedQuery(`insert into "public"."twitch_channel" ("name", "laststream", "join", "opayid") values (:name, :laststream, :join, :opayid) returning *`, p) + if err != nil { + return err + } + err = rows.StructScan(p) + return +} + // UpdateStream - func (p *TwitchChannel) UpdateStream(streamID string) (err error) { query := `update "public"."twitch_channel" set "laststream" = $1 where "id" = $2` diff --git a/module/line-message/message-type.go b/module/line-message/message-type.go index dd62aaf..a21d085 100644 --- a/module/line-message/message-type.go +++ b/module/line-message/message-type.go @@ -2,6 +2,7 @@ package linemsg import ( lineobj "git.trj.tw/golang/mtfosbot/module/line-message/line-object" + msgcmd "git.trj.tw/golang/mtfosbot/module/message-command" ) func messageType(e *lineobj.EventObject) { @@ -31,8 +32,8 @@ func textMsg(e *lineobj.EventObject) { // group action if e.Source.Type == "group" { - if _, ok := mtxt.(string); ok { - + if txt, ok := mtxt.(string); ok { + msgcmd.ParseLineMsg(txt, e.ReplyToken, e.Source) } } return diff --git a/module/message-command/line-group.go b/module/message-command/line-group.go new file mode 100644 index 0000000..3d3b829 --- /dev/null +++ b/module/message-command/line-group.go @@ -0,0 +1,148 @@ +package msgcmd + +import ( + "strconv" + "strings" + + "git.trj.tw/golang/mtfosbot/module/apis/twitch" + + "git.trj.tw/golang/mtfosbot/model" + lineobj "git.trj.tw/golang/mtfosbot/module/line-message/line-object" +) + +func selectAct(cmd, sub, txt string, s *lineobj.SourceObject) (res string) { + switch cmd { + case "addgroup": + return addLineGroup(sub, txt, s) + case "addpage": + return addFacebookPage(sub, txt, s) + case "addtwitch": + return addTwitchChannel(sub, txt, s) + } + return +} + +func addLineGroup(sub, txt string, s *lineobj.SourceObject) (res string) { + // args = groupName notify + exists, err := model.CheckGroup(s.GroupID) + if err != nil { + return "run check group error" + } + if exists { + return "group exists" + } + args := strings.Split(strings.Trim(txt, " "), " ") + if len(args) < 2 { + return "command args not match" + } + i, err := strconv.ParseInt(args[1], 10, 8) + if err != nil || i < 0 || i > 1 { + return "notify plases input 1 or 0" + } + + _, err = model.AddLineGroup(args[0], s.UserID, i == 1) + if err != nil { + return "add group fail" + } + + return "Success" +} + +func addFacebookPage(sub, txt string, s *lineobj.SourceObject) (res string) { + // args = pageid tmpl + exists, err := model.CheckGroup(s.GroupID) + if err != nil { + return "run check group error" + } + if !exists { + return "group not exists" + } + ok, err := model.CheckGroupOwner(s.UserID, s.GroupID) + if err != nil { + return "run check group owner fail" + } + if !ok { + return "not owner" + } + + args := strings.Split(strings.Trim(txt, " "), " ") + if len(args) < 2 { + return "command args not match" + } + + page, err := model.GetFacebookPage(args[0]) + if err != nil { + return "check facebook page fail" + } + if page == nil { + page = &model.FacebookPage{ + ID: args[0], + } + err = page.AddPage() + if err != nil { + return "add facebook page fail" + } + } + + rt := &model.LineFacebookRT{ + Line: s.GroupID, + Facebook: args[0], + Tmpl: strings.Join(args[1:], " "), + } + + err = rt.AddRT() + if err != nil { + return "add facebook page fail" + } + + return "Success" +} + +func addTwitchChannel(sub, txt string, s *lineobj.SourceObject) (res string) { + // args = twitchLogin type tmpl + exists, err := model.CheckGroup(s.GroupID) + if err != nil { + return "run check group error" + } + if !exists { + return "group not exists" + } + ok, err := model.CheckGroupOwner(s.UserID, s.GroupID) + if err != nil { + return "run check group owner fail" + } + if !ok { + return "not owner" + } + + args := strings.Split(strings.Trim(txt, " "), " ") + if len(args) < 3 { + return "command args not match" + } + + info := twitch.GetUserDataByName(args[0]) + if info == nil { + return "get twitch user id fail" + } + + ch := &model.TwitchChannel{ + ID: info.ID, + Name: info.DisplayName, + } + err = ch.Add() + if err != nil { + return "add twitch channel fail" + } + + rt := &model.LineTwitchRT{ + Line: s.GroupID, + Twitch: info.ID, + Tmpl: strings.Join(args[2:], " "), + } + err = rt.AddRT() + if err != nil { + return "add rt data fail" + } + + return "Success" +} diff --git a/module/message-command/message-command.go b/module/message-command/message-command.go new file mode 100644 index 0000000..fb373fe --- /dev/null +++ b/module/message-command/message-command.go @@ -0,0 +1,102 @@ +package msgcmd + +import ( + "regexp" + "strings" + + "git.trj.tw/golang/mtfosbot/model" + + "git.trj.tw/golang/mtfosbot/module/apis/line" + lineobj "git.trj.tw/golang/mtfosbot/module/line-message/line-object" +) + +func parseCMD(in string) (c [][]string) { + re, err := regexp.Compile("{{(.+?)}}") + if err != nil { + return + } + + c = re.FindAllStringSubmatch(in, -1) + + return +} + +// ParseLineMsg - +func ParseLineMsg(txt, replyToken string, source *lineobj.SourceObject) { + if source.Type != "group" { + return + } + strs := strings.Split(strings.Trim(txt, " "), " ") + // skip empty string + if len(strs[0]) == 0 { + return + } + + if strings.HasPrefix(strs[0], "!") || strings.HasPrefix(strs[0], "!") { + // nor cmd + cmd := strs[0][1:] + if len(cmd) == 0 { + return + } + c, err := model.GetGroupCommand(cmd, source.GroupID) + if err != nil || c == nil { + return + } + + str := runCMD(strings.Join(strs[1:], " "), c.Message, source) + m := parseResult(str) + line.ReplyMessage(replyToken, m) + + } else { + // key cmd + c, err := model.GetGroupKeyCommand(strs[0], source.GroupID) + if err != nil || c == nil { + return + } + + str := runCMD(strings.Join(strs[1:], " "), c.Message, source) + m := parseResult(str) + line.ReplyMessage(replyToken, m) + + } +} + +func parseResult(str string) interface{} { + var m interface{} + + if strings.HasPrefix(str, "$image$") { + str = strings.Replace(str, "$image$", "", 1) + strs := strings.Split(str, ";") + m = &line.ImageMessage{ + OriginalContentURL: strs[0], + PreviewImageURL: strs[1], + } + } else { + m = &line.TextMessage{ + Text: str, + } + } + + return m +} + +func runCMD(txt, c string, s *lineobj.SourceObject) (res string) { + cmdAct := parseCMD(c) + if len(cmdAct) == 0 { + return c + } + res = c + for _, v := range cmdAct { + if len(v) > 1 { + // run cmd + m := strings.Split(v[1], "=") + sub := "" + if len(m) > 1 { + sub = strings.Join(m[1:], " ") + } + cmdRes := selectAct(m[0], sub, txt, s) + res = strings.Replace(res, v[1], cmdRes, 1) + } + } + return +} diff --git a/module/twitch-irc/twitch-irc.go b/module/twitch-irc/twitch-irc.go index 8db255e..3b8f83c 100644 --- a/module/twitch-irc/twitch-irc.go +++ b/module/twitch-irc/twitch-irc.go @@ -73,13 +73,6 @@ func JoinChannel(ch string) { }, } queue.Add(m) - - // msg := &irc.Message{} - // msg.Command = "JOIN" - // msg.Params = []string{ - // fmt.Sprintf("#%s", ch), - // } - // client.WriteMessage(msg) } // LeaveChannel -