~fnux/matrix-alertmanager-receiver

b3074bee047ea8d6496eed6fc1bc9b1b1aa7d490 — Timothée Floure 6 months ago ce8691e
Matrix: join room, send dummy message
1 files changed, 42 insertions(+), 6 deletions(-)

M main.go
M main.go => main.go +42 -6
@@ 6,9 6,15 @@ import (
	"fmt"
	"log"
	"net/http"
	"io/ioutil"
	"encoding/json"
	"github.com/matrix-org/gomatrix"
)

type Alert struct {

}

func main() {
	// Initialize logger.
	var logger *log.Logger = log.New(os.Stdout, "", log.Flags())


@@ 37,14 43,26 @@ func main() {
		logger.Fatalf("Could not log in to Matrix (%v): %v", *homeserver, err)
	}

	/*
	logger.Printf("Syncing with Matrix homserver (%v)", *homeserver)
	err = matrixClient.Sync()
	joinedRooms, err := matrixClient.JoinedRooms()
	if err != nil {
		logger.Fatalf("Could not sync with Matrix homeserver (%v): %v", *homeserver, err)
		logger.Fatalf("Could not fetch joined rooms: %v", err)
	}

	alreadyJoinedTarget := false
	for _, roomID := range joinedRooms.JoinedRooms {
		// FIXME: will only work if target is a roomID, not an alias.
		if *target == roomID {
			alreadyJoinedTarget = true
		}
	}

	if !alreadyJoinedTarget {
		logger.Printf("Trying to join %v...", *target)
		_, err := matrixClient.JoinRoom(*target, "", nil)
		if err != nil {
			logger.Fatalf("Failed to join %v: %v", *target, err)
		}
	}
	*/
	_ = matrixClient

	// Initialize HTTP serve (= listen for incoming requests).
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {


@@ 54,6 72,24 @@ You will find more details on: http://git.sr.ht/~fnux/matrix-prometheus-alertman
	})

	http.HandleFunc("/alert", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != http.MethodPost {
			w.WriteHeader(http.StatusMethodNotAllowed)
			return
		}

		var alert Alert
		reqBody, _ := ioutil.ReadAll(r.Body)
		json.Unmarshal(reqBody, &alert)

		// Check validity

		logger.Printf("Sending message")
		_, err := matrixClient.SendText(*target, "spouik spouik spouik")
		if err != nil {
			logger.Fatalf("Failed to send message: %v", err)
		}

		w.WriteHeader(http.StatusOK)
	})

	var listenAddr = fmt.Sprintf(":%v", *port)