Files
trbot/utils/handler_router/match/message.go
2026-03-28 13:14:07 +08:00

51 lines
1.3 KiB
Go

package match
import (
"strings"
"github.com/go-telegram/bot/models"
)
type MessageMF func(update *models.Update) bool
/*
All match functions should not check whether `update` and
its subfields (such as `Message`, `CallbackQuery`) are nil.
this step should be handled internally,
calling match functions manually may trigger a panic.
*/
// SlashCommand will match `command` in `/command`.
func SlashCommand(command string) MessageMF {
return func(update *models.Update) bool {
for _, e := range update.Message.Entities {
if e.Offset == 0 && e.Type == models.MessageEntityTypeBotCommand {
return update.Message.Text[1:e.Length] == command
}
}
return false
}
}
// SlashCommandMaybeUsername will match `command` in `/command` and `/command@username`.
func SlashCommandMaybeUsername(cmd, username string) MessageMF {
return func(update *models.Update) bool {
for _, e := range update.Message.Entities {
if e.Offset == 0 && e.Type == models.MessageEntityTypeBotCommand {
return update.Message.Text[1:e.Length] == cmd || update.Message.Text[1:e.Length] == cmd+"@"+username
}
}
return false
}
}
// TextContains will match `text` in `sometextor...`.
func TextContains(text string) MessageMF {
return func(update *models.Update) bool {
return strings.Contains(update.Message.Text, text)
}
}