diff options
author | Wim <wim@42.be> | 2017-07-15 16:49:47 +0200 |
---|---|---|
committer | Wim <wim@42.be> | 2017-07-15 16:49:47 +0200 |
commit | 9dcd51fb80baa75c50323b8094552bfd660a77e8 (patch) | |
tree | 3017a17e166363bd7e776b18d3e4a7e82e606d40 /bridge/slack | |
parent | 6dee988b76852c6b15ef59061896ce48ce6ba978 (diff) | |
download | matterbridge-msglm-9dcd51fb80baa75c50323b8094552bfd660a77e8.tar.gz matterbridge-msglm-9dcd51fb80baa75c50323b8094552bfd660a77e8.tar.bz2 matterbridge-msglm-9dcd51fb80baa75c50323b8094552bfd660a77e8.zip |
Refactor connecting logic slack/mattermost. Fixes #216
Diffstat (limited to 'bridge/slack')
-rw-r--r-- | bridge/slack/slack.go | 57 |
1 files changed, 44 insertions, 13 deletions
diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index 6555d40b..1e956e9c 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -1,6 +1,7 @@ package bslack import ( + "errors" "fmt" "github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/matterhook" @@ -53,22 +54,52 @@ func (b *Bslack) Command(cmd string) string { } func (b *Bslack) Connect() error { - if b.Config.WebhookURL != "" && b.Config.WebhookBindAddress != "" { - flog.Info("Connecting using webhookurl and webhookbindaddress") - b.mh = matterhook.New(b.Config.WebhookURL, - matterhook.Config{BindAddress: b.Config.WebhookBindAddress}) - } else if b.Config.WebhookURL != "" { - flog.Info("Connecting using webhookurl (for posting) and token") + if b.Config.WebhookBindAddress != "" { + if b.Config.WebhookURL != "" { + flog.Info("Connecting using webhookurl (sending) and webhookbindaddress (receiving)") + b.mh = matterhook.New(b.Config.WebhookURL, + matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify, + BindAddress: b.Config.WebhookBindAddress}) + } else if b.Config.Token != "" { + flog.Info("Connecting using token (sending)") + b.sc = slack.New(b.Config.Token) + b.rtm = b.sc.NewRTM() + go b.rtm.ManageConnection() + flog.Info("Connecting using webhookbindaddress (receiving)") + b.mh = matterhook.New(b.Config.WebhookURL, + matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify, + BindAddress: b.Config.WebhookBindAddress}) + } else { + flog.Info("Connecting using webhookbindaddress (receiving)") + b.mh = matterhook.New(b.Config.WebhookURL, + matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify, + BindAddress: b.Config.WebhookBindAddress}) + } + go b.handleSlack() + return nil + } + if b.Config.WebhookURL != "" { + flog.Info("Connecting using webhookurl (sending)") b.mh = matterhook.New(b.Config.WebhookURL, - matterhook.Config{DisableServer: true}) - } else { - flog.Info("Connecting using token") + matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify, + DisableServer: true}) + if b.Config.Token != "" { + flog.Info("Connecting using token (receiving)") + b.sc = slack.New(b.Config.Token) + b.rtm = b.sc.NewRTM() + go b.rtm.ManageConnection() + go b.handleSlack() + } + } else if b.Config.Token != "" { + flog.Info("Connecting using token (sending and receiving)") b.sc = slack.New(b.Config.Token) b.rtm = b.sc.NewRTM() go b.rtm.ManageConnection() + go b.handleSlack() + } + if b.Config.WebhookBindAddress == "" && b.Config.WebhookURL == "" && b.Config.Token == "" { + return errors.New("No connection method found. See that you have WebhookBindAddress, WebhookURL or Token configured.") } - flog.Info("Connection succeeded") - go b.handleSlack() return nil } @@ -79,7 +110,7 @@ func (b *Bslack) Disconnect() error { func (b *Bslack) JoinChannel(channel string) error { // we can only join channels using the API - if b.Config.WebhookURL == "" || b.Config.WebhookBindAddress == "" { + if b.Config.WebhookURL == "" && b.Config.WebhookBindAddress == "" { if strings.HasPrefix(b.Config.Token, "xoxb") { // TODO check if bot has already joined channel return nil @@ -176,7 +207,7 @@ func (b *Bslack) getChannelByID(ID string) (*slack.Channel, error) { func (b *Bslack) handleSlack() { mchan := make(chan *MMMessage) - if b.Config.WebhookBindAddress != "" && b.Config.WebhookURL != "" { + if b.Config.WebhookBindAddress != "" { flog.Debugf("Choosing webhooks based receiving") go b.handleMatterHook(mchan) } else { |