@@ -2,8 +2,6 @@ package aireply
2
2
3
3
import (
4
4
"fmt"
5
- "io/ioutil"
6
- "net/http"
7
5
"net/url"
8
6
"regexp"
9
7
"strings"
@@ -17,7 +15,7 @@ import (
17
15
type QYKReply struct {}
18
16
19
17
const (
20
- qykURL = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=%s "
18
+ qykURL = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=%v "
21
19
qykBotName = "菲菲"
22
20
)
23
21
@@ -32,28 +30,12 @@ func (*QYKReply) String() string {
32
30
// Talk 取得带 CQ 码的回复消息
33
31
func (* QYKReply ) Talk (msg , nickname string ) string {
34
32
msg = strings .ReplaceAll (msg , nickname , qykBotName )
35
-
36
33
u := fmt .Sprintf (qykURL , url .QueryEscape (msg ))
37
- client := & http.Client {}
38
- req , err := http .NewRequest ("GET" , u , nil )
39
- if err != nil {
40
- return "ERROR:" + err .Error ()
41
- }
42
- // 自定义Header
43
- req .Header .Set ("User-Agent" , web .RandUA ())
44
- req .Header .Set ("Connection" , "keep-alive" )
45
- req .Header .Set ("Host" , "api.qingyunke.com" )
46
- resp , err := client .Do (req )
47
- if err != nil {
48
- return "ERROR:" + err .Error ()
49
- }
50
- defer resp .Body .Close ()
51
- bytes , err := ioutil .ReadAll (resp .Body )
34
+ data , err := web .RequestDataWith (web .NewDefaultClient (), u , "GET" , "" , web .RandUA ())
52
35
if err != nil {
53
36
return "ERROR:" + err .Error ()
54
37
}
55
-
56
- replystr := gjson .Get (binary .BytesToString (bytes ), "content" ).String ()
38
+ replystr := gjson .Get (binary .BytesToString (data ), "content" ).String ()
57
39
replystr = strings .ReplaceAll (replystr , "{face:" , "[CQ:face,id=" )
58
40
replystr = strings .ReplaceAll (replystr , "{br}" , "\n " )
59
41
replystr = strings .ReplaceAll (replystr , "}" , "]" )
@@ -67,26 +49,11 @@ func (*QYKReply) TalkPlain(msg, nickname string) string {
67
49
msg = strings .ReplaceAll (msg , nickname , qykBotName )
68
50
69
51
u := fmt .Sprintf (qykURL , url .QueryEscape (msg ))
70
- client := & http.Client {}
71
- req , err := http .NewRequest ("GET" , u , nil )
72
- if err != nil {
73
- return "ERROR: " + err .Error ()
74
- }
75
- // 自定义Header
76
- req .Header .Set ("User-Agent" , web .RandUA ())
77
- req .Header .Set ("Connection" , "keep-alive" )
78
- req .Header .Set ("Host" , "api.qingyunke.com" )
79
- resp , err := client .Do (req )
52
+ data , err := web .RequestDataWith (web .NewDefaultClient (), u , "GET" , "" , web .RandUA ())
80
53
if err != nil {
81
- return "ERROR: " + err .Error ()
82
- }
83
- defer resp .Body .Close ()
84
- bytes , err := ioutil .ReadAll (resp .Body )
85
- if err != nil {
86
- return "ERROR: " + err .Error ()
54
+ return "ERROR:" + err .Error ()
87
55
}
88
-
89
- replystr := gjson .Get (binary .BytesToString (bytes ), "content" ).String ()
56
+ replystr := gjson .Get (binary .BytesToString (data ), "content" ).String ()
90
57
replystr = qykMatchFace .ReplaceAllLiteralString (replystr , "" )
91
58
replystr = strings .ReplaceAll (replystr , "{br}" , "\n " )
92
59
replystr = strings .ReplaceAll (replystr , qykBotName , nickname )
0 commit comments