-
Notifications
You must be signed in to change notification settings - Fork 1
/
misskeyApi.go
159 lines (128 loc) · 3.11 KB
/
misskeyApi.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package misskeyapi
import (
"bytes"
"encoding/json"
"errors"
"io"
"log"
"net/http"
"strconv"
"github.com/tpc3/Noa/lib/config"
)
var (
getIDEndpoint = "https://" + config.Loadconfig.Misskey.Host + "/api/i"
getnotesEndpoint = "https://" + config.Loadconfig.Misskey.Host + "/api/notes/timeline"
sendnotesEndpoint = "https://" + config.Loadconfig.Misskey.Host + "/api/notes/create"
)
func MisskeyGetuserID(token string) string {
requestBody := GetIDRequest{
Token: token,
}
requestJson, err := json.Marshal(requestBody)
if err != nil {
log.Fatal("Marshaling json err: ", err)
}
req, err := http.NewRequest("POST", getIDEndpoint, bytes.NewBuffer(requestJson))
if err != nil {
log.Fatal("Creating http request error: ", err)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Sending http request error: ", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatal("HTTP status = ", strconv.Itoa(resp.StatusCode))
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal("Reading body error: ", err)
}
var response IDResponse
err = json.Unmarshal(body, &response)
if err != nil {
log.Fatal("Unmarshal json error: ", err)
}
return response.ID
}
func MisskeyGetnotesRequest(token string, botID string) ([]string, error) {
check := true
requestBody := GetnotesRequest{
Limit: 100,
Token: token,
}
requestJson, err := json.Marshal(requestBody)
if err != nil {
check = false
}
req, err := http.NewRequest("POST", getnotesEndpoint, bytes.NewBuffer(requestJson))
if err != nil {
check = false
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
check = false
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
check = false
}
body, err := io.ReadAll(resp.Body)
if err != nil {
check = false
}
response := make([]*NotesResponse, 0)
err = json.Unmarshal(body, &response)
if err != nil {
check = false
}
var resarray []string
for i := 0; i < len(response); i++ {
if response[i].User.Id == botID || response[i].Text == "" || response[i].RenoteId != "" || response[i].Visibility == "followers" || response[i].Visibility == "specified" {
continue
} else {
resarray = append(resarray, response[i].Text)
}
}
if check {
return resarray, nil
} else {
return resarray, errors.New("API error")
}
}
func MisskeySendnotesRequest(token string, text string) error {
check := true
requestBody := NotesRequest{
Visibility: "home",
Text: text,
Token: token,
LocalOnly: true,
}
requestJson, err := json.Marshal(requestBody)
if err != nil {
check = false
}
req, err := http.NewRequest("POST", sendnotesEndpoint, bytes.NewBuffer(requestJson))
if err != nil {
check = false
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
check = false
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
check = false
}
if check {
return nil
} else {
return errors.New("API error")
}
}