Add TmplContext struct with reply information
This commit is contained in:
parent
684d1ae676
commit
85eb337e3b
@ -77,7 +77,6 @@ func compileReplies(replies []Reply) error {
|
|||||||
|
|
||||||
tmpl, err := template.
|
tmpl, err := template.
|
||||||
New(strconv.Itoa(i)).
|
New(strconv.Itoa(i)).
|
||||||
Funcs(tmplFuncs).
|
|
||||||
Funcs(sprig.TxtFuncMap()).
|
Funcs(sprig.TxtFuncMap()).
|
||||||
Parse(reply.Msg)
|
Parse(reply.Msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -9,4 +9,4 @@ password = "ExamplePassword123"
|
|||||||
# after "!!BOT_TEST"
|
# after "!!BOT_TEST"
|
||||||
[[reply]]
|
[[reply]]
|
||||||
regex = "!!BOT_TEST (.*)"
|
regex = "!!BOT_TEST (.*)"
|
||||||
msg = "{{match . 0 1}}"
|
msg = "{{.Match 0 1}}"
|
69
main.go
69
main.go
@ -7,6 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
"github.com/vmihailenco/msgpack/v5"
|
"github.com/vmihailenco/msgpack/v5"
|
||||||
@ -15,6 +16,44 @@ import (
|
|||||||
"go.arsenm.dev/logger/log"
|
"go.arsenm.dev/logger/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type itemType uint8
|
||||||
|
|
||||||
|
const (
|
||||||
|
comment itemType = iota
|
||||||
|
post
|
||||||
|
)
|
||||||
|
|
||||||
|
type item struct {
|
||||||
|
Type itemType
|
||||||
|
ID int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (it itemType) String() string {
|
||||||
|
switch it {
|
||||||
|
case comment:
|
||||||
|
return "comment"
|
||||||
|
case post:
|
||||||
|
return "post"
|
||||||
|
default:
|
||||||
|
return "<unknown>"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Submatches []string
|
||||||
|
|
||||||
|
func (sm Submatches) Item(i int) string {
|
||||||
|
return sm[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
type TmplContext struct {
|
||||||
|
Matches []Submatches
|
||||||
|
Type itemType
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tc TmplContext) Match(i, j int) string {
|
||||||
|
return tc.Matches[i][j]
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
configPath := pflag.StringP("config", "c", "./lemmy-reply-bot.toml", "Path to the config file")
|
configPath := pflag.StringP("config", "c", "./lemmy-reply-bot.toml", "Path to the config file")
|
||||||
dryRun := pflag.BoolP("dry-run", "D", false, "Don't actually send comments, just check for matches")
|
dryRun := pflag.BoolP("dry-run", "D", false, "Don't actually send comments, just check for matches")
|
||||||
@ -60,18 +99,6 @@ func main() {
|
|||||||
commentWorker(ctx, c, replyCh)
|
commentWorker(ctx, c, replyCh)
|
||||||
}
|
}
|
||||||
|
|
||||||
type itemType uint8
|
|
||||||
|
|
||||||
const (
|
|
||||||
comment itemType = iota
|
|
||||||
post
|
|
||||||
)
|
|
||||||
|
|
||||||
type item struct {
|
|
||||||
Type itemType
|
|
||||||
ID int
|
|
||||||
}
|
|
||||||
|
|
||||||
func commentWorker(ctx context.Context, c *lemmy.WSClient, replyCh chan<- replyJob) {
|
func commentWorker(ctx context.Context, c *lemmy.WSClient, replyCh chan<- replyJob) {
|
||||||
repliedIDs := map[item]struct{}{}
|
repliedIDs := map[item]struct{}{}
|
||||||
|
|
||||||
@ -116,7 +143,10 @@ func commentWorker(ctx context.Context, c *lemmy.WSClient, replyCh chan<- replyJ
|
|||||||
}
|
}
|
||||||
|
|
||||||
matches := re.FindAllStringSubmatch(cr.CommentView.Comment.Content, -1)
|
matches := re.FindAllStringSubmatch(cr.CommentView.Comment.Content, -1)
|
||||||
job.Content, err = executeTmpl(compiledTmpls[reply.Regex], matches)
|
job.Content, err = executeTmpl(compiledTmpls[reply.Regex], TmplContext{
|
||||||
|
Matches: toSubmatches(matches),
|
||||||
|
Type: comment,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Error while executing template").Err(err).Send()
|
log.Warn("Error while executing template").Err(err).Send()
|
||||||
continue
|
continue
|
||||||
@ -153,7 +183,10 @@ func commentWorker(ctx context.Context, c *lemmy.WSClient, replyCh chan<- replyJ
|
|||||||
job := replyJob{PostID: pr.PostView.Post.ID}
|
job := replyJob{PostID: pr.PostView.Post.ID}
|
||||||
|
|
||||||
matches := re.FindAllStringSubmatch(body, -1)
|
matches := re.FindAllStringSubmatch(body, -1)
|
||||||
job.Content, err = executeTmpl(compiledTmpls[reply.Regex], matches)
|
job.Content, err = executeTmpl(compiledTmpls[reply.Regex], TmplContext{
|
||||||
|
Matches: toSubmatches(matches),
|
||||||
|
Type: post,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Error while executing template").Err(err).Send()
|
log.Warn("Error while executing template").Err(err).Send()
|
||||||
continue
|
continue
|
||||||
@ -213,9 +246,9 @@ func commentReplyWorker(ctx context.Context, c *lemmy.WSClient, ch <-chan replyJ
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func executeTmpl(tmpl *template.Template, matches [][]string) (string, error) {
|
func executeTmpl(tmpl *template.Template, tc TmplContext) (string, error) {
|
||||||
sb := &strings.Builder{}
|
sb := &strings.Builder{}
|
||||||
err := tmpl.Execute(sb, matches)
|
err := tmpl.Execute(sb, tc)
|
||||||
return sb.String(), err
|
return sb.String(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -232,3 +265,7 @@ func joinAll(c *lemmy.WSClient) {
|
|||||||
log.Fatal("Error joining WebSocket community context").Err(err).Send()
|
log.Fatal("Error joining WebSocket community context").Err(err).Send()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func toSubmatches(s [][]string) []Submatches {
|
||||||
|
return *(*[]Submatches)(unsafe.Pointer(&s))
|
||||||
|
}
|
||||||
|
24
tmpl.go
24
tmpl.go
@ -1,24 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import "text/template"
|
|
||||||
|
|
||||||
var tmplFuncs = template.FuncMap{
|
|
||||||
"match": func(matches [][]string, i int, j int) string {
|
|
||||||
if len(matches) <= i {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(matches[i]) <= j {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return matches[i][j]
|
|
||||||
},
|
|
||||||
"item": func(items []string, i int) string {
|
|
||||||
if len(items) <= i {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return items[i]
|
|
||||||
},
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user