Ability to select HTTP Method in callbacks
This commit is contained in:
parent
f2e5684477
commit
4c50b33e1c
30
README.md
30
README.md
|
@ -33,15 +33,21 @@ allowed-indexes:
|
|||
- 1337
|
||||
pool-interval: 5m
|
||||
telegram-api-key: "telegram api key"
|
||||
arm-callback-urls:
|
||||
- "http://192.168.1.10/hello"
|
||||
- "http://example.com/api"
|
||||
disarm-callback-urls:
|
||||
- "http://192.168.1.10/bye"
|
||||
- "http://example.com/api2"
|
||||
alarm-callback-urls:
|
||||
- "http://192.168.1.10/ohno"
|
||||
- "http://example.com/api3"
|
||||
arm-callbacks:
|
||||
- uri: "http://192.168.1.10/hello"
|
||||
method: "POST"
|
||||
- uri: "http://example.com/api"
|
||||
method: "GET"
|
||||
disarm-callbacks:
|
||||
- uri: "http://192.168.1.10/bye"
|
||||
method: "POST"
|
||||
- uri: "http://example.com/api2"
|
||||
method: "GET"
|
||||
alarm-callbacks:
|
||||
- uri: "http://192.168.1.10/ohno"
|
||||
method: "POST"
|
||||
- uri: "http://example.com/api3"
|
||||
method: "GET"
|
||||
matterbridge:
|
||||
- uri: "http://localhost:4242/api/message"
|
||||
token: "test_token_1"
|
||||
|
@ -70,9 +76,9 @@ To configure that set the `matterbridge` part of the config file with the follow
|
|||
|
||||
Set the values in following parts of the config file:
|
||||
|
||||
- `arm-callback-urls` - for an URL that will be POST when partition **0** is armed
|
||||
- `disarm-callback-urls` - for an URL that will be POST when partition **0** is unarmed
|
||||
- `alarm-callback-urls` - for an URL that will be POST when **any** partition alarm is activated
|
||||
- `arm-callbacks` - for an URL that will be requested when partition **0** is armed. Use `uri` to specify the URI that will be requested, and method to select which HTTP Method will be used (allowed values: `GET`, `POST`)
|
||||
- `disarm-callbacks` - for an URL that will be requested when partition **0** is unarmed. Use `uri` to specify the URI that will be requested, and method to select which HTTP Method will be used (allowed values: `GET`, `POST`)
|
||||
- `alarm-callbacks` - for an URL that will be requested when **any** partition alarm is activated. Use `uri` to specify the URI that will be requested, and method to select which HTTP Method will be used (allowed values: `GET`, `POST`)
|
||||
|
||||
### Filtering events by change type
|
||||
|
||||
|
|
17
config.go
17
config.go
|
@ -33,15 +33,20 @@ type MatterbridgeConfig struct {
|
|||
Username string `yaml:"username"`
|
||||
}
|
||||
|
||||
type HttpCallbackConfig struct {
|
||||
URI string `yaml:"uri"`
|
||||
Method string `yaml:"method"`
|
||||
}
|
||||
|
||||
type AppConfig struct {
|
||||
SatelAddr string `yaml:"satel-addr"`
|
||||
ChatIds []int64 `yaml:"tg-chat-ids"`
|
||||
AllowedTypes []SatelChangeType `yaml:"allowed-types"`
|
||||
AllowedIndexes []int `yaml:"allowed-indexes"`
|
||||
PoolInterval OwnDuration `yaml:"pool-interval"`
|
||||
ArmCallbackUrls []string `yaml:"arm-callback-urls"`
|
||||
DisarmCallbackUrls []string `yaml:"disarm-callback-urls"`
|
||||
AlarmCallbackUrls []string `yaml:"alarm-callback-urls"`
|
||||
ArmCallbacks []HttpCallbackConfig `yaml:"arm-callbacks"`
|
||||
DisarmCallbacks []HttpCallbackConfig `yaml:"disarm-callbacks"`
|
||||
AlarmCallbacks []HttpCallbackConfig `yaml:"alarm-callbacks"`
|
||||
WriteMemoryProfile bool `yaml:"write-memory-profile"`
|
||||
Matterbridge []MatterbridgeConfig `yaml:"matterbridge"`
|
||||
TelegramApiKey string `yaml:"telegram-api-key"`
|
||||
|
@ -167,13 +172,13 @@ func MakeConfig(logger *log.Logger) AppConfig {
|
|||
config.WriteMemoryProfile = false
|
||||
|
||||
if len(os.Getenv("NOTIFY_URL_ARM")) != 0 {
|
||||
config.ArmCallbackUrls = append(config.ArmCallbackUrls, os.Getenv("NOTIFY_URL_ARM"))
|
||||
logger.Fatal("NOTIFY_URL_ARM is not supported anymore. Please use the config file instead.")
|
||||
}
|
||||
if len(os.Getenv("NOTIFY_URL_DISARM")) != 0 {
|
||||
config.DisarmCallbackUrls = append(config.DisarmCallbackUrls, os.Getenv("NOTIFY_URL_DISARM"))
|
||||
logger.Fatal("NOTIFY_URL_DISARM is not supported anymore. Please use the config file instead.")
|
||||
}
|
||||
if len(os.Getenv("ALARM_URL_ARM")) != 0 {
|
||||
config.AlarmCallbackUrls = append(config.AlarmCallbackUrls, os.Getenv("ALARM_URL_ARM"))
|
||||
logger.Fatal("ALARM_URL_ARM is not supported anymore. Please use the config file instead.")
|
||||
}
|
||||
|
||||
getCmdLineParams(&config, logger)
|
||||
|
|
|
@ -23,15 +23,21 @@ allowed-indexes:
|
|||
- 5678
|
||||
- 1337
|
||||
pool-interval: 5m
|
||||
arm-callback-urls:
|
||||
- "test arm callback url"
|
||||
- "second test arm callback url"
|
||||
disarm-callback-urls:
|
||||
- "test disarm callback url"
|
||||
- "second test disarm callback url"
|
||||
alarm-callback-urls:
|
||||
- "test alarm callback url"
|
||||
- "second test alarm callback url"
|
||||
arm-callbacks:
|
||||
- uri: "test arm callback url"
|
||||
method: "POST"
|
||||
- uri: "second test arm callback url"
|
||||
method: "GET"
|
||||
disarm-callbacks:
|
||||
- uri: "test disarm callback url"
|
||||
method: "POST"
|
||||
- uri: "second test disarm callback url"
|
||||
method: "GET"
|
||||
alarm-callbacks:
|
||||
- uri: "test alarm callback url"
|
||||
method: "POST"
|
||||
- uri: "second test alarm callback url"
|
||||
method: "GET"
|
||||
telegram-api-key: "test api key"
|
||||
matterbridge:
|
||||
- uri: test_uri_1
|
||||
|
@ -54,9 +60,12 @@ func TestParseYamlConfig(t *testing.T) {
|
|||
a.ElementsMatch([]int{5678, 1337}, actualConfig.AllowedIndexes)
|
||||
a.ElementsMatch([]SatelChangeType{{satel.ZoneIsolate}, {satel.ZoneAlarm}}, actualConfig.AllowedTypes)
|
||||
a.Equal(5*time.Minute, actualConfig.PoolInterval.GetDuration())
|
||||
a.ElementsMatch([]string{"test arm callback url", "second test arm callback url"}, actualConfig.ArmCallbackUrls)
|
||||
a.ElementsMatch([]string{"test disarm callback url", "second test disarm callback url"}, actualConfig.DisarmCallbackUrls)
|
||||
a.ElementsMatch([]string{"test alarm callback url", "second test alarm callback url"}, actualConfig.AlarmCallbackUrls)
|
||||
a.ElementsMatch([]HttpCallbackConfig{{"test arm callback url", "POST"}, {"second test arm callback url", "GET"}},
|
||||
actualConfig.ArmCallbacks)
|
||||
a.ElementsMatch([]HttpCallbackConfig{{"test disarm callback url", "POST"}, {"second test disarm callback url", "GET"}},
|
||||
actualConfig.DisarmCallbacks)
|
||||
a.ElementsMatch([]HttpCallbackConfig{{"test alarm callback url", "POST"}, {"second test alarm callback url", "GET"}},
|
||||
actualConfig.AlarmCallbacks)
|
||||
a.Equal("test api key", actualConfig.TelegramApiKey)
|
||||
|
||||
a.Equal(actualConfig.Matterbridge[0].URI, "test_uri_1")
|
||||
|
|
|
@ -88,25 +88,33 @@ func MakeNofityViaHTTPSync(config AppConfig, logger *log.Logger) *NotifyViaHTTPS
|
|||
return &NotifyViaHTTPSync{SyncFilterImpl[GenericMessage]{}, config, logger}
|
||||
}
|
||||
|
||||
func doHttpNotification(url string, logger *log.Logger) {
|
||||
func doHttpNotification(callbackConfig HttpCallbackConfig, logger *log.Logger) {
|
||||
|
||||
if len(url) == 0 {
|
||||
if len(callbackConfig.URI) == 0 {
|
||||
return
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodPost, url, nil)
|
||||
var method = http.MethodPost
|
||||
if callbackConfig.Method == "GET" {
|
||||
method = http.MethodGet
|
||||
} else if callbackConfig.Method == "POST" {
|
||||
method = http.MethodPost
|
||||
} else {
|
||||
logger.Print("Unknown method ", callbackConfig.Method, " , using POST instead.")
|
||||
}
|
||||
req, err := http.NewRequest(method, callbackConfig.URI, nil)
|
||||
|
||||
client := http.Client{
|
||||
Timeout: httpTimeout,
|
||||
}
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
logger.Print("Could not POST ", url, ": ", err)
|
||||
logger.Print("Could not ", callbackConfig.Method, " ", callbackConfig.URI, ": ", err)
|
||||
return
|
||||
}
|
||||
logger.Print("Notified via HTTP with result ", res.StatusCode)
|
||||
}
|
||||
|
||||
func notifyAllHttp(urls []string, logger *log.Logger) {
|
||||
func notifyAllHttp(urls []HttpCallbackConfig, logger *log.Logger) {
|
||||
for _, uri := range urls {
|
||||
doHttpNotification(uri, logger)
|
||||
}
|
||||
|
@ -117,9 +125,9 @@ inner_arm:
|
|||
for _, basicElement := range msg.Messages {
|
||||
if (basicElement.Index == NotificationPartitionIndex) && (basicElement.Type == satel.ArmedPartition) {
|
||||
if basicElement.Value == ArmedPartition_Armed {
|
||||
notifyAllHttp(notifyViaHttp.config.ArmCallbackUrls, notifyViaHttp.logger)
|
||||
notifyAllHttp(notifyViaHttp.config.ArmCallbacks, notifyViaHttp.logger)
|
||||
} else {
|
||||
notifyAllHttp(notifyViaHttp.config.DisarmCallbackUrls, notifyViaHttp.logger)
|
||||
notifyAllHttp(notifyViaHttp.config.DisarmCallbacks, notifyViaHttp.logger)
|
||||
}
|
||||
break inner_arm
|
||||
}
|
||||
|
@ -128,7 +136,7 @@ inner_alarm:
|
|||
for _, basicElement := range msg.Messages {
|
||||
if basicElement.Type == satel.PartitionAlarm {
|
||||
if basicElement.Value == PartitionAlarm_Alarm {
|
||||
notifyAllHttp(notifyViaHttp.config.AlarmCallbackUrls, notifyViaHttp.logger)
|
||||
notifyAllHttp(notifyViaHttp.config.AlarmCallbacks, notifyViaHttp.logger)
|
||||
break inner_alarm
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue