Reworked KarmaTriggers, docTopic filter
This commit is contained in:
parent
149644173a
commit
e64db6f6d7
28
filters.go
28
filters.go
|
@ -1,19 +1,41 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
neco "git.nefrace.ru/nefrace/nechotron"
|
||||
)
|
||||
|
||||
func karmaTriggers(u *neco.Update) bool {
|
||||
good, bad := GetTriggers()
|
||||
all := append(good, bad...)
|
||||
triggers := GetTriggers()
|
||||
|
||||
text := u.Text()
|
||||
for _, t := range all {
|
||||
for t, v := range triggers {
|
||||
if strings.HasPrefix(text, t) {
|
||||
u.Ctx = context.WithValue(u.Ctx, neco.FilteredValue("karmaTrigger"), t)
|
||||
u.Ctx = context.WithValue(u.Ctx, neco.FilteredValue("karmaValue"), v)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
var docRegex = regexp.MustCompile(`док(ументац[а-я]+|[а-я])? ((п)?о )?(?P<topic>@?[\w\d\s]{1,32})`)
|
||||
|
||||
func docRequest(u *neco.Update) bool {
|
||||
text := u.Text()
|
||||
matches := docRegex.FindStringSubmatch(text)
|
||||
if len(matches) == 0 {
|
||||
return false
|
||||
}
|
||||
result := make(map[string]string)
|
||||
for i, name := range docRegex.SubexpNames() {
|
||||
if i != 0 && name != "" {
|
||||
result[name] = matches[i]
|
||||
}
|
||||
}
|
||||
u.Ctx = context.WithValue(u.Ctx, neco.FilteredValue("docTopic"), result["topic"])
|
||||
return true
|
||||
}
|
||||
|
|
16
handlers.go
16
handlers.go
|
@ -9,6 +9,11 @@ import (
|
|||
"github.com/NicoNex/echotron/v3"
|
||||
)
|
||||
|
||||
func handleDocRequest(u *nechotron.Update) error {
|
||||
topic := u.Ctx.Value(nechotron.FilteredValue("docTopic")).(string)
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleKarma(u *nechotron.Update) error {
|
||||
from, _ := u.Ctx.Value("userfrom").(*User)
|
||||
to, _ := u.Ctx.Value("userto").(*User)
|
||||
|
@ -23,15 +28,8 @@ func handleKarma(u *nechotron.Update) error {
|
|||
}()
|
||||
return err
|
||||
}
|
||||
good, bad := GetTriggers()
|
||||
value := 0
|
||||
text := u.Text()
|
||||
if StringHasPrefix(text, good...) {
|
||||
value = 1
|
||||
}
|
||||
if StringHasPrefix(text, bad...) {
|
||||
value = -1
|
||||
}
|
||||
value := u.Ctx.Value(nechotron.FilteredValue("karmaValue")).(int)
|
||||
// trigger := u.Ctx.Value(nechotron.FilteredValue("karmaTrigger")).(string)
|
||||
store := tongo.NewStore[KarmaShot](db)
|
||||
fromKarma, _ := store.Count(u.Ctx, tongo.E("to", from.ID))
|
||||
totalFromKarma := from.KarmaOffset + fromKarma
|
||||
|
|
7
karma.go
7
karma.go
|
@ -1,6 +1,9 @@
|
|||
package main
|
||||
|
||||
// Returns slices of good and bad triggers
|
||||
func GetTriggers() ([]string, []string) {
|
||||
return []string{"+", "спасибо", "благодарю", "👍"}, []string{"-", "👎"}
|
||||
func GetTriggers() map[string]int {
|
||||
return map[string]int{
|
||||
"+": 1,
|
||||
"-": -1,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,12 +11,10 @@ import (
|
|||
|
||||
func ExecTimeLogger(next nechotron.UpdateHandler) nechotron.UpdateHandler {
|
||||
return func(u *nechotron.Update) error {
|
||||
log.Printf("=== EXECUTING UPDATE [%s] ===", u.UpdateID.String())
|
||||
start := time.Now()
|
||||
err := next(u)
|
||||
t := time.Since(start)
|
||||
log.Println("Update was handled in %d microseconds", t.Microseconds())
|
||||
log.Printf("=== END OF [%s] ===", u.UpdateID.String())
|
||||
log.Printf("Update [%s] was handled in %d microseconds", u.UpdateID.String(), t.Microseconds())
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,8 @@ var MainState = neco.State{
|
|||
HandleFilter(karmaTriggers, handleKarma)
|
||||
replies := neco.NewDispatcher().
|
||||
HandleFilter(neco.IsReply, replyDispatcher.Run)
|
||||
|
||||
return neco.ChainRun(u, mainCommands, replies)
|
||||
docs := neco.NewDispatcher().
|
||||
HandleFilter(docRequest, handleDocRequest)
|
||||
return neco.ChainRun(u, mainCommands, replies, docs)
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue