chore: move '/internal/pkg' to '/pkg'
This commit is contained in:
66
pkg/core/notifier/providers/dingtalkbot/dingtalkbot.go
Normal file
66
pkg/core/notifier/providers/dingtalkbot/dingtalkbot.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package dingtalkbot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/url"
|
||||
|
||||
"github.com/blinkbean/dingtalk"
|
||||
|
||||
"github.com/usual2970/certimate/pkg/core"
|
||||
)
|
||||
|
||||
type NotifierProviderConfig struct {
|
||||
// 钉钉机器人的 Webhook 地址。
|
||||
WebhookUrl string `json:"webhookUrl"`
|
||||
// 钉钉机器人的 Secret。
|
||||
Secret string `json:"secret"`
|
||||
}
|
||||
|
||||
type NotifierProvider struct {
|
||||
config *NotifierProviderConfig
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
var _ core.Notifier = (*NotifierProvider)(nil)
|
||||
|
||||
func NewNotifierProvider(config *NotifierProviderConfig) (*NotifierProvider, error) {
|
||||
if config == nil {
|
||||
return nil, errors.New("the configuration of the notifier provider is nil")
|
||||
}
|
||||
|
||||
return &NotifierProvider{
|
||||
config: config,
|
||||
logger: slog.Default(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (n *NotifierProvider) SetLogger(logger *slog.Logger) {
|
||||
if logger == nil {
|
||||
n.logger = slog.New(slog.DiscardHandler)
|
||||
} else {
|
||||
n.logger = logger
|
||||
}
|
||||
}
|
||||
|
||||
func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (*core.NotifyResult, error) {
|
||||
webhookUrl, err := url.Parse(n.config.WebhookUrl)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("dingtalk api error: invalid webhook url: %w", err)
|
||||
}
|
||||
|
||||
var bot *dingtalk.DingTalk
|
||||
if n.config.Secret == "" {
|
||||
bot = dingtalk.InitDingTalk([]string{webhookUrl.Query().Get("access_token")}, "")
|
||||
} else {
|
||||
bot = dingtalk.InitDingTalkWithSecret(webhookUrl.Query().Get("access_token"), n.config.Secret)
|
||||
}
|
||||
|
||||
if err := bot.SendTextMessage(subject + "\n" + message); err != nil {
|
||||
return nil, fmt.Errorf("dingtalk api error: %w", err)
|
||||
}
|
||||
|
||||
return &core.NotifyResult{}, nil
|
||||
}
|
||||
64
pkg/core/notifier/providers/dingtalkbot/dingtalkbot_test.go
Normal file
64
pkg/core/notifier/providers/dingtalkbot/dingtalkbot_test.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package dingtalkbot_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
provider "github.com/usual2970/certimate/pkg/core/notifier/providers/dingtalkbot"
|
||||
)
|
||||
|
||||
const (
|
||||
mockSubject = "test_subject"
|
||||
mockMessage = "test_message"
|
||||
)
|
||||
|
||||
var (
|
||||
fWebhookUrl string
|
||||
fSecret string
|
||||
)
|
||||
|
||||
func init() {
|
||||
argsPrefix := "CERTIMATE_NOTIFIER_DINGTALKBOT_"
|
||||
|
||||
flag.StringVar(&fWebhookUrl, argsPrefix+"WEBHOOKURL", "", "")
|
||||
flag.StringVar(&fSecret, argsPrefix+"SECRET", "", "")
|
||||
}
|
||||
|
||||
/*
|
||||
Shell command to run this test:
|
||||
|
||||
go test -v ./dingtalkbot_test.go -args \
|
||||
--CERTIMATE_NOTIFIER_DINGTALKBOT_WEBHOOKURL="https://example.com/your-webhook-url" \
|
||||
--CERTIMATE_NOTIFIER_DINGTALKBOT_SECRET="your-secret"
|
||||
*/
|
||||
func TestNotify(t *testing.T) {
|
||||
flag.Parse()
|
||||
|
||||
t.Run("Notify", func(t *testing.T) {
|
||||
t.Log(strings.Join([]string{
|
||||
"args:",
|
||||
fmt.Sprintf("WEBHOOKURL: %v", fWebhookUrl),
|
||||
fmt.Sprintf("SECRET: %v", fSecret),
|
||||
}, "\n"))
|
||||
|
||||
notifier, err := provider.NewNotifierProvider(&provider.NotifierProviderConfig{
|
||||
WebhookUrl: fWebhookUrl,
|
||||
Secret: fSecret,
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("err: %+v", err)
|
||||
return
|
||||
}
|
||||
|
||||
res, err := notifier.Notify(context.Background(), mockSubject, mockMessage)
|
||||
if err != nil {
|
||||
t.Errorf("err: %+v", err)
|
||||
return
|
||||
}
|
||||
|
||||
t.Logf("ok: %v", res)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user