feat(modules): match commands case-insensitively

Mobile keyboards autocapitalize, so a typed /Ping silently did nothing and
read as the bot being broken. Registered names are lowercase-only per
validateCommand, so folding case cannot make two commands collide, and the
canonical Command.Name still drives stats, metrics, hooks, and logs.
This commit is contained in:
2026-07-19 20:09:16 +07:00
parent 2a6c41e6ba
commit c71b17b772
2 changed files with 31 additions and 1 deletions
+7 -1
View File
@@ -121,6 +121,12 @@ func logCommand(name string, update *models.Update, err error) {
//
// Telegram routes /cmd@otherbot only to otherbot, so an @suffix present in
// the entity addresses *this* bot — no need to verify against our username.
//
// Matching is case-insensitive so /PING and /Ping reach the same handler as
// /ping — mobile keyboards autocapitalize, and a typed command that silently
// does nothing reads as the bot being broken. Registered names are lowercase by
// validateCommand, so folding case can never make two commands collide, and the
// canonical Command.Name still drives stats, metrics, hooks, and logs.
func matchCommand(name string, update *models.Update) bool {
if update == nil || update.Message == nil {
return false
@@ -141,7 +147,7 @@ func matchCommand(name string, update *models.Update) bool {
if i := strings.IndexByte(tok, '@'); i >= 0 {
tok = tok[:i]
}
if tok == name {
if strings.EqualFold(tok, name) {
return true
}
}
+24
View File
@@ -116,6 +116,30 @@ func TestMatchCommand(t *testing.T) {
update: mkUpdate("hi /help", cmd(3, 5)),
expect: true,
},
{
name: "uppercase command matches",
want: "help",
update: mkUpdate("/HELP", cmd(0, 5)),
expect: true,
},
{
name: "mixed-case command matches",
want: "help",
update: mkUpdate("/Help", cmd(0, 5)),
expect: true,
},
{
name: "uppercase command with botname matches",
want: "help",
update: mkUpdate("/HELP@miti99bot", cmd(0, 15)),
expect: true,
},
{
name: "case folding does not match a different command",
want: "help",
update: mkUpdate("/INFO", cmd(0, 5)),
expect: false,
},
{
name: "nil update",
want: "help",