about summary refs log tree commit diff
path: root/send.go
blob: 3547eaaac0e99d6b59d16724264920a7850c17a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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("<pre><samp style=\"font-family: monospace\">%s\n</samp></pre>\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
}