Debian Codegraph @

pkg/services/chat/rocketchat/rocketchat_json.go

raw
1 package rocketchat
2
3 import (
4 "encoding/json"
5 "fmt"
6
7 "github.com/nicholas-fedor/shoutrrr/pkg/types"
8 )
9
10 // JSON represents the payload structure for the Rocket.Chat service.
11 type JSON struct {
12 Text string `json:"text"`
13 UserName string `json:"username,omitempty"`
14 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 CreateJSONPayload(config *Config, message string, params *types.Params) ([]byte, error) {
28 payload := JSON{
29 Text: message,
30 UserName: config.UserName,
31 Channel: config.Channel,
32 }
33
34 if params != nil {
35 if value, found := (*params)["username"]; found {
36 payload.UserName = value
37 }
38
39 if value, found := (*params)["channel"]; found {
40 payload.Channel = value
41 }
42 }
43
44 payloadBytes, err := json.Marshal(payload)
45 if err != nil {
46 return nil, fmt.Errorf("marshaling Rocket.Chat payload to JSON: %w", err)
47 }
48
49 return payloadBytes, nil
50 }