-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.go
86 lines (70 loc) · 1.65 KB
/
message.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"io"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
type message struct {
account account
content, visibility string
feed feed
}
func (msg *message) post() error {
if debug {
log.Printf("msg:\n\n%v", msg)
log.Printf("msg.feed:\n\n%v", msg.feed)
}
apiURL := msg.account.InstanceURL + "/api/v1/statuses"
data := url.Values{}
data.Set("status", msg.content)
data.Set("visibility", msg.feed.Visibility)
data.Set("sensitive", strconv.FormatBool(msg.feed.Sensitive))
data.Set("spoiler_text", msg.feed.ContentWarning)
if debug {
log.Printf("Data:\n\n%v", data)
}
switch msg.feed.Format {
case "markdown":
data.Set("content_type", "text/markdown")
case "html":
data.Set("content_type", "text/html")
case "plain":
data.Set("content_type", "text/plain")
case "bbcode":
data.Set("content_type", "text/bbcode")
}
var req *http.Request
var body io.Reader
body = strings.NewReader(data.Encode())
if debug {
log.Printf("Message:\n\n%s", msg.content)
}
req, err := http.NewRequest("POST", apiURL, body)
if err != nil {
return err
}
if debug {
log.Printf("Request:\n\n%v", body)
}
// Set Headers
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Authorization", "Bearer "+msg.account.AccessToken)
req.Header.Set("User-Agent", conf.HttpConfig.UserAgent)
c := &http.Client{Timeout: time.Second * 10}
if !debugOrDryRun {
var resp *http.Response
resp, err = c.Do(req)
if err != nil {
return err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
log.Println("response Status: ", resp.Status)
}
return nil
}