ProjectWIND/core/api.go

948 lines
28 KiB
Go
Raw Normal View History

2024-12-05 01:00:07 +08:00
package core
2024-11-28 15:20:29 +08:00
import (
2024-12-07 17:16:44 +08:00
"ProjectWIND/LOG"
"ProjectWIND/wba"
"crypto/rand"
"fmt"
"strings"
2024-11-28 15:20:29 +08:00
)
2024-12-07 17:16:44 +08:00
type apiInfo struct{}
2024-12-14 17:51:09 +08:00
//一、Protocol模块
/*
2024-12-14 17:51:09 +08:00
关于Protocol模块的说明
2024-11-28 15:20:29 +08:00
1.所有API请求按照OneBot11标准使用JSON格式进行数据交换api命名为由原文档中蛇形命名法改为双驼峰命名法
2024-12-07 17:16:44 +08:00
2.无响应的API请求使用ws协议处理有响应的API需添加echo字段
3.wind会从配置文件中读取API请求的url请确保正确填写
*/
//1.无响应API,使用ws协议处理
2024-11-28 15:20:29 +08:00
// UnsafelySendMsg 发送消息(自动判断消息类型)
func (a *apiInfo) UnsafelySendMsg(messageType string, groupId int64, userId int64, message string, autoEscape bool) {
2025-02-13 13:32:24 +08:00
// 构建发送消息的JSON数据
var messageData wba.APIRequestInfo
messageData.Action = "send_msg"
switch messageType {
case "private":
{
messageData.Params.UserId = userId
break
}
case "group":
{
messageData.Params.GroupId = groupId
break
}
default:
{
LOG.Error("发送消息(UnsafelySendMsg)时,消息类型错误: %v", messageType)
2025-02-13 13:32:24 +08:00
}
}
messageData.Params.Message = message
messageData.Params.AutoEscape = autoEscape
// 发送消息
_, err := wsAPI(messageData)
if err != nil {
LOG.Error("发送消息时,发送失败: %v", err)
2025-02-13 13:32:24 +08:00
return
}
LOG.Info("发送消息(UnsafelySendMsg)(至:%v-%v:%v):%v", messageType, groupId, userId, message)
2025-02-13 13:32:24 +08:00
return
}
// UnsafelySendPrivateMsg 发送私聊消息
func (a *apiInfo) UnsafelySendPrivateMsg(userId int64, message string, autoEscape bool) {
2025-02-13 13:32:24 +08:00
// 构建发送消息的JSON数据
var messageData wba.APIRequestInfo
messageData.Action = "send_private_msg"
messageData.Params.UserId = userId
messageData.Params.Message = message
messageData.Params.AutoEscape = autoEscape
// 发送消息
_, err := wsAPI(messageData)
if err != nil {
LOG.Error("发送私聊消息(UnsafelySendPrivateMsg)时,发送失败: %v", err)
2025-02-13 13:32:24 +08:00
return
}
LOG.Info("发送私聊消息(UnsafelySendPrivateMsg)(至:%v):%v", userId, message)
2025-02-13 13:32:24 +08:00
return
}
// UnsafelySendGroupMsg 发送群消息
func (a *apiInfo) UnsafelySendGroupMsg(groupId int64, message string, autoEscape bool) {
2025-02-13 13:32:24 +08:00
// 构建发送消息的JSON数据
var messageData wba.APIRequestInfo
messageData.Action = "send_group_msg"
messageData.Params.GroupId = groupId
messageData.Params.Message = message
messageData.Params.AutoEscape = autoEscape
// 发送消息
_, err := wsAPI(messageData)
if err != nil {
LOG.Error("发送群消息(UnsafelySendGroupMsg)时,发送失败: %v", err)
2025-02-13 13:32:24 +08:00
return
}
LOG.Info("发送群消息(UnsafelySendGroupMsg)(至:%v):%v", groupId, message)
2025-02-13 13:32:24 +08:00
return
}
// SendMsg 回复消息(自动判断消息类型)
func (a *apiInfo) SendMsg(msg wba.MessageEventInfo, message string, autoEscape bool) {
2024-11-28 15:20:29 +08:00
// 构建发送消息的JSON数据
2024-12-07 17:16:44 +08:00
var messageData wba.APIRequestInfo
messageType := msg.MessageType
2024-11-28 15:20:29 +08:00
messageData.Action = "send_msg"
switch messageType {
case "private":
{
messageData.Params.UserId = msg.UserId
break
}
2024-11-28 15:20:29 +08:00
case "group":
{
messageData.Params.GroupId = msg.GroupId
break
}
2024-11-28 15:20:29 +08:00
default:
{
LOG.Error("回复消息(SendMsg)时,消息类型错误: %v", messageType)
}
}
messageData.Params.Message = message
messageData.Params.AutoEscape = autoEscape
// 发送消息
2024-12-13 22:04:56 +08:00
_, err := wsAPI(messageData)
2024-12-07 17:16:44 +08:00
if err != nil {
LOG.Error("回复消息时,发送失败: %v", err)
2024-12-07 17:16:44 +08:00
return
}
LOG.Info("回复消息(SendMsg)(至:%v-%v:%v-%v):%v", msg.MessageType, msg.GroupId, msg.UserId, msg.Sender.Nickname, message)
2024-12-07 17:16:44 +08:00
return
}
// SendPrivateMsg 回复私聊消息
func (a *apiInfo) SendPrivateMsg(msg wba.MessageEventInfo, message string, autoEscape bool) {
// 构建发送消息的JSON数据
2024-12-07 17:16:44 +08:00
var messageData wba.APIRequestInfo
messageData.Action = "send_private_msg"
messageData.Params.UserId = msg.UserId
messageData.Params.Message = message
messageData.Params.AutoEscape = autoEscape
// 发送消息
2024-12-13 22:04:56 +08:00
_, err := wsAPI(messageData)
2024-12-07 17:16:44 +08:00
if err != nil {
LOG.Error("回复消息(SendPrivateMsg)时,发送失败: %v", err)
2024-12-07 17:16:44 +08:00
return
}
LOG.Info("回复消息(SendPrivateMsg)(至:%v-%v:%v-%v):%v", msg.MessageType, msg.GroupId, msg.UserId, msg.Sender.Nickname, message)
2024-12-07 17:16:44 +08:00
return
}
// SendGroupMsg 回复群消息
func (a *apiInfo) SendGroupMsg(msg wba.MessageEventInfo, message string, autoEscape bool) {
// 构建发送消息的JSON数据
2024-12-07 17:16:44 +08:00
var messageData wba.APIRequestInfo
messageData.Action = "send_group_msg"
messageData.Params.GroupId = msg.GroupId
2024-11-28 15:20:29 +08:00
messageData.Params.Message = message
messageData.Params.AutoEscape = autoEscape
// 发送消息
2024-12-13 22:04:56 +08:00
_, err := wsAPI(messageData)
2024-12-07 17:16:44 +08:00
if err != nil {
LOG.Error("回复消息(SendGroupMsg)时,发送失败: %v", err)
2024-12-07 17:16:44 +08:00
return
}
LOG.Info("回复消息(SendGroupMsg)(至:%v-%v:%v-%v):%v", msg.MessageType, msg.GroupId, msg.UserId, msg.Sender.Nickname, message)
return
}
// UnsafelyDeleteMsg 撤回消息
func (a *apiInfo) UnsafelyDeleteMsg(messageId int32) {
// 构建删除消息的JSON数据
var messageData wba.APIRequestInfo
messageData.Action = "delete_msg"
messageData.Params.MessageId = messageId
_, err := wsAPI(messageData)
if err != nil {
LOG.Error("撤回消息(UnsafeDeleteMsg)时,发送失败: %v", err)
return
}
LOG.Info("撤回消息(UnsafeDeleteMsg):[id:%v]", messageId)
2024-12-07 17:16:44 +08:00
return
}
2024-12-07 17:16:44 +08:00
// DeleteMsg 撤回消息
func (a *apiInfo) DeleteMsg(msg wba.MessageEventInfo) {
// 构建删除消息的JSON数据
2024-12-07 17:16:44 +08:00
var messageData wba.APIRequestInfo
messageData.Action = "delete_msg"
messageData.Params.MessageId = msg.MessageId
2024-12-13 22:04:56 +08:00
_, err := wsAPI(messageData)
2024-12-07 17:16:44 +08:00
if err != nil {
LOG.Error("撤回消息(DeleteMsg)时,发送失败: %v", err)
2024-12-07 17:16:44 +08:00
return
}
LOG.Info("撤回消息(DeleteMsg):[id:%v]%v", msg.MessageId, msg.RawMessage)
2024-12-07 17:16:44 +08:00
return
}
2024-12-07 17:16:44 +08:00
// SendLike 发送赞
func (a *apiInfo) SendLike(userId int64, times int) {
// 构建发送赞的JSON数据
2024-12-07 17:16:44 +08:00
var messageData wba.APIRequestInfo
messageData.Action = "send_like"
messageData.Params.UserId = userId
messageData.Params.Times = times
2024-12-13 22:04:56 +08:00
_, err := wsAPI(messageData)
2024-12-07 17:16:44 +08:00
if err != nil {
LOG.Error("发送赞(SendLike)时,发送失败: %v", err)
2024-12-07 17:16:44 +08:00
return
}
LOG.Info("发送赞(SendLike)(至:%v):%v", userId, times)
2024-12-07 17:16:44 +08:00
return
}
2024-12-07 17:16:44 +08:00
// SetGroupKick 将指定用户移出群聊(需要群主或管理员权限)
func (a *apiInfo) SetGroupKick(groupId int64, userId int64, rejectAddRequest bool) {
var messageData wba.APIRequestInfo
messageData.Action = "set_group_kick"
messageData.Params.GroupId = groupId
messageData.Params.UserId = userId
messageData.Params.RejectAddRequest = rejectAddRequest
2024-12-13 22:04:56 +08:00
_, err := wsAPI(messageData)
2024-12-07 17:16:44 +08:00
if err != nil {
LOG.Error("移出群聊(SetGroupKick)时,发送失败: %v", err)
2024-12-07 17:16:44 +08:00
return
}
LOG.Info("移出群聊(SetGroupKick)(从:%v-%v):%v", groupId, userId, rejectAddRequest)
2024-12-07 17:16:44 +08:00
return
}
2024-12-07 17:16:44 +08:00
// SetGroupBan 将指定用户禁言(需要群主或管理员权限)
func (a *apiInfo) SetGroupBan(groupId int64, userId int64, duration int32) {
var messageData wba.APIRequestInfo
messageData.Action = "set_group_ban"
messageData.Params.GroupId = groupId
messageData.Params.UserId = userId
messageData.Params.Duration = duration
2024-12-13 22:04:56 +08:00
_, err := wsAPI(messageData)
if err != nil {
LOG.Error("禁言群成员(SetGroupBan)时,执行失败: %v", err)
2024-12-07 17:16:44 +08:00
return
}
LOG.Info("禁言群成员(SetGroupBan)(在:%v-%v):%v", groupId, userId, duration)
2024-12-07 17:16:44 +08:00
return
}
2024-12-07 17:16:44 +08:00
// SetGroupWholeBan 设置全员禁言(需要群主或管理员权限)
func (a *apiInfo) SetGroupWholeBan(groupId int64, enable bool) {
var messageData wba.APIRequestInfo
messageData.Action = "set_group_whole_ban"
messageData.Params.GroupId = groupId
messageData.Params.Enable = enable
2024-12-13 22:04:56 +08:00
_, err := wsAPI(messageData)
2024-12-07 17:16:44 +08:00
if err != nil {
LOG.Error("设置全员禁言(SetGroupWholeBan)时,执行失败: %v", err)
2024-12-07 17:16:44 +08:00
return
}
LOG.Info("设置全员禁言(SetGroupWholeBan)(在:%v):%v", groupId, enable)
2024-12-07 17:16:44 +08:00
return
}
2024-12-07 17:16:44 +08:00
// SetGroupAdmin 设置群管理员(需要群主权限)
func (a *apiInfo) SetGroupAdmin(groupId int64, userId int64, enable bool) {
var messageData wba.APIRequestInfo
messageData.Action = "set_group_admin"
messageData.Params.GroupId = groupId
messageData.Params.UserId = userId
messageData.Params.Enable = enable
2024-12-13 22:04:56 +08:00
_, err := wsAPI(messageData)
if err != nil {
LOG.Error("设置群管理员(SetGroupAdmin)时,执行失败: %v", err)
2024-12-07 17:16:44 +08:00
return
}
LOG.Info("设置群管理员(SetGroupAdmin)(在:%v-%v):%v", groupId, userId, enable)
2024-12-07 17:16:44 +08:00
return
}
2024-12-07 17:16:44 +08:00
// SetGroupCard 设置群名片(需要群主或管理员权限)
func (a *apiInfo) SetGroupCard(groupId int64, userId int64, card string) {
var messageData wba.APIRequestInfo
messageData.Action = "set_group_card"
messageData.Params.GroupId = groupId
messageData.Params.UserId = userId
messageData.Params.Card = card
2024-12-13 22:04:56 +08:00
_, err := wsAPI(messageData)
2024-12-07 17:16:44 +08:00
if err != nil {
LOG.Error("设置群名片(SetGroupCard)时,执行失败: %v", err)
2024-12-07 17:16:44 +08:00
return
}
LOG.Info("设置群名片(SetGroupCard)(在:%v-%v):%v", groupId, userId, card)
2024-12-07 17:16:44 +08:00
return
}
2024-12-07 17:16:44 +08:00
// SetGroupName 设置群名称(可能需要群主或管理员权限)
2024-12-14 17:51:09 +08:00
func (a *apiInfo) SetGroupName(groupId int64, groupName string) {
2024-12-07 17:16:44 +08:00
var messageData wba.APIRequestInfo
messageData.Action = "set_group_name"
messageData.Params.GroupId = groupId
messageData.Params.GroupName = groupName
2024-12-13 22:04:56 +08:00
_, err := wsAPI(messageData)
2024-12-07 17:16:44 +08:00
if err != nil {
LOG.Error("设置群名称(SetGroupName)时,执行失败: %v", err)
2024-12-07 17:16:44 +08:00
return
}
LOG.Info("设置群名称(SetGroupName)(在:%v):%v", groupId, groupName)
2024-12-07 17:16:44 +08:00
return
}
2024-12-07 17:16:44 +08:00
// SetGroupLeave 退出群聊
2024-12-14 17:51:09 +08:00
func (a *apiInfo) SetGroupLeave(groupId int64, isDismiss bool) {
2024-12-07 17:16:44 +08:00
var messageData wba.APIRequestInfo
messageData.Action = "set_group_leave"
messageData.Params.GroupId = groupId
messageData.Params.IsDismiss = isDismiss
2024-12-13 22:04:56 +08:00
_, err := wsAPI(messageData)
2024-12-07 17:16:44 +08:00
if err != nil {
LOG.Error("退出群聊(SetGroupLeave)时,执行失败: %v", err)
2024-12-07 17:16:44 +08:00
return
}
LOG.Info("退出群聊(SetGroupLeave)(在:%v):%v", groupId, isDismiss)
2024-12-07 17:16:44 +08:00
return
}
2024-12-07 17:16:44 +08:00
// SetGroupSpecialTitle 设置群专属头衔(需要群主权限)
func (a *apiInfo) SetGroupSpecialTitle(groupId int64, userId int64, specialTitle string) {
2024-12-07 17:16:44 +08:00
var messageData wba.APIRequestInfo
messageData.Action = "set_group_special_title"
messageData.Params.GroupId = groupId
messageData.Params.UserId = userId
messageData.Params.SpecialTitle = specialTitle
messageData.Params.Duration = -1
2024-12-13 22:04:56 +08:00
_, err := wsAPI(messageData)
2024-12-07 17:16:44 +08:00
if err != nil {
LOG.Error("设置群特殊头衔(SetGroupSpecialTitle)时,执行失败: %v", err)
2024-12-07 17:16:44 +08:00
return
}
LOG.Info("设置群特殊头衔(SetGroupSpecialTitle)(在:%v-%v):%v-%v", groupId, userId, specialTitle)
2024-12-07 17:16:44 +08:00
return
}
2024-12-07 17:16:44 +08:00
// SetFriendAddRequest 处理加好友请求
2024-12-14 17:51:09 +08:00
func (a *apiInfo) SetFriendAddRequest(flag string, approve bool, remark string) {
2024-12-07 17:16:44 +08:00
var messageData wba.APIRequestInfo
messageData.Action = "set_friend_add_request"
messageData.Params.Flag = flag
messageData.Params.Approve = approve
messageData.Params.Remark = remark
2024-12-13 22:04:56 +08:00
_, err := wsAPI(messageData)
2024-12-07 17:16:44 +08:00
if err != nil {
LOG.Error("处理加好友请求(SetFriendAddRequest)时,执行失败: %v", err)
2024-12-07 17:16:44 +08:00
return
}
LOG.Info("处理加好友请求(SetFriendAddRequest)(在:%v):%v-%v-%v", flag, approve, remark)
2024-12-07 17:16:44 +08:00
return
}
2024-12-07 17:16:44 +08:00
// SetGroupAddRequest 处理加群请求/邀请
2024-12-14 17:51:09 +08:00
func (a *apiInfo) SetGroupAddRequest(flag string, subType string, approve bool, reason string) {
2024-12-07 17:16:44 +08:00
var messageData wba.APIRequestInfo
messageData.Action = "set_group_add_request"
messageData.Params.Flag = flag
messageData.Params.SubType = subType
messageData.Params.Approve = approve
messageData.Params.Reason = reason
2024-12-13 22:04:56 +08:00
_, err := wsAPI(messageData)
if err != nil {
LOG.Error("处理加群请求/邀请(SetGroupAddRequest)时,执行失败: %v", err)
2024-12-07 17:16:44 +08:00
return
}
LOG.Info("处理加群请求/邀请(SetGroupAddRequest)(在:%v-%v-%v):%v", flag, subType, approve, reason)
2024-12-07 17:16:44 +08:00
return
}
2024-12-14 17:51:09 +08:00
// SetRestart 重启
func (a *apiInfo) SetRestart(delay int32) {
var messageData wba.APIRequestInfo
messageData.Action = "set_restart"
messageData.Params.Delay = delay
_, err := wsAPI(messageData)
if err != nil {
LOG.Error("设置重启(SetRestart)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return
}
LOG.Info("设置重启(SetRestart):%v", delay)
2024-12-14 17:51:09 +08:00
return
}
// CleanCache 清理缓存
func (a *apiInfo) CleanCache() {
var messageData wba.APIRequestInfo
messageData.Action = "clean_cache"
_, err := wsAPI(messageData)
if err != nil {
LOG.Error("清理缓存(CleanCache)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return
}
LOG.Info("清理缓存(CleanCache)")
2024-12-14 17:51:09 +08:00
return
}
2024-12-12 15:11:31 +08:00
// 2.有响应API需添加echo字段
2024-12-14 17:51:09 +08:00
// GetLoginInfo 获取登录信息
func (a *apiInfo) GetLoginInfo() (Response wba.APIResponseInfo) {
LOG.Info("获取登录信息(GetLoginInfo)")
2024-12-13 22:04:56 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_login_info"
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("获取登录信息(GetLoginInfo)时生成UUID失败: %v", err)
2024-12-13 22:04:56 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("获取登录信息(GetLoginInfo)时,执行失败: %v", err)
2024-12-13 22:04:56 +08:00
return wba.APIResponseInfo{}
}
return Response
2024-12-12 15:11:31 +08:00
}
2024-12-07 17:16:44 +08:00
2024-12-14 17:51:09 +08:00
// GetVersionInfo 获取协议信息
func (a *apiInfo) GetVersionInfo() (Response wba.APIResponseInfo) {
LOG.Info("获取协议信息(GetVersionInfo)")
2024-12-14 17:51:09 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_version_info"
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("获取协议信息(GetVersionInfo)时生成UUID失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("获取登录信息(GetVersionInfo)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
return Response
}
// GetMsg 获取消息
func (a *apiInfo) GetMsg(messageId int32) (Response wba.APIResponseInfo) {
LOG.Info("获取消息(GetMsg)")
2024-12-14 17:51:09 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_msg"
messageData.Params.MessageId = messageId
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("获取消息(GetMsg)时生成UUID失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("获取消息(GetMsg)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
return Response
}
// GetForwardMsg 获取合并转发消息
func (a *apiInfo) GetForwardMsg(id string) (Response wba.APIResponseInfo) {
LOG.Info("获取合并转发消息(GetForwardMsg)")
2024-12-14 17:51:09 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_forward_msg"
messageData.Params.Id = id
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("获取合并转发消息(GetForwardMsg)时生成UUID失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("获取合并转发消息(GetForwardMsg)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
return Response
}
// GetStrangerInfo 获取陌生人信息
func (a *apiInfo) GetStrangerInfo(userId int64, noCache bool) (Response wba.APIResponseInfo) {
LOG.Info("获取陌生人信息(GetStrangerInfo)")
2024-12-14 17:51:09 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_stranger_info"
messageData.Params.UserId = userId
messageData.Params.NoCache = noCache
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("获取陌生人信息(GetStrangerInfo)时生成UUID失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("获取陌生人信息(GetStrangerInfo)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
return Response
}
// GetFriendList 获取好友列表
func (a *apiInfo) GetFriendList() (Response wba.APIResponseInfo) {
LOG.Info("获取好友列表(GetFriendList)")
2024-12-14 17:51:09 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_friend_list"
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("获取好友列表(GetFriendList)时生成UUID失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("获取好友列表(GetFriendList)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
return Response
}
// GetGroupList 获取群列表
func (a *apiInfo) GetGroupList() (Response wba.APIResponseInfo) {
LOG.Info("获取群列表(GetGroupList)")
2024-12-14 17:51:09 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_group_list"
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("获取群列表(GetGroupList)时生成UUID失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("获取群列表(GetGroupList)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
return Response
}
// GetGroupInfo 获取群信息
func (a *apiInfo) GetGroupInfo(groupId int64, noCache bool) (Response wba.APIResponseInfo) {
LOG.Info("获取群信息(GetGroupInfo)")
2024-12-14 17:51:09 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_group_info"
messageData.Params.GroupId = groupId
messageData.Params.NoCache = noCache
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("获取群信息(GetGroupInfo)时生成UUID失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("获取群信息(GetGroupInfo)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
return Response
}
// GetGroupMemberInfo 获取群成员信息
func (a *apiInfo) GetGroupMemberInfo(groupId int64, userId int64, noCache bool) (Response wba.APIResponseInfo) {
LOG.Info("获取群成员信息(GetGroupMemberInfo)")
2024-12-14 17:51:09 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_group_member_info"
messageData.Params.GroupId = groupId
messageData.Params.UserId = userId
messageData.Params.NoCache = noCache
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("获取群成员信息(GetGroupMemberInfo)时生成UUID失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("获取群成员信息(GetGroupMemberInfo)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
return Response
}
// GetGroupMemberList 获取群成员列表
func (a *apiInfo) GetGroupMemberList(groupId int64) (Response wba.APIResponseInfo) {
LOG.Info("获取群成员列表(GetGroupMemberList)")
2024-12-14 17:51:09 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_group_member_list"
messageData.Params.GroupId = groupId
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("获取群成员列表(GetGroupMemberList)时生成UUID失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("获取群成员列表(GetGroupMemberList)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
return Response
}
// GetGroupHonorInfo 获取群荣誉信息
2024-12-27 15:56:59 +08:00
func (a *apiInfo) GetGroupHonorInfo(groupId int64, Type string) (Response wba.APIResponseInfo) {
LOG.Info("获取群荣誉信息(GetGroupHonorInfo)")
2024-12-14 17:51:09 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_group_honor_info"
messageData.Params.GroupId = groupId
2024-12-27 15:56:59 +08:00
messageData.Params.Type = Type
2024-12-14 17:51:09 +08:00
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("获取群荣誉信息(GetGroupHonorInfo)时生成UUID失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("获取群荣誉信息(GetGroupHonorInfo)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
return Response
}
// GetCookies 获取Cookies
func (a *apiInfo) GetCookies(domain string) (Response wba.APIResponseInfo) {
LOG.Info("获取Cookies(GetCookies)")
2024-12-14 17:51:09 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_cookies"
messageData.Params.Domain = domain
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("获取Cookies(GetCookies)时生成UUID失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("获取Cookies(GetCookies)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
return Response
}
// GetCSRFToken 获取CSRF Token
func (a *apiInfo) GetCSRFToken() (Response wba.APIResponseInfo) {
LOG.Info("获取CSRF Token(GetCSRFToken)")
2024-12-14 17:51:09 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_csrf_token"
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("获取CSRF Token(GetCSRFToken)时生成UUID失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("获取CSRF Token(GetCSRFToken)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
return Response
}
// GetCredentials 获取登录令牌
func (a *apiInfo) GetCredentials(domain string) (Response wba.APIResponseInfo) {
LOG.Info("获取登录令牌(GetCredentials)")
2024-12-14 17:51:09 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_credentials"
messageData.Params.Domain = domain
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("获取登录令牌(GetCredentials)时生成UUID失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("获取登录令牌(GetCredentials)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
return Response
}
// GetRecord 获取语音
func (a *apiInfo) GetRecord(file string, outFormat string) (Response wba.APIResponseInfo) {
LOG.Info("获取语音(GetRecord)")
2024-12-14 17:51:09 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_record"
messageData.Params.File = file
messageData.Params.OutFormat = outFormat
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("获取语音(GetRecord)时生成UUID失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("获取语音(GetRecord)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
return Response
}
// GetImage 获取图片
func (a *apiInfo) GetImage(file string) (Response wba.APIResponseInfo) {
LOG.Info("获取图片(GetImage)")
2024-12-14 17:51:09 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_image"
messageData.Params.File = file
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("获取图片(GetImage)时生成UUID失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("获取图片(GetImage)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
return Response
}
// CanSendImage 检查是否可以发送图片
func (a *apiInfo) CanSendImage() (Response wba.APIResponseInfo) {
LOG.Info("检查是否可以发送图片(CanSendImage)")
2024-12-14 17:51:09 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "can_send_image"
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("检查是否可以发送图片(CanSendImage)时生成UUID失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("检查是否可以发送图片(CanSendImage)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
return Response
}
// CanSendRecord 检查是否可以发送语音
func (a *apiInfo) CanSendRecord() (Response wba.APIResponseInfo) {
LOG.Info("检查是否可以发送语音(CanSendRecord)")
2024-12-14 17:51:09 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "can_send_record"
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("检查是否可以发送语音(CanSendRecord)时生成UUID失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("检查是否可以发送语音(CanSendRecord)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
return Response
}
// GetStatus 获取状态
func (a *apiInfo) GetStatus() (Response wba.APIResponseInfo) {
LOG.Info("获取状态(GetStatus)")
2024-12-14 17:51:09 +08:00
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_status"
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.Error("获取状态(GetStatus)时生成UUID失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.Error("获取状态(GetStatus)时,执行失败: %v", err)
2024-12-14 17:51:09 +08:00
return wba.APIResponseInfo{}
}
return Response
}
//二、LOG模块
/*
关于LOG模块的说明
1.日志模块使用go-logging库日志级别分为DEBUGInfoWarnError
2024-12-14 17:51:09 +08:00
2.日志模块提供LogWith方法可以自定义日志级别调用级别为DEBUG时会打印输出调用者的文件名函数名行号
3.日志模块提供Log方法默认日志级别为INFO
*/
func (a *apiInfo) LogWith(level string, content string, args ...interface{}) {
level = strings.ToLower(level)
2024-12-14 17:51:09 +08:00
switch level {
case "trace":
LOG.Trace(content, args...)
return
case "debug":
LOG.Debug(content, args...)
return
case "notice":
LOG.Notice(content, args...)
2024-12-14 17:51:09 +08:00
return
case "warn":
LOG.Warn(content, args...)
2024-12-14 17:51:09 +08:00
return
case "error":
LOG.Error(content, args...)
2024-12-14 17:51:09 +08:00
return
default:
LOG.Info(content, args...)
2024-12-14 17:51:09 +08:00
return
}
}
func (a *apiInfo) Log(content string, args ...interface{}) {
LOG.Info(content, args...)
2024-12-14 17:51:09 +08:00
}
//database模块
2025-03-03 14:13:48 +08:00
// //数据库部分允许字符串变量的读写操作,允许获取配置项操作
// func (ai *AppInfo) VarSet(dataMap string, unit string, id string, key string, value string) {
// if ai.dbHandler != nil {
// ai.dbHandler.Set(ai.Name, dataMap, unit, id, key, value)
// }
// }
// // VarGet 获取变量
// func (ai *AppInfo) VarGet(dataMap string, unit string, id string, key string) (string, bool) {
// if ai.dbHandler != nil {
// res, ok := ai.dbHandler.Get(ai.Name, dataMap, unit, id, key, false)
// if !ok {
// return "", false
// }
// resStr, ok := res.(string)
// if !ok {
// return "", false
// }
// return resStr, true
// }
// return "", false
// }
// // GetIntConfig 获取整数配置
// func (ai *AppInfo) GetIntConfig(dataMap string, key string) (int64, bool) {
// if ai.dbHandler != nil {
// res, ok := ai.dbHandler.Get(ai.Name, dataMap, "config", "number", key, true)
// if !ok {
// return 0, false
// }
// resInt, ok := res.(int64)
// if !ok {
// return 0, false
// }
// return resInt, true
// }
// return 0, false
// }
// // GetStringConfig 获取字符串配置
// func (ai *AppInfo) GetStringConfig(dataMap string, key string) (string, bool) {
// if ai.dbHandler != nil {
// res, ok := ai.dbHandler.Get(ai.Name, dataMap, "config", "string", key, true)
// if !ok {
// return "", false
// }
// resStr, ok := res.(string)
// if !ok {
// return "", false
// }
// return resStr, true
// }
// return "", false
// }
// // GetFloatConfig 获取浮点数配置
// func (ai *AppInfo) GetFloatConfig(dataMap string, key string) (float64, bool) {
// if ai.dbHandler != nil {
// res, ok := ai.dbHandler.Get(ai.Name, dataMap, "config", "float", key, true)
// if !ok {
// return 0, false
// }
// resFloat, ok := res.(float64)
// if !ok {
// return 0, false
// }
// return resFloat, true
// }
// return 0, false
// }
// // GetIntSliceConfig 获取整数切片配置
// func (ai *AppInfo) GetIntSliceConfig(dataMap string, key string) ([]int64, bool) {
// if ai.dbHandler != nil {
// res, ok := ai.dbHandler.Get(ai.Name, dataMap, "config", "number_slice", key, true)
// if !ok {
// return nil, false
// }
// resSlice, ok := res.([]int64)
// if !ok {
// return nil, false
// }
// return resSlice, true
// }
// return nil, false
// }
// // GetStringSliceConfig 获取字符串切片配置
// func (ai *AppInfo) GetStringSliceConfig(dataMap string, key string) ([]string, bool) {
// if ai.dbHandler != nil {
// res, ok := ai.dbHandler.Get(ai.Name, dataMap, "config", "string_slice", key, true)
// if !ok {
// return nil, false
// }
// resSlice, ok := res.([]string)
// if !ok {
// return nil, false
// }
// return resSlice, true
// }
// return nil, false
// }
2024-12-27 15:56:59 +08:00
// 文件管理模块
//TODO: 文件管理模块待实现
//终端连接模块
//TODO: 终端模块待实现
//核心信息调用模块
2024-12-07 17:16:44 +08:00
var AppApi apiInfo
func GenerateUUID() (string, error) {
uuid := make([]byte, 16)
_, err := rand.Read(uuid)
2024-11-28 15:20:29 +08:00
if err != nil {
2024-12-07 17:16:44 +08:00
return "", err
2024-11-28 15:20:29 +08:00
}
2024-12-07 17:16:44 +08:00
// 设置UUID版本号版本4将第6字节的高4位设置为0100
uuid[6] = (uuid[6] & 0x0F) | 0x40
// 设置UUID变体RFC 4122规范定义的变体将第8字节的高4位设置为10
uuid[8] = (uuid[8] & 0x3F) | 0x80
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
2024-11-28 15:20:29 +08:00
}