mirror of
https://github.com/fumiama/ReiBot.git
synced 2026-06-11 05:30:38 +08:00
extend regex to callbackquery
This commit is contained in:
@@ -59,11 +59,11 @@ func main() {
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
rei "github.com/fumiama/ReiBot"
|
rei "github.com/fumiama/ReiBot"
|
||||||
tgba "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
tgba "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -86,7 +86,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
_, err := bot.Send(tgba.NewMessage(msg.Chat.ID, msg.Text[len("测试"):]))
|
_, err := bot.Send(tgba.NewMessage(msg.Chat.ID, msg.Text[len("测试"):]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("[ERRO]", err)
|
log.Errorln(err)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
OnEditedMessage: func(updateid int, bot *rei.TelegramClient, msg *tgba.Message) {
|
OnEditedMessage: func(updateid int, bot *rei.TelegramClient, msg *tgba.Message) {
|
||||||
@@ -98,7 +98,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
_, err := bot.Send(tgba.NewMessage(msg.Chat.ID, "已编辑:"+msg.Text[len("测试"):]))
|
_, err := bot.Send(tgba.NewMessage(msg.Chat.ID, "已编辑:"+msg.Text[len("测试"):]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("[ERRO]", err)
|
log.Errorln(err)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
17
client.go
17
client.go
@@ -1,7 +1,6 @@
|
|||||||
package rei
|
package rei
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -9,7 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/RomiChan/syncx"
|
"github.com/RomiChan/syncx"
|
||||||
tgba "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
tgba "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||||
"github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TelegramClient ...
|
// TelegramClient ...
|
||||||
@@ -25,7 +24,7 @@ var (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
logsetter.Do(func() {
|
logsetter.Do(func() {
|
||||||
_ = tgba.SetLogger(logrus.StandardLogger())
|
_ = tgba.SetLogger(log.StandardLogger())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,7 +41,7 @@ func NewTelegramClient(c *Bot) (tc TelegramClient) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
tp := t.Field(i).Name[2:]
|
tp := t.Field(i).Name[2:]
|
||||||
log.Println("[INFO] register handler", tp)
|
log.Println("register handler", tp)
|
||||||
handler := f.Interface()
|
handler := f.Interface()
|
||||||
tc.b.handlers[tp] = *(*GeneralHandleType)(unsafe.Add(unsafe.Pointer(&handler), unsafe.Sizeof(uintptr(0))))
|
tc.b.handlers[tp] = *(*GeneralHandleType)(unsafe.Add(unsafe.Pointer(&handler), unsafe.Sizeof(uintptr(0))))
|
||||||
}
|
}
|
||||||
@@ -52,11 +51,11 @@ func NewTelegramClient(c *Bot) (tc TelegramClient) {
|
|||||||
|
|
||||||
// Connect ...
|
// Connect ...
|
||||||
func (tc *TelegramClient) Connect() {
|
func (tc *TelegramClient) Connect() {
|
||||||
log.Println("[INFO] 开始尝试连接到Telegram服务器, token:", tc.b.Token)
|
log.Println("开始尝试连接到Telegram服务器, token:", tc.b.Token)
|
||||||
for {
|
for {
|
||||||
ba, err := tgba.NewBotAPI(tc.b.Token)
|
ba, err := tgba.NewBotAPI(tc.b.Token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("[WARN] 连接到Telegram服务器时出现错误:", err, ", token:", tc.b.Token)
|
log.Warnln("连接到Telegram服务器时出现错误:", err, ", token:", tc.b.Token)
|
||||||
time.Sleep(2 * time.Second) // 等待两秒后重新连接
|
time.Sleep(2 * time.Second) // 等待两秒后重新连接
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -66,18 +65,18 @@ func (tc *TelegramClient) Connect() {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
clients.Store(tc.Self.ID, tc)
|
clients.Store(tc.Self.ID, tc)
|
||||||
log.Println("[INFO] 连接到Telegram服务器成功, token:", tc.b.Token)
|
log.Println("连接到Telegram服务器成功, token:", tc.b.Token)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen 开始监听事件
|
// Listen 开始监听事件
|
||||||
func (tc *TelegramClient) Listen() {
|
func (tc *TelegramClient) Listen() {
|
||||||
log.Println("[INFO] 开始监听", tc.Self.UserName, "的事件")
|
log.Println("开始监听", tc.Self.UserName, "的事件")
|
||||||
for {
|
for {
|
||||||
updates := tc.GetUpdatesChan(tc.b.UpdateConfig)
|
updates := tc.GetUpdatesChan(tc.b.UpdateConfig)
|
||||||
for update := range updates {
|
for update := range updates {
|
||||||
tc.processEvent(update)
|
tc.processEvent(update)
|
||||||
}
|
}
|
||||||
log.Println("[WARN] Telegram服务器连接断开...")
|
log.Warnln("Telegram服务器连接断开...")
|
||||||
clients.Delete(tc.Self.ID)
|
clients.Delete(tc.Self.ID)
|
||||||
time.Sleep(time.Millisecond * time.Duration(3))
|
time.Sleep(time.Millisecond * time.Duration(3))
|
||||||
tc.Connect()
|
tc.Connect()
|
||||||
|
|||||||
13
engine.go
13
engine.go
@@ -226,7 +226,7 @@ func OnMessageRegex(regexPattern string, rules ...Rule) *Matcher {
|
|||||||
return defaultEngine.OnMessageRegex(regexPattern, rules...)
|
return defaultEngine.OnMessageRegex(regexPattern, rules...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnRegex 正则触发器
|
// OnMessageRegex 正则触发器
|
||||||
func (e *Engine) OnMessageRegex(regexPattern string, rules ...Rule) *Matcher {
|
func (e *Engine) OnMessageRegex(regexPattern string, rules ...Rule) *Matcher {
|
||||||
matcher := &Matcher{
|
matcher := &Matcher{
|
||||||
Type: "Message",
|
Type: "Message",
|
||||||
@@ -364,3 +364,14 @@ func (e *Engine) OnMessageShell(command string, model interface{}, rules ...Rule
|
|||||||
e.matchers = append(e.matchers, matcher)
|
e.matchers = append(e.matchers, matcher)
|
||||||
return StoreMatcher(matcher)
|
return StoreMatcher(matcher)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnCallbackQueryRegex 正则触发器
|
||||||
|
func (e *Engine) OnCallbackQueryRegex(regexPattern string, rules ...Rule) *Matcher {
|
||||||
|
matcher := &Matcher{
|
||||||
|
Type: "CallbackQuery",
|
||||||
|
Rules: append([]Rule{RegexRule(regexPattern)}, rules...),
|
||||||
|
Engine: e,
|
||||||
|
}
|
||||||
|
e.matchers = append(e.matchers, matcher)
|
||||||
|
return StoreMatcher(matcher)
|
||||||
|
}
|
||||||
|
|||||||
11
event.go
11
event.go
@@ -1,10 +1,10 @@
|
|||||||
package rei
|
package rei
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
tgba "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
tgba "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Event ...
|
// Event ...
|
||||||
@@ -33,7 +33,7 @@ func (tc *TelegramClient) processEvent(update tgba.Update) {
|
|||||||
matcherLock.RUnlock()
|
matcherLock.RUnlock()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
log.Println("[INFO] pass", tp, "event to plugins")
|
log.Println("pass", tp, "event to plugins")
|
||||||
matchers := make([]*Matcher, n)
|
matchers := make([]*Matcher, n)
|
||||||
copy(matchers, matcherMap[tp])
|
copy(matchers, matcherMap[tp])
|
||||||
matcherLock.RUnlock()
|
matcherLock.RUnlock()
|
||||||
@@ -46,8 +46,11 @@ func (tc *TelegramClient) processEvent(update tgba.Update) {
|
|||||||
State: State{},
|
State: State{},
|
||||||
Caller: tc,
|
Caller: tc,
|
||||||
}
|
}
|
||||||
if tp == "Message" {
|
switch tp {
|
||||||
|
case "Message":
|
||||||
ctx.Message = (*tgba.Message)(f.UnsafePointer())
|
ctx.Message = (*tgba.Message)(f.UnsafePointer())
|
||||||
|
case "CallbackQuery":
|
||||||
|
ctx.Message = (*tgba.CallbackQuery)(f.UnsafePointer()).Message
|
||||||
}
|
}
|
||||||
match(ctx, matchers)
|
match(ctx, matchers)
|
||||||
continue
|
continue
|
||||||
@@ -56,7 +59,7 @@ func (tc *TelegramClient) processEvent(update tgba.Update) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
log.Println("[INFO] process", tp, "event")
|
log.Println("process", tp, "event")
|
||||||
go h(update.UpdateID, tc, f.UnsafePointer())
|
go h(update.UpdateID, tc, f.UnsafePointer())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
rei "github.com/fumiama/ReiBot"
|
rei "github.com/fumiama/ReiBot"
|
||||||
tgba "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
tgba "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -28,7 +28,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
_, err := bot.Send(tgba.NewMessage(msg.Chat.ID, msg.Text[len("测试"):]))
|
_, err := bot.Send(tgba.NewMessage(msg.Chat.ID, msg.Text[len("测试"):]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("[ERRO]", err)
|
log.Errorln(err)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
OnEditedMessage: func(updateid int, bot *rei.TelegramClient, msg *tgba.Message) {
|
OnEditedMessage: func(updateid int, bot *rei.TelegramClient, msg *tgba.Message) {
|
||||||
@@ -40,7 +40,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
_, err := bot.Send(tgba.NewMessage(msg.Chat.ID, "已编辑:"+msg.Text[len("测试"):]))
|
_, err := bot.Send(tgba.NewMessage(msg.Chat.ID, "已编辑:"+msg.Text[len("测试"):]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("[ERRO]", err)
|
log.Errorln(err)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
27
rules.go
27
rules.go
@@ -100,15 +100,28 @@ func CommandRule(commands ...string) Rule {
|
|||||||
func RegexRule(regexPattern string) Rule {
|
func RegexRule(regexPattern string) Rule {
|
||||||
regex := regexp.MustCompile(regexPattern)
|
regex := regexp.MustCompile(regexPattern)
|
||||||
return func(ctx *Ctx) bool {
|
return func(ctx *Ctx) bool {
|
||||||
msg, ok := ctx.Value.(*tgba.Message)
|
switch msg := ctx.Value.(type) {
|
||||||
if !ok || msg.Text == "" { // 确保无空
|
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
|
return false
|
||||||
}
|
}
|
||||||
if matched := regex.FindStringSubmatch(msg.Text); matched != nil {
|
|
||||||
ctx.State["regex_matched"] = matched
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user