1
0
mirror of https://github.com/fumiama/ReiBot.git synced 2026-06-08 20:10:30 +08:00

extend regex to callbackquery

This commit is contained in:
源文雨
2022-06-11 00:55:16 +08:00
parent 6d9724e8f5
commit cc43153a54
6 changed files with 53 additions and 27 deletions

View File

@@ -100,15 +100,28 @@ func CommandRule(commands ...string) Rule {
func RegexRule(regexPattern string) Rule {
regex := regexp.MustCompile(regexPattern)
return func(ctx *Ctx) bool {
msg, ok := ctx.Value.(*tgba.Message)
if !ok || msg.Text == "" { // 确保无空
switch msg := ctx.Value.(type) {
case *tgba.Message:
if msg.Text == "" { // 确保无空
return false
}
if matched := regex.FindStringSubmatch(msg.Text); matched != nil {
ctx.State["regex_matched"] = matched
return true
}
return false
case *tgba.CallbackQuery:
if msg.Data == "" {
return false
}
if matched := regex.FindStringSubmatch(msg.Data); matched != nil {
ctx.State["regex_matched"] = matched
return true
}
return false
default:
return false
}
if matched := regex.FindStringSubmatch(msg.Text); matched != nil {
ctx.State["regex_matched"] = matched
return true
}
return false
}
}