feat: initial whatsmeow bot with command handler and context helpers
Core framework (Handler, Command, Default muxer) in core/ package. Command self-registration via init() with blank imports in cmds/. Context helpers (send, reply, download, upload) in context/ package. Permission system with GROUP_ONLY / PRIVATE_ONLY checks. WhatsApp client configured as Safari with history sync disabled.
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.mau.fi/whatsmeow"
|
||||
waE2E "go.mau.fi/whatsmeow/proto/waE2E"
|
||||
"go.mau.fi/whatsmeow/types/events"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type Permission string
|
||||
|
||||
const (
|
||||
PermissionGroupOnly Permission = "GROUP_ONLY"
|
||||
PermissionPrivateOnly Permission = "PRIVATE_ONLY"
|
||||
)
|
||||
|
||||
// CheckCommandPermissions returns an error message if the command's
|
||||
// permissions deny the event context, or empty string if allowed.
|
||||
func CheckCommandPermissions(cmd *Command, evt *events.Message) string {
|
||||
for _, p := range cmd.Permissions {
|
||||
switch p {
|
||||
case PermissionGroupOnly:
|
||||
if !evt.Info.IsGroup {
|
||||
return "This command can only be used in groups"
|
||||
}
|
||||
case PermissionPrivateOnly:
|
||||
if evt.Info.IsGroup {
|
||||
return "This command can only be used in private chat"
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// sendPermissionDenied sends a plain text reply indicating why the
|
||||
// command was blocked, so the caller gets feedback instead of silence.
|
||||
func sendPermissionDenied(client *whatsmeow.Client, evt *events.Message, text string) {
|
||||
client.SendMessage(context.Background(), evt.Info.Chat, &waE2E.Message{
|
||||
ExtendedTextMessage: &waE2E.ExtendedTextMessage{
|
||||
Text: proto.String(text),
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user