1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-06-30 11:53:25 +00:00

Compare commits

...

5 Commits

Author SHA1 Message Date
e754bd36ed update. 2020-08-03 12:20:33 +08:00
fb47a66931 local image sending supported. #8 2020-08-03 12:17:29 +08:00
4be73b2834 fix quick operation no response without at_sender. 2020-08-03 00:17:34 +08:00
e2227d582a fix reverse ws read error. 2020-08-02 23:48:53 +08:00
06fccbd9ef fix http api send raw msg error. 2020-08-02 15:21:19 +08:00
8 changed files with 91 additions and 51 deletions

View File

@ -96,28 +96,36 @@ func (bot *CQBot) CQGetGroupMemberInfo(groupId, userId int64, noCache bool) MSG
} }
// https://cqhttp.cc/docs/4.15/#/API?id=send_group_msg-%E5%8F%91%E9%80%81%E7%BE%A4%E6%B6%88%E6%81%AF // https://cqhttp.cc/docs/4.15/#/API?id=send_group_msg-%E5%8F%91%E9%80%81%E7%BE%A4%E6%B6%88%E6%81%AF
func (bot *CQBot) CQSendGroupMessage(groupId int64, m gjson.Result) MSG { func (bot *CQBot) CQSendGroupMessage(groupId int64, i interface{}) MSG {
if m.Type == gjson.String { var str string
str := m.Str if m, ok := i.(gjson.Result); ok {
if str == "" { if m.Type == gjson.JSON {
return Failed(100) elem := bot.ConvertObjectMessage(m, true)
mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem})
if mid == -1 {
return Failed(100)
}
return OK(MSG{"message_id": mid})
} }
elem := bot.ConvertStringMessage(str, true) str = func() string {
mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem}) if m.Str != "" {
if mid == -1 { return m.Str
return Failed(100) }
} return m.Raw
return OK(MSG{"message_id": mid}) }()
} }
if m.Type == gjson.JSON { if s, ok := i.(string); ok {
elem := bot.ConvertObjectMessage(m, true) str = s
mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem})
if mid == -1 {
return Failed(100)
}
return OK(MSG{"message_id": mid})
} }
return Failed(100) if str == "" {
return Failed(100)
}
elem := bot.ConvertStringMessage(str, true)
mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem})
if mid == -1 {
return Failed(100)
}
return OK(MSG{"message_id": mid})
} }
func (bot *CQBot) CQSendGroupForwardMessage(groupId int64, m gjson.Result) MSG { func (bot *CQBot) CQSendGroupForwardMessage(groupId int64, m gjson.Result) MSG {
@ -191,25 +199,36 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupId int64, m gjson.Result) MSG {
} }
// https://cqhttp.cc/docs/4.15/#/API?id=send_private_msg-%E5%8F%91%E9%80%81%E7%A7%81%E8%81%8A%E6%B6%88%E6%81%AF // https://cqhttp.cc/docs/4.15/#/API?id=send_private_msg-%E5%8F%91%E9%80%81%E7%A7%81%E8%81%8A%E6%B6%88%E6%81%AF
func (bot *CQBot) CQSendPrivateMessage(userId int64, m gjson.Result) MSG { func (bot *CQBot) CQSendPrivateMessage(userId int64, i interface{}) MSG {
if m.Type == gjson.String { var str string
str := m.Str if m, ok := i.(gjson.Result); ok {
elem := bot.ConvertStringMessage(str, false) if m.Type == gjson.JSON {
mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem}) elem := bot.ConvertObjectMessage(m, true)
if mid == -1 { mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem})
return Failed(100) if mid == -1 {
return Failed(100)
}
return OK(MSG{"message_id": mid})
} }
return OK(MSG{"message_id": mid}) str = func() string {
if m.Str != "" {
return m.Str
}
return m.Raw
}()
} }
if m.Type == gjson.JSON { if s, ok := i.(string); ok {
elem := bot.ConvertObjectMessage(m, true) str = s
mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem})
if mid == -1 {
return Failed(100)
}
return OK(MSG{"message_id": mid})
} }
return Failed(100) if str == "" {
return Failed(100)
}
elem := bot.ConvertStringMessage(str, false)
mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem})
if mid == -1 {
return Failed(100)
}
return OK(MSG{"message_id": mid})
} }
// https://cqhttp.cc/docs/4.15/#/API?id=set_group_card-%E8%AE%BE%E7%BD%AE%E7%BE%A4%E5%90%8D%E7%89%87%EF%BC%88%E7%BE%A4%E5%A4%87%E6%B3%A8%EF%BC%89 // https://cqhttp.cc/docs/4.15/#/API?id=set_group_card-%E8%AE%BE%E7%BD%AE%E7%BE%A4%E5%90%8D%E7%89%87%EF%BC%88%E7%BE%A4%E5%A4%87%E6%B3%A8%EF%BC%89
@ -343,12 +362,14 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) MSG {
msgType := context.Get("message_type").Str msgType := context.Get("message_type").Str
reply := operation.Get("reply") reply := operation.Get("reply")
if reply.Exists() { if reply.Exists() {
at := true /*
if operation.Get("at_sender").Exists() { at := true
at = operation.Get("at_sender").Bool() if operation.Get("at_sender").Exists() {
} at = operation.Get("at_sender").Bool()
}
*/
// TODO: 处理at字段 // TODO: 处理at字段
if msgType == "group" && at { if msgType == "group" {
bot.CQSendGroupMessage(context.Get("group_id").Int(), reply) bot.CQSendGroupMessage(context.Get("group_id").Int(), reply)
} }
if msgType == "private" { if msgType == "private" {

View File

@ -93,7 +93,7 @@ func (bot *CQBot) SendGroupMessage(groupId int64, m *message.SendingMessage) int
if i, ok := elem.(*message.ImageElement); ok { if i, ok := elem.(*message.ImageElement); ok {
gm, err := bot.Client.UploadGroupImage(groupId, i.Data) gm, err := bot.Client.UploadGroupImage(groupId, i.Data)
if err != nil { if err != nil {
log.Warnf("警告: 群 %v 消息图片上传失败.", groupId) log.Warnf("警告: 群 %v 消息图片上传失败: %v", groupId, err)
continue continue
} }
newElem = append(newElem, gm) newElem = append(newElem, gm)

View File

@ -10,8 +10,10 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"io/ioutil" "io/ioutil"
"net/url"
"path" "path"
"regexp" "regexp"
"runtime"
"strconv" "strconv"
"strings" "strings"
) )
@ -176,6 +178,20 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (message.
} }
return message.NewImage(b), nil return message.NewImage(b), nil
} }
if strings.HasPrefix(f, "file") {
fu, err := url.Parse(f)
if err != nil {
return nil, err
}
if strings.HasPrefix(fu.Path, "/") && runtime.GOOS == `windows` {
fu.Path = fu.Path[1:]
}
b, err := ioutil.ReadFile(fu.Path)
if err != nil {
return nil, err
}
return message.NewImage(b), nil
}
if global.PathExists(path.Join(global.IMAGE_PATH, f)) { if global.PathExists(path.Join(global.IMAGE_PATH, f)) {
b, err := ioutil.ReadFile(path.Join(global.IMAGE_PATH, f)) b, err := ioutil.ReadFile(path.Join(global.IMAGE_PATH, f))
if err != nil { if err != nil {

View File

@ -53,6 +53,7 @@ func (bot *CQBot) groupMessageEvent(c *client.QQClient, m *message.GroupMessage)
"name": file.Name, "name": file.Name,
"size": file.Size, "size": file.Size,
"busid": file.Busid, "busid": file.Busid,
"url": c.GetGroupFileUrl(m.GroupCode, file.Path, file.Busid),
}, },
"self_id": c.Uin, "self_id": c.Uin,
"time": time.Now().Unix(), "time": time.Now().Unix(),

3
go.mod
View File

@ -3,7 +3,7 @@ module github.com/Mrs4s/go-cqhttp
go 1.14 go 1.14
require ( require (
github.com/Mrs4s/MiraiGo v0.0.0-20200802045511-04aad9705bdc github.com/Mrs4s/MiraiGo v0.0.0-20200803034534-d8ed1a49ff8b
github.com/gin-gonic/gin v1.6.3 github.com/gin-gonic/gin v1.6.3
github.com/gorilla/websocket v1.4.2 github.com/gorilla/websocket v1.4.2
github.com/guonaihong/gout v0.1.1 github.com/guonaihong/gout v0.1.1
@ -15,7 +15,6 @@ require (
github.com/tidwall/gjson v1.6.0 github.com/tidwall/gjson v1.6.0
github.com/xujiajun/nutsdb v0.5.0 github.com/xujiajun/nutsdb v0.5.0
github.com/yinghau76/go-ascii-art v0.0.0-20190517192627-e7f465a30189 github.com/yinghau76/go-ascii-art v0.0.0-20190517192627-e7f465a30189
golang.org/x/image v0.0.0-20200618115811-c13761719519
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
) )

9
go.sum
View File

@ -1,7 +1,11 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Mrs4s/MiraiGo v0.0.0-20200802045511-04aad9705bdc h1:mtPgcLy1VNr+nTA7vQibBYpYpFALJ+ChVkUwA8KNKfI= github.com/Mrs4s/MiraiGo v0.0.0-20200802150153-a7761bfb7571 h1:OQQhH7IVRX5BqjUwrlg6mKVUlxKoBzOPUNwZEToGQ00=
github.com/Mrs4s/MiraiGo v0.0.0-20200802045511-04aad9705bdc/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU= github.com/Mrs4s/MiraiGo v0.0.0-20200802150153-a7761bfb7571/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
github.com/Mrs4s/MiraiGo v0.0.0-20200802165028-683ad4b6cbb5 h1:NF3YM6Bv2/sA6yc1wxuBuHRfVBhQws02INRpqrkqmh4=
github.com/Mrs4s/MiraiGo v0.0.0-20200802165028-683ad4b6cbb5/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
github.com/Mrs4s/MiraiGo v0.0.0-20200803034534-d8ed1a49ff8b h1:XQEeWyot+xvUDV9btSbI+fAJdWXxRLBWE8wRyzpgmfA=
github.com/Mrs4s/MiraiGo v0.0.0-20200803034534-d8ed1a49ff8b/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0= github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE= github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
@ -103,7 +107,6 @@ github.com/yinghau76/go-ascii-art v0.0.0-20190517192627-e7f465a30189 h1:4UJw9if5
github.com/yinghau76/go-ascii-art v0.0.0-20190517192627-e7f465a30189/go.mod h1:rIrm5geMiBhPQkdfUm8gDFi/WiHneOp1i9KjmJqc+9I= github.com/yinghau76/go-ascii-art v0.0.0-20190517192627-e7f465a30189/go.mod h1:rIrm5geMiBhPQkdfUm8gDFi/WiHneOp1i9KjmJqc+9I=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/image v0.0.0-20200618115811-c13761719519/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=

View File

@ -239,7 +239,7 @@ func (s *httpServer) SendPrivateMessage(c *gin.Context) {
c.JSON(200, s.bot.CQSendPrivateMessage(uid, gjson.Parse(msg))) c.JSON(200, s.bot.CQSendPrivateMessage(uid, gjson.Parse(msg)))
return return
} }
c.JSON(200, s.bot.CQSendPrivateMessage(uid, gjson.Result{Type: gjson.String, Str: msg})) c.JSON(200, s.bot.CQSendPrivateMessage(uid, msg))
} }
func (s *httpServer) SendGroupMessage(c *gin.Context) { func (s *httpServer) SendGroupMessage(c *gin.Context) {
@ -249,7 +249,7 @@ func (s *httpServer) SendGroupMessage(c *gin.Context) {
c.JSON(200, s.bot.CQSendGroupMessage(gid, gjson.Parse(msg))) c.JSON(200, s.bot.CQSendGroupMessage(gid, gjson.Parse(msg)))
return return
} }
c.JSON(200, s.bot.CQSendGroupMessage(gid, gjson.Result{Type: gjson.String, Str: msg})) c.JSON(200, s.bot.CQSendGroupMessage(gid, msg))
} }
func (s *httpServer) SendGroupForwardMessage(c *gin.Context) { func (s *httpServer) SendGroupForwardMessage(c *gin.Context) {

View File

@ -157,12 +157,12 @@ func (c *websocketClient) connectUniversal() {
func (c *websocketClient) listenApi(conn *wsc.Conn, u bool) { func (c *websocketClient) listenApi(conn *wsc.Conn, u bool) {
defer conn.Close() defer conn.Close()
for { for {
buf := make([]byte, 10240) var buf []byte
l, err := conn.Read(buf) err := wsc.Message.Receive(conn, &buf)
if err != nil { if err != nil {
break break
} }
j := gjson.ParseBytes(buf[:l]) j := gjson.ParseBytes(buf)
t := strings.ReplaceAll(j.Get("action").Str, "_async", "") t := strings.ReplaceAll(j.Get("action").Str, "_async", "")
log.Debugf("反向WS接收到API调用: %v 参数: %v", t, j.Get("params").Raw) log.Debugf("反向WS接收到API调用: %v 参数: %v", t, j.Get("params").Raw)
if f, ok := wsApi[t]; ok { if f, ok := wsApi[t]; ok {