From c3eaaab161371197b4f3bbd8145698c5788219df Mon Sep 17 00:00:00 2001 From: Maksim Ivanyukhin Date: Mon, 8 Oct 2018 18:56:46 +0300 Subject: [PATCH 1/3] add ParseMode config for Telegram --- provider/telegram/telegram.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/provider/telegram/telegram.go b/provider/telegram/telegram.go index 90d3468..4dac41d 100644 --- a/provider/telegram/telegram.go +++ b/provider/telegram/telegram.go @@ -2,27 +2,41 @@ package telegram import ( "strconv" + "strings" "github.com/messagebird/sachet" "gopkg.in/telegram-bot-api.v4" ) type TelegramConfig struct { - Token string `yaml:"token"` + Token string `yaml:"token"` + ParseMode string `yaml:"parse_mode"` } type Telegram struct { - bot *tgbotapi.BotAPI + bot *tgbotapi.BotAPI + ParseMode string } func NewTelegram(config TelegramConfig) (*Telegram, error) { + ParseMode := strings.ToLower(config.ParseMode) + switch ParseMode { + case "md", "markdown": + ParseMode = "Markdown" + case "html", "h": + ParseMode = "HTML" + default: + ParseMode = "" + } + bot, err := tgbotapi.NewBotAPI(config.Token) if err != nil { return nil, err } return &Telegram{ - bot: bot, + bot: bot, + ParseMode: ParseMode, }, nil } @@ -33,6 +47,7 @@ func (tg *Telegram) Send(message sachet.Message) error { return err } msg := tgbotapi.NewMessage(chatID, message.Text) + msg.ParseMode = tg.ParseMode _, err = tg.bot.Send(msg) if err != nil { return err From 8037a9ee56698bdab0febb7dfd1a34aeb31335f6 Mon Sep 17 00:00:00 2001 From: Benjamin Delacour Date: Fri, 12 Nov 2021 22:00:30 +0100 Subject: [PATCH 2/3] pass config to Telegram struct + rename TelegramConfig to Config + stop parsing parse_mode (trust config) --- cmd/sachet/config.go | 2 +- provider/telegram/telegram.go | 28 +++++++++------------------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/cmd/sachet/config.go b/cmd/sachet/config.go index 9720618..68ecc8f 100644 --- a/cmd/sachet/config.go +++ b/cmd/sachet/config.go @@ -53,7 +53,7 @@ var config struct { Exotel exotel.ExotelConfig CM cm.CMConfig MailruIM mailruim.MailruIMConfig - Telegram telegram.TelegramConfig + Telegram telegram.Config Turbosms turbosms.TurbosmsConfig Smsc smsc.SmscConfig OTC otc.OTCConfig diff --git a/provider/telegram/telegram.go b/provider/telegram/telegram.go index 4dac41d..cbf8b85 100644 --- a/provider/telegram/telegram.go +++ b/provider/telegram/telegram.go @@ -1,42 +1,30 @@ package telegram import ( - "strconv" - "strings" - "github.com/messagebird/sachet" "gopkg.in/telegram-bot-api.v4" + "strconv" ) -type TelegramConfig struct { +type Config struct { Token string `yaml:"token"` ParseMode string `yaml:"parse_mode"` } type Telegram struct { bot *tgbotapi.BotAPI - ParseMode string + config *Config } -func NewTelegram(config TelegramConfig) (*Telegram, error) { - ParseMode := strings.ToLower(config.ParseMode) - switch ParseMode { - case "md", "markdown": - ParseMode = "Markdown" - case "html", "h": - ParseMode = "HTML" - default: - ParseMode = "" - } - +func NewTelegram(config Config) (*Telegram, error) { bot, err := tgbotapi.NewBotAPI(config.Token) if err != nil { return nil, err } return &Telegram{ - bot: bot, - ParseMode: ParseMode, + bot: bot, + config: &config, }, nil } @@ -46,8 +34,10 @@ func (tg *Telegram) Send(message sachet.Message) error { if err != nil { return err } + msg := tgbotapi.NewMessage(chatID, message.Text) - msg.ParseMode = tg.ParseMode + msg.ParseMode = tg.config.ParseMode + _, err = tg.bot.Send(msg) if err != nil { return err From 18cb0bfac9be295f4d204545a155bb47e3acc34e Mon Sep 17 00:00:00 2001 From: Benjamin Delacour Date: Mon, 15 Nov 2021 11:03:20 +0100 Subject: [PATCH 3/3] add DisableWebPagePreview feature to Telegram provider --- provider/telegram/telegram.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/provider/telegram/telegram.go b/provider/telegram/telegram.go index cbf8b85..2d5ec59 100644 --- a/provider/telegram/telegram.go +++ b/provider/telegram/telegram.go @@ -7,13 +7,14 @@ import ( ) type Config struct { - Token string `yaml:"token"` - ParseMode string `yaml:"parse_mode"` + Token string `yaml:"token"` + ParseMode string `yaml:"parse_mode"` + DisableWebPagePreview bool `yaml:"disable_web_page_preview"` } type Telegram struct { - bot *tgbotapi.BotAPI - config *Config + bot *tgbotapi.BotAPI + config *Config } func NewTelegram(config Config) (*Telegram, error) { @@ -37,6 +38,7 @@ func (tg *Telegram) Send(message sachet.Message) error { msg := tgbotapi.NewMessage(chatID, message.Text) msg.ParseMode = tg.config.ParseMode + msg.DisableWebPagePreview = tg.config.DisableWebPagePreview _, err = tg.bot.Send(msg) if err != nil {