Simplify the Slack bridge's connection logic.
- Refactor of complex connection if-else logic. - Print warning when using Slack legacy tokens.
This commit is contained in:
@@ -90,55 +90,43 @@ func (b *Bslack) Command(cmd string) string {
|
|||||||
func (b *Bslack) Connect() error {
|
func (b *Bslack) Connect() error {
|
||||||
b.RLock()
|
b.RLock()
|
||||||
defer b.RUnlock()
|
defer b.RUnlock()
|
||||||
if b.GetString(incomingWebhookConfig) != "" {
|
|
||||||
if b.GetString(outgoingWebhookConfig) != "" {
|
if b.GetString(incomingWebhookConfig) == "" && b.GetString(outgoingWebhookConfig) == "" && b.GetString(tokenConfig) == "" {
|
||||||
b.Log.Info("Connecting using webhookurl (sending) and webhookbindaddress (receiving)")
|
return errors.New("no connection method found: WebhookBindAddress, WebhookURL or Token need to be configured")
|
||||||
b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{
|
|
||||||
InsecureSkipVerify: b.GetBool(skipTLSConfig),
|
|
||||||
BindAddress: b.GetString(incomingWebhookConfig),
|
|
||||||
})
|
|
||||||
} else if b.GetString(tokenConfig) != "" {
|
|
||||||
b.Log.Info("Connecting using token (sending)")
|
|
||||||
b.sc = slack.New(b.GetString(tokenConfig))
|
|
||||||
b.rtm = b.sc.NewRTM()
|
|
||||||
go b.rtm.ManageConnection()
|
|
||||||
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
|
||||||
b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{
|
|
||||||
InsecureSkipVerify: b.GetBool(skipTLSConfig),
|
|
||||||
BindAddress: b.GetString(incomingWebhookConfig),
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
|
||||||
b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{
|
|
||||||
InsecureSkipVerify: b.GetBool(skipTLSConfig),
|
|
||||||
BindAddress: b.GetString(incomingWebhookConfig),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
go b.handleSlack()
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
if b.GetString(outgoingWebhookConfig) != "" {
|
|
||||||
b.Log.Info("Connecting using webhookurl (sending)")
|
// If we have a token we use the Slack websocket-based RTM for both sending and receiving.
|
||||||
b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{
|
if token := b.GetString(tokenConfig); token != "" {
|
||||||
InsecureSkipVerify: b.GetBool(skipTLSConfig),
|
// Print a warning for legacy non-bot tokens (#527).
|
||||||
DisableServer: true,
|
if !strings.HasPrefix(token, "xoxb") {
|
||||||
})
|
b.Log.Warnf("Using legacy-style non-bot user. It is STRONGLY recommended to use a proper bot-token instead.")
|
||||||
if b.GetString(tokenConfig) != "" {
|
b.Log.Warnf("Slack may deprecate legacy tokens at short notice. See the Matterbridge GitHub wiki for a migration guide.")
|
||||||
b.Log.Info("Connecting using token (receiving)")
|
return nil
|
||||||
b.sc = slack.New(b.GetString(tokenConfig))
|
|
||||||
b.rtm = b.sc.NewRTM()
|
|
||||||
go b.rtm.ManageConnection()
|
|
||||||
go b.handleSlack()
|
|
||||||
}
|
}
|
||||||
} else if b.GetString(tokenConfig) != "" {
|
|
||||||
b.Log.Info("Connecting using token (sending and receiving)")
|
b.Log.Info("Connecting via websocket (receiving + sending) using token")
|
||||||
b.sc = slack.New(b.GetString(tokenConfig))
|
b.sc = slack.New(token)
|
||||||
b.rtm = b.sc.NewRTM()
|
b.rtm = b.sc.NewRTM()
|
||||||
go b.rtm.ManageConnection()
|
go b.rtm.ManageConnection()
|
||||||
go b.handleSlack()
|
go b.handleSlack()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
if b.GetString(incomingWebhookConfig) == "" && b.GetString(outgoingWebhookConfig) == "" && b.GetString(tokenConfig) == "" {
|
|
||||||
return errors.New("no connection method found. See that you have WebhookBindAddress, WebhookURL or Token configured")
|
// In absence of a token we fall back to incoming and outgoing Webhooks.
|
||||||
|
b.mh = matterhook.New(
|
||||||
|
"",
|
||||||
|
matterhook.Config{
|
||||||
|
InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
||||||
|
DisableServer: true,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if b.GetString(outgoingWebhookConfig) != "" {
|
||||||
|
b.mh.Url = b.GetString(outgoingWebhookConfig)
|
||||||
|
}
|
||||||
|
if b.GetString(incomingWebhookConfig) != "" {
|
||||||
|
b.mh.BindAddress = b.GetString(incomingWebhookConfig)
|
||||||
|
b.mh.DisableServer = false
|
||||||
|
go b.handleSlack()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user