| 1 |
package slack |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/url" |
| 6 |
|
| 7 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 8 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 9 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 10 |
) |
| 11 |
|
| 12 |
// Config for the slack service. |
| 13 |
type Config struct { |
| 14 |
standard.EnumlessConfig |
| 15 |
|
| 16 |
BotName string `desc:"Bot name" key:"botname,username" optional:"uses bot default"` |
| 17 |
Icon string `desc:"Use emoji or URL as icon (based on presence of http(s):// prefix)" key:"icon,icon_emoji,icon_url" optional:"" default:""` |
| 18 |
Token Token `desc:"API Bot token" url:"user,pass"` |
| 19 |
Color string `desc:"Message left-hand border color" key:"color" optional:"default border color"` |
| 20 |
Title string `desc:"Prepended text above the message" key:"title" optional:"omitted"` |
| 21 |
Channel string `desc:"Channel to send messages to in Cxxxxxxxxxx format" url:"host"` |
| 22 |
ThreadTS string `desc:"ts value of the parent message (to send message as reply in thread)" key:"thread_ts" optional:""` |
| 23 |
} |
| 24 |
|
| 25 |
const ( |
| 26 |
// Scheme is the identifying part of this service's configuration URL. |
| 27 |
Scheme = "slack" |
| 28 |
) |
| 29 |
|
| 30 |
// GetURL returns a URL representation of it's current field values. |
| 31 |
func (c *Config) GetURL() *url.URL { |
| 32 |
resolver := format.NewPropKeyResolver(c) |
| 33 |
|
| 34 |
return c.getURL(&resolver) |
| 35 |
} |
| 36 |
|
| 37 |
// SetURL updates a ServiceConfig from a URL representation of it's field values. |
| 38 |
func (c *Config) SetURL(serviceURL *url.URL) error { |
| 39 |
resolver := format.NewPropKeyResolver(c) |
| 40 |
|
| 41 |
return c.setURL(&resolver, serviceURL) |
| 42 |
} |
| 43 |
|
| 44 |
func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL { |
| 45 |
return &url.URL{ |
| 46 |
User: c.Token.UserInfo(), |
| 47 |
Host: c.Channel, |
| 48 |
Scheme: Scheme, |
| 49 |
ForceQuery: false, |
| 50 |
RawQuery: format.BuildQuery(resolver), |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error { |
| 55 |
var token string |
| 56 |
|
| 57 |
if len(serviceURL.Path) > 1 { |
| 58 |
// Reading legacy config URL format |
| 59 |
token = serviceURL.Hostname() + serviceURL.Path |
| 60 |
c.Channel = "webhook" |
| 61 |
c.BotName = serviceURL.User.Username() |
| 62 |
} else { |
| 63 |
token = serviceURL.User.String() |
| 64 |
c.Channel = serviceURL.Hostname() |
| 65 |
} |
| 66 |
|
| 67 |
if serviceURL.String() != "slack://[email protected]" { |
| 68 |
if err := c.Token.SetFromProp(token); err != nil { |
| 69 |
return err |
| 70 |
} |
| 71 |
} else { |
| 72 |
c.Token.raw = token // Set raw token without validation |
| 73 |
} |
| 74 |
|
| 75 |
for key, vals := range serviceURL.Query() { |
| 76 |
if err := resolver.Set(key, vals[0]); err != nil { |
| 77 |
return fmt.Errorf("setting query parameter %q to %q: %w", key, vals[0], err) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return nil |
| 82 |
} |
| 83 |
|
| 84 |
// CreateConfigFromURL to use within the slack service. |
| 85 |
func CreateConfigFromURL(serviceURL *url.URL) (*Config, error) { |
| 86 |
config := Config{} |
| 87 |
err := config.SetURL(serviceURL) |
| 88 |
|
| 89 |
return &config, err |
| 90 |
} |