🎨 重构API接口,添加RobotInfo结构以支持多个实例

This commit is contained in:
李寻欢 2025-04-23 15:06:04 +08:00
parent d7d235279a
commit 1c77ce02eb
16 changed files with 198 additions and 107 deletions

View File

@ -3,6 +3,9 @@ package xybot
import (
"encoding/json"
"errors"
"slices"
"strings"
"gitee.ltd/lxh/xybot/core"
"gitee.ltd/lxh/xybot/friend"
"gitee.ltd/lxh/xybot/group"
@ -12,8 +15,6 @@ import (
"gitee.ltd/lxh/xybot/tool"
"gitee.ltd/lxh/xybot/user"
"github.com/go-resty/resty/v2"
"slices"
"strings"
)
type Client struct {
@ -47,7 +48,7 @@ func NewClient(wxId, baseUrl string, debug bool) (cli *Client, err error) {
baseUrl = "http://" + baseUrl
}
// 设置一下机器人微信Id的值
core.WxId = wxId
wxInfo := core.NewInfo(wxId)
cli = &Client{}
cli.httpClient = resty.New().
@ -58,13 +59,13 @@ func NewClient(wxId, baseUrl string, debug bool) (cli *Client, err error) {
SetJSONUnmarshaler(json.Unmarshal).
OnBeforeRequest(requestPreCheck)
cli.Login = login.New(cli.httpClient)
cli.Friend = friend.New(cli.httpClient)
cli.Group = group.New(cli.httpClient)
cli.Message = message.New(cli.httpClient)
cli.User = user.New(cli.httpClient)
cli.HongBao = hongbao.New(cli.httpClient)
cli.Tool = tool.New(cli.httpClient)
cli.Login = login.New(cli.httpClient, wxInfo)
cli.Friend = friend.New(cli.httpClient, wxInfo)
cli.Group = group.New(cli.httpClient, wxInfo)
cli.Message = message.New(cli.httpClient, wxInfo)
cli.User = user.New(cli.httpClient, wxInfo)
cli.HongBao = hongbao.New(cli.httpClient, wxInfo)
cli.Tool = tool.New(cli.httpClient, wxInfo)
return
}
@ -81,7 +82,8 @@ func requestPreCheck(_ *resty.Client, req *resty.Request) (err error) {
// 如果是白名单请求,直接返回
return nil
}
if core.WxId == "" {
if req.Header.Get("WeChatId") == "" {
// 如果WxId为空直接返回
return errors.New("wxId不能为空")
}

View File

@ -1,4 +1,31 @@
package core
// WxId 微信Id将所有接口调用的wxId值都放到这儿来方便判断是否可用
var WxId = ""
// RobotInfo
// @description: 机器人信息
type RobotInfo struct {
wxId string // 机器人微信Id
}
// SetId
// @description: 设置机器人Id
// @receiver i
// @param wxId
func (i *RobotInfo) SetId(wxId string) {
i.wxId = wxId
}
// GetId
// @description: 获取机器人Id
// @receiver i
// @return string
func (i *RobotInfo) GetId() string {
return i.wxId
}
// NewInfo
// @description: 创建机器人信息
// @param wxId
// @return *RobotInfo
func NewInfo(wxId string) *RobotInfo {
return &RobotInfo{wxId: wxId}
}

View File

@ -1,6 +1,9 @@
package friend
import "github.com/go-resty/resty/v2"
import (
"gitee.ltd/lxh/xybot/core"
"github.com/go-resty/resty/v2"
)
type API interface {
AcceptFriend(scene int, v1, v2 string) (err error) // 接受好友请求
@ -10,9 +13,10 @@ type API interface {
}
type service struct {
client *resty.Client
client *resty.Client
robotInfo *core.RobotInfo
}
func New(client *resty.Client) API {
return &service{client: client}
func New(client *resty.Client, robotInfo *core.RobotInfo) API {
return &service{client: client, robotInfo: robotInfo}
}

View File

@ -3,7 +3,6 @@ package friend
import (
"errors"
"gitee.ltd/lxh/xybot/base"
"gitee.ltd/lxh/xybot/core"
"slices"
"strings"
)
@ -18,9 +17,10 @@ import (
func (s service) AcceptFriend(scene int, v1, v2 string) (err error) {
var result base.Response[base.EmptyResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"Scene": scene,
"V1": v1,
"V2": v2,
@ -38,9 +38,10 @@ func (s service) AcceptFriend(scene int, v1, v2 string) (err error) {
func (s service) GetContact(wxIds []string) (contactList []ContactListItem, err error) {
var result base.Response[GetContactResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"RequestWxids": strings.Join(wxIds, ","),
}).Post("/GetContact")
if err = result.CheckError(err); err != nil {
@ -64,9 +65,10 @@ func (s service) GetContractDetail(wxIds []string) (contactList []ContactListIte
var result base.Response[GetContactResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"RequestWxids": strings.Join(wxIds, ","),
"Chatroom": "",
}).Post("/GetContractDetail")
@ -86,9 +88,10 @@ func (s service) GetContractDetail(wxIds []string) (contactList []ContactListIte
func (s service) GetContractList(clearSystemAccount bool) (wxIds []string, err error) {
var result base.Response[GetContractListResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"CurrentChatroomContactSeq": 0,
"CurrentWxcontactSeq": 0,
}).Post("/GetContractList")

View File

@ -1,6 +1,9 @@
package group
import "github.com/go-resty/resty/v2"
import (
"gitee.ltd/lxh/xybot/core"
"github.com/go-resty/resty/v2"
)
type API interface {
AddChatroomMember(chatroom string, inviteWxId string) (err error) // 添加群聊成员(群聊最多40人)
@ -12,9 +15,10 @@ type API interface {
}
type service struct {
client *resty.Client
client *resty.Client
robotInfo *core.RobotInfo
}
func New(client *resty.Client) API {
return &service{client: client}
func New(client *resty.Client, robotInfo *core.RobotInfo) API {
return &service{client: client, robotInfo: robotInfo}
}

View File

@ -2,7 +2,6 @@ package group
import (
"gitee.ltd/lxh/xybot/base"
"gitee.ltd/lxh/xybot/core"
"strings"
)
@ -15,9 +14,10 @@ import (
func (s service) AddChatroomMember(chatroom string, inviteWxId string) (err error) {
var result base.Response[base.EmptyResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"Chatroom": chatroom,
"InviteWxids": inviteWxId,
}).Post("/AddChatroomMember")
@ -34,9 +34,10 @@ func (s service) AddChatroomMember(chatroom string, inviteWxId string) (err erro
func (s service) InviteChatroomMember(chatroom string, inviteWxIds []string) (err error) {
var result base.Response[base.EmptyResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"Chatroom": chatroom,
"InviteWxids": strings.Join(inviteWxIds, ","),
}).Post("/InviteChatroomMember")
@ -53,9 +54,10 @@ func (s service) InviteChatroomMember(chatroom string, inviteWxIds []string) (er
func (s service) GetChatroomInfo(chatroom string) (info GetChatroomInfoResponse, err error) {
var result base.Response[GetChatroomInfoResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"Chatroom": chatroom,
}).Post("/GetChatroomInfo")
if err = result.CheckError(err); err != nil {
@ -74,9 +76,10 @@ func (s service) GetChatroomInfo(chatroom string) (info GetChatroomInfoResponse,
func (s service) GetChatroomInfoNoAnnounce(chatroom []string) (info []ChatroomInfoItem, err error) {
var result base.Response[GetChatroomInfoNoAnnounce]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"Chatroom": strings.Join(chatroom, ","),
}).Post("/GetChatroomInfoNoAnnounce")
if err = result.CheckError(err); err != nil {
@ -95,9 +98,10 @@ func (s service) GetChatroomInfoNoAnnounce(chatroom []string) (info []ChatroomIn
func (s service) GetChatroomMemberDetail(chatroom string) (members []ChatRoomMemberItem, err error) {
var result base.Response[GetChatroomMemberDetailResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"Chatroom": chatroom,
}).Post("/GetChatroomMemberDetail")
if err = result.CheckError(err); err != nil {
@ -117,9 +121,10 @@ func (s service) GetChatroomMemberDetail(chatroom string) (members []ChatRoomMem
func (s service) GetChatroomQRCode(chatroom string) (imgBase64Str, notify string, err error) {
var result base.Response[GetChatroomQRCodeResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"Chatroom": chatroom,
}).Post("/GetChatroomQRCode")
if err = result.CheckError(err); err != nil {

View File

@ -1,15 +1,19 @@
package hongbao
import "github.com/go-resty/resty/v2"
import (
"gitee.ltd/lxh/xybot/core"
"github.com/go-resty/resty/v2"
)
type API interface {
GetHongBaoDetail(xml, encryptKey, encryptUserinfo string) (resp any, err error) // 获取红包详情
}
type service struct {
client *resty.Client
client *resty.Client
robotInfo *core.RobotInfo
}
func New(client *resty.Client) API {
return &service{client: client}
func New(client *resty.Client, robotInfo *core.RobotInfo) API {
return &service{client: client, robotInfo: robotInfo}
}

View File

@ -2,7 +2,6 @@ package hongbao
import (
"gitee.ltd/lxh/xybot/base"
"gitee.ltd/lxh/xybot/core"
)
// GetHongBaoDetail
@ -14,12 +13,12 @@ import (
// @return resp
// @return err
func (s service) GetHongBaoDetail(xml, encryptKey, encryptUserinfo string) (resp any, err error) {
var result base.Response[any]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"Xml": xml,
"EncryptKey": encryptKey,
"EncryptUserinfo": encryptUserinfo,

View File

@ -1,6 +1,9 @@
package login
import "github.com/go-resty/resty/v2"
import (
"gitee.ltd/lxh/xybot/core"
"github.com/go-resty/resty/v2"
)
type API interface {
GetQRCode(deviceId, deviceName string) (resp GetQRCodeResponse, err error) // 获取登录二维码
@ -15,9 +18,10 @@ type API interface {
}
type service struct {
client *resty.Client
client *resty.Client
robotInfo *core.RobotInfo
}
func New(client *resty.Client) API {
return &service{client: client}
func New(client *resty.Client, robotInfo *core.RobotInfo) API {
return &service{client: client, robotInfo: robotInfo}
}

View File

@ -3,7 +3,6 @@ package login
import (
"fmt"
"gitee.ltd/lxh/xybot/base"
"gitee.ltd/lxh/xybot/core"
)
// GetQRCode
@ -35,7 +34,11 @@ func (s service) GetQRCode(deviceId, deviceName string) (resp GetQRCodeResponse,
// @return err
func (s service) AwakenLogin() (resp AwakenLoginResponse, err error) {
var result base.Response[AwakenLoginResponse]
_, err = s.client.R().SetResult(&result).SetBody(map[string]any{"Wxid": core.WxId}).Post("/AwakenLogin")
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{"Wxid": s.robotInfo.GetId()}).
Post("/AwakenLogin")
if err = result.CheckError(err); err != nil {
return
}
@ -62,7 +65,7 @@ func (s service) CheckUuid(uuid string) (resp CheckUuidResponse, err error) {
resp = result.Data
// 这儿做一下处理在登录成功之后更新一下wxId的值免得再初始化一次client
if resp.AcctSectResp.Username != "" {
core.WxId = resp.AcctSectResp.Username
s.robotInfo.SetId(resp.AcctSectResp.Username)
}
return
}
@ -73,7 +76,11 @@ func (s service) CheckUuid(uuid string) (resp CheckUuidResponse, err error) {
// @return err
func (s service) AutoHeartbeatStart() (err error) {
var result base.Response[base.EmptyResponse]
_, err = s.client.R().SetResult(&result).SetBody(map[string]any{"Wxid": core.WxId}).Post("/AutoHeartbeatStart")
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{"Wxid": s.robotInfo.GetId()}).
Post("/AutoHeartbeatStart")
err = result.CheckError(err)
return
}
@ -85,7 +92,11 @@ func (s service) AutoHeartbeatStart() (err error) {
// @return err
func (s service) AutoHeartbeatStatus() (running bool, err error) {
var result AutoHeartbeatStatusResponse
_, err = s.client.R().SetResult(&result).SetBody(map[string]any{"Wxid": core.WxId}).Post("/AutoHeartbeatStatus")
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{"Wxid": s.robotInfo.GetId()}).
Post("/AutoHeartbeatStatus")
if err == nil {
if !result.Success {
err = fmt.Errorf("[%d] %s", result.Code, result.Message)
@ -102,7 +113,11 @@ func (s service) AutoHeartbeatStatus() (running bool, err error) {
// @return err
func (s service) AutoHeartbeatStop() (err error) {
var result base.Response[base.EmptyResponse]
_, err = s.client.R().SetResult(&result).SetBody(map[string]any{"Wxid": core.WxId}).Post("/AutoHeartbeatStop")
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{"Wxid": s.robotInfo.GetId()}).
Post("/AutoHeartbeatStop")
err = result.CheckError(err)
return
}
@ -113,7 +128,11 @@ func (s service) AutoHeartbeatStop() (err error) {
// @return err
func (s service) Heartbeat() (err error) {
var result base.Response[base.EmptyResponse]
_, err = s.client.R().SetResult(&result).SetBody(map[string]any{"Wxid": core.WxId}).Post("/Heartbeat")
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{"Wxid": s.robotInfo.GetId()}).
Post("/Heartbeat")
err = result.CheckError(err)
return
}
@ -125,7 +144,11 @@ func (s service) Heartbeat() (err error) {
// @return err
func (s service) GetCachedInfo() (resp GetCachedInfoResponse, err error) {
var result base.Response[GetCachedInfoResponse]
_, err = s.client.R().SetResult(&result).SetBody(map[string]any{"Wxid": core.WxId}).Post("/GetCachedInfo")
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{"Wxid": s.robotInfo.GetId()}).
Post("/GetCachedInfo")
if err = result.CheckError(err); err != nil {
return
}
@ -139,7 +162,11 @@ func (s service) GetCachedInfo() (resp GetCachedInfoResponse, err error) {
// @return err
func (s service) Logout() (err error) {
var result base.Response[base.EmptyResponse]
_, err = s.client.R().SetResult(&result).SetBody(map[string]any{"Wxid": core.WxId}).Post("/Logout")
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{"Wxid": s.robotInfo.GetId()}).
Post("/Logout")
err = result.CheckError(err)
return
}

View File

@ -1,6 +1,9 @@
package message
import "github.com/go-resty/resty/v2"
import (
"gitee.ltd/lxh/xybot/core"
"github.com/go-resty/resty/v2"
)
type API interface {
RevokeMsg(toWxId string, clientMsgId, createTime, newMsgId int) (err error) // 撤回消息
@ -19,9 +22,10 @@ type API interface {
}
type service struct {
client *resty.Client
client *resty.Client
robotInfo *core.RobotInfo
}
func New(client *resty.Client) API {
return &service{client: client}
func New(client *resty.Client, robotInfo *core.RobotInfo) API {
return &service{client: client, robotInfo: robotInfo}
}

View File

@ -3,7 +3,6 @@ package message
import (
"errors"
"gitee.ltd/lxh/xybot/base"
"gitee.ltd/lxh/xybot/core"
"strings"
)
@ -16,12 +15,12 @@ import (
// @param newMsgId int 返回消息的NewMsgId字段
// @return err
func (s service) RevokeMsg(toWxId string, clientMsgId, createTime, newMsgId int) (err error) {
var result base.Response[base.EmptyResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"ClientMsgId": clientMsgId,
"CreateTime": createTime,
@ -42,12 +41,12 @@ func (s service) RevokeMsg(toWxId string, clientMsgId, createTime, newMsgId int)
// @return newMsgId int 返回消息的NewMsgId字段
// @return err
func (s service) SendAppMsg(toWxId, xml string, appMsgType int) (clientMsgId, createTime, newMsgId int, err error) {
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Xml": xml,
"Type": appMsgType,
@ -71,12 +70,12 @@ func (s service) SendAppMsg(toWxId, xml string, appMsgType int) (clientMsgId, cr
// @return newMsgId
// @return err
func (s service) SendCDNFileMsg(toWxId, xml string) (clientMsgId, createTime, newMsgId int, err error) {
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Content": xml,
}).Post("/SendCDNFileMsg")
@ -99,12 +98,12 @@ func (s service) SendCDNFileMsg(toWxId, xml string) (clientMsgId, createTime, ne
// @return newMsgId
// @return err
func (s service) SendCDNImgMsg(toWxId, xml string) (clientMsgId, createTime, newMsgId int, err error) {
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Content": xml,
}).Post("/SendCDNImgMsg")
@ -127,12 +126,12 @@ func (s service) SendCDNImgMsg(toWxId, xml string) (clientMsgId, createTime, new
// @return newMsgId
// @return err
func (s service) SendCDNVideoMsg(toWxId, xml string) (clientMsgId, createTime, newMsgId int, err error) {
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Content": xml,
}).Post("/SendCDNVideoMsg")
@ -157,12 +156,12 @@ func (s service) SendCDNVideoMsg(toWxId, xml string) (clientMsgId, createTime, n
// @return newMsgId
// @return err
func (s service) SendCardMsg(toWxId, cardWxId, cardNickname, cardAlias string) (clientMsgId, createTime, newMsgId int, err error) {
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"CardWxid": cardWxId,
"CardNickname": cardNickname,
@ -186,12 +185,12 @@ func (s service) SendCardMsg(toWxId, cardWxId, cardNickname, cardAlias string) (
// @return resp
// @return err
func (s service) SendEmojiMsg(toWxId, md5 string, length int) (resp any, err error) {
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Md5": md5,
"TotalLen": length,
@ -213,12 +212,12 @@ func (s service) SendEmojiMsg(toWxId, md5 string, length int) (resp any, err err
// @return newMsgId
// @return err
func (s service) SendImageMsg(toWxId, imgBase64Str string) (clientMsgId, createTime, newMsgId int, err error) {
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Base64": imgBase64Str,
}).Post("/SendImageMsg")
@ -244,12 +243,12 @@ func (s service) SendImageMsg(toWxId, imgBase64Str string) (clientMsgId, createT
// @return newMsgId
// @return err
func (s service) SendShareLink(toWxId, url, title, description, thumbUrl string) (clientMsgId, createTime, newMsgId int, err error) {
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Url": url,
"Title": title,
@ -276,12 +275,12 @@ func (s service) SendShareLink(toWxId, url, title, description, thumbUrl string)
// @return newMsgId
// @return err
func (s service) SendTextMsg(toWxId, content string, atUser []string) (clientMsgId, createTime, newMsgId int, err error) {
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Content": content,
"Type": 1,
@ -308,12 +307,12 @@ func (s service) SendTextMsg(toWxId, content string, atUser []string) (clientMsg
// @return newMsgId
// @return err
func (s service) SendVideoMsg(toWxId, video, cover string, duration int) (clientMsgId, createTime, newMsgId int, err error) {
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Base64": video,
"ImageBase64": cover,
@ -340,7 +339,6 @@ func (s service) SendVideoMsg(toWxId, video, cover string, duration int) (client
// @return newMsgId
// @return err
func (s service) SendVoiceMsg(toWxId, voice, format string, duration int) (clientMsgId, createTime, newMsgId int, err error) {
formatMap := map[string]int{"amr": 0, "wav": 4, "mp3": 4}
if _, ok := formatMap[format]; !ok {
err = errors.New("不支持的语音格式")
@ -349,9 +347,10 @@ func (s service) SendVoiceMsg(toWxId, voice, format string, duration int) (clien
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Base64": voice,
"VoiceTime": duration,
@ -372,12 +371,12 @@ func (s service) SendVoiceMsg(toWxId, voice, format string, duration int) (clien
// @return msg
// @return err
func (s service) Sync() (msg []SyncMessage, err error) {
var result base.Response[SyncResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"Scene": 0,
"Synckey": "",
}).Post("/Sync")

View File

@ -1,6 +1,9 @@
package tool
import "github.com/go-resty/resty/v2"
import (
"gitee.ltd/lxh/xybot/core"
"github.com/go-resty/resty/v2"
)
type API interface {
CheckDatabaseOK() (running bool, err error) // 检查数据库是否正常
@ -14,9 +17,10 @@ type API interface {
}
type service struct {
client *resty.Client
client *resty.Client
robotInfo *core.RobotInfo
}
func New(client *resty.Client) API {
return &service{client: client}
func New(client *resty.Client, robotInfo *core.RobotInfo) API {
return &service{client: client, robotInfo: robotInfo}
}

View File

@ -3,7 +3,6 @@ package tool
import (
"fmt"
"gitee.ltd/lxh/xybot/base"
"gitee.ltd/lxh/xybot/core"
)
// CheckDatabaseOK
@ -42,9 +41,10 @@ func (s service) IsRunning() (flag bool, err error) {
func (s service) CdnDownloadImg(aesKey, cdnMidImgUrl string) (dataBase64Str string, err error) {
var result base.Response[string]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"AesKey": aesKey,
"Cdnmidimgurl": cdnMidImgUrl,
}).Post("/CdnDownloadImg")
@ -64,9 +64,10 @@ func (s service) CdnDownloadImg(aesKey, cdnMidImgUrl string) (dataBase64Str stri
func (s service) DownloadAttach(attachId string) (dataBase64Str string, err error) {
var result base.Response[DownloadAttachResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"AttachId": attachId,
}).Post("/DownloadAttach")
if err = result.CheckError(err); err != nil {
@ -86,9 +87,10 @@ func (s service) DownloadVideo(msgId int) (dataBase64Str string, err error) {
var result base.Response[DownloadAttachResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"MsgId": msgId,
}).Post("/DownloadVideo")
if err = result.CheckError(err); err != nil {
@ -107,12 +109,12 @@ func (s service) DownloadVideo(msgId int) (dataBase64Str string, err error) {
// @return dataBase64Str
// @return err
func (s service) DownloadVoice(msgId int, voiceUrl string, length int) (dataBase64Str string, err error) {
var result base.Response[DownloadAttachResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"MsgId": msgId,
"Voiceurl": voiceUrl,
"Length": length,
@ -130,12 +132,12 @@ func (s service) DownloadVoice(msgId int, voiceUrl string, length int) (dataBase
// @param param
// @return err
func (s service) SetProxy(param SetProxyRequest) (err error) {
var result base.Response[base.EmptyResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"Proxy": map[string]any{
"ProxyIp": fmt.Sprintf("%s:%d", param.Ip, param.Port),
"ProxyUser": param.Username,
@ -155,12 +157,12 @@ func (s service) SetProxy(param SetProxyRequest) (err error) {
// @param stepCount
// @return err
func (s service) SetStep(stepCount int) (err error) {
var result base.Response[base.EmptyResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{
"Wxid": core.WxId,
"Wxid": s.robotInfo.GetId(),
"StepCount": stepCount,
}).Post("/SetStep")
if err = result.CheckError(err); err != nil {

View File

@ -1,6 +1,9 @@
package user
import "github.com/go-resty/resty/v2"
import (
"gitee.ltd/lxh/xybot/core"
"github.com/go-resty/resty/v2"
)
type API interface {
GetMyQRCode() (str string, err error) // 获取个人二维码
@ -8,9 +11,10 @@ type API interface {
}
type service struct {
client *resty.Client
client *resty.Client
robotInfo *core.RobotInfo
}
func New(client *resty.Client) API {
return &service{client: client}
func New(client *resty.Client, robotInfo *core.RobotInfo) API {
return &service{client: client, robotInfo: robotInfo}
}

View File

@ -2,7 +2,6 @@ package user
import (
"gitee.ltd/lxh/xybot/base"
"gitee.ltd/lxh/xybot/core"
)
// GetMyQRCode
@ -11,11 +10,11 @@ import (
// @return str string 图片的base64编码字符串
// @return err error 错误信息
func (s service) GetMyQRCode() (str string, err error) {
var result base.Response[GetMyQRCodeResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{"Wxid": core.WxId, "Style": 0}).
SetBody(map[string]any{"Wxid": s.robotInfo.GetId(), "Style": 0}).
Post("/GetMyQRCode")
if err = result.CheckError(err); err != nil {
return
@ -30,11 +29,11 @@ func (s service) GetMyQRCode() (str string, err error) {
// @return resp 用户信息
// @return err
func (s service) GetProfile() (resp GetProfileResponse, err error) {
var result base.Response[GetProfileResponse]
_, err = s.client.R().
SetHeader("WeChatId", s.robotInfo.GetId()).
SetResult(&result).
SetBody(map[string]any{"Wxid": core.WxId}).
SetBody(map[string]any{"Wxid": s.robotInfo.GetId()}).
Post("/GetProfile")
if err = result.CheckError(err); err != nil {
return