github.com/triarius/goreleaser@v1.12.5/internal/pipe/telegram/telegram.go (about)

     1  package telegram
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/caarlos0/env/v6"
     7  	"github.com/caarlos0/log"
     8  	api "github.com/go-telegram-bot-api/telegram-bot-api"
     9  	"github.com/triarius/goreleaser/internal/tmpl"
    10  	"github.com/triarius/goreleaser/pkg/context"
    11  )
    12  
    13  const defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}`
    14  
    15  type Pipe struct{}
    16  
    17  func (Pipe) String() string                 { return "telegram" }
    18  func (Pipe) Skip(ctx *context.Context) bool { return !ctx.Config.Announce.Telegram.Enabled }
    19  
    20  type Config struct {
    21  	ConsumerToken string `env:"TELEGRAM_TOKEN,notEmpty"`
    22  }
    23  
    24  func (Pipe) Default(ctx *context.Context) error {
    25  	if ctx.Config.Announce.Telegram.MessageTemplate == "" {
    26  		ctx.Config.Announce.Telegram.MessageTemplate = defaultMessageTemplate
    27  	}
    28  	return nil
    29  }
    30  
    31  func (Pipe) Announce(ctx *context.Context) error {
    32  	msg, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Telegram.MessageTemplate)
    33  	if err != nil {
    34  		return fmt.Errorf("announce: failed to announce to telegram: %w", err)
    35  	}
    36  
    37  	var cfg Config
    38  	if err := env.Parse(&cfg); err != nil {
    39  		return fmt.Errorf("announce: failed to announce to telegram: %w", err)
    40  	}
    41  
    42  	log.Infof("posting: '%s'", msg)
    43  	bot, err := api.NewBotAPI(cfg.ConsumerToken)
    44  	if err != nil {
    45  		return fmt.Errorf("announce: failed to announce to telegram: %w", err)
    46  	}
    47  
    48  	tm := api.NewMessage(ctx.Config.Announce.Telegram.ChatID, msg)
    49  	tm.ParseMode = "MarkdownV2"
    50  	_, err = bot.Send(tm)
    51  	if err != nil {
    52  		return fmt.Errorf("announce: failed to announce to telegram: %w", err)
    53  	}
    54  	log.Debug("message sent")
    55  	return nil
    56  }