| 1 |
package telegram |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"net/url" |
| 7 |
"strings" |
| 8 |
|
| 9 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 10 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 11 |
) |
| 12 |
|
| 13 |
// Config holds settings for the Telegram notification service. |
| 14 |
type Config struct { |
| 15 |
Token string `url:"user"` |
| 16 |
Preview bool ` default:"Yes" desc:"If disabled, no web page preview will be displayed for URLs" key:"preview"` |
| 17 |
Notification bool ` default:"Yes" desc:"If disabled, sends Message silently" key:"notification"` |
| 18 |
ParseMode parseMode ` default:"None" desc:"How the text Message should be parsed" key:"parsemode"` |
| 19 |
Chats []string ` desc:"Chat IDs or Channel names (using @channel-name)" key:"chats,channels"` |
| 20 |
Title string ` default:"" desc:"Notification title, optionally set by the sender" key:"title"` |
| 21 |
} |
| 22 |
|
| 23 |
// Scheme identifies this service in configuration URLs. |
| 24 |
const ( |
| 25 |
Scheme = "telegram" |
| 26 |
) |
| 27 |
|
| 28 |
// ErrInvalidToken indicates an invalid Telegram token format or content. |
| 29 |
var ( |
| 30 |
ErrInvalidToken = errors.New("invalid telegram token") |
| 31 |
ErrNoChannelsDefined = errors.New("no channels defined in config URL") |
| 32 |
) |
| 33 |
|
| 34 |
// Enums returns the fields that use an EnumFormatter for their values. |
| 35 |
func (c *Config) Enums() map[string]types.EnumFormatter { |
| 36 |
return map[string]types.EnumFormatter{ |
| 37 |
"ParseMode": ParseModes.Enum, |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// GetURL generates a URL from the current configuration values. |
| 42 |
func (c *Config) GetURL() *url.URL { |
| 43 |
resolver := format.NewPropKeyResolver(c) |
| 44 |
|
| 45 |
return c.getURL(&resolver) |
| 46 |
} |
| 47 |
|
| 48 |
// SetURL updates the configuration from a URL representation. |
| 49 |
func (c *Config) SetURL(serviceURL *url.URL) error { |
| 50 |
resolver := format.NewPropKeyResolver(c) |
| 51 |
|
| 52 |
return c.setURL(&resolver, serviceURL) |
| 53 |
} |
| 54 |
|
| 55 |
// getURL constructs a URL from the Config's fields using the provided resolver. |
| 56 |
func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL { |
| 57 |
tokenParts := strings.Split(c.Token, ":") |
| 58 |
|
| 59 |
return &url.URL{ |
| 60 |
User: url.UserPassword(tokenParts[0], tokenParts[1]), |
| 61 |
Host: Scheme, |
| 62 |
Scheme: Scheme, |
| 63 |
ForceQuery: true, |
| 64 |
RawQuery: format.BuildQuery(resolver), |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
// setURL updates the Config from a URL using the provided resolver. |
| 69 |
func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error { |
| 70 |
password, _ := serviceURL.User.Password() |
| 71 |
|
| 72 |
token := serviceURL.User.Username() + ":" + password |
| 73 |
if serviceURL.String() != "telegram://[email protected]" { |
| 74 |
if !IsTokenValid(token) { |
| 75 |
return fmt.Errorf("%w: %s", ErrInvalidToken, token) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
for key, vals := range serviceURL.Query() { |
| 80 |
if err := resolver.Set(key, vals[0]); err != nil { |
| 81 |
return fmt.Errorf("setting config property %q from URL query: %w", key, err) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if serviceURL.String() != "telegram://[email protected]" { |
| 86 |
if len(c.Chats) < 1 { |
| 87 |
return ErrNoChannelsDefined |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
c.Token = token |
| 92 |
|
| 93 |
return nil |
| 94 |
} |