forked from Ink-33/BiliAu2Card
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (111 loc) · 3.06 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
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
package main
import (
_ "crypto/hmac"
_ "crypto/sha1"
"fmt"
"io/ioutil"
"log"
"net/http"
"regexp"
"strings"
"github.com/tidwall/gjson"
)
var cqsecret string = gjson.Get(readConfig(), "HttpAPIPosSecret").String()
//Read config file.
func readConfig() string {
file, err := ioutil.ReadFile("conf.json")
if err != nil {
log.Fatal(err)
}
result := string(file)
return result
}
//Get audio number by regexp.
func getAu(msg string) (au string) {
if strings.Contains(msg, "CQ:rich") {
return ""
}
reg, err := regexp.Compile("(?i)au[0-9]+")
if err != nil {
log.Fatalln(err)
}
str := strings.Join(reg.FindAllString(msg, 1), "")
return str
}
//Handle meassage and send music card.
func au2card(MsgInfo MsgInfo) {
au := getAu(MsgInfo.Message)
if au != "" {
log.Println("BiliAu2Card: Created request for", au, "from:", MsgInfo.SenderID)
Auinfo := getAuInfo(au)
if !Auinfo.AuStatus {
msgMake := "BiliAu2Card: AU" + Auinfo.AuNumber + Auinfo.AuMsg
log.Println(msgMake)
switch MsgInfo.MsgType {
case "private":
cqSendPrivateMsg(MsgInfo.SenderID, msgMake)
break
case "group":
cqSendGroupMsg(MsgInfo.GroupID, msgMake)
break
}
} else {
cqCodeMake := "[CQ:music,type=custom,url=" + Auinfo.AuJumpURL + ",audio=" + Auinfo.AuURL + ",title=" + Auinfo.AuTitle + ",content=" + Auinfo.AuDesp + ",image=" + Auinfo.AuCoverURL + "@180w_180h]"
switch MsgInfo.MsgType {
case "private":
cqSendPrivateMsg(MsgInfo.SenderID, cqCodeMake)
break
case "group":
cqSendGroupMsg(MsgInfo.GroupID, cqCodeMake)
break
}
}
} else {
log.Println("BiliAu2Card: Ingore message:", MsgInfo.Message, "from:", MsgInfo.SenderID)
}
}
//handleMsg converts HTTP Post Body to MsgInfo Struct.
func handleMsg(raw []byte) (MsgInfo MsgInfo) {
MsgInfo.MsgType = gjson.GetBytes(raw, "message_type").String()
MsgInfo.GroupID = gjson.GetBytes(raw, "group_id").String()
MsgInfo.Message = gjson.GetBytes(raw, "message").String()
MsgInfo.SenderID = gjson.GetBytes(raw, "user_id").String()
return
}
//Handle request type before handling message.
func handleHTTP(w http.ResponseWriter, r *http.Request) {
method := r.Method
if method != "POST" {
w.WriteHeader(400)
fmt.Fprint(w, "Bad request.")
} else {
//signature := r.Header.Get("X-Signature")
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatalln(err)
}
//TODO: Add HMAC-SHA1 signature verification.
/*
mac1 := hmac.New(sha1.New, []byte(cqsecret))
fmt.Println(string(body[:]))
mac1.Write(body)
fmt.Printf("%x\n",mac1.Sum(nil))
fmt.Println(signature)
fmt.Println(hmac.Equal(mac1.Sum(nil), []byte(signature)))
*/
au2card(handleMsg(body))
}
}
func main() {
config := readConfig()
path := gjson.Get(config, "BiliAu2Card.0.ListeningPath").String()
port := gjson.Get(config, "BiliAu2Card.0.ListeningPort").String()
log.Println("BiliAu2Card: Powered by Ink33")
log.Println("BiliAu2Card: Start listening", path, port)
http.HandleFunc(path, handleHTTP)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.Fatal("ListenAndServe", err)
}
}