diff --git a/handler.go b/handler.go index 3b51ff7..05e4b7e 100644 --- a/handler.go +++ b/handler.go @@ -134,6 +134,7 @@ func onJoin(c tele.Context) error { } status.UserFullName = c.Sender().FirstName + " " + c.Sender().LastName + status.UserFullName = sanitizeName(status.UserFullName) db.Set(kvID, status, minikv.DefaultExpiration) diff --git a/helper.go b/helper.go index 189dc05..aaf53b3 100644 --- a/helper.go +++ b/helper.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "strings" tele "gopkg.in/tucnak/telebot.v3" ) @@ -24,3 +25,22 @@ func stringInSlice(a string, list []string) bool { } return false } + +func removeRedundantSpaces(s string) string { + return strings.Join(strings.Fields(s), " ") +} + +func sanitizeName(s string) string { + var result strings.Builder + for i := 0; i < len(s); i++ { + b := s[i] + if ('a' <= b && b <= 'z') || + ('A' <= b && b <= 'Z') || + ('0' <= b && b <= '9') || + b == ' ' { + result.WriteByte(b) + } + } + clean := removeRedundantSpaces(result.String()) + return clean +}