Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
G
go-cqhttp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nanahira
go-cqhttp
Commits
4820eb2f
Commit
4820eb2f
authored
Aug 17, 2020
by
Richard Chien
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support "auto_escape" in api and quick operation
parent
00d80d5d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
84 additions
and
20 deletions
+84
-20
coolq/api.go
coolq/api.go
+20
-10
global/param.go
global/param.go
+50
-0
server/http.go
server/http.go
+7
-4
server/websocket.go
server/websocket.go
+7
-6
No files found.
coolq/api.go
View file @
4820eb2f
...
...
@@ -96,7 +96,7 @@ 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
func
(
bot
*
CQBot
)
CQSendGroupMessage
(
groupId
int64
,
i
interface
{})
MSG
{
func
(
bot
*
CQBot
)
CQSendGroupMessage
(
groupId
int64
,
i
interface
{}
,
autoEscape
bool
)
MSG
{
var
str
string
if
m
,
ok
:=
i
.
(
gjson
.
Result
);
ok
{
if
m
.
Type
==
gjson
.
JSON
{
...
...
@@ -113,14 +113,18 @@ func (bot *CQBot) CQSendGroupMessage(groupId int64, i interface{}) MSG {
}
return
m
.
Raw
}()
}
if
s
,
ok
:=
i
.
(
string
);
ok
{
}
else
if
s
,
ok
:=
i
.
(
string
);
ok
{
str
=
s
}
if
str
==
""
{
return
Failed
(
100
)
}
elem
:=
bot
.
ConvertStringMessage
(
str
,
true
)
var
elem
[]
message
.
IMessageElement
if
autoEscape
{
elem
=
append
(
elem
,
message
.
NewText
(
str
))
}
else
{
elem
=
bot
.
ConvertStringMessage
(
str
,
true
)
}
// fix at display
for
_
,
e
:=
range
elem
{
if
at
,
ok
:=
e
.
(
*
message
.
AtElement
);
ok
&&
at
.
Target
!=
0
{
...
...
@@ -211,7 +215,7 @@ 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
func
(
bot
*
CQBot
)
CQSendPrivateMessage
(
userId
int64
,
i
interface
{})
MSG
{
func
(
bot
*
CQBot
)
CQSendPrivateMessage
(
userId
int64
,
i
interface
{}
,
autoEscape
bool
)
MSG
{
var
str
string
if
m
,
ok
:=
i
.
(
gjson
.
Result
);
ok
{
if
m
.
Type
==
gjson
.
JSON
{
...
...
@@ -228,14 +232,18 @@ func (bot *CQBot) CQSendPrivateMessage(userId int64, i interface{}) MSG {
}
return
m
.
Raw
}()
}
if
s
,
ok
:=
i
.
(
string
);
ok
{
}
else
if
s
,
ok
:=
i
.
(
string
);
ok
{
str
=
s
}
if
str
==
""
{
return
Failed
(
100
)
}
elem
:=
bot
.
ConvertStringMessage
(
str
,
false
)
var
elem
[]
message
.
IMessageElement
if
autoEscape
{
elem
=
append
(
elem
,
message
.
NewText
(
str
))
}
else
{
elem
=
bot
.
ConvertStringMessage
(
str
,
false
)
}
mid
:=
bot
.
SendPrivateMessage
(
userId
,
&
message
.
SendingMessage
{
Elements
:
elem
})
if
mid
==
-
1
{
return
Failed
(
100
)
...
...
@@ -374,6 +382,7 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) MSG {
msgType
:=
context
.
Get
(
"message_type"
)
.
Str
reply
:=
operation
.
Get
(
"reply"
)
if
reply
.
Exists
()
{
autoEscape
:=
global
.
EnsureBool
(
operation
.
Get
(
"auto_escape"
),
false
)
/*
at := true
if operation.Get("at_sender").Exists() {
...
...
@@ -382,10 +391,11 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) MSG {
*/
// TODO: 处理at字段
if
msgType
==
"group"
{
bot
.
CQSendGroupMessage
(
context
.
Get
(
"group_id"
)
.
Int
(),
reply
)
bot
.
CQSendGroupMessage
(
context
.
Get
(
"group_id"
)
.
Int
(),
reply
,
autoEscape
)
}
if
msgType
==
"private"
{
bot
.
CQSendPrivateMessage
(
context
.
Get
(
"user_id"
)
.
Int
(),
reply
)
// TODO: 处理auto_escape
bot
.
CQSendPrivateMessage
(
context
.
Get
(
"user_id"
)
.
Int
(),
reply
,
autoEscape
)
}
}
if
msgType
==
"group"
{
...
...
global/param.go
0 → 100644
View file @
4820eb2f
package
global
import
(
"github.com/tidwall/gjson"
"strings"
)
var
trueSet
=
map
[
string
]
struct
{}{
"true"
:
struct
{}{},
"yes"
:
struct
{}{},
"1"
:
struct
{}{},
}
var
falseSet
=
map
[
string
]
struct
{}{
"false"
:
struct
{}{},
"no"
:
struct
{}{},
"0"
:
struct
{}{},
}
func
EnsureBool
(
p
interface
{},
defaultVal
bool
)
bool
{
var
str
string
if
b
,
ok
:=
p
.
(
bool
);
ok
{
return
b
}
if
j
,
ok
:=
p
.
(
gjson
.
Result
);
ok
{
if
!
j
.
Exists
()
{
return
defaultVal
}
if
j
.
Type
==
gjson
.
True
{
return
true
}
if
j
.
Type
==
gjson
.
False
{
return
false
}
if
j
.
Type
!=
gjson
.
String
{
return
defaultVal
}
str
=
j
.
Str
}
else
if
s
,
ok
:=
p
.
(
string
);
ok
{
str
=
s
}
str
=
strings
.
ToLower
(
str
)
if
_
,
ok
:=
trueSet
[
str
];
ok
{
return
true
}
if
_
,
ok
:=
falseSet
[
str
];
ok
{
return
false
}
return
defaultVal
}
server/http.go
View file @
4820eb2f
...
...
@@ -9,6 +9,7 @@ import (
"time"
"github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/global"
"github.com/gin-gonic/gin"
"github.com/guonaihong/gout"
log
"github.com/sirupsen/logrus"
...
...
@@ -249,21 +250,23 @@ func (s *httpServer) SendMessage(c *gin.Context) {
func
(
s
*
httpServer
)
SendPrivateMessage
(
c
*
gin
.
Context
)
{
uid
,
_
:=
strconv
.
ParseInt
(
getParam
(
c
,
"user_id"
),
10
,
64
)
msg
,
t
:=
getParamWithType
(
c
,
"message"
)
autoEscape
:=
global
.
EnsureBool
(
getParam
(
c
,
"auto_escape"
),
false
)
if
t
==
gjson
.
JSON
{
c
.
JSON
(
200
,
s
.
bot
.
CQSendPrivateMessage
(
uid
,
gjson
.
Parse
(
msg
)))
c
.
JSON
(
200
,
s
.
bot
.
CQSendPrivateMessage
(
uid
,
gjson
.
Parse
(
msg
)
,
autoEscape
))
return
}
c
.
JSON
(
200
,
s
.
bot
.
CQSendPrivateMessage
(
uid
,
msg
))
c
.
JSON
(
200
,
s
.
bot
.
CQSendPrivateMessage
(
uid
,
msg
,
autoEscape
))
}
func
(
s
*
httpServer
)
SendGroupMessage
(
c
*
gin
.
Context
)
{
gid
,
_
:=
strconv
.
ParseInt
(
getParam
(
c
,
"group_id"
),
10
,
64
)
msg
,
t
:=
getParamWithType
(
c
,
"message"
)
autoEscape
:=
global
.
EnsureBool
(
getParam
(
c
,
"auto_escape"
),
false
)
if
t
==
gjson
.
JSON
{
c
.
JSON
(
200
,
s
.
bot
.
CQSendGroupMessage
(
gid
,
gjson
.
Parse
(
msg
)))
c
.
JSON
(
200
,
s
.
bot
.
CQSendGroupMessage
(
gid
,
gjson
.
Parse
(
msg
)
,
autoEscape
))
return
}
c
.
JSON
(
200
,
s
.
bot
.
CQSendGroupMessage
(
gid
,
msg
))
c
.
JSON
(
200
,
s
.
bot
.
CQSendGroupMessage
(
gid
,
msg
,
autoEscape
))
}
func
(
s
*
httpServer
)
SendGroupForwardMessage
(
c
*
gin
.
Context
)
{
...
...
server/websocket.go
View file @
4820eb2f
...
...
@@ -337,28 +337,29 @@ var wsApi = map[string]func(*coolq.CQBot, gjson.Result) coolq.MSG{
)
},
"send_msg"
:
func
(
bot
*
coolq
.
CQBot
,
p
gjson
.
Result
)
coolq
.
MSG
{
autoEscape
:=
global
.
EnsureBool
(
p
.
Get
(
"auto_escape"
),
false
)
if
p
.
Get
(
"message_type"
)
.
Str
==
"private"
{
return
bot
.
CQSendPrivateMessage
(
p
.
Get
(
"user_id"
)
.
Int
(),
p
.
Get
(
"message"
))
return
bot
.
CQSendPrivateMessage
(
p
.
Get
(
"user_id"
)
.
Int
(),
p
.
Get
(
"message"
)
,
autoEscape
)
}
if
p
.
Get
(
"message_type"
)
.
Str
==
"group"
{
return
bot
.
CQSendGroupMessage
(
p
.
Get
(
"group_id"
)
.
Int
(),
p
.
Get
(
"message"
))
return
bot
.
CQSendGroupMessage
(
p
.
Get
(
"group_id"
)
.
Int
(),
p
.
Get
(
"message"
)
,
autoEscape
)
}
if
p
.
Get
(
"group_id"
)
.
Int
()
!=
0
{
return
bot
.
CQSendGroupMessage
(
p
.
Get
(
"group_id"
)
.
Int
(),
p
.
Get
(
"message"
))
return
bot
.
CQSendGroupMessage
(
p
.
Get
(
"group_id"
)
.
Int
(),
p
.
Get
(
"message"
)
,
autoEscape
)
}
if
p
.
Get
(
"user_id"
)
.
Int
()
!=
0
{
return
bot
.
CQSendPrivateMessage
(
p
.
Get
(
"user_id"
)
.
Int
(),
p
.
Get
(
"message"
))
return
bot
.
CQSendPrivateMessage
(
p
.
Get
(
"user_id"
)
.
Int
(),
p
.
Get
(
"message"
)
,
autoEscape
)
}
return
coolq
.
MSG
{}
},
"send_group_msg"
:
func
(
bot
*
coolq
.
CQBot
,
p
gjson
.
Result
)
coolq
.
MSG
{
return
bot
.
CQSendGroupMessage
(
p
.
Get
(
"group_id"
)
.
Int
(),
p
.
Get
(
"message"
))
return
bot
.
CQSendGroupMessage
(
p
.
Get
(
"group_id"
)
.
Int
(),
p
.
Get
(
"message"
)
,
global
.
EnsureBool
(
p
.
Get
(
"auto_escape"
),
false
)
)
},
"send_group_forward_msg"
:
func
(
bot
*
coolq
.
CQBot
,
p
gjson
.
Result
)
coolq
.
MSG
{
return
bot
.
CQSendGroupForwardMessage
(
p
.
Get
(
"group_id"
)
.
Int
(),
p
.
Get
(
"messages"
))
},
"send_private_msg"
:
func
(
bot
*
coolq
.
CQBot
,
p
gjson
.
Result
)
coolq
.
MSG
{
return
bot
.
CQSendPrivateMessage
(
p
.
Get
(
"user_id"
)
.
Int
(),
p
.
Get
(
"message"
))
return
bot
.
CQSendPrivateMessage
(
p
.
Get
(
"user_id"
)
.
Int
(),
p
.
Get
(
"message"
)
,
global
.
EnsureBool
(
p
.
Get
(
"auto_escape"
),
false
)
)
},
"delete_msg"
:
func
(
bot
*
coolq
.
CQBot
,
p
gjson
.
Result
)
coolq
.
MSG
{
return
bot
.
CQDeleteMessage
(
int32
(
p
.
Get
(
"message_id"
)
.
Int
()))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment