-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (52 loc) · 1.26 KB
/
main.go
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)
const (
APIURL = "https://dynamicdisplay.recurse.com/matrix/api/message"
)
// DynamicDisplayRequest contains the data needed to POST a message
type DynamicDisplayRequest struct {
Name string `json:"author"`
Message string `json:"message"`
}
// Displays a message on RC Floor 5
// See also:
// https://dynamicdisplay.recurse.com
func main() {
messageToDisplay := "hello world!"
if len(os.Args) > 1 {
messageToDisplay = strings.Join(os.Args[1:], " ")
}
ddr := &DynamicDisplayRequest{
Name: "golang",
Message: messageToDisplay,
}
buffer := new(bytes.Buffer)
json.NewEncoder(buffer).Encode(ddr)
request, err := http.NewRequest("POST", APIURL, buffer)
request.Header.Set("Content-Type", "application/json")
if err != nil {
panic(err)
}
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
fmt.Printf("problem while posting; error: %v", err)
return
}
defer response.Body.Close()
fmt.Printf("Response Status: %v\n", response.Status)
body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("problem while reading response; error: %v", err)
return
}
fmt.Printf("Response Body: %v\n", string(body))
}