Reworked KarmaTriggers, docTopic filter
This commit is contained in:
		
							
								
								
									
										28
									
								
								filters.go
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								filters.go
									
									
									
									
									
								
							@ -1,19 +1,41 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
 | 
						"regexp"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	neco "git.nefrace.ru/nefrace/nechotron"
 | 
						neco "git.nefrace.ru/nefrace/nechotron"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func karmaTriggers(u *neco.Update) bool {
 | 
					func karmaTriggers(u *neco.Update) bool {
 | 
				
			||||||
	good, bad := GetTriggers()
 | 
						triggers := GetTriggers()
 | 
				
			||||||
	all := append(good, bad...)
 | 
					
 | 
				
			||||||
	text := u.Text()
 | 
						text := u.Text()
 | 
				
			||||||
	for _, t := range all {
 | 
						for t, v := range triggers {
 | 
				
			||||||
		if strings.HasPrefix(text, t) {
 | 
							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 true
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return false
 | 
						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"
 | 
						"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 {
 | 
					func handleKarma(u *nechotron.Update) error {
 | 
				
			||||||
	from, _ := u.Ctx.Value("userfrom").(*User)
 | 
						from, _ := u.Ctx.Value("userfrom").(*User)
 | 
				
			||||||
	to, _ := u.Ctx.Value("userto").(*User)
 | 
						to, _ := u.Ctx.Value("userto").(*User)
 | 
				
			||||||
@ -23,15 +28,8 @@ func handleKarma(u *nechotron.Update) error {
 | 
				
			|||||||
		}()
 | 
							}()
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	good, bad := GetTriggers()
 | 
						value := u.Ctx.Value(nechotron.FilteredValue("karmaValue")).(int)
 | 
				
			||||||
	value := 0
 | 
						// trigger := u.Ctx.Value(nechotron.FilteredValue("karmaTrigger")).(string)
 | 
				
			||||||
	text := u.Text()
 | 
					 | 
				
			||||||
	if StringHasPrefix(text, good...) {
 | 
					 | 
				
			||||||
		value = 1
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if StringHasPrefix(text, bad...) {
 | 
					 | 
				
			||||||
		value = -1
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	store := tongo.NewStore[KarmaShot](db)
 | 
						store := tongo.NewStore[KarmaShot](db)
 | 
				
			||||||
	fromKarma, _ := store.Count(u.Ctx, tongo.E("to", from.ID))
 | 
						fromKarma, _ := store.Count(u.Ctx, tongo.E("to", from.ID))
 | 
				
			||||||
	totalFromKarma := from.KarmaOffset + fromKarma
 | 
						totalFromKarma := from.KarmaOffset + fromKarma
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										7
									
								
								karma.go
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								karma.go
									
									
									
									
									
								
							@ -1,6 +1,9 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Returns slices of good and bad triggers
 | 
					// Returns slices of good and bad triggers
 | 
				
			||||||
func GetTriggers() ([]string, []string) {
 | 
					func GetTriggers() map[string]int {
 | 
				
			||||||
	return []string{"+", "спасибо", "благодарю", "👍"}, []string{"-", "👎"}
 | 
						return map[string]int{
 | 
				
			||||||
 | 
							"+": 1,
 | 
				
			||||||
 | 
							"-": -1,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -11,12 +11,10 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func ExecTimeLogger(next nechotron.UpdateHandler) nechotron.UpdateHandler {
 | 
					func ExecTimeLogger(next nechotron.UpdateHandler) nechotron.UpdateHandler {
 | 
				
			||||||
	return func(u *nechotron.Update) error {
 | 
						return func(u *nechotron.Update) error {
 | 
				
			||||||
		log.Printf("=== EXECUTING UPDATE [%s] ===", u.UpdateID.String())
 | 
					 | 
				
			||||||
		start := time.Now()
 | 
							start := time.Now()
 | 
				
			||||||
		err := next(u)
 | 
							err := next(u)
 | 
				
			||||||
		t := time.Since(start)
 | 
							t := time.Since(start)
 | 
				
			||||||
		log.Println("Update was handled in %d microseconds", t.Microseconds())
 | 
							log.Printf("Update [%s] was handled in %d microseconds", u.UpdateID.String(), t.Microseconds())
 | 
				
			||||||
		log.Printf("=== END OF [%s] ===", u.UpdateID.String())
 | 
					 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -15,7 +15,8 @@ var MainState = neco.State{
 | 
				
			|||||||
			HandleFilter(karmaTriggers, handleKarma)
 | 
								HandleFilter(karmaTriggers, handleKarma)
 | 
				
			||||||
		replies := neco.NewDispatcher().
 | 
							replies := neco.NewDispatcher().
 | 
				
			||||||
			HandleFilter(neco.IsReply, replyDispatcher.Run)
 | 
								HandleFilter(neco.IsReply, replyDispatcher.Run)
 | 
				
			||||||
 | 
							docs := neco.NewDispatcher().
 | 
				
			||||||
		return neco.ChainRun(u, mainCommands, replies)
 | 
								HandleFilter(docRequest, handleDocRequest)
 | 
				
			||||||
 | 
							return neco.ChainRun(u, mainCommands, replies, docs)
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user