52 lines
1.0 KiB
Go
52 lines
1.0 KiB
Go
package adapter
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
type wecomConfig struct {
|
|
WebhookURL string `json:"webhook_url"`
|
|
}
|
|
|
|
type wecomMessage struct {
|
|
MsgType string `json:"msgtype"`
|
|
Markdown *wecomMD `json:"markdown"`
|
|
}
|
|
|
|
type wecomMD struct {
|
|
Content string `json:"content"`
|
|
}
|
|
|
|
type WeComSender struct{}
|
|
|
|
func (s *WeComSender) Type() string { return "wecom" }
|
|
|
|
func (s *WeComSender) Send(title, content string, config json.RawMessage) error {
|
|
var cfg wecomConfig
|
|
if err := json.Unmarshal(config, &cfg); err != nil {
|
|
return fmt.Errorf("parse wecom config: %w", err)
|
|
}
|
|
|
|
msg := wecomMessage{
|
|
MsgType: "markdown",
|
|
Markdown: &wecomMD{
|
|
Content: fmt.Sprintf("## %s\n%s", title, content),
|
|
},
|
|
}
|
|
|
|
body, _ := json.Marshal(msg)
|
|
resp, err := http.Post(cfg.WebhookURL, "application/json", bytes.NewReader(body))
|
|
if err != nil {
|
|
return fmt.Errorf("wecom send: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode >= 400 {
|
|
return fmt.Errorf("wecom returned status %d", resp.StatusCode)
|
|
}
|
|
return nil
|
|
}
|