From 6c242710d7b1e4ba7c7d9b76437529d1d00c7c67 Mon Sep 17 00:00:00 2001 From: hanemile Date: Fri, 10 Jul 2020 16:37:52 +0200 Subject: the functions handling almost everything I need --- send.go | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 send.go (limited to 'send.go') diff --git a/send.go b/send.go new file mode 100644 index 0000000..3547eaa --- /dev/null +++ b/send.go @@ -0,0 +1,113 @@ +package matrix + +import ( + "bytes" + "encoding/json" + "fmt" + "time" + + "gopkg.in/h2non/gentleman.v2" + "gopkg.in/h2non/gentleman.v2/plugins/body" + "gopkg.in/h2non/gentleman.v2/plugins/headers" + "gopkg.in/h2non/gentleman.v2/plugins/query" +) + +// Send allows sending messages +func Send(authinfo Authinfo, roomID string, message string) error { + cli := gentleman.New() + cli.URL(authinfo.HomeServer) + + eventType := "m.room.message" + txID := fmt.Sprintf("%d", time.Now().UnixNano()) + + req := cli.Request() + req.Path(fmt.Sprintf("/_matrix/client/r0/rooms/%s/send/%s/%s", roomID, eventType, txID)) + req.Method("PUT") + + formattedMessage := fmt.Sprintf("
%s\n
\n", message) + + // Define the JSON payload via body plugin + data := map[string]string{ + "msgtype": "m.text", + "body": "A", // notifications + "format": "org.matrix.custom.html", + "formatted_body": formattedMessage, + } + req.Use(body.JSON(data)) + req.Use(query.Set("access_token", authinfo.AccessToken)) + + res, err := req.Send() + if err != nil { + fmt.Println("ERR1") + return err + } + if !res.Ok { + fmt.Println("ERR2") + fmt.Println(res) + return err + } + fmt.Printf("Sent %s\n", message) + return nil +} + +// Upload uploads stuff to the matrix homeserver returning the files MXC +func Upload(authinfo Authinfo, filename string, file *bytes.Buffer) (UploadResponse, error) { + cli := gentleman.New() + cli.URL(authinfo.HomeServer) + + req := cli.Request() + req.Path("/_matrix/media/r0/upload") + req.Method("POST") + + req.Use(headers.Set("Content-Type", "image/png")) + req.Use(headers.Set("filename", filename)) + + req.Use(body.Reader(file)) + req.Use(query.Set("access_token", authinfo.AccessToken)) + + res, err := req.Send() + if err != nil { + fmt.Println("ERR1") + return UploadResponse{}, err + } + if !res.Ok { + fmt.Println("ERR2") + fmt.Println(res) + return UploadResponse{}, err + } + + var uploadResponse UploadResponse + if err := json.Unmarshal(res.Bytes(), &uploadResponse); err != nil { + return UploadResponse{}, err + } + return uploadResponse, nil +} + +// SendImage sends the image with the given mxc ID to the room (currently +// hardcoded some lines below) +func SendImage(authinfo Authinfo, roomID string, image map[string]interface{}) error { + cli := gentleman.New() + cli.URL(authinfo.HomeServer) + + eventType := "m.room.message" + txID := fmt.Sprintf("%d", time.Now().UnixNano()) + + req := cli.Request() + req.Path(fmt.Sprintf("/_matrix/client/r0/rooms/%s/send/%s/%s", roomID, eventType, txID)) + req.Method("PUT") + + req.Use(body.JSON(image)) + req.Use(query.Set("access_token", authinfo.AccessToken)) + + res, err := req.Send() + if err != nil { + fmt.Println("ERR1") + return err + } + if !res.Ok { + fmt.Println("ERR2") + fmt.Println(res) + return err + } + return nil +} -- cgit 1.4.1