Skip to content

Commit

Permalink
Remove diacritics from command
Browse files Browse the repository at this point in the history
  • Loading branch information
kozaktomas committed Nov 24, 2024
1 parent c68a73e commit fa5700a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
2 changes: 2 additions & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/gorilla/mux v1.8.1
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b
github.com/joho/godotenv v1.5.1
github.com/kozaktomas/diacritics v0.0.0-20240813054239-69e1feaff414
github.com/lib/pq v1.10.9
github.com/prometheus/client_golang v1.20.2
github.com/redis/go-redis/v9 v9.6.1
Expand Down Expand Up @@ -38,5 +39,6 @@ require (
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/kozaktomas/diacritics v0.0.0-20240813054239-69e1feaff414 h1:zWdcztqXVMVyn/EckRW9pTYFitETQ9LbllvHgRCdoBk=
github.com/kozaktomas/diacritics v0.0.0-20240813054239-69e1feaff414/go.mod h1:jQczigoLtVLrhiIMR4lGFaMDsC18mdz+Ec/wO2IJk04=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down Expand Up @@ -85,6 +87,8 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
23 changes: 15 additions & 8 deletions backend/pkg/hook/botka.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/kotrzina/keg-scale/pkg/config"
"github.com/kotrzina/keg-scale/pkg/utils"
"github.com/kotrzina/keg-scale/pkg/wa"
"github.com/kozaktomas/diacritics"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -103,7 +104,7 @@ func (b *Botka) SendOpen() {
func (b *Botka) helpHandler() wa.EventHandler {
return wa.EventHandler{
MatchFunc: func(msg string) bool {
sanitized := sanitizeCommand(msg)
sanitized := b.sanitizeCommand(msg)
return strings.HasPrefix(sanitized, "help") ||
strings.HasPrefix(sanitized, "napoveda") ||
strings.HasPrefix(sanitized, "pomoc")
Expand All @@ -124,7 +125,7 @@ func (b *Botka) helpHandler() wa.EventHandler {
func (b *Botka) helloHandler() wa.EventHandler {
return wa.EventHandler{
MatchFunc: func(msg string) bool {
sanitized := sanitizeCommand(msg)
sanitized := b.sanitizeCommand(msg)
return strings.HasPrefix(sanitized, "hello") ||
strings.HasPrefix(sanitized, "hi") ||
strings.HasPrefix(sanitized, "ahoj") ||
Expand All @@ -144,7 +145,7 @@ func (b *Botka) helloHandler() wa.EventHandler {
func (b *Botka) pubHandler() wa.EventHandler {
return wa.EventHandler{
MatchFunc: func(msg string) bool {
sanitized := sanitizeCommand(msg)
sanitized := b.sanitizeCommand(msg)
return strings.HasPrefix(sanitized, "pub") ||
strings.HasPrefix(sanitized, "hospoda")
},
Expand All @@ -166,7 +167,7 @@ func (b *Botka) pubHandler() wa.EventHandler {
func (b *Botka) kegHandler() wa.EventHandler {
return wa.EventHandler{
MatchFunc: func(msg string) bool {
sanitized := sanitizeCommand(msg)
sanitized := b.sanitizeCommand(msg)
return strings.HasPrefix(sanitized, "becka") ||
strings.HasPrefix(sanitized, "keg")
},
Expand All @@ -191,7 +192,7 @@ func (b *Botka) kegHandler() wa.EventHandler {
func (b *Botka) pricesHandler() wa.EventHandler {
return wa.EventHandler{
MatchFunc: func(msg string) bool {
return strings.HasPrefix(sanitizeCommand(msg), "cenik")
return strings.HasPrefix(b.sanitizeCommand(msg), "cenik")
},
HandleFunc: func(from, _ string) error {
reply := "Ceník: \n" +
Expand All @@ -206,7 +207,7 @@ func (b *Botka) pricesHandler() wa.EventHandler {
func (b *Botka) warehouseHandler() wa.EventHandler {
return wa.EventHandler{
MatchFunc: func(msg string) bool {
return strings.HasPrefix(sanitizeCommand(msg), "sklad")
return strings.HasPrefix(b.sanitizeCommand(msg), "sklad")
},
HandleFunc: func(from, _ string) error {
b.mtx.RLock()
Expand Down Expand Up @@ -235,6 +236,12 @@ func (b *Botka) warehouseHandler() wa.EventHandler {
}
}

func sanitizeCommand(command string) string {
return strings.ToLower(strings.TrimSpace(strings.TrimPrefix(command, "/")))
func (b *Botka) sanitizeCommand(command string) string {
c := strings.ToLower(strings.TrimSpace(strings.TrimPrefix(command, "/")))
c, err := diacritics.Remove(c)
if err != nil {
b.logger.Fatalf("could not remove diacritics: %v", err) // should never happen
}

return c
}
8 changes: 7 additions & 1 deletion backend/pkg/hook/botka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ func TestWhatsApp_SanitizeCommand(t *testing.T) {
command: "/CENIK",
want: "cenik",
},
{
command: "/BEČKA",
want: "becka",
},
}

b := Botka{}
for _, tt := range tests {
t.Run(tt.command, func(t *testing.T) {
if got := sanitizeCommand(tt.command); got != tt.want {
if got := b.sanitizeCommand(tt.command); got != tt.want {
t.Errorf("sanitizeCommand() = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit fa5700a

Please sign in to comment.