2021-10-07 15:01:31 +02:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-01-07 17:20:11 -03:00
|
|
|
"strconv"
|
2021-10-07 15:01:31 +02:00
|
|
|
|
2023-04-07 01:50:09 +00:00
|
|
|
"github.com/caarlos0/env/v8"
|
2022-06-21 21:11:15 -03:00
|
|
|
"github.com/caarlos0/log"
|
2021-10-07 15:01:31 +02:00
|
|
|
api "github.com/go-telegram-bot-api/telegram-bot-api"
|
|
|
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
|
|
|
)
|
|
|
|
|
2021-11-06 21:39:43 +03:00
|
|
|
const defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}`
|
2021-10-07 15:01:31 +02:00
|
|
|
|
|
|
|
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 {
|
2023-01-07 17:20:11 -03:00
|
|
|
tpl := tmpl.New(ctx)
|
|
|
|
msg, err := tpl.Apply(ctx.Config.Announce.Telegram.MessageTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("telegram: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
chatIDStr, err := tpl.Apply(ctx.Config.Announce.Telegram.ChatID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("telegram: %w", err)
|
|
|
|
}
|
|
|
|
chatID, err := strconv.ParseInt(chatIDStr, 10, 64)
|
2021-10-07 15:01:31 +02:00
|
|
|
if err != nil {
|
2022-12-28 12:24:21 -03:00
|
|
|
return fmt.Errorf("telegram: %w", err)
|
2021-10-07 15:01:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var cfg Config
|
|
|
|
if err := env.Parse(&cfg); err != nil {
|
2022-12-28 12:24:21 -03:00
|
|
|
return fmt.Errorf("telegram: %w", err)
|
2021-10-07 15:01:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("posting: '%s'", msg)
|
|
|
|
bot, err := api.NewBotAPI(cfg.ConsumerToken)
|
|
|
|
if err != nil {
|
2022-12-28 12:24:21 -03:00
|
|
|
return fmt.Errorf("telegram: %w", err)
|
2021-10-07 15:01:31 +02:00
|
|
|
}
|
|
|
|
|
2023-01-07 17:20:11 -03:00
|
|
|
tm := api.NewMessage(chatID, msg)
|
2022-10-05 14:35:01 +02:00
|
|
|
tm.ParseMode = "MarkdownV2"
|
2021-10-07 15:01:31 +02:00
|
|
|
_, err = bot.Send(tm)
|
|
|
|
if err != nil {
|
2022-12-28 12:24:21 -03:00
|
|
|
return fmt.Errorf("telegram: %w", err)
|
2021-10-07 15:01:31 +02:00
|
|
|
}
|
|
|
|
log.Debug("message sent")
|
|
|
|
return nil
|
|
|
|
}
|