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