File: ifttt_json.go

package info (click to toggle)
golang-github-nicholas-fedor-shoutrrr 0.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,680 kB
  • sloc: sh: 74; makefile: 58
file content (61 lines) | stat: -rw-r--r-- 1,404 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package ifttt

import (
	"encoding/json"
	"fmt"

	"github.com/nicholas-fedor/shoutrrr/pkg/types"
)

// ValueFieldOne represents the Value1 field in the IFTTT payload.
const (
	ValueFieldOne   = 1 // Represents Value1 field
	ValueFieldTwo   = 2 // Represents Value2 field
	ValueFieldThree = 3 // Represents Value3 field
)

// jsonPayload represents the notification payload sent to the IFTTT webhook API.
type jsonPayload struct {
	Value1 string `json:"value1"`
	Value2 string `json:"value2"`
	Value3 string `json:"value3"`
}

// createJSONToSend generates a JSON payload for the IFTTT webhook API.
func createJSONToSend(config *Config, message string, params *types.Params) ([]byte, error) {
	payload := jsonPayload{
		Value1: config.Value1,
		Value2: config.Value2,
		Value3: config.Value3,
	}

	if params != nil {
		if value, found := (*params)["value1"]; found {
			payload.Value1 = value
		}

		if value, found := (*params)["value2"]; found {
			payload.Value2 = value
		}

		if value, found := (*params)["value3"]; found {
			payload.Value3 = value
		}
	}

	switch config.UseMessageAsValue {
	case ValueFieldOne:
		payload.Value1 = message
	case ValueFieldTwo:
		payload.Value2 = message
	case ValueFieldThree:
		payload.Value3 = message
	}

	jsonBytes, err := json.Marshal(payload)
	if err != nil {
		return nil, fmt.Errorf("marshaling IFTTT payload to JSON: %w", err)
	}

	return jsonBytes, nil
}