From 41008e4dee630268698d631a1a7ac119c3dd2b5f Mon Sep 17 00:00:00 2001 From: Marcus Noble Date: Mon, 28 Feb 2022 08:07:59 +0000 Subject: [PATCH] Added nexmo SMS parsing Signed-off-by: Marcus Noble --- main.go | 1 + nexmo.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 nexmo.go diff --git a/main.go b/main.go index 4271fa8..84e86a3 100644 --- a/main.go +++ b/main.go @@ -55,6 +55,7 @@ func main() { app.Use(logger.New()) app.Post("/", HandlePayloadPost) app.Post("/alertmanager", HandleAlertmanagerPayloadPost) + app.Post("/nexmo/sms", HandleNexmoSMSPost) app.Listen(fmt.Sprintf(":%s", port)) } diff --git a/nexmo.go b/nexmo.go new file mode 100644 index 0000000..a2e8d3e --- /dev/null +++ b/nexmo.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + + "github.com/gofiber/fiber/v2" + "maunium.net/go/mautrix" + "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/format" + "maunium.net/go/mautrix/id" +) + +type NexmoSMS struct { + ID string `json:"messageId"` + From string `json:"msisdn"` + Text string `json:"test"` + Type string `json:"type"` +} + +func HandleNexmoSMSPost(c *fiber.Ctx) error { + payload := NexmoSMS{} + if err := c.BodyParser(&payload); err != nil { + fmt.Println("Failed to parse payload", err) + return err + } + + msg := fmt.Sprintf(` +New Message from: %s + +> %s`, payload.From, payload.Text) + + _, err := matrixClient.SendMessageEvent( + id.RoomID(getRoom("SMS")), + event.EventMessage, + format.RenderMarkdown(msg, 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) +}