Skip to content

Commit

Permalink
Merge pull request #48 from ryok-0319/enable-slack-env-vars
Browse files Browse the repository at this point in the history
Enable slack env vars for channel id and bot name
  • Loading branch information
b4b4r07 authored Sep 24, 2019
2 parents c6550df + a06aa33 commit 5ee81dd
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions notifier/slack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import (
// EnvToken is Slack API Token
const EnvToken = "SLACK_TOKEN"

// EnvChannelID is Slack channel ID
const EnvChannelID = "SLACK_CHANNEL_ID"

// EnvBotName is Slack bot name
const EnvBotName = "SLACK_BOT_NAME"

// Client is a API client for Slack
type Client struct {
*slack.Client
Expand Down Expand Up @@ -51,6 +57,19 @@ func NewClient(cfg Config) (*Client, error) {
if token == "" {
return &Client{}, errors.New("slack token is missing")
}

channel := cfg.Channel
channel = strings.TrimPrefix(channel, "$")
if channel == EnvChannelID {
channel = os.Getenv(EnvChannelID)
}

botname := cfg.Botname
botname = strings.TrimPrefix(botname, "$")
if botname == EnvBotName {
botname = os.Getenv(EnvBotName)
}

client := slack.New(token)
c := &Client{
Config: cfg,
Expand All @@ -60,8 +79,8 @@ func NewClient(cfg Config) (*Client, error) {
c.Notify = (*NotifyService)(&c.common)
c.API = &Slack{
Client: client,
Channel: cfg.Channel,
Botname: cfg.Botname,
Channel: channel,
Botname: botname,
}
return c, nil
}

0 comments on commit 5ee81dd

Please sign in to comment.