55 "bytes"
66 "encoding/json"
77 "math/rand"
8+ "net/http"
89 "net/url"
910 "os"
1011 "strings"
@@ -106,7 +107,7 @@ func init() {
106107 }
107108 ctx .SendChain (message .Text ("成功!" ))
108109 })
109- engine .OnRegex (`^设置🦙API地址\s*(http.*)\s*$` ).SetBlock (true ).Handle (func (ctx * zero.Ctx ) {
110+ engine .OnRegex (`^设置🦙API地址\s*(http.*)\s*$` , zero . SuperUserPermission , zero . OnlyPrivate ).SetBlock (true ).Handle (func (ctx * zero.Ctx ) {
110111 alpacapiurl = ctx .State ["regex_matched" ].([]string )[1 ]
111112 err := os .WriteFile (alpacapifile , binary .StringToBytes (alpacapiurl ), 0644 )
112113 if err != nil {
@@ -115,7 +116,7 @@ func init() {
115116 }
116117 ctx .SendChain (message .Text ("成功!" ))
117118 })
118- engine .OnRegex (`^设置🦙token\s*([0-9a-f]{112})\s*$` ).SetBlock (true ).Handle (func (ctx * zero.Ctx ) {
119+ engine .OnRegex (`^设置🦙token\s*([0-9a-f]{112})\s*$` , zero . SuperUserPermission , zero . OnlyPrivate ).SetBlock (true ).Handle (func (ctx * zero.Ctx ) {
119120 alpacatoken = ctx .State ["regex_matched" ].([]string )[1 ]
120121 err := os .WriteFile (alpacatokenfile , binary .StringToBytes (alpacatoken ), 0644 )
121122 if err != nil {
@@ -181,16 +182,26 @@ func init() {
181182 }).SetBlock (false ).Handle (func (ctx * zero.Ctx ) {
182183 msg := ctx .ExtractPlainText ()
183184 if msg != "" {
184- data , err := web .GetData (alpacapiurl + "/reply?msg=" + url .QueryEscape (msg ))
185- if err == nil {
186- type reply struct {
187- Msg string
188- }
189- m := reply {}
190- err := json .Unmarshal (data , & m )
191- if err == nil && len (m .Msg ) > 0 {
192- ctx .Send (message .ReplyWithMessage (ctx .Event .MessageID , message .Text (m .Msg )))
193- }
185+ data , err := web .RequestDataWithHeaders (http .DefaultClient , alpacapiurl + "/reply?msg=" + url .QueryEscape (msg ), "GET" ,
186+ func (r * http.Request ) error {
187+ r .Header .Set ("Authorization" , alpacatoken )
188+ return nil
189+ }, nil )
190+ if err != nil {
191+ logrus .Warnln ("[chat] 🦙 err:" , err )
192+ return
193+ }
194+ type reply struct {
195+ Msg string
196+ }
197+ m := reply {}
198+ err = json .Unmarshal (data , & m )
199+ if err != nil {
200+ logrus .Warnln ("[chat] 🦙 unmarshal err:" , err )
201+ return
202+ }
203+ if len (m .Msg ) > 0 {
204+ ctx .Send (message .ReplyWithMessage (ctx .Event .MessageID , message .Text (m .Msg )))
194205 }
195206 }
196207 })
0 commit comments