From e64db6f6d76aef4ad9f31f77f98a4b19a05ac8ce Mon Sep 17 00:00:00 2001 From: nefrace Date: Mon, 13 Feb 2023 01:40:20 +0300 Subject: [PATCH] Reworked KarmaTriggers, docTopic filter --- filters.go | 28 +++++++++++++++++++++++++--- handlers.go | 16 +++++++--------- karma.go | 7 +++++-- middleware.go | 4 +--- states.go | 5 +++-- 5 files changed, 41 insertions(+), 19 deletions(-) diff --git a/filters.go b/filters.go index ae2aff4..da88a5d 100644 --- a/filters.go +++ b/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@?[\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 +} diff --git a/handlers.go b/handlers.go index 5e3c1a2..c6ae8a1 100644 --- a/handlers.go +++ b/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 diff --git a/karma.go b/karma.go index d673cd4..6754c60 100644 --- a/karma.go +++ b/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, + } } diff --git a/middleware.go b/middleware.go index bc949de..9652518 100644 --- a/middleware.go +++ b/middleware.go @@ -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 } } diff --git a/states.go b/states.go index 87631dc..84bd8ca 100644 --- a/states.go +++ b/states.go @@ -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) }, }