feat(钉钉通道): 支持仅使用 access_token 配置钉钉机器人
Motivation: 钉钉机器人的 webhook 前缀固定不变,要求用户填写完整 URL 冗余且容易出错。简化通道配置项,降低接入配置成本,同时保证存量配置不受影响。 Changes: * 钉钉通道配置改用 access_token,webhook 前缀由内部固定拼接 * 保留对旧 webhook_url 配置的兼容,access_token 存在时优先生效 * 新增 webhook 地址解析逻辑的单元测试,覆盖优先级与回退场景 * 同步更新 README、HTTP 请求示例及设计文档中的配置示例 * E2E 测试改用 E2E_DINGTALK_ACCESS_TOKEN 环境变量并按新格式创建通道
This commit is contained in:
@@ -13,9 +13,21 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// 钉钉自定义机器人 webhook 前缀固定,配置时只需提供 access_token
|
||||
const dingtalkWebhookPrefix = "https://oapi.dingtalk.com/robot/send?access_token="
|
||||
|
||||
type dingtalkConfig struct {
|
||||
WebhookURL string `json:"webhook_url"`
|
||||
Secret string `json:"secret,omitempty"`
|
||||
AccessToken string `json:"access_token"`
|
||||
Secret string `json:"secret,omitempty"`
|
||||
// WebhookURL 兼容旧配置;新配置优先使用 AccessToken
|
||||
WebhookURL string `json:"webhook_url,omitempty"`
|
||||
}
|
||||
|
||||
func (c *dingtalkConfig) webhookURL() string {
|
||||
if c.AccessToken != "" {
|
||||
return dingtalkWebhookPrefix + c.AccessToken
|
||||
}
|
||||
return c.WebhookURL
|
||||
}
|
||||
|
||||
type dingtalkMessage struct {
|
||||
@@ -41,16 +53,16 @@ func (s *DingTalkSender) Send(title, content string, config json.RawMessage) err
|
||||
}
|
||||
|
||||
if s.limiter != nil {
|
||||
if err := s.limiter.Acquire(context.Background(), DingTalkLimitKey(cfg.WebhookURL)); err != nil {
|
||||
if err := s.limiter.Acquire(context.Background(), DingTalkLimitKey(cfg.webhookURL())); err != nil {
|
||||
return fmt.Errorf("dingtalk rate limit: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
reqURL := cfg.WebhookURL
|
||||
reqURL := cfg.webhookURL()
|
||||
if cfg.Secret != "" {
|
||||
timestamp := time.Now().UnixMilli()
|
||||
sign := dingtalkSign(timestamp, cfg.Secret)
|
||||
reqURL = fmt.Sprintf("%s×tamp=%d&sign=%s", cfg.WebhookURL, timestamp, sign)
|
||||
reqURL = fmt.Sprintf("%s×tamp=%d&sign=%s", reqURL, timestamp, sign)
|
||||
}
|
||||
|
||||
msg := dingtalkMessage{
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package adapter
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDingtalkConfigWebhookURL(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg dingtalkConfig
|
||||
wantURL string
|
||||
}{
|
||||
{
|
||||
name: "access token only",
|
||||
cfg: dingtalkConfig{AccessToken: "tok-abc"},
|
||||
wantURL: "https://oapi.dingtalk.com/robot/send?access_token=tok-abc",
|
||||
},
|
||||
{
|
||||
name: "access token takes precedence over legacy url",
|
||||
cfg: dingtalkConfig{AccessToken: "tok-abc", WebhookURL: "https://legacy.example/webhook"},
|
||||
wantURL: "https://oapi.dingtalk.com/robot/send?access_token=tok-abc",
|
||||
},
|
||||
{
|
||||
name: "fallback to legacy url",
|
||||
cfg: dingtalkConfig{WebhookURL: "https://legacy.example/webhook"},
|
||||
wantURL: "https://legacy.example/webhook",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := tt.cfg.webhookURL(); got != tt.wantURL {
|
||||
t.Fatalf("webhookURL() = %q, want %q", got, tt.wantURL)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user