Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add APIURL to telegram config #168

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions receivers/telegram/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Config struct {
DisableWebPagePreview bool `json:"disable_web_page_preview,omitempty" yaml:"disable_web_page_preview,omitempty"`
ProtectContent bool `json:"protect_content,omitempty" yaml:"protect_content,omitempty"`
DisableNotifications bool `json:"disable_notifications,omitempty" yaml:"disable_notifications,omitempty"`
APIURL string `json:"api_url,omitempty" yaml:"api_url,omitempty"`
}

func NewConfig(jsonData json.RawMessage, decryptFn receivers.DecryptFunc) (Config, error) {
Expand Down Expand Up @@ -71,5 +72,8 @@ func NewConfig(jsonData json.RawMessage, decryptFn receivers.DecryptFunc) (Confi
if !found {
return settings, fmt.Errorf("unknown parse_mode, must be Markdown, MarkdownV2, HTML or None")
}
if settings.APIURL == "" {
settings.APIURL = APIURL
}
return settings, nil
}
9 changes: 9 additions & 0 deletions receivers/telegram/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestNewConfig(t *testing.T) {
name: "Minimal valid configuration",
settings: `{ "bottoken": "test-token", "chatid": "test-chat-id" }`,
expectedConfig: Config{
APIURL: APIURL,
BotToken: "test-token",
ChatID: "test-chat-id",
Message: templates.DefaultMessageEmbed,
Expand All @@ -59,6 +60,7 @@ func TestNewConfig(t *testing.T) {
"bottoken": []byte("test-token"),
},
expectedConfig: Config{
APIURL: APIURL,
BotToken: "test-token",
ChatID: "test-chat-id",
Message: templates.DefaultMessageEmbed,
Expand All @@ -75,6 +77,7 @@ func TestNewConfig(t *testing.T) {
"bottoken": []byte("test-token-key"),
},
expectedConfig: Config{
APIURL: APIURL,
BotToken: "test-token-key",
ChatID: "test-chat-id",
Message: templates.DefaultMessageEmbed,
Expand All @@ -96,6 +99,7 @@ func TestNewConfig(t *testing.T) {
"bottoken": []byte("test-token"),
},
expectedConfig: Config{
APIURL: APIURL,
BotToken: "test-token",
ChatID: "chat-id",
Message: templates.DefaultMessageEmbed,
Expand All @@ -109,6 +113,7 @@ func TestNewConfig(t *testing.T) {
name: "Extracts all fields",
settings: FullValidConfigForTesting,
expectedConfig: Config{
APIURL: "https://api.test.com/bot%s/%s",
BotToken: "test-token",
ChatID: "12345678",
Message: "test-message",
Expand All @@ -124,6 +129,7 @@ func TestNewConfig(t *testing.T) {
settings: FullValidConfigForTesting,
secureSettings: receiversTesting.ReadSecretsJSONForTesting(FullValidSecretsForTesting),
expectedConfig: Config{
APIURL: "https://api.test.com/bot%s/%s",
BotToken: "test-secret-token",
ChatID: "12345678",
Message: "test-message",
Expand All @@ -149,6 +155,7 @@ func TestNewConfig(t *testing.T) {
"bottoken": []byte("test-token"),
},
expectedConfig: Config{
APIURL: APIURL,
BotToken: "test-token",
ChatID: "12345678",
Message: templates.DefaultMessageEmbed,
Expand All @@ -165,6 +172,7 @@ func TestNewConfig(t *testing.T) {
"bottoken": []byte("test-token"),
},
expectedConfig: Config{
APIURL: APIURL,
BotToken: "test-token",
ChatID: "12345678",
Message: templates.DefaultMessageEmbed,
Expand All @@ -181,6 +189,7 @@ func TestNewConfig(t *testing.T) {
"bottoken": []byte("test-token"),
},
expectedConfig: Config{
APIURL: APIURL,
BotToken: "test-token",
ChatID: "12345678",
Message: templates.DefaultMessageEmbed,
Expand Down
2 changes: 1 addition & 1 deletion receivers/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (tn *Notifier) newWebhookSyncCmd(action string, fn func(writer *multipart.W
}

cmd := &receivers.SendWebhookSettings{
URL: fmt.Sprintf(APIURL, tn.settings.BotToken, action),
URL: fmt.Sprintf(tn.settings.APIURL, tn.settings.BotToken, action),
Body: b.String(),
HTTPMethod: "POST",
HTTPHeader: map[string]string{
Expand Down
1 change: 1 addition & 0 deletions receivers/telegram/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package telegram

// FullValidConfigForTesting is a string representation of a JSON object that contains all fields supported by the notifier Config. It can be used without secrets.
const FullValidConfigForTesting = `{
"api_url": "https://api.test.com/bot%s/%s",
"bottoken" :"test-token",
"chatid" :"12345678",
"message" :"test-message",
Expand Down