chore: move '/internal/pkg' to '/pkg'
This commit is contained in:
69
pkg/core/notifier/providers/telegrambot/telegrambot.go
Normal file
69
pkg/core/notifier/providers/telegrambot/telegrambot.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package telegrambot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
|
||||
"github.com/usual2970/certimate/pkg/core"
|
||||
)
|
||||
|
||||
type NotifierProviderConfig struct {
|
||||
// Telegram Bot API Token。
|
||||
BotToken string `json:"botToken"`
|
||||
// Telegram Chat ID。
|
||||
ChatId int64 `json:"chatId"`
|
||||
}
|
||||
|
||||
type NotifierProvider struct {
|
||||
config *NotifierProviderConfig
|
||||
logger *slog.Logger
|
||||
httpClient *resty.Client
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
client := resty.New()
|
||||
|
||||
return &NotifierProvider{
|
||||
config: config,
|
||||
logger: slog.Default(),
|
||||
httpClient: client,
|
||||
}, 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) {
|
||||
// REF: https://core.telegram.org/bots/api#sendmessage
|
||||
req := n.httpClient.R().
|
||||
SetContext(ctx).
|
||||
SetHeader("Content-Type", "application/json").
|
||||
SetHeader("User-Agent", "certimate").
|
||||
SetBody(map[string]any{
|
||||
"chat_id": n.config.ChatId,
|
||||
"text": subject + "\n" + message,
|
||||
})
|
||||
resp, err := req.Post(fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", n.config.BotToken))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("telegram api error: failed to send request: %w", err)
|
||||
} else if resp.IsError() {
|
||||
return nil, fmt.Errorf("telegram api error: unexpected status code: %d, resp: %s", resp.StatusCode(), resp.String())
|
||||
}
|
||||
|
||||
return &core.NotifyResult{}, nil
|
||||
}
|
||||
64
pkg/core/notifier/providers/telegrambot/telegrambot_test.go
Normal file
64
pkg/core/notifier/providers/telegrambot/telegrambot_test.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package telegrambot_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
provider "github.com/usual2970/certimate/pkg/core/notifier/providers/telegrambot"
|
||||
)
|
||||
|
||||
const (
|
||||
mockSubject = "test_subject"
|
||||
mockMessage = "test_message"
|
||||
)
|
||||
|
||||
var (
|
||||
fApiToken string
|
||||
fChatId int64
|
||||
)
|
||||
|
||||
func init() {
|
||||
argsPrefix := "CERTIMATE_NOTIFIER_TELEGRAMBOT_"
|
||||
|
||||
flag.StringVar(&fApiToken, argsPrefix+"APITOKEN", "", "")
|
||||
flag.Int64Var(&fChatId, argsPrefix+"CHATID", 0, "")
|
||||
}
|
||||
|
||||
/*
|
||||
Shell command to run this test:
|
||||
|
||||
go test -v ./telegrambot_test.go -args \
|
||||
--CERTIMATE_NOTIFIER_TELEGRAMBOT_APITOKEN="your-api-token" \
|
||||
--CERTIMATE_NOTIFIER_TELEGRAMBOT_CHATID=123456
|
||||
*/
|
||||
func TestNotify(t *testing.T) {
|
||||
flag.Parse()
|
||||
|
||||
t.Run("Notify", func(t *testing.T) {
|
||||
t.Log(strings.Join([]string{
|
||||
"args:",
|
||||
fmt.Sprintf("APITOKEN: %v", fApiToken),
|
||||
fmt.Sprintf("CHATID: %v", fChatId),
|
||||
}, "\n"))
|
||||
|
||||
notifier, err := provider.NewNotifierProvider(&provider.NotifierProviderConfig{
|
||||
BotToken: fApiToken,
|
||||
ChatId: fChatId,
|
||||
})
|
||||
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