tank/slack.go
Marcus Noble 1d26f341c7
Strip newlines from Slack payload
Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>
2024-07-27 11:09:40 +01:00

51 lines
1.1 KiB
Go

package main
import (
"encoding/json"
"fmt"
"strings"
"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 {
body := string(c.Body())
body = strings.ReplaceAll(body, "\n", "\\n")
payload := slack.WebhookMessage{}
if err := json.Unmarshal([]byte(body), &payload); err != nil {
fmt.Println("Failed to parse payload", string(c.Body()), 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)
}