Files
Cyrene_v2/backend/services/platform-bridge/webhook/adapter.go
T
Aska 17ef73bcbe feat(bridge): add multi-platform bridge — QQ, Telegram, Discord, Webhook
- platform-bridge: unified message model + Adapter interface
- qq/adapter: OBv11 WebSocket protocol with @mention detection
- telegram/discord: stub adapters with send capability
- webhook: HTTP POST receiver for generic webhook integration
- Router: message routing from platform → AI-Core → back
- Group chat rules: 500 char limit, 3s gap, silence keywords
- main.go: conditional adapter loading via env vars
2026-08-12 12:30:55 +08:00

36 lines
1.5 KiB
Go

package webhook
import (
"encoding/json"
"fmt"
"net/http"
"time"
"git.yeij.top/AskaEth/Cyrene_v2/services/platform-bridge"
)
type Config struct{ Name, ListenAddr, APIKey string }
type Adapter struct {
cfg Config; incoming chan *bridge.UnifiedMessage; server *http.Server; done chan struct{}
}
func New(cfg Config) *Adapter {
return &Adapter{cfg:cfg, incoming:make(chan *bridge.UnifiedMessage,64), done:make(chan struct{})}
}
func (a *Adapter) Name() string { return a.cfg.Name }
func (a *Adapter) Platform() string { return "webhook" }
func (a *Adapter) Start() error {
mux := http.NewServeMux()
mux.HandleFunc("/webhook", func(w http.ResponseWriter, r *http.Request) {
if a.cfg.APIKey != "" && r.Header.Get("X-API-Key") != a.cfg.APIKey { w.WriteHeader(401); return }
var body map[string]any; json.NewDecoder(r.Body).Decode(&body)
c,_:=body["content"].(string); s,_:=body["source"].(string)
a.incoming <- &bridge.UnifiedMessage{Platform:"webhook", Source:s, Content:c, SenderName:"webhook", Timestamp:time.Now()}
w.WriteHeader(200)
})
a.server = &http.Server{Addr: a.cfg.ListenAddr, Handler: mux}
go a.server.ListenAndServe(); return nil
}
func (a *Adapter) Stop() error { if a.server!=nil { a.server.Close() }; close(a.done); return nil }
func (a *Adapter) Incoming() <-chan *bridge.UnifiedMessage { return a.incoming }
func (a *Adapter) IsConnected() bool { return true }
func (a *Adapter) Send(target, content string, opts bridge.SendOptions) error {
fmt.Printf("[webhook:%s] -> %s: %s\n", a.cfg.Name, target, content); return nil
}