tank/slack.go
Marcus Noble 42f9b8da76
Added Slack webhook handling
Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>
2024-07-27 10:24:17 +01:00

47 lines
1.0 KiB
Go

package main
import (
"encoding/json"
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/slack-go/slack"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/format"
"maunium.net/go/mautrix/id"
)
func HandleSlackPayloadPost(c *fiber.Ctx) error {
payload := slack.WebhookMessage{}
if err := c.BodyParser(&payload); err != nil {
fmt.Println("Failed to parse payload", err)
return err
}
fmt.Println("Got slack payload")
s, _ := json.MarshalIndent(payload, "", "\t")
fmt.Println(string(s))
room := payload.Channel
if room == "" {
room = c.Query("room", *defaultRoom)
}
_, err := matrixClient.SendMessageEvent(
id.RoomID(getRoom(room)),
event.EventMessage,
format.RenderMarkdown(payload.Text, true, true),
)
if err != nil {
fmt.Println("Failed sending to Matrix", err)
if httpErr, ok := err.(mautrix.HTTPError); ok {
return c.Status(httpErr.Response.StatusCode).SendString(httpErr.RespError.Err)
}
return fiber.ErrInternalServerError
}
return c.SendStatus(fiber.StatusOK)
}