|
| 1 | +import ( |
| 2 | + "bytes" |
| 3 | + "encoding/json" |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "strings" |
| 7 | + "github.com/MythicMeta/MythicContainer/webhookstructs" |
| 8 | +) |
| 9 | +type GoogleChatMessage struct { |
| 10 | + Text string `json:"text,omitempty"` |
| 11 | +} |
| 12 | +func sendGoogleChatMessage(webhookURL string, msg webhookstructs.SlackWebhookMessage) error { |
| 13 | + var builder strings.Builder |
| 14 | + for _, att := range msg.Attachments { |
| 15 | + if att.Title != "" { |
| 16 | + builder.WriteString(fmt.Sprintf("*%s*\n", att.Title)) |
| 17 | + } |
| 18 | + if att.Blocks != nil { |
| 19 | + for _, block := range *att.Blocks { |
| 20 | + if block.Text != nil && block.Text.Text != "" { |
| 21 | + builder.WriteString(fmt.Sprintf("%s\n", block.Text.Text)) |
| 22 | + } |
| 23 | + if block.Fields != nil { |
| 24 | + for _, f := range *block.Fields { |
| 25 | + if f.Text != "" { |
| 26 | + builder.WriteString(fmt.Sprintf("%s\n", f.Text)) |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + builder.WriteString("\n") |
| 33 | + } |
| 34 | + payload := GoogleChatMessage{ |
| 35 | + Text: builder.String(), |
| 36 | + } |
| 37 | + jsonData, err := json.Marshal(payload) |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(jsonData)) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + defer resp.Body.Close() |
| 46 | + if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 47 | + return fmt.Errorf("google chat webhook returned status %s", resp.Status) |
| 48 | + } |
| 49 | + return nil |
| 50 | +} |
0 commit comments