This repository has been archived by the owner on Aug 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cqFunction.go
56 lines (50 loc) · 1.52 KB
/
cqFunction.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
package main
import (
"io/ioutil"
"log"
"net/http"
"net/url"
"github.com/tidwall/gjson"
)
//Send Group message by using CoolQ HttpAPI
func cqSendGroupMsg(id, msg string) {
conf := readConfig()
cqAddr := gjson.Get(conf, "CoolQ.0.Api.HttpAPIAddr").String()
cqToken := gjson.Get(conf, "CoolQ.0.Api.HttpAPIToken").String()
getWbeContent(cqAddr + "/send_group_msg?access_token=" + cqToken + "&group_id=" + id + "&message=" + url.QueryEscape(msg))
}
//Send private message by using CoolQ HttpAPI
func cqSendPrivateMsg(id, msg string) {
conf := readConfig()
cqAddr := gjson.Get(conf, "CoolQ.0.Api.HttpAPIAddr").String()
cqToken := gjson.Get(conf, "CoolQ.0.Api.HttpAPIToken").String()
getWbeContent(cqAddr + "/send_private_msg?access_token=" + cqToken + "&user_id=" + id + "&message=" + url.QueryEscape(msg))
}
//Get web Content by using GET request.
func getWbeContent(url string) (body []byte) {
client := &http.Client{}
request, err := http.NewRequest("GET", url, nil)
request.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4117.2 Safari/537.36")
if err != nil {
log.Fatalln(err)
}
response, err := client.Do(request)
if err != nil {
log.Fatalln(err)
}
defer response.Body.Close()
content, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalln(err)
}
return content
}
//Read config file.
func readConfig() string {
file, err := ioutil.ReadFile("conf.json")
if err != nil {
log.Fatal(err)
}
result := string(file)
return result
}