admin_backend/middleware/feishu/handle/card.go
2026-04-15 14:48:13 +08:00

86 lines
2.8 KiB
Go

package handle
import (
"backend/Type"
"backend/util"
"context"
"encoding/json"
"log"
"github.com/larksuite/oapi-sdk-go/v3/event/dispatcher/callback"
)
var handle map[string]func(ctx context.Context, event *callback.CardActionTriggerEvent) (*callback.CardActionTriggerResponse, error)
func init() {
handle = make(map[string]func(ctx context.Context, event *callback.CardActionTriggerEvent) (*callback.CardActionTriggerResponse, error))
handle["complete_alarm"] = NotifyCard
handle["client"] = NotifyClientCard
}
func Handle(ctx context.Context, event *callback.CardActionTriggerEvent) (*callback.CardActionTriggerResponse, error) {
if h, ok := handle[event.Event.Action.Value["action"].(string)]; ok {
return h(ctx, event)
}
return nil, nil
}
func NotifyCard(ctx context.Context, event *callback.CardActionTriggerEvent) (*callback.CardActionTriggerResponse, error) {
d := Type.NotifyData{
Host: event.Event.Action.Value["HostName"].(string),
EventName: event.Event.Action.Value["EventName"].(string),
Severity: event.Event.Action.Value["EventSeverity"].(string),
AlarmTime: event.Event.Action.Value["AlarmTime"].(string),
Info: event.Event.Action.FormValue["notes_input"].(string),
NotifyMsg: event.Event.Action.Value["NotifyMsg"].(string),
Id: event.Event.Operator.OpenID,
}
str := util.ParseTmpl("./template/card_notify.tmpl", d)
r := make(map[string]interface{})
err := json.Unmarshal([]byte(str), &r)
if err != nil {
log.Println(str)
log.Printf("notify card %v", err)
}
return &callback.CardActionTriggerResponse{
Toast: &callback.Toast{
Type: "info",
Content: "已收到",
},
Card: &callback.Card{
Type: "raw",
Data: r,
},
}, nil
}
func NotifyClientCard(ctx context.Context, event *callback.CardActionTriggerEvent) (*callback.CardActionTriggerResponse, error) {
d := Type.NotifyClientData{
Log: event.Event.Action.Value["Log"].(string),
StackTrace: event.Event.Action.Value["StackTrace"].(string),
DeviceModel: event.Event.Action.Value["DeviceModel"].(string),
OperatingSystem: event.Event.Action.Value["OperatingSystem"].(string),
AlarmTime: event.Event.Action.Value["AlarmTime"].(string),
Info: event.Event.Action.FormValue["notes_input"].(string),
DeviceUniqueIdentifier: event.Event.Action.Value["DeviceUniqueIdentifier"].(string),
Id: event.Event.Operator.OpenID,
}
str := util.ParseTmpl("./template/card_client_notify.tmpl", d)
r := make(map[string]interface{})
err := json.Unmarshal([]byte(str), &r)
if err != nil {
log.Println(str)
log.Printf("notify card %v", err)
}
return &callback.CardActionTriggerResponse{
Toast: &callback.Toast{
Type: "info",
Content: "已收到",
},
Card: &callback.Card{
Type: "raw",
Data: r,
},
}, nil
}