1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-08 03:31:59 +02:00
goreleaser/internal/pipe/telegram/telegram.go
Batuhan Apaydın b99ccc029c
feat: add message template support for git providers (#2626)
* feat: add message template support for git providers

Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com>
Co-authored-by: Furkan Türkal <furkan.turkal@trendyol.com>
Co-authored-by: Erkan Zileli <erkan.zileli@trendyol.com>
Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com>

* Apply suggestions from code review

Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Co-authored-by: Furkan Türkal <furkan.turkal@trendyol.com>
Co-authored-by: Erkan Zileli <erkan.zileli@trendyol.com>
2021-11-06 15:39:43 -03:00

56 lines
1.5 KiB
Go

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 {{ .ReleaseURL }}`
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
}