Skip to content

Commit

Permalink
pass config to Telegram struct + rename TelegramConfig to Config + st…
Browse files Browse the repository at this point in the history
…op parsing parse_mode (trust config)
  • Loading branch information
Benjamin Delacour committed Nov 12, 2021
1 parent 7f4bc48 commit 8037a9e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd/sachet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 9 additions & 19 deletions provider/telegram/telegram.go
Original file line number Diff line number Diff line change
@@ -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
}

Expand All @@ -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
Expand Down

0 comments on commit 8037a9e

Please sign in to comment.