| 1 | package rocketchatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/rocketchat`/ |
| 2 | |
| 3 | import ( |
| 4 | "encoding/jsonscip-go gomod github.com/golang/go/src . `encoding/json`/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src . fmt/" |
| 6 | |
| 7 | "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/" |
| 8 | ) |
| 9 | |
| 10 | // JSON represents the payload structure for the Rocket.Chat service. |
| 11 | type JSONscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/rocketchat`/JSON# struct { |
| 12 | Textscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/rocketchat`/JSON#Text. string `json:"text"` |
| 13 | UserNamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/rocketchat`/JSON#UserName. string `json:"username,omitempty"` |
| 14 | Channelscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/rocketchat`/JSON#Channel. string `json:"channel,omitempty"` |
| 15 | } |
| 16 | |
| 17 | // CreateJSONPayload generates a JSON payload compatible with the Rocket.Chat webhook API. |
| 18 | // |
| 19 | // Params: |
| 20 | // - config: The Rocket.Chat configuration containing user and channel settings |
| 21 | // - message: The message text to include in the payload |
| 22 | // - params: Optional parameters that can override config values (username, channel) |
| 23 | // |
| 24 | // Returns: |
| 25 | // - []byte: The JSON payload as a byte slice |
| 26 | // - error: An error if JSON marshaling fails |
| 27 | func CreateJSONPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/rocketchat`/CreateJSONPayload().(config *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/rocketchat`/Config#, message string, params *typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Paramsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Params#) ([]byte, error) { |
| 28 | payload := JSONscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/rocketchat`/JSON#{ |
| 29 | Textscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/rocketchat`/JSON#Text.: message, |
| 30 | UserNamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/rocketchat`/JSON#UserName.: config.UserNamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/rocketchat`/Config#UserName., |
| 31 | Channelscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/rocketchat`/JSON#Channel.: config.Channelscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/rocketchat`/Config#Channel., |
| 32 | } |
| 33 | |
| 34 | if params != nil { |
| 35 | if value, found := (*params)["username"]; found { |
| 36 | payload.UserNamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/rocketchat`/JSON#UserName. = value |
| 37 | } |
| 38 | |
| 39 | if value, found := (*params)["channel"]; found { |
| 40 | payload.Channelscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/rocketchat`/JSON#Channel. = value |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | payloadBytes, err := jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src . `encoding/json`/Marshal().(payload) |
| 45 | if err != nil { |
| 46 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("marshaling Rocket.Chat payload to JSON: %w", err) |
| 47 | } |
| 48 | |
| 49 | return payloadBytes, nil |
| 50 | } |