Files
Ian Ngethe Muchiri 7ef90440f2 MG-853 - Add Slack output integration (#315)
* add slack integration

Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>

* allow support for multiple message options

Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>

* add message to slack struct

Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>

* update template name

Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>

* group postgres and slack

Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>

---------

Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>
2025-09-26 11:24:31 +02:00

69 lines
1.5 KiB
Go

// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package outputs
import (
"encoding/json"
"strings"
"github.com/absmach/supermq/pkg/errors"
"github.com/absmach/supermq/pkg/messaging"
)
type templateVal struct {
Message *messaging.Message
Result any
}
// OutputType is the indicator for type of the output
// so we can move it to the Go instead calling Go from Lua.
type OutputType uint
const (
ChannelsType OutputType = iota
AlarmsType
SaveSenMLType
EmailType
SaveRemotePgType
SlackType
)
var (
scriptKindToString = [...]string{"channels", "alarms", "save_senml", "email", "save_remote_pg", "slack"}
stringToScriptKind = map[string]OutputType{
"channels": ChannelsType,
"alarms": AlarmsType,
"save_senml": SaveSenMLType,
"email": EmailType,
"save_remote_pg": SaveRemotePgType,
"slack": SlackType,
}
)
func (s OutputType) String() string {
if int(s) < 0 || int(s) >= len(scriptKindToString) {
return "unknown"
}
return scriptKindToString[s]
}
// MarshalJSON converts OutputType to JSON.
func (s *OutputType) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
// UnmarshalJSON parses JSON string into OutputType.
func (s *OutputType) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
lower := strings.ToLower(str)
if val, ok := stringToScriptKind[lower]; ok {
*s = val
return nil
}
return errors.New("invalid OutputType: " + str)
}