You've already forked goreleaser
mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-09-16 09:26:52 +02:00
Add telegram announcer (#2563)
* feat: add telegram announcer * chore: remove hooks and use the Send method of the bot API * chore: add docs and default config * chore: add docs and default config * chore: fix tabs * chore: add docs * chore: fix pr comments * chore: fix pr comments * chore: fix pr comments * chore: add missing links in docs
This commit is contained in:
committed by
GitHub
parent
aca255493b
commit
d32454e279
2
go.mod
2
go.mod
@@ -129,7 +129,9 @@ require (
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.4.0 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.7.0 // indirect
|
||||
github.com/aws/smithy-go v1.8.0 // indirect
|
||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible // indirect
|
||||
github.com/golang-jwt/jwt/v4 v4.0.0 // indirect
|
||||
github.com/gorilla/websocket v1.4.2 // indirect
|
||||
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
|
||||
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@@ -295,6 +295,8 @@ github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+
|
||||
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
|
||||
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
|
||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
|
||||
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
|
||||
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
|
||||
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
|
||||
@@ -632,6 +634,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
|
||||
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
|
||||
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
|
||||
github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLDRpvE+3b7gP/C2YyLFYxNmcLnPTMe0=
|
||||
github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk=
|
||||
github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk=
|
||||
|
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/goreleaser/goreleaser/internal/pipe/slack"
|
||||
"github.com/goreleaser/goreleaser/internal/pipe/smtp"
|
||||
"github.com/goreleaser/goreleaser/internal/pipe/teams"
|
||||
"github.com/goreleaser/goreleaser/internal/pipe/telegram"
|
||||
"github.com/goreleaser/goreleaser/internal/pipe/twitter"
|
||||
"github.com/goreleaser/goreleaser/internal/tmpl"
|
||||
"github.com/goreleaser/goreleaser/pkg/context"
|
||||
@@ -35,6 +36,7 @@ var announcers = []Announcer{
|
||||
slack.Pipe{},
|
||||
smtp.Pipe{},
|
||||
teams.Pipe{},
|
||||
telegram.Pipe{},
|
||||
twitter.Pipe{},
|
||||
}
|
||||
|
||||
|
55
internal/pipe/telegram/telegram.go
Normal file
55
internal/pipe/telegram/telegram.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/caarlos0/env/v6"
|
||||
api "github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"github.com/goreleaser/goreleaser/internal/tmpl"
|
||||
"github.com/goreleaser/goreleaser/pkg/context"
|
||||
)
|
||||
|
||||
const defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .GitURL }}/releases/tag/{{ .Tag }}`
|
||||
|
||||
type Pipe struct{}
|
||||
|
||||
func (Pipe) String() string { return "telegram" }
|
||||
func (Pipe) Skip(ctx *context.Context) bool { return !ctx.Config.Announce.Telegram.Enabled }
|
||||
|
||||
type Config struct {
|
||||
ConsumerToken string `env:"TELEGRAM_TOKEN,notEmpty"`
|
||||
}
|
||||
|
||||
func (Pipe) Default(ctx *context.Context) error {
|
||||
if ctx.Config.Announce.Telegram.MessageTemplate == "" {
|
||||
ctx.Config.Announce.Telegram.MessageTemplate = defaultMessageTemplate
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (Pipe) Announce(ctx *context.Context) error {
|
||||
msg, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Telegram.MessageTemplate)
|
||||
if err != nil {
|
||||
return fmt.Errorf("announce: failed to announce to telegram: %w", err)
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := env.Parse(&cfg); err != nil {
|
||||
return fmt.Errorf("announce: failed to announce to telegram: %w", err)
|
||||
}
|
||||
|
||||
log.Infof("posting: '%s'", msg)
|
||||
bot, err := api.NewBotAPI(cfg.ConsumerToken)
|
||||
if err != nil {
|
||||
return fmt.Errorf("announce: failed to announce to telegram: %w", err)
|
||||
}
|
||||
|
||||
tm := api.NewMessage(ctx.Config.Announce.Telegram.ChatID, msg)
|
||||
_, err = bot.Send(tm)
|
||||
if err != nil {
|
||||
return fmt.Errorf("announce: failed to announce to telegram: %w", err)
|
||||
}
|
||||
log.Debug("message sent")
|
||||
return nil
|
||||
}
|
57
internal/pipe/telegram/telegram_test.go
Normal file
57
internal/pipe/telegram/telegram_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/goreleaser/goreleaser/pkg/config"
|
||||
"github.com/goreleaser/goreleaser/pkg/context"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestStringer(t *testing.T) {
|
||||
require.Equal(t, Pipe{}.String(), "telegram")
|
||||
}
|
||||
|
||||
func TestDefault(t *testing.T) {
|
||||
ctx := context.New(config.Project{})
|
||||
require.NoError(t, Pipe{}.Default(ctx))
|
||||
require.Equal(t, ctx.Config.Announce.Telegram.MessageTemplate, defaultMessageTemplate)
|
||||
}
|
||||
|
||||
func TestAnnounceInvalidTemplate(t *testing.T) {
|
||||
ctx := context.New(config.Project{
|
||||
Announce: config.Announce{
|
||||
Telegram: config.Telegram{
|
||||
MessageTemplate: "{{ .Foo }",
|
||||
},
|
||||
},
|
||||
})
|
||||
require.EqualError(t, Pipe{}.Announce(ctx), `announce: failed to announce to telegram: template: tmpl:1: unexpected "}" in operand`)
|
||||
}
|
||||
|
||||
func TestAnnounceMissingEnv(t *testing.T) {
|
||||
ctx := context.New(config.Project{
|
||||
Announce: config.Announce{
|
||||
Telegram: config.Telegram{},
|
||||
},
|
||||
})
|
||||
require.NoError(t, Pipe{}.Default(ctx))
|
||||
require.EqualError(t, Pipe{}.Announce(ctx), `announce: failed to announce to telegram: env: environment variable "TELEGRAM_TOKEN" should not be empty`)
|
||||
}
|
||||
|
||||
func TestSkip(t *testing.T) {
|
||||
t.Run("skip", func(t *testing.T) {
|
||||
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
|
||||
})
|
||||
|
||||
t.Run("dont skip", func(t *testing.T) {
|
||||
ctx := context.New(config.Project{
|
||||
Announce: config.Announce{
|
||||
Telegram: config.Telegram{
|
||||
Enabled: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
require.False(t, Pipe{}.Skip(ctx))
|
||||
})
|
||||
}
|
@@ -717,6 +717,7 @@ type Announce struct {
|
||||
Teams Teams `yaml:"teams,omitempty"`
|
||||
SMTP SMTP `yaml:"smtp,omitempty"`
|
||||
Mattermost Mattermost `yaml:"mattermost,omitempty"`
|
||||
Telegram Telegram `yaml:"telegram,omitempty"`
|
||||
}
|
||||
|
||||
type Twitter struct {
|
||||
@@ -781,6 +782,12 @@ type SMTP struct {
|
||||
InsecureSkipVerify bool `yaml:"insecure_skip_verify,omitempty"`
|
||||
}
|
||||
|
||||
type Telegram struct {
|
||||
Enabled bool `yaml:"enabled,omitempty"`
|
||||
MessageTemplate string `yaml:"message_template,omitempty"`
|
||||
ChatID int64 `yaml:"chat_id,omitempty"`
|
||||
}
|
||||
|
||||
// Load config file.
|
||||
func Load(file string) (config Project, err error) {
|
||||
f, err := os.Open(file) // #nosec
|
||||
|
@@ -29,6 +29,7 @@ import (
|
||||
"github.com/goreleaser/goreleaser/internal/pipe/snapshot"
|
||||
"github.com/goreleaser/goreleaser/internal/pipe/sourcearchive"
|
||||
"github.com/goreleaser/goreleaser/internal/pipe/teams"
|
||||
"github.com/goreleaser/goreleaser/internal/pipe/telegram"
|
||||
"github.com/goreleaser/goreleaser/internal/pipe/twitter"
|
||||
"github.com/goreleaser/goreleaser/pkg/context"
|
||||
)
|
||||
@@ -72,4 +73,5 @@ var Defaulters = []Defaulter{
|
||||
smtp.Pipe{},
|
||||
mattermost.Pipe{},
|
||||
milestone.Pipe{},
|
||||
telegram.Pipe{},
|
||||
}
|
||||
|
29
www/docs/customization/announce/telegram.md
Normal file
29
www/docs/customization/announce/telegram.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# Telegram
|
||||
|
||||
For it to work, you'll need to [create a new Telegram app](https://core.telegram.org/bots), and set
|
||||
some environment variables on your pipeline:
|
||||
|
||||
- `TELEGRAM_TOKEN`
|
||||
|
||||
Also you need to know your channel's chat ID to talk with.
|
||||
|
||||
Then, you can add something like the following to your `.goreleaser.yml` config:
|
||||
|
||||
```yaml
|
||||
# .goreleaser.yml
|
||||
announce:
|
||||
telegram:
|
||||
# Whether its enabled or not.
|
||||
# Defaults to false.
|
||||
enabled: true
|
||||
|
||||
# Integer representation of your channel
|
||||
chat_id: 123456
|
||||
|
||||
# Message template to use while publishing.
|
||||
# Defaults to `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .GitURL }}/releases/tag/{{ .Tag }}`
|
||||
message_template: 'Awesome project {{.Tag}} is out!'
|
||||
```
|
||||
|
||||
!!! tip
|
||||
Learn more about the [name template engine](/customization/templates/).
|
@@ -114,6 +114,7 @@ nav:
|
||||
- customization/announce/teams.md
|
||||
- customization/announce/twitter.md
|
||||
- customization/announce/mattermost.md
|
||||
- customization/announce/telegram.md
|
||||
- Command Line Usage:
|
||||
- goreleaser: cmd/goreleaser.md
|
||||
- goreleaser init: cmd/goreleaser_init.md
|
||||
|
Reference in New Issue
Block a user