| 1 | package telegram |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 11 | "github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 12 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 13 | ) |
| 14 | |
| 15 | // Service sends notifications to configured Telegram chats. |
| 16 | type Service struct { |
| 17 | standard.Standard |
| 18 | |
| 19 | Config *Config |
| 20 | pkr format.PropKeyResolver |
| 21 | httpClient types.HTTPClient |
| 22 | } |
| 23 | |
| 24 | // apiFormat defines the Telegram API endpoint template. |
| 25 | const ( |
| 26 | apiFormat = "https://api.telegram.org/bot%s/%s" |
| 27 | maxlength = 4096 |
| 28 | defaultHTTPTimeout = 10 * time.Second |
| 29 | ) |
| 30 | |
| 31 | // ErrMessageTooLong indicates that the message exceeds the maximum allowed length. |
| 32 | var ( |
| 33 | ErrMessageTooLong = errors.New("Message exceeds the max length") |
| 34 | ) |
| 35 | |
| 36 | // GetConfig returns the current configuration for the service. |
| 37 | func (s *Service) GetConfig() *Config { |
| 38 | return s.Config |
| 39 | } |
| 40 | |
| 41 | // GetID returns the identifier for this service. |
| 42 | func (s *Service) GetID() string { |
| 43 | return Scheme |
| 44 | } |
| 45 | |
| 46 | // Initialize configures the service with a URL and logger. |
| 47 | func (s *Service) Initialize(serviceURL *url.URL, logger types.StdLogger) error { |
| 48 | s.SetLogger(logger) |
| 49 | s.Config = &Config{ |
| 50 | Preview: true, |
| 51 | Notification: true, |
| 52 | } |
| 53 | s.pkr = format.NewPropKeyResolver(s.Config) |
| 54 | |
| 55 | if err := s.Config.setURL(&s.pkr, serviceURL); err != nil { |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | // Send delivers a notification message to Telegram. |
| 63 | func (s *Service) Send(message string, params *types.Params) error { |
| 64 | if len(message) > maxlength { |
| 65 | return ErrMessageTooLong |
| 66 | } |
| 67 | |
| 68 | config := *s.Config |
| 69 | if err := s.pkr.UpdateConfigFromParams(&config, params); err != nil { |
| 70 | return fmt.Errorf("updating config from params: %w", err) |
| 71 | } |
| 72 | |
| 73 | return s.sendMessageForChatIDs(message, &config) |
| 74 | } |
| 75 | |
| 76 | // SetHTTPClient sets a custom HTTP client for the service. |
| 77 | func (s *Service) SetHTTPClient(client types.HTTPClient) { |
| 78 | s.httpClient = client |
| 79 | } |
| 80 | |
| 81 | // httpClientOrDefault returns the custom client or a default client. |
| 82 | func (s *Service) httpClientOrDefault() types.HTTPClient { |
| 83 | if s.httpClient != nil { |
| 84 | return s.httpClient |
| 85 | } |
| 86 | |
| 87 | return &http.Client{Timeout: defaultHTTPTimeout} |
| 88 | } |
| 89 | |
| 90 | // sendMessageForChatIDs sends the message to all configured chat IDs. |
| 91 | func (s *Service) sendMessageForChatIDs(message string, config *Config) error { |
| 92 | for _, chat := range s.Config.Chats { |
| 93 | if err := s.sendMessageToAPI(message, chat, config); err != nil { |
| 94 | return err |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return nil |
| 99 | } |
| 100 | |
| 101 | // sendMessageToAPI sends a message to the Telegram API for a specific chat. |
| 102 | func (s *Service) sendMessageToAPI(message, chat string, config *Config) error { |
| 103 | client := &Client{token: config.Token, httpClient: s.httpClientOrDefault()} |
| 104 | payload := createSendMessagePayload(message, chat, config) |
| 105 | _, err := client.SendMessage(&payload) |
| 106 | |
| 107 | return err |
| 108 | } |