| 1 | package ifttt |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | |
| 7 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 8 | ) |
| 9 | |
| 10 | // jsonPayload represents the notification payload sent to the IFTTT webhook API. |
| 11 | type jsonPayload struct { |
| 12 | Value1 string `json:"value1"` |
| 13 | Value2 string `json:"value2"` |
| 14 | Value3 string `json:"value3"` |
| 15 | } |
| 16 | |
| 17 | // ValueFieldOne represents the Value1 field in the IFTTT payload. |
| 18 | const ( |
| 19 | ValueFieldOne = 1 // Represents Value1 field |
| 20 | ValueFieldTwo = 2 // Represents Value2 field |
| 21 | ValueFieldThree = 3 // Represents Value3 field |
| 22 | ) |
| 23 | |
| 24 | // createJSONToSend generates a JSON payload for the IFTTT webhook API. |
| 25 | func createJSONToSend(config *Config, message string, params *types.Params) ([]byte, error) { |
| 26 | payload := jsonPayload{ |
| 27 | Value1: config.Value1, |
| 28 | Value2: config.Value2, |
| 29 | Value3: config.Value3, |
| 30 | } |
| 31 | |
| 32 | if params != nil { |
| 33 | if value, found := (*params)["value1"]; found { |
| 34 | payload.Value1 = value |
| 35 | } |
| 36 | |
| 37 | if value, found := (*params)["value2"]; found { |
| 38 | payload.Value2 = value |
| 39 | } |
| 40 | |
| 41 | if value, found := (*params)["value3"]; found { |
| 42 | payload.Value3 = value |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | switch config.UseMessageAsValue { |
| 47 | case ValueFieldOne: |
| 48 | payload.Value1 = message |
| 49 | case ValueFieldTwo: |
| 50 | payload.Value2 = message |
| 51 | case ValueFieldThree: |
| 52 | payload.Value3 = message |
| 53 | } |
| 54 | |
| 55 | jsonBytes, err := json.Marshal(payload) |
| 56 | if err != nil { |
| 57 | return nil, fmt.Errorf("marshaling IFTTT payload to JSON: %w", err) |
| 58 | } |
| 59 | |
| 60 | return jsonBytes, nil |
| 61 | } |