2021-07-07 10:05:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-08-12 11:59:11 +00:00
|
|
|
"encoding/json"
|
2021-07-07 10:05:36 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2021-12-05 13:33:22 +00:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
2021-07-07 10:05:36 +00:00
|
|
|
"maunium.net/go/mautrix"
|
2023-04-25 19:59:17 +00:00
|
|
|
"maunium.net/go/mautrix/event"
|
|
|
|
"maunium.net/go/mautrix/format"
|
2021-07-07 10:05:36 +00:00
|
|
|
"maunium.net/go/mautrix/id"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
matrixClient *mautrix.Client
|
|
|
|
defaultRoom *string
|
|
|
|
port = "3000"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
homeserverURL, ok := os.LookupEnv("HOMESERVER_URL")
|
|
|
|
if !ok {
|
|
|
|
panic("HOMESERVER_URL env var required")
|
|
|
|
}
|
|
|
|
userID, ok := os.LookupEnv("USER_ID")
|
|
|
|
if !ok {
|
|
|
|
panic("USER_ID env var required")
|
|
|
|
} else {
|
|
|
|
userID = strings.Replace(userID, homeserverURL, "", -1)
|
|
|
|
userID = strings.TrimPrefix(userID, "@")
|
|
|
|
}
|
|
|
|
accessToken, ok := os.LookupEnv("ACCESS_TOKEN")
|
|
|
|
if !ok {
|
|
|
|
panic("ACCESS_TOKEN env var required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p, ok := os.LookupEnv("PORT"); ok {
|
|
|
|
port = p
|
|
|
|
}
|
|
|
|
|
|
|
|
room, ok := os.LookupEnv("DEFAULT_ROOM")
|
|
|
|
if ok {
|
|
|
|
defaultRoom = &room
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := mautrix.NewClient(homeserverURL, id.NewUserID(userID, homeserverURL), accessToken)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
matrixClient = client
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := fiber.New()
|
2021-12-05 13:33:22 +00:00
|
|
|
app.Use(logger.New())
|
2021-07-07 10:05:36 +00:00
|
|
|
app.Post("/", HandlePayloadPost)
|
2021-12-05 12:10:25 +00:00
|
|
|
app.Post("/alertmanager", HandleAlertmanagerPayloadPost)
|
2022-02-28 08:07:59 +00:00
|
|
|
app.Post("/nexmo/sms", HandleNexmoSMSPost)
|
2021-07-07 10:05:36 +00:00
|
|
|
app.Listen(fmt.Sprintf(":%s", port))
|
|
|
|
}
|
|
|
|
|
2021-12-05 15:30:58 +00:00
|
|
|
func getRoom(roomID string) string {
|
|
|
|
room := parseRoom(roomID)
|
|
|
|
if strings.HasPrefix(room, "#") || !strings.HasPrefix("!", room) {
|
|
|
|
if !strings.HasPrefix(room, "#") {
|
|
|
|
room = fmt.Sprintf("#%s", room)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := matrixClient.ResolveAlias(id.RoomAlias(room))
|
|
|
|
if err == nil {
|
|
|
|
room = resp.RoomID.String()
|
|
|
|
} else {
|
|
|
|
room = strings.Replace(room, "#", "!", 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return room
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseRoom(room string) string {
|
|
|
|
prefix := ""
|
|
|
|
local := ""
|
|
|
|
domain := ""
|
|
|
|
|
|
|
|
parts := strings.Split(room, ":")
|
|
|
|
if len(parts) == 2 {
|
|
|
|
domain = parts[1]
|
|
|
|
} else {
|
|
|
|
domain = matrixClient.HomeserverURL.Host
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(parts[0], "!") {
|
|
|
|
prefix = "!"
|
|
|
|
parts[0] = strings.TrimPrefix(parts[0], "!")
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(parts[0], "#") {
|
|
|
|
prefix = "#"
|
|
|
|
parts[0] = strings.TrimPrefix(parts[0], "#")
|
|
|
|
}
|
|
|
|
|
|
|
|
local = parts[0]
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s%s:%s", prefix, local, domain)
|
|
|
|
}
|
|
|
|
|
2021-07-07 10:05:36 +00:00
|
|
|
func HandlePayloadPost(c *fiber.Ctx) error {
|
|
|
|
payload := Payload{}
|
|
|
|
|
|
|
|
if err := c.BodyParser(&payload); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := payload.Validate(defaultRoom); err != nil {
|
|
|
|
fmt.Println("Invalid payload - ", err)
|
|
|
|
return c.Status(fiber.StatusBadRequest).SendString(err.Error())
|
|
|
|
}
|
2023-08-12 11:14:00 +00:00
|
|
|
|
|
|
|
overrideRoom := c.Query("roomID")
|
|
|
|
if overrideRoom != "" {
|
|
|
|
fmt.Printf("Override room from query string: %s\n", overrideRoom)
|
2023-08-12 12:01:39 +00:00
|
|
|
payload.RoomID = getRoom(overrideRoom)
|
2023-08-12 11:14:00 +00:00
|
|
|
}
|
|
|
|
overrideType := c.Query("type")
|
|
|
|
if overrideType != "" {
|
2023-08-12 11:59:11 +00:00
|
|
|
fmt.Printf("Override type from query string: %s\n", overrideType)
|
2023-08-12 11:14:00 +00:00
|
|
|
payload.Type = PayloadType(overrideType)
|
|
|
|
}
|
2021-07-07 10:05:36 +00:00
|
|
|
|
2023-08-12 11:59:11 +00:00
|
|
|
s, _ := json.MarshalIndent(payload, "", "\t")
|
|
|
|
fmt.Println(string(s))
|
|
|
|
|
2021-07-07 10:05:36 +00:00
|
|
|
switch payload.Type {
|
|
|
|
case PayloadTypeText:
|
|
|
|
_, err := matrixClient.SendText(id.RoomID(payload.RoomID), payload.Message)
|
|
|
|
if err != nil {
|
|
|
|
if httpErr, ok := err.(mautrix.HTTPError); ok {
|
|
|
|
return c.Status(httpErr.Response.StatusCode).SendString(httpErr.RespError.Err)
|
|
|
|
}
|
|
|
|
return fiber.ErrInternalServerError
|
|
|
|
}
|
2023-04-25 19:59:17 +00:00
|
|
|
case PayloadTypeMarkdown:
|
|
|
|
_, err := matrixClient.SendMessageEvent(
|
|
|
|
id.RoomID(payload.RoomID),
|
|
|
|
event.EventMessage,
|
|
|
|
format.RenderMarkdown(payload.Message, true, true),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if httpErr, ok := err.(mautrix.HTTPError); ok {
|
|
|
|
return c.Status(httpErr.Response.StatusCode).SendString(httpErr.RespError.Err)
|
|
|
|
}
|
|
|
|
return fiber.ErrInternalServerError
|
|
|
|
}
|
2021-07-07 10:05:36 +00:00
|
|
|
case PayloadTypeNotice:
|
|
|
|
_, err := matrixClient.SendNotice(id.RoomID(payload.RoomID), payload.Message)
|
|
|
|
if err != nil {
|
|
|
|
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)
|
|
|
|
}
|