package webhook import ( "encoding/json" "git.yeij.top/AskaEth/Cyrene_v2/internal/logger" "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 { logger.Infof("[webhook:%s] -> %s: %s\n", a.cfg.Name, target, content); return nil }