mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-06 12:03:50 +08:00
refactor: 将http,ws的api调用统一
方便以后添加接口,添加中间件
This commit is contained in:
parent
550e8d3531
commit
8ef877777d
382
server/api.go
Normal file
382
server/api.go
Normal file
@ -0,0 +1,382 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Mrs4s/go-cqhttp/coolq"
|
||||
"github.com/Mrs4s/go-cqhttp/global"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
type resultGetter interface {
|
||||
Get(string) gjson.Result
|
||||
}
|
||||
|
||||
type apiCaller struct {
|
||||
bot *coolq.CQBot
|
||||
}
|
||||
|
||||
func getLoginInfo(bot *coolq.CQBot, _ resultGetter) coolq.MSG {
|
||||
return bot.CQGetLoginInfo()
|
||||
}
|
||||
|
||||
func getFriendList(bot *coolq.CQBot, _ resultGetter) coolq.MSG {
|
||||
return bot.CQGetFriendList()
|
||||
}
|
||||
|
||||
func getGroupList(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQGetGroupList(p.Get("no_cache").Bool())
|
||||
}
|
||||
|
||||
func getGroupInfo(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQGetGroupList(p.Get("no_cache").Bool())
|
||||
}
|
||||
|
||||
func getGroupMemberList(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQGetGroupMemberList(p.Get("group_id").Int(), p.Get("no_cache").Bool())
|
||||
}
|
||||
|
||||
func getGroupMemberInfo(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQGetGroupMemberInfo(
|
||||
p.Get("group_id").Int(), p.Get("user_id").Int(),
|
||||
)
|
||||
}
|
||||
func sendMSG(bot *coolq.CQBot, p resultGetter) 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"), autoEscape)
|
||||
}
|
||||
if p.Get("message_type").Str == "group" {
|
||||
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"), autoEscape)
|
||||
}
|
||||
if p.Get("user_id").Int() != 0 {
|
||||
return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("message"), autoEscape)
|
||||
}
|
||||
return coolq.MSG{}
|
||||
}
|
||||
|
||||
func sendGroupMSG(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQSendGroupMessage(p.Get("group_id").Int(), p.Get("message"),
|
||||
global.EnsureBool(p.Get("auto_escape"), false))
|
||||
}
|
||||
|
||||
func sendGroupForwardMSG(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQSendGroupForwardMessage(p.Get("group_id").Int(), p.Get("messages"))
|
||||
}
|
||||
|
||||
func sendPrivateMSG(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("message"),
|
||||
global.EnsureBool(p.Get("auto_escape"), false))
|
||||
}
|
||||
|
||||
func deleteMSG(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQDeleteMessage(int32(p.Get("message_id").Int()))
|
||||
}
|
||||
|
||||
func setFriendAddRequest(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
apr := true
|
||||
if p.Get("approve").Exists() {
|
||||
apr = p.Get("approve").Bool()
|
||||
}
|
||||
return bot.CQProcessFriendRequest(p.Get("flag").Str, apr)
|
||||
}
|
||||
|
||||
func setGroupAddRequest(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
subType := p.Get("sub_type").Str
|
||||
apr := true
|
||||
if subType == "" {
|
||||
subType = p.Get("type").Str
|
||||
}
|
||||
if p.Get("approve").Exists() {
|
||||
apr = p.Get("approve").Bool()
|
||||
}
|
||||
return bot.CQProcessGroupRequest(p.Get("flag").Str, subType, p.Get("reason").Str, apr)
|
||||
}
|
||||
|
||||
func setGroupCard(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQSetGroupCard(p.Get("group_id").Int(), p.Get("user_id").Int(), p.Get("card").Str)
|
||||
}
|
||||
|
||||
func setGroupSpecialTitle(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQSetGroupSpecialTitle(p.Get("group_id").Int(), p.Get("user_id").Int(), p.Get("special_title").Str)
|
||||
}
|
||||
|
||||
func setGroupKick(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQSetGroupKick(p.Get("group_id").Int(), p.Get("user_id").Int(), p.Get("message").Str, p.Get("reject_add_request").Bool())
|
||||
}
|
||||
|
||||
func setGroupBan(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQSetGroupBan(p.Get("group_id").Int(), p.Get("user_id").Int(), func() uint32 {
|
||||
if p.Get("duration").Exists() {
|
||||
return uint32(p.Get("duration").Int())
|
||||
}
|
||||
return 1800
|
||||
}())
|
||||
}
|
||||
|
||||
func setGroupWholeBan(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQSetGroupWholeBan(p.Get("group_id").Int(), func() bool {
|
||||
if p.Get("enable").Exists() {
|
||||
return p.Get("enable").Bool()
|
||||
}
|
||||
return true
|
||||
}())
|
||||
}
|
||||
|
||||
func setGroupName(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQSetGroupName(p.Get("group_id").Int(), p.Get("group_name").Str)
|
||||
}
|
||||
|
||||
func setGroupAdmin(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQSetGroupAdmin(p.Get("group_id").Int(), p.Get("user_id").Int(), func() bool {
|
||||
if p.Get("enable").Exists() {
|
||||
return p.Get("enable").Bool()
|
||||
}
|
||||
return true
|
||||
}())
|
||||
}
|
||||
|
||||
func sendGroupNotice(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQSetGroupMemo(p.Get("group_id").Int(), p.Get("content").Str)
|
||||
}
|
||||
|
||||
func setGroupLeave(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQSetGroupLeave(p.Get("group_id").Int())
|
||||
}
|
||||
|
||||
func getImage(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQGetImage(p.Get("file").Str)
|
||||
}
|
||||
|
||||
func getForwardMSG(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
id := p.Get("message_id").Str
|
||||
if id == "" {
|
||||
id = p.Get("id").Str
|
||||
}
|
||||
return bot.CQGetForwardMessage(id)
|
||||
}
|
||||
|
||||
func getMSG(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQGetMessage(int32(p.Get("message_id").Int()))
|
||||
}
|
||||
|
||||
func downloadFile(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
headers := map[string]string{}
|
||||
headersToken := p.Get("headers")
|
||||
if headersToken.IsArray() {
|
||||
for _, sub := range headersToken.Array() {
|
||||
str := strings.SplitN(sub.String(), "=", 2)
|
||||
if len(str) == 2 {
|
||||
headers[str[0]] = str[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
if headersToken.Type == gjson.String {
|
||||
lines := strings.Split(headersToken.String(), "\r\n")
|
||||
for _, sub := range lines {
|
||||
str := strings.SplitN(sub, "=", 2)
|
||||
if len(str) == 2 {
|
||||
headers[str[0]] = str[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
return bot.CQDownloadFile(p.Get("url").Str, headers, int(p.Get("thread_count").Int()))
|
||||
}
|
||||
|
||||
func getGroupHonorInfo(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQGetGroupHonorInfo(p.Get("group_id").Int(), p.Get("type").Str)
|
||||
}
|
||||
|
||||
func setRestart(_ *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
var delay int64
|
||||
delay = p.Get("delay").Int()
|
||||
if delay < 0 {
|
||||
delay = 0
|
||||
}
|
||||
defer func(delay int64) {
|
||||
time.Sleep(time.Duration(delay) * time.Millisecond)
|
||||
Restart <- struct{}{}
|
||||
}(delay)
|
||||
return coolq.MSG{"data": nil, "retcode": 0, "status": "async"}
|
||||
|
||||
}
|
||||
|
||||
func canSendImage(bot *coolq.CQBot, _ resultGetter) coolq.MSG {
|
||||
return bot.CQCanSendImage()
|
||||
}
|
||||
|
||||
func canSendRecord(bot *coolq.CQBot, _ resultGetter) coolq.MSG {
|
||||
return bot.CQCanSendRecord()
|
||||
}
|
||||
|
||||
func getStrangerInfo(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQGetStrangerInfo(p.Get("user_id").Int())
|
||||
}
|
||||
|
||||
func getStatus(bot *coolq.CQBot, _ resultGetter) coolq.MSG {
|
||||
return bot.CQGetStatus()
|
||||
}
|
||||
|
||||
func getVersionInfo(bot *coolq.CQBot, _ resultGetter) coolq.MSG {
|
||||
return bot.CQGetVersionInfo()
|
||||
}
|
||||
|
||||
func getGroupSystemMSG(bot *coolq.CQBot, _ resultGetter) coolq.MSG {
|
||||
return bot.CQGetGroupSystemMessages()
|
||||
}
|
||||
|
||||
func getGroupFileSystemInfo(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQGetGroupFileSystemInfo(p.Get("group_id").Int())
|
||||
}
|
||||
|
||||
func getGroupRootFiles(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQGetGroupRootFiles(p.Get("group_id").Int())
|
||||
}
|
||||
|
||||
func getGroupFilesByFolder(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQGetGroupFilesByFolderID(p.Get("group_id").Int(), p.Get("folder_id").Str)
|
||||
}
|
||||
|
||||
func getGroupFileURL(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQGetGroupFileURL(p.Get("group_id").Int(), p.Get("file_id").Str, int32(p.Get("busid").Int()))
|
||||
}
|
||||
|
||||
func uploadGroupFile(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQUploadGroupFile(p.Get("group_id").Int(), p.Get("file").Str, p.Get("name").Str, p.Get("folder").Str)
|
||||
}
|
||||
|
||||
func getGroupMsgHistory(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQGetGroupMessageHistory(p.Get("group_id").Int(), p.Get("message_seq").Int())
|
||||
}
|
||||
|
||||
func getVipInfo(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQGetVipInfo(p.Get("user_id").Int())
|
||||
}
|
||||
|
||||
func reloadEventFilter(bot *coolq.CQBot, _ resultGetter) coolq.MSG {
|
||||
return bot.CQReloadEventFilter()
|
||||
}
|
||||
|
||||
func getGroupAtAllRemain(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQGetAtAllRemain(p.Get("group_id").Int())
|
||||
}
|
||||
|
||||
func ocrImage(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQOcrImage(p.Get("image").Str)
|
||||
}
|
||||
|
||||
func getOnlineClients(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQGetOnlineClients(p.Get("no_cache").Bool())
|
||||
}
|
||||
|
||||
func getWordSlices(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQGetWordSlices(p.Get("content").Str)
|
||||
}
|
||||
|
||||
func setGroupPortrait(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQSetGroupPortrait(p.Get("group_id").Int(), p.Get("file").String(), p.Get("cache").String())
|
||||
}
|
||||
|
||||
func setEssenceMSG(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQSetEssenceMessage(int32(p.Get("message_id").Int()))
|
||||
}
|
||||
|
||||
func deleteEssenceMSG(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQDeleteEssenceMessage(int32(p.Get("message_id").Int()))
|
||||
}
|
||||
|
||||
func getEssenceMsgList(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQGetEssenceMessageList(p.Get("group_id").Int())
|
||||
}
|
||||
|
||||
func checkUrlSafely(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQCheckURLSafely(p.Get("url").String())
|
||||
}
|
||||
|
||||
func setGroupAnonymousBan(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
obj := p.Get("anonymous")
|
||||
flag := p.Get("anonymous_flag")
|
||||
if !flag.Exists() {
|
||||
flag = p.Get("flag")
|
||||
}
|
||||
if !flag.Exists() && !obj.Exists() {
|
||||
return coolq.Failed(100, "FLAG_NOT_FOUND", "flag未找到")
|
||||
}
|
||||
if !flag.Exists() {
|
||||
flag = obj.Get("flag")
|
||||
}
|
||||
return bot.CQSetGroupAnonymousBan(p.Get("group_id").Int(), flag.String(), int32(p.Get("duration").Int()))
|
||||
}
|
||||
|
||||
func handleQuickOperation(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||
return bot.CQHandleQuickOperation(p.Get("context"), p.Get("operation"))
|
||||
}
|
||||
|
||||
var API = map[string]func(*coolq.CQBot, resultGetter) coolq.MSG{
|
||||
"get_login_info": getLoginInfo,
|
||||
"get_friend_list": getFriendList,
|
||||
"get_group_list": getGroupList,
|
||||
"get_group_info": getGroupInfo,
|
||||
"get_group_member_list": getGroupMemberList,
|
||||
"get_group_member_info": getGroupMemberInfo,
|
||||
"send_msg": sendMSG,
|
||||
"send_group_msg": sendGroupMSG,
|
||||
"send_group_forward_msg": sendGroupForwardMSG,
|
||||
"send_private_msg": sendPrivateMSG,
|
||||
"delete_msg": deleteMSG,
|
||||
"set_friend_add_request": setFriendAddRequest,
|
||||
"set_group_add_request": setGroupAddRequest,
|
||||
"set_group_card": setGroupCard,
|
||||
"set_group_special_title": setGroupSpecialTitle,
|
||||
"set_group_kick": setGroupKick,
|
||||
"set_group_ban": setGroupBan,
|
||||
"set_group_whole_ban": setGroupWholeBan,
|
||||
"set_group_name": setGroupName,
|
||||
"set_group_admin": setGroupAdmin,
|
||||
"_send_group_notice": sendGroupNotice,
|
||||
"set_group_leave": setGroupLeave,
|
||||
"get_image": getImage,
|
||||
"get_forward_msg": getForwardMSG,
|
||||
"get_msg": getMSG,
|
||||
"download_file": downloadFile,
|
||||
"get_group_honor_info": getGroupHonorInfo,
|
||||
"set_restart": setRestart,
|
||||
"can_send_image": canSendImage,
|
||||
"can_send_record": canSendRecord,
|
||||
"get_stranger_info": getStrangerInfo,
|
||||
"get_status": getStatus,
|
||||
"get_version_info": getVersionInfo,
|
||||
"get_group_system_msg": getGroupSystemMSG,
|
||||
"get_group_file_system_info": getGroupFileSystemInfo,
|
||||
"get_group_root_files": getGroupRootFiles,
|
||||
"get_group_files_by_folder": getGroupFilesByFolder,
|
||||
"get_group_file_url": getGroupFileURL,
|
||||
"upload_group_file": uploadGroupFile,
|
||||
"get_group_msg_history": getGroupMsgHistory,
|
||||
"_get_vip_info": getVipInfo,
|
||||
"reload_event_filter": reloadEventFilter,
|
||||
".ocr_image": ocrImage,
|
||||
"ocr_image": ocrImage,
|
||||
"get_group_at_all_remain": getGroupAtAllRemain,
|
||||
"get_online_clients": getOnlineClients,
|
||||
".get_word_slices": getWordSlices,
|
||||
"set_group_portrait": setGroupPortrait,
|
||||
"set_essence_msg": setEssenceMSG,
|
||||
"delete_essence_msg": deleteEssenceMSG,
|
||||
"get_essence_msg_list": getEssenceMsgList,
|
||||
"check_url_safely": checkUrlSafely,
|
||||
"set_group_anonymous_ban": setGroupAnonymousBan,
|
||||
".handle_quick_operation": handleQuickOperation,
|
||||
}
|
||||
|
||||
func (api *apiCaller) callAPI(action string, p resultGetter) coolq.MSG {
|
||||
if f, ok := API[action]; ok {
|
||||
return f(api.bot, p)
|
||||
} else {
|
||||
return coolq.Failed(404, "API_NOT_FOUND", "API不存在")
|
||||
}
|
||||
}
|
517
server/http.go
517
server/http.go
@ -7,16 +7,14 @@ import (
|
||||
"encoding/hex"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/guonaihong/gout/dataflow"
|
||||
|
||||
"github.com/Mrs4s/go-cqhttp/coolq"
|
||||
"github.com/Mrs4s/go-cqhttp/global"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/guonaihong/gout"
|
||||
"github.com/guonaihong/gout/dataflow"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
@ -25,6 +23,7 @@ type httpServer struct {
|
||||
engine *gin.Engine
|
||||
bot *coolq.CQBot
|
||||
HTTP *http.Server
|
||||
api *apiCaller
|
||||
}
|
||||
|
||||
type httpClient struct {
|
||||
@ -34,6 +33,10 @@ type httpClient struct {
|
||||
timeout int32
|
||||
}
|
||||
|
||||
type httpContext struct {
|
||||
ctx *gin.Context
|
||||
}
|
||||
|
||||
var cqHTTPServer = &httpServer{}
|
||||
|
||||
// Debug 是否启用Debug模式
|
||||
@ -43,6 +46,7 @@ func (s *httpServer) Run(addr, authToken string, bot *coolq.CQBot) {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
s.engine = gin.New()
|
||||
s.bot = bot
|
||||
s.api = &apiCaller{s.bot}
|
||||
s.engine.Use(func(c *gin.Context) {
|
||||
if c.Request.Method != "GET" && c.Request.Method != "POST" {
|
||||
log.Warnf("已拒绝客户端 %v 的请求: 方法错误", c.Request.RemoteAddr)
|
||||
@ -156,522 +160,29 @@ func (s *httpServer) HandleActions(c *gin.Context) {
|
||||
global.RateLimit(context.Background())
|
||||
action := strings.ReplaceAll(c.Param("action"), "_async", "")
|
||||
log.Debugf("HTTPServer接收到API调用: %v", action)
|
||||
if f, ok := httpAPI[action]; ok {
|
||||
f(s, c)
|
||||
} else {
|
||||
c.JSON(200, coolq.Failed(404))
|
||||
}
|
||||
c.JSON(200, s.api.callAPI(action, httpContext{ctx: c}))
|
||||
}
|
||||
|
||||
// GetLoginInfo 获取登录号信息
|
||||
func GetLoginInfo(s *httpServer, c *gin.Context) {
|
||||
c.JSON(200, s.bot.CQGetLoginInfo())
|
||||
}
|
||||
|
||||
// GetFriendList 获取好友列表
|
||||
func GetFriendList(s *httpServer, c *gin.Context) {
|
||||
c.JSON(200, s.bot.CQGetFriendList())
|
||||
}
|
||||
|
||||
// GetGroupList 获取群列表
|
||||
func GetGroupList(s *httpServer, c *gin.Context) {
|
||||
nc := getParamOrDefault(c, "no_cache", "false")
|
||||
c.JSON(200, s.bot.CQGetGroupList(nc == "true"))
|
||||
}
|
||||
|
||||
// GetGroupInfo 获取群信息
|
||||
func GetGroupInfo(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
nc := getParamOrDefault(c, "no_cache", "false")
|
||||
c.JSON(200, s.bot.CQGetGroupInfo(gid, nc == "true"))
|
||||
}
|
||||
|
||||
// GetGroupMemberList 获取群成员列表
|
||||
func GetGroupMemberList(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
nc := getParamOrDefault(c, "no_cache", "false")
|
||||
c.JSON(200, s.bot.CQGetGroupMemberList(gid, nc == "true"))
|
||||
}
|
||||
|
||||
// GetGroupMemberInfo 获取群成员信息
|
||||
func GetGroupMemberInfo(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
||||
c.JSON(200, s.bot.CQGetGroupMemberInfo(gid, uid))
|
||||
}
|
||||
|
||||
// GetGroupFileSystemInfo 扩展API-获取群文件系统信息
|
||||
func GetGroupFileSystemInfo(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
c.JSON(200, s.bot.CQGetGroupFileSystemInfo(gid))
|
||||
}
|
||||
|
||||
// GetGroupRootFiles 扩展API-获取群根目录文件列表
|
||||
func GetGroupRootFiles(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
c.JSON(200, s.bot.CQGetGroupRootFiles(gid))
|
||||
}
|
||||
|
||||
// GetGroupFilesByFolderID 扩展API-获取群子目录文件列表
|
||||
func GetGroupFilesByFolderID(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
folderID := getParam(c, "folder_id")
|
||||
c.JSON(200, s.bot.CQGetGroupFilesByFolderID(gid, folderID))
|
||||
}
|
||||
|
||||
// GetGroupFileURL 扩展API-获取群文件资源链接
|
||||
func GetGroupFileURL(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
fid := getParam(c, "file_id")
|
||||
busid, _ := strconv.ParseInt(getParam(c, "busid"), 10, 32)
|
||||
c.JSON(200, s.bot.CQGetGroupFileURL(gid, fid, int32(busid)))
|
||||
}
|
||||
|
||||
// UploadGroupFile 扩展API-上传群文件
|
||||
func UploadGroupFile(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
c.JSON(200, s.bot.CQUploadGroupFile(gid, getParam(c, "file"), getParam(c, "name"), getParam(c, "folder")))
|
||||
}
|
||||
|
||||
// SendMessage 发送消息
|
||||
//
|
||||
// https://git.io/JtwTQ
|
||||
func SendMessage(s *httpServer, c *gin.Context) {
|
||||
if getParam(c, "message_type") == "private" {
|
||||
SendPrivateMessage(s, c)
|
||||
return
|
||||
}
|
||||
if getParam(c, "message_type") == "group" {
|
||||
SendGroupMessage(s, c)
|
||||
return
|
||||
}
|
||||
if getParam(c, "group_id") != "" {
|
||||
SendGroupMessage(s, c)
|
||||
return
|
||||
}
|
||||
if getParam(c, "user_id") != "" {
|
||||
SendPrivateMessage(s, c)
|
||||
}
|
||||
}
|
||||
|
||||
// SendPrivateMessage 发送私聊消息
|
||||
func SendPrivateMessage(s *httpServer, 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), autoEscape))
|
||||
return
|
||||
}
|
||||
c.JSON(200, s.bot.CQSendPrivateMessage(uid, msg, autoEscape))
|
||||
}
|
||||
|
||||
// SendGroupMessage 发送群消息
|
||||
func SendGroupMessage(s *httpServer, 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), autoEscape))
|
||||
return
|
||||
}
|
||||
c.JSON(200, s.bot.CQSendGroupMessage(gid, msg, autoEscape))
|
||||
}
|
||||
|
||||
// SendGroupForwardMessage 扩展API-发送合并转发(群)
|
||||
func SendGroupForwardMessage(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
msg := getParam(c, "messages")
|
||||
c.JSON(200, s.bot.CQSendGroupForwardMessage(gid, gjson.Parse(msg)))
|
||||
}
|
||||
|
||||
// GetImage 获取图片(修改自OneBot)
|
||||
func GetImage(s *httpServer, c *gin.Context) {
|
||||
file := getParam(c, "file")
|
||||
c.JSON(200, s.bot.CQGetImage(file))
|
||||
}
|
||||
|
||||
// GetMessage 获取消息
|
||||
func GetMessage(s *httpServer, c *gin.Context) {
|
||||
mid, _ := strconv.ParseInt(getParam(c, "message_id"), 10, 32)
|
||||
c.JSON(200, s.bot.CQGetMessage(int32(mid)))
|
||||
}
|
||||
|
||||
// GetGroupHonorInfo 获取群荣誉信息
|
||||
func GetGroupHonorInfo(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
c.JSON(200, s.bot.CQGetGroupHonorInfo(gid, getParam(c, "type")))
|
||||
}
|
||||
|
||||
// ProcessFriendRequest 处理加好友请求
|
||||
func ProcessFriendRequest(s *httpServer, c *gin.Context) {
|
||||
flag := getParam(c, "flag")
|
||||
approve := getParamOrDefault(c, "approve", "true")
|
||||
c.JSON(200, s.bot.CQProcessFriendRequest(flag, approve == "true"))
|
||||
}
|
||||
|
||||
// ProcessGroupRequest 处理加群请求/邀请
|
||||
func ProcessGroupRequest(s *httpServer, c *gin.Context) {
|
||||
flag := getParam(c, "flag")
|
||||
subType := getParam(c, "sub_type")
|
||||
if subType == "" {
|
||||
subType = getParam(c, "type")
|
||||
}
|
||||
approve := getParamOrDefault(c, "approve", "true")
|
||||
c.JSON(200, s.bot.CQProcessGroupRequest(flag, subType, getParam(c, "reason"), approve == "true"))
|
||||
}
|
||||
|
||||
// SetGroupCard 设置群名片(群备注)
|
||||
func SetGroupCard(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
||||
c.JSON(200, s.bot.CQSetGroupCard(gid, uid, getParam(c, "card")))
|
||||
}
|
||||
|
||||
// SetSpecialTitle 设置群组专属头衔
|
||||
func SetSpecialTitle(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
||||
c.JSON(200, s.bot.CQSetGroupSpecialTitle(gid, uid, getParam(c, "special_title")))
|
||||
}
|
||||
|
||||
// SetGroupKick 群组踢人
|
||||
func SetGroupKick(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
||||
msg := getParam(c, "message")
|
||||
block := getParamOrDefault(c, "reject_add_request", "false")
|
||||
c.JSON(200, s.bot.CQSetGroupKick(gid, uid, msg, block == "true"))
|
||||
}
|
||||
|
||||
// SetGroupBan 群组单人禁言
|
||||
func SetGroupBan(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
||||
i, _ := strconv.ParseInt(getParamOrDefault(c, "duration", "1800"), 10, 64)
|
||||
c.JSON(200, s.bot.CQSetGroupBan(gid, uid, uint32(i)))
|
||||
}
|
||||
|
||||
// SetWholeBan 群组全员禁言
|
||||
func SetWholeBan(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
c.JSON(200, s.bot.CQSetGroupWholeBan(gid, getParamOrDefault(c, "enable", "true") == "true"))
|
||||
}
|
||||
|
||||
// SetGroupName 设置群名
|
||||
func SetGroupName(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
c.JSON(200, s.bot.CQSetGroupName(gid, getParam(c, "group_name")))
|
||||
}
|
||||
|
||||
// SetGroupAdmin 群组设置管理员
|
||||
func SetGroupAdmin(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
||||
c.JSON(200, s.bot.CQSetGroupAdmin(gid, uid, getParamOrDefault(c, "enable", "true") == "true"))
|
||||
}
|
||||
|
||||
// SendGroupNotice 扩展API-发送群公告
|
||||
func SendGroupNotice(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
c.JSON(200, s.bot.CQSetGroupMemo(gid, getParam(c, "content")))
|
||||
}
|
||||
|
||||
// SetGroupLeave 退出群组
|
||||
func SetGroupLeave(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
c.JSON(200, s.bot.CQSetGroupLeave(gid))
|
||||
}
|
||||
|
||||
// SetRestart 重启 OneBot 实现
|
||||
//
|
||||
// https://git.io/JtwkJ
|
||||
func SetRestart(s *httpServer, c *gin.Context) {
|
||||
delay, _ := strconv.ParseInt(getParam(c, "delay"), 10, 64)
|
||||
c.JSON(200, coolq.MSG{"data": nil, "retcode": 0, "status": "async"})
|
||||
go func(delay int64) {
|
||||
time.Sleep(time.Duration(delay) * time.Millisecond)
|
||||
Restart <- struct{}{}
|
||||
}(delay)
|
||||
|
||||
}
|
||||
|
||||
// GetForwardMessage 获取合并转发消息
|
||||
func GetForwardMessage(s *httpServer, c *gin.Context) {
|
||||
resID := getParam(c, "message_id")
|
||||
if resID == "" {
|
||||
resID = getParam(c, "id")
|
||||
}
|
||||
c.JSON(200, s.bot.CQGetForwardMessage(resID))
|
||||
}
|
||||
|
||||
// GetGroupSystemMessage 扩展API-获取群文件系统消息
|
||||
func GetGroupSystemMessage(s *httpServer, c *gin.Context) {
|
||||
c.JSON(200, s.bot.CQGetGroupSystemMessages())
|
||||
}
|
||||
|
||||
// DeleteMessage 撤回消息
|
||||
func DeleteMessage(s *httpServer, c *gin.Context) {
|
||||
mid, _ := strconv.ParseInt(getParam(c, "message_id"), 10, 32)
|
||||
c.JSON(200, s.bot.CQDeleteMessage(int32(mid)))
|
||||
}
|
||||
|
||||
// CanSendImage 检查是否可以发送图片(此处永远返回true)
|
||||
func CanSendImage(s *httpServer, c *gin.Context) {
|
||||
c.JSON(200, s.bot.CQCanSendImage())
|
||||
}
|
||||
|
||||
// CanSendRecord 检查是否可以发送语音(此处永远返回true)
|
||||
func CanSendRecord(s *httpServer, c *gin.Context) {
|
||||
c.JSON(200, s.bot.CQCanSendRecord())
|
||||
}
|
||||
|
||||
// GetStatus 获取运行状态
|
||||
func GetStatus(s *httpServer, c *gin.Context) {
|
||||
c.JSON(200, s.bot.CQGetStatus())
|
||||
}
|
||||
|
||||
// GetVersionInfo 获取版本信息
|
||||
func GetVersionInfo(s *httpServer, c *gin.Context) {
|
||||
c.JSON(200, s.bot.CQGetVersionInfo())
|
||||
}
|
||||
|
||||
// ReloadEventFilter 扩展API-重载事件过滤器
|
||||
func ReloadEventFilter(s *httpServer, c *gin.Context) {
|
||||
c.JSON(200, s.bot.CQReloadEventFilter())
|
||||
}
|
||||
|
||||
// GetVipInfo 扩展API-获取VIP信息
|
||||
func GetVipInfo(s *httpServer, c *gin.Context) {
|
||||
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
||||
c.JSON(200, s.bot.CQGetVipInfo(uid))
|
||||
}
|
||||
|
||||
// GetStrangerInfo 获取陌生人信息
|
||||
func GetStrangerInfo(s *httpServer, c *gin.Context) {
|
||||
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
||||
c.JSON(200, s.bot.CQGetStrangerInfo(uid))
|
||||
}
|
||||
|
||||
// GetGroupAtAllRemain 扩展API-获取群 @全体成员 剩余次数
|
||||
func GetGroupAtAllRemain(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
c.JSON(200, s.bot.CQGetAtAllRemain(gid))
|
||||
}
|
||||
|
||||
// SetGroupAnonymousBan 群组匿名用户禁言
|
||||
func SetGroupAnonymousBan(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
d, _ := strconv.ParseInt(getParam(c, "duration"), 10, 64)
|
||||
flag := getParam(c, "flag")
|
||||
if flag == "" {
|
||||
flag = getParam(c, "anonymous_flag")
|
||||
}
|
||||
if flag == "" {
|
||||
o := gjson.Parse(getParam(c, "anonymous"))
|
||||
flag = o.Get("flag").String()
|
||||
}
|
||||
c.JSON(200, s.bot.CQSetGroupAnonymousBan(gid, flag, int32(d)))
|
||||
}
|
||||
|
||||
// GetGroupMessageHistory 获取群消息历史记录
|
||||
func GetGroupMessageHistory(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
seq, _ := strconv.ParseInt(getParam(c, "message_seq"), 10, 64)
|
||||
c.JSON(200, s.bot.CQGetGroupMessageHistory(gid, seq))
|
||||
}
|
||||
|
||||
// GetOnlineClients 扩展API-获取当前账号在线客户端列表
|
||||
func GetOnlineClients(s *httpServer, c *gin.Context) {
|
||||
c.JSON(200, s.bot.CQGetOnlineClients(getParamOrDefault(c, "no_cache", "false") == "true"))
|
||||
}
|
||||
|
||||
// HandleQuickOperation 隐藏API-对事件执行快速操作
|
||||
func HandleQuickOperation(s *httpServer, c *gin.Context) {
|
||||
if c.Request.Method != "POST" {
|
||||
c.AbortWithStatus(404)
|
||||
return
|
||||
}
|
||||
if i, ok := c.Get("json_body"); ok {
|
||||
body := i.(gjson.Result)
|
||||
c.JSON(200, s.bot.CQHandleQuickOperation(body.Get("context"), body.Get("operation")))
|
||||
}
|
||||
}
|
||||
|
||||
// DownloadFile 扩展API-下载文件到缓存目录
|
||||
func DownloadFile(s *httpServer, c *gin.Context) {
|
||||
url := getParam(c, "url")
|
||||
tc, _ := strconv.Atoi(getParam(c, "thread_count"))
|
||||
h, t := getParamWithType(c, "headers")
|
||||
headers := map[string]string{}
|
||||
if t == gjson.Null || t == gjson.String {
|
||||
lines := strings.Split(h, "\r\n")
|
||||
for _, sub := range lines {
|
||||
str := strings.SplitN(sub, "=", 2)
|
||||
if len(str) == 2 {
|
||||
headers[str[0]] = str[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
if t == gjson.JSON {
|
||||
arr := gjson.Parse(h)
|
||||
for _, sub := range arr.Array() {
|
||||
str := strings.SplitN(sub.String(), "=", 2)
|
||||
if len(str) == 2 {
|
||||
headers[str[0]] = str[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
println(url, tc, h, t)
|
||||
c.JSON(200, s.bot.CQDownloadFile(url, headers, tc))
|
||||
}
|
||||
|
||||
// OcrImage 扩展API-图片OCR
|
||||
func OcrImage(s *httpServer, c *gin.Context) {
|
||||
img := getParam(c, "image")
|
||||
c.JSON(200, s.bot.CQOcrImage(img))
|
||||
}
|
||||
|
||||
// GetWordSlices 隐藏API-获取中文分词
|
||||
func GetWordSlices(s *httpServer, c *gin.Context) {
|
||||
content := getParam(c, "content")
|
||||
c.JSON(200, s.bot.CQGetWordSlices(content))
|
||||
}
|
||||
|
||||
// SetGroupPortrait 扩展API-设置群头像
|
||||
func SetGroupPortrait(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
file := getParam(c, "file")
|
||||
cache := getParam(c, "cache")
|
||||
c.JSON(200, s.bot.CQSetGroupPortrait(gid, file, cache))
|
||||
}
|
||||
|
||||
// SetEssenceMsg 扩展API-设置精华消息
|
||||
func SetEssenceMsg(s *httpServer, c *gin.Context) {
|
||||
mid, _ := strconv.ParseInt(getParam(c, "message_id"), 10, 64)
|
||||
c.JSON(200, s.bot.CQSetEssenceMessage(int32(mid)))
|
||||
}
|
||||
|
||||
// DeleteEssenceMsg 扩展API-移出精华消息
|
||||
func DeleteEssenceMsg(s *httpServer, c *gin.Context) {
|
||||
mid, _ := strconv.ParseInt(getParam(c, "message_id"), 10, 64)
|
||||
c.JSON(200, s.bot.CQDeleteEssenceMessage(int32(mid)))
|
||||
}
|
||||
|
||||
// GetEssenceMsgList 扩展API-获取精华消息列表
|
||||
func GetEssenceMsgList(s *httpServer, c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
c.JSON(200, s.bot.CQGetEssenceMessageList(gid))
|
||||
}
|
||||
|
||||
// CheckURLSafely 扩展API-检查链接安全性
|
||||
func CheckURLSafely(s *httpServer, c *gin.Context) {
|
||||
c.JSON(200, s.bot.CQCheckURLSafely(getParam(c, "url")))
|
||||
}
|
||||
|
||||
func getParamOrDefault(c *gin.Context, k, def string) string {
|
||||
r := getParam(c, k)
|
||||
if r != "" {
|
||||
return r
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func getParam(c *gin.Context, k string) string {
|
||||
p, _ := getParamWithType(c, k)
|
||||
return p
|
||||
}
|
||||
|
||||
func getParamWithType(c *gin.Context, k string) (string, gjson.Type) {
|
||||
func (h httpContext) Get(k string) gjson.Result {
|
||||
c := h.ctx
|
||||
if q := c.Query(k); q != "" {
|
||||
return q, gjson.Null
|
||||
return gjson.Result{Type: gjson.String, Str: q}
|
||||
}
|
||||
if c.Request.Method == "POST" {
|
||||
if h := c.Request.Header.Get("Content-Type"); h != "" {
|
||||
if strings.Contains(h, "application/x-www-form-urlencoded") {
|
||||
if p, ok := c.GetPostForm(k); ok {
|
||||
return p, gjson.Null
|
||||
return gjson.Result{Type: gjson.String, Str: p}
|
||||
}
|
||||
}
|
||||
if strings.Contains(h, "application/json") {
|
||||
if obj, ok := c.Get("json_body"); ok {
|
||||
res := obj.(gjson.Result).Get(k)
|
||||
if res.Exists() {
|
||||
switch res.Type {
|
||||
case gjson.JSON:
|
||||
return res.Raw, gjson.JSON
|
||||
case gjson.String:
|
||||
return res.Str, gjson.String
|
||||
case gjson.Number:
|
||||
return strconv.FormatInt(res.Int(), 10), gjson.Number // 似乎没有需要接受 float 类型的api
|
||||
case gjson.True:
|
||||
return "true", gjson.True
|
||||
case gjson.False:
|
||||
return "false", gjson.False
|
||||
}
|
||||
}
|
||||
return obj.(gjson.Result).Get(k)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", gjson.Null
|
||||
}
|
||||
|
||||
var httpAPI = map[string]func(s *httpServer, c *gin.Context){
|
||||
"get_login_info": GetLoginInfo,
|
||||
"get_friend_list": GetFriendList,
|
||||
"get_group_list": GetGroupList,
|
||||
"get_group_info": GetGroupInfo,
|
||||
"get_group_member_list": GetGroupMemberList,
|
||||
"get_group_member_info": GetGroupMemberInfo,
|
||||
"get_group_file_system_info": GetGroupFileSystemInfo,
|
||||
"get_group_root_files": GetGroupRootFiles,
|
||||
"get_group_files_by_folder": GetGroupFilesByFolderID,
|
||||
"get_group_file_url": GetGroupFileURL,
|
||||
"upload_group_file": UploadGroupFile,
|
||||
"get_essence_msg_list": GetEssenceMsgList,
|
||||
"send_msg": SendMessage,
|
||||
"send_group_msg": SendGroupMessage,
|
||||
"send_group_forward_msg": SendGroupForwardMessage,
|
||||
"send_private_msg": SendPrivateMessage,
|
||||
"delete_msg": DeleteMessage,
|
||||
"delete_essence_msg": DeleteEssenceMsg,
|
||||
"set_friend_add_request": ProcessFriendRequest,
|
||||
"set_group_add_request": ProcessGroupRequest,
|
||||
"set_group_card": SetGroupCard,
|
||||
"set_group_special_title": SetSpecialTitle,
|
||||
"set_group_kick": SetGroupKick,
|
||||
"set_group_ban": SetGroupBan,
|
||||
"set_group_whole_ban": SetWholeBan,
|
||||
"set_group_name": SetGroupName,
|
||||
"set_group_admin": SetGroupAdmin,
|
||||
"set_essence_msg": SetEssenceMsg,
|
||||
"set_restart": SetRestart,
|
||||
"_send_group_notice": SendGroupNotice,
|
||||
"set_group_leave": SetGroupLeave,
|
||||
"get_image": GetImage,
|
||||
"get_forward_msg": GetForwardMessage,
|
||||
"get_msg": GetMessage,
|
||||
"get_group_system_msg": GetGroupSystemMessage,
|
||||
"get_group_honor_info": GetGroupHonorInfo,
|
||||
"can_send_image": CanSendImage,
|
||||
"can_send_record": CanSendRecord,
|
||||
"get_status": GetStatus,
|
||||
"get_version_info": GetVersionInfo,
|
||||
"_get_vip_info": GetVipInfo,
|
||||
"get_stranger_info": GetStrangerInfo,
|
||||
"reload_event_filter": ReloadEventFilter,
|
||||
"set_group_portrait": SetGroupPortrait,
|
||||
"set_group_anonymous_ban": SetGroupAnonymousBan,
|
||||
"get_group_msg_history": GetGroupMessageHistory,
|
||||
"check_url_safely": CheckURLSafely,
|
||||
"download_file": DownloadFile,
|
||||
".handle_quick_operation": HandleQuickOperation,
|
||||
".ocr_image": OcrImage,
|
||||
"ocr_image": OcrImage,
|
||||
"get_group_at_all_remain": GetGroupAtAllRemain,
|
||||
"get_online_clients": GetOnlineClients,
|
||||
".get_word_slices": GetWordSlices,
|
||||
return gjson.Result{Type: gjson.String, Str: ""}
|
||||
}
|
||||
|
||||
func (s *httpServer) ShutDown() {
|
||||
|
@ -38,6 +38,7 @@ type WebSocketClient struct {
|
||||
type webSocketConn struct {
|
||||
*websocket.Conn
|
||||
sync.Mutex
|
||||
apiCaller apiCaller
|
||||
}
|
||||
|
||||
// WebSocketServer 初始化一个WebSocketServer实例
|
||||
@ -106,7 +107,7 @@ func (c *WebSocketClient) connectAPI() {
|
||||
return
|
||||
}
|
||||
log.Infof("已连接到反向WebSocket API服务器 %v", c.conf.ReverseAPIURL)
|
||||
wrappedConn := &webSocketConn{Conn: conn}
|
||||
wrappedConn := &webSocketConn{Conn: conn, apiCaller: apiCaller{c.bot}}
|
||||
go c.listenAPI(wrappedConn, false)
|
||||
}
|
||||
|
||||
@ -138,7 +139,7 @@ func (c *WebSocketClient) connectEvent() {
|
||||
}
|
||||
|
||||
log.Infof("已连接到反向WebSocket Event服务器 %v", c.conf.ReverseEventURL)
|
||||
c.eventConn = &webSocketConn{Conn: conn}
|
||||
c.eventConn = &webSocketConn{Conn: conn, apiCaller: apiCaller{c.bot}}
|
||||
}
|
||||
|
||||
func (c *WebSocketClient) connectUniversal() {
|
||||
@ -168,7 +169,7 @@ func (c *WebSocketClient) connectUniversal() {
|
||||
log.Warnf("反向WebSocket 握手时出现错误: %v", err)
|
||||
}
|
||||
|
||||
wrappedConn := &webSocketConn{Conn: conn}
|
||||
wrappedConn := &webSocketConn{Conn: conn, apiCaller: apiCaller{c.bot}}
|
||||
go c.listenAPI(wrappedConn, true)
|
||||
c.universalConn = wrappedConn
|
||||
}
|
||||
@ -250,7 +251,7 @@ func (s *webSocketServer) event(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
log.Infof("接受 WebSocket 连接: %v (/event)", r.RemoteAddr)
|
||||
|
||||
conn := &webSocketConn{Conn: c}
|
||||
conn := &webSocketConn{Conn: c, apiCaller: apiCaller{s.bot}}
|
||||
|
||||
s.eventConnMutex.Lock()
|
||||
s.eventConn = append(s.eventConn, conn)
|
||||
@ -273,7 +274,7 @@ func (s *webSocketServer) api(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
log.Infof("接受 WebSocket 连接: %v (/api)", r.RemoteAddr)
|
||||
conn := &webSocketConn{Conn: c}
|
||||
conn := &webSocketConn{Conn: c, apiCaller: apiCaller{s.bot}}
|
||||
go s.listenAPI(conn)
|
||||
}
|
||||
|
||||
@ -299,7 +300,7 @@ func (s *webSocketServer) any(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
log.Infof("接受 WebSocket 连接: %v (/)", r.RemoteAddr)
|
||||
conn := &webSocketConn{Conn: c}
|
||||
conn := &webSocketConn{Conn: c, apiCaller: apiCaller{s.bot}}
|
||||
s.eventConn = append(s.eventConn, conn)
|
||||
s.listenAPI(conn)
|
||||
}
|
||||
@ -318,7 +319,7 @@ func (s *webSocketServer) listenAPI(c *webSocketConn) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *webSocketConn) handleRequest(bot *coolq.CQBot, payload []byte) {
|
||||
func (c *webSocketConn) handleRequest(_ *coolq.CQBot, payload []byte) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
log.Printf("处置WS命令时发生无法恢复的异常:%v\n%s", err, debug.Stack())
|
||||
@ -329,23 +330,13 @@ func (c *webSocketConn) handleRequest(bot *coolq.CQBot, payload []byte) {
|
||||
j := gjson.ParseBytes(payload)
|
||||
t := strings.ReplaceAll(j.Get("action").Str, "_async", "")
|
||||
log.Debugf("WS接收到API调用: %v 参数: %v", t, j.Get("params").Raw)
|
||||
if f, ok := wsAPI[t]; ok {
|
||||
ret := f(bot, j.Get("params"))
|
||||
if j.Get("echo").Exists() {
|
||||
ret["echo"] = j.Get("echo").Value()
|
||||
}
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
_ = c.WriteJSON(ret)
|
||||
} else {
|
||||
ret := coolq.Failed(1404, "API_NOT_FOUND", "API不存在")
|
||||
if j.Get("echo").Exists() {
|
||||
ret["echo"] = j.Get("echo").Value()
|
||||
}
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
_ = c.WriteJSON(ret)
|
||||
ret := c.apiCaller.callAPI(t, j.Get("params"))
|
||||
if j.Get("echo").Exists() {
|
||||
ret["echo"] = j.Get("echo").Value()
|
||||
}
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
_ = c.WriteJSON(ret)
|
||||
}
|
||||
|
||||
func (s *webSocketServer) onBotPushEvent(m coolq.MSG) {
|
||||
@ -371,254 +362,3 @@ func (s *webSocketServer) onBotPushEvent(m coolq.MSG) {
|
||||
conn.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
var wsAPI = map[string]func(*coolq.CQBot, gjson.Result) coolq.MSG{
|
||||
"get_login_info": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetLoginInfo()
|
||||
},
|
||||
"get_friend_list": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetFriendList()
|
||||
},
|
||||
"get_group_list": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetGroupList(p.Get("no_cache").Bool())
|
||||
},
|
||||
"get_group_info": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetGroupInfo(p.Get("group_id").Int(), p.Get("no_cache").Bool())
|
||||
},
|
||||
"get_group_member_list": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetGroupMemberList(p.Get("group_id").Int(), p.Get("no_cache").Bool())
|
||||
},
|
||||
"get_group_member_info": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetGroupMemberInfo(
|
||||
p.Get("group_id").Int(), p.Get("user_id").Int(),
|
||||
)
|
||||
},
|
||||
"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"), autoEscape)
|
||||
}
|
||||
if p.Get("message_type").Str == "group" {
|
||||
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"), autoEscape)
|
||||
}
|
||||
if p.Get("user_id").Int() != 0 {
|
||||
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"), 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"), 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()))
|
||||
},
|
||||
"set_friend_add_request": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
apr := true
|
||||
if p.Get("approve").Exists() {
|
||||
apr = p.Get("approve").Bool()
|
||||
}
|
||||
return bot.CQProcessFriendRequest(p.Get("flag").Str, apr)
|
||||
},
|
||||
"set_group_add_request": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
subType := p.Get("sub_type").Str
|
||||
apr := true
|
||||
if subType == "" {
|
||||
subType = p.Get("type").Str
|
||||
}
|
||||
if p.Get("approve").Exists() {
|
||||
apr = p.Get("approve").Bool()
|
||||
}
|
||||
return bot.CQProcessGroupRequest(p.Get("flag").Str, subType, p.Get("reason").Str, apr)
|
||||
},
|
||||
"set_group_card": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQSetGroupCard(p.Get("group_id").Int(), p.Get("user_id").Int(), p.Get("card").Str)
|
||||
},
|
||||
"set_group_special_title": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQSetGroupSpecialTitle(p.Get("group_id").Int(), p.Get("user_id").Int(), p.Get("special_title").Str)
|
||||
},
|
||||
"set_group_kick": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQSetGroupKick(p.Get("group_id").Int(), p.Get("user_id").Int(), p.Get("message").Str, p.Get("reject_add_request").Bool())
|
||||
},
|
||||
"set_group_ban": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQSetGroupBan(p.Get("group_id").Int(), p.Get("user_id").Int(), func() uint32 {
|
||||
if p.Get("duration").Exists() {
|
||||
return uint32(p.Get("duration").Int())
|
||||
}
|
||||
return 1800
|
||||
}())
|
||||
},
|
||||
"set_group_whole_ban": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQSetGroupWholeBan(p.Get("group_id").Int(), func() bool {
|
||||
if p.Get("enable").Exists() {
|
||||
return p.Get("enable").Bool()
|
||||
}
|
||||
return true
|
||||
}())
|
||||
},
|
||||
"set_group_name": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQSetGroupName(p.Get("group_id").Int(), p.Get("group_name").Str)
|
||||
},
|
||||
"set_group_admin": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQSetGroupAdmin(p.Get("group_id").Int(), p.Get("user_id").Int(), func() bool {
|
||||
if p.Get("enable").Exists() {
|
||||
return p.Get("enable").Bool()
|
||||
}
|
||||
return true
|
||||
}())
|
||||
},
|
||||
"_send_group_notice": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQSetGroupMemo(p.Get("group_id").Int(), p.Get("content").Str)
|
||||
},
|
||||
"set_group_leave": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQSetGroupLeave(p.Get("group_id").Int())
|
||||
},
|
||||
"get_image": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetImage(p.Get("file").Str)
|
||||
},
|
||||
"get_forward_msg": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
id := p.Get("message_id").Str
|
||||
if id == "" {
|
||||
id = p.Get("id").Str
|
||||
}
|
||||
return bot.CQGetForwardMessage(id)
|
||||
},
|
||||
"get_msg": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetMessage(int32(p.Get("message_id").Int()))
|
||||
},
|
||||
"download_file": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
headers := map[string]string{}
|
||||
headersToken := p.Get("headers")
|
||||
if headersToken.IsArray() {
|
||||
for _, sub := range headersToken.Array() {
|
||||
str := strings.SplitN(sub.String(), "=", 2)
|
||||
if len(str) == 2 {
|
||||
headers[str[0]] = str[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
if headersToken.Type == gjson.String {
|
||||
lines := strings.Split(headersToken.String(), "\r\n")
|
||||
for _, sub := range lines {
|
||||
str := strings.SplitN(sub, "=", 2)
|
||||
if len(str) == 2 {
|
||||
headers[str[0]] = str[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
return bot.CQDownloadFile(p.Get("url").Str, headers, int(p.Get("thread_count").Int()))
|
||||
},
|
||||
"get_group_honor_info": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetGroupHonorInfo(p.Get("group_id").Int(), p.Get("type").Str)
|
||||
},
|
||||
"set_restart": func(c *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
var delay int64
|
||||
delay = p.Get("delay").Int()
|
||||
if delay < 0 {
|
||||
delay = 0
|
||||
}
|
||||
defer func(delay int64) {
|
||||
time.Sleep(time.Duration(delay) * time.Millisecond)
|
||||
Restart <- struct{}{}
|
||||
}(delay)
|
||||
return coolq.MSG{"data": nil, "retcode": 0, "status": "async"}
|
||||
|
||||
},
|
||||
"can_send_image": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQCanSendImage()
|
||||
},
|
||||
"can_send_record": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQCanSendRecord()
|
||||
},
|
||||
"get_stranger_info": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetStrangerInfo(p.Get("user_id").Int())
|
||||
},
|
||||
"get_status": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetStatus()
|
||||
},
|
||||
"get_version_info": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetVersionInfo()
|
||||
},
|
||||
"get_group_system_msg": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetGroupSystemMessages()
|
||||
},
|
||||
"get_group_file_system_info": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetGroupFileSystemInfo(p.Get("group_id").Int())
|
||||
},
|
||||
"get_group_root_files": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetGroupRootFiles(p.Get("group_id").Int())
|
||||
},
|
||||
"get_group_files_by_folder": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetGroupFilesByFolderID(p.Get("group_id").Int(), p.Get("folder_id").Str)
|
||||
},
|
||||
"get_group_file_url": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetGroupFileURL(p.Get("group_id").Int(), p.Get("file_id").Str, int32(p.Get("busid").Int()))
|
||||
},
|
||||
"upload_group_file": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQUploadGroupFile(p.Get("group_id").Int(), p.Get("file").Str, p.Get("name").Str, p.Get("folder").Str)
|
||||
},
|
||||
"get_group_msg_history": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetGroupMessageHistory(p.Get("group_id").Int(), p.Get("message_seq").Int())
|
||||
},
|
||||
"_get_vip_info": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetVipInfo(p.Get("user_id").Int())
|
||||
},
|
||||
"reload_event_filter": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQReloadEventFilter()
|
||||
},
|
||||
".ocr_image": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQOcrImage(p.Get("image").Str)
|
||||
},
|
||||
"ocr_image": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQOcrImage(p.Get("image").Str)
|
||||
},
|
||||
"get_group_at_all_remain": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetAtAllRemain(p.Get("group_id").Int())
|
||||
},
|
||||
"get_online_clients": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetOnlineClients(p.Get("no_cache").Bool())
|
||||
},
|
||||
".get_word_slices": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetWordSlices(p.Get("content").Str)
|
||||
},
|
||||
"set_group_portrait": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQSetGroupPortrait(p.Get("group_id").Int(), p.Get("file").String(), p.Get("cache").String())
|
||||
},
|
||||
"set_essence_msg": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQSetEssenceMessage(int32(p.Get("message_id").Int()))
|
||||
},
|
||||
"delete_essence_msg": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQDeleteEssenceMessage(int32(p.Get("message_id").Int()))
|
||||
},
|
||||
"get_essence_msg_list": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetEssenceMessageList(p.Get("group_id").Int())
|
||||
},
|
||||
"check_url_safely": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQCheckURLSafely(p.Get("url").String())
|
||||
},
|
||||
"set_group_anonymous_ban": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
obj := p.Get("anonymous")
|
||||
flag := p.Get("anonymous_flag")
|
||||
if !flag.Exists() {
|
||||
flag = p.Get("flag")
|
||||
}
|
||||
if !flag.Exists() && !obj.Exists() {
|
||||
return coolq.Failed(100, "FLAG_NOT_FOUND", "flag未找到")
|
||||
}
|
||||
if !flag.Exists() {
|
||||
flag = obj.Get("flag")
|
||||
}
|
||||
return bot.CQSetGroupAnonymousBan(p.Get("group_id").Int(), flag.String(), int32(p.Get("duration").Int()))
|
||||
},
|
||||
".handle_quick_operation": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQHandleQuickOperation(p.Get("context"), p.Get("operation"))
|
||||
},
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user