Compare commits

...

7 Commits
v0.0.1 ... main

17 changed files with 253 additions and 202 deletions

View File

@ -1,74 +1,5 @@
# 一个XYBotV2的Golang语言的Client封装
```go
package main
## 使用方法
import (
"fmt"
xybotclient "gitee.ltd/lxh/xybot"
"log"
)
func main() {
// 创建客户端
client, err := xybotclient.NewClient("wxid_n5sg7tjup7f322", "http://10.0.0.78:9001", false)
if err != nil {
log.Fatalf("创建客户端失败: %v", err)
}
// 检查API是否在运行
isRunning, err := client.Tool.IsRunning()
if err != nil {
log.Fatalf("检查API是否运行失败: %v", err)
}
if !isRunning {
log.Fatalf("微信API服务未启动")
}
fmt.Println("微信API服务已启动")
// 获取登录缓存信息
loginInfo, err := client.Login.GetCachedInfo()
if err != nil {
log.Fatalf("获取登录缓存信息失败: %v", err)
}
fmt.Printf("登录缓存信息: %+v\n", loginInfo)
// 获取好友列表
friends, err := client.Friend.GetContractList(true)
if err != nil {
log.Fatalf("获取好友列表失败: %v", err)
}
fmt.Printf("好友列表: %+v\n", friends)
// 获取群聊详情
chatroomInfo, err := client.Group.GetChatroomInfo("20675221672@chatroom")
if err != nil {
log.Fatalf("获取群聊详情失败: %v", err)
}
fmt.Printf("群聊详情: %+v\n", chatroomInfo)
// 获取不含公告的详情
chatroomInfoNoNotice, err := client.Group.GetChatroomInfoNoAnnounce([]string{"20675221672@chatroom"})
if err != nil {
log.Fatalf("获取不含公告的详情失败: %v", err)
}
fmt.Printf("不含公告的群聊详情: %+v\n", chatroomInfoNoNotice)
// 获取群聊成员详情
chatroomMemberInfo, err := client.Group.GetChatroomMemberDetail("20675221672@chatroom")
if err != nil {
log.Fatalf("获取群聊成员详情失败: %v", err)
}
fmt.Printf("群聊成员详情: %+v\n", chatroomMemberInfo)
// 获取一个二维码试试
qrCode, notifyStr, err := client.Group.GetChatroomQRCode("20675221672@chatroom")
if err != nil {
log.Fatalf("获取群聊二维码失败: %v", err)
}
fmt.Printf("群聊二维码: %s\n%s\n", qrCode, notifyStr)
}
```
参考example目录下的代码

View File

@ -3,6 +3,10 @@ package xybot
import (
"encoding/json"
"errors"
"slices"
"strings"
"gitee.ltd/lxh/xybot/core"
"gitee.ltd/lxh/xybot/friend"
"gitee.ltd/lxh/xybot/group"
"gitee.ltd/lxh/xybot/hongbao"
@ -14,8 +18,6 @@ import (
)
type Client struct {
// 基础URL
wxId string
// HTTP客户端
httpClient *resty.Client
@ -41,10 +43,12 @@ func NewClient(wxId, baseUrl string, debug bool) (cli *Client, err error) {
err = errors.New("baseUrl 不得为空,格式为: http://10.0.0.11:9001")
return
}
if wxId == "" {
err = errors.New("wxId 不得为空")
return
// 如果URL没有以http://或https://开头自动添加http://
if !strings.HasPrefix(baseUrl, "http://") && !strings.HasPrefix(baseUrl, "https://") {
baseUrl = "http://" + baseUrl
}
// 设置一下机器人微信Id的值
wxInfo := core.NewInfo(wxId)
cli = &Client{}
cli.httpClient = resty.New().
@ -52,15 +56,36 @@ func NewClient(wxId, baseUrl string, debug bool) (cli *Client, err error) {
SetDebug(debug).
SetHeader("User-Agent", "lxh-xybot-client").
SetJSONMarshaler(json.Marshal).
SetJSONUnmarshaler(json.Unmarshal)
SetJSONUnmarshaler(json.Unmarshal).
OnBeforeRequest(requestPreCheck)
cli.Login = login.New(cli.httpClient, wxId)
cli.Friend = friend.New(cli.httpClient, wxId)
cli.Group = group.New(cli.httpClient, wxId)
cli.Message = message.New(cli.httpClient, wxId)
cli.User = user.New(cli.httpClient, wxId)
cli.HongBao = hongbao.New(cli.httpClient, wxId)
cli.Tool = tool.New(cli.httpClient, wxId)
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
}
// requestPreCheck
// @description: 请求前置检查
// @param _ *resty.Client
// @param req *resty.Request
// @return err error
func requestPreCheck(_ *resty.Client, req *resty.Request) (err error) {
// 在这儿判断指定请求地址的时候WxId是否有值
whiteUrls := []string{"/GetQRCode", "/CheckUuid", "/IsRunning", "/CheckDatabaseOK"}
if slices.Contains(whiteUrls, req.URL) {
// 如果是白名单请求,直接返回
return nil
}
if req.Header.Get("WeChatId") == "" {
// 如果WxId为空直接返回
return errors.New("wxId不能为空")
}
return nil
}

31
core/info.go Normal file
View File

@ -0,0 +1,31 @@
package core
// 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,23 +1,22 @@
package friend
import "github.com/go-resty/resty/v2"
import (
"gitee.ltd/lxh/xybot/core"
"github.com/go-resty/resty/v2"
)
type API interface {
SendFriendRequest(scene int, v1, v2, verifyContent string) (username string, err error) // 发送好友请求
AcceptFriend(scene int, v1, v2 string) (err error) // 接受好友请求
GetContact(wxIds []string) (contactList []ContactListItem, err error) // 通过wxid获取联系人详情
GetContractDetail(wxIds []string) (contactList []ContactListItem, err error) // 获取联系人详情
GetContractList(clearSystemAccount bool) (wxIds []string, err error) // 获取联系人列表
AcceptFriend(scene int, v1, v2 string) (err error) // 接受好友请求
GetContact(wxIds []string) (contactList []ContactListItem, err error) // 通过wxid获取联系人详情
GetContractDetail(wxIds []string) (contactList []ContactListItem, err error) // 获取联系人详情
GetContractList(clearSystemAccount bool) (wxIds []string, err error) // 获取联系人列表
}
type service struct {
client *resty.Client
wxId string
client *resty.Client
robotInfo *core.RobotInfo
}
func New(client *resty.Client, wxId string) API {
return &service{
client: client,
wxId: wxId,
}
func New(client *resty.Client, robotInfo *core.RobotInfo) API {
return &service{client: client, robotInfo: robotInfo}
}

View File

@ -7,19 +7,6 @@ import (
"strings"
)
// SendFriendRequest
// @description: 发送好友请求
// @receiver s
// @param scene
// @param v1
// @param v2
// @param verifyContent
// @return username
// @return err
func (s service) SendFriendRequest(scene int, v1, v2, verifyContent string) (username string, err error) {
return
}
// AcceptFriend
// @description: 接受好友请求
// @receiver s
@ -30,9 +17,10 @@ func (s service) SendFriendRequest(scene int, v1, v2, verifyContent string) (use
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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"Scene": scene,
"V1": v1,
"V2": v2,
@ -50,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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"RequestWxids": strings.Join(wxIds, ","),
}).Post("/GetContact")
if err = result.CheckError(err); err != nil {
@ -76,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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"RequestWxids": strings.Join(wxIds, ","),
"Chatroom": "",
}).Post("/GetContractDetail")
@ -98,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": s.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,13 +15,10 @@ type API interface {
}
type service struct {
client *resty.Client
wxId string
client *resty.Client
robotInfo *core.RobotInfo
}
func New(client *resty.Client, wxId string) API {
return &service{
client: client,
wxId: wxId,
}
func New(client *resty.Client, robotInfo *core.RobotInfo) API {
return &service{client: client, robotInfo: robotInfo}
}

View File

@ -14,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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"Chatroom": chatroom,
"InviteWxids": inviteWxId,
}).Post("/AddChatroomMember")
@ -33,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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"Chatroom": chatroom,
"InviteWxids": strings.Join(inviteWxIds, ","),
}).Post("/InviteChatroomMember")
@ -52,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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"Chatroom": chatroom,
}).Post("/GetChatroomInfo")
if err = result.CheckError(err); err != nil {
@ -73,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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"Chatroom": strings.Join(chatroom, ","),
}).Post("/GetChatroomInfoNoAnnounce")
if err = result.CheckError(err); err != nil {
@ -94,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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"Chatroom": chatroom,
}).Post("/GetChatroomMemberDetail")
if err = result.CheckError(err); err != nil {
@ -116,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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"Chatroom": chatroom,
}).Post("/GetChatroomQRCode")
if err = result.CheckError(err); err != nil {

View File

@ -1,19 +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
wxId string
client *resty.Client
robotInfo *core.RobotInfo
}
func New(client *resty.Client, wxId string) API {
return &service{
client: client,
wxId: wxId,
}
func New(client *resty.Client, robotInfo *core.RobotInfo) API {
return &service{client: client, robotInfo: robotInfo}
}

View File

@ -1,6 +1,8 @@
package hongbao
import "gitee.ltd/lxh/xybot/base"
import (
"gitee.ltd/lxh/xybot/base"
)
// GetHongBaoDetail
// @description: 获取红包详情
@ -13,9 +15,10 @@ import "gitee.ltd/lxh/xybot/base"
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": s.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,13 +18,10 @@ type API interface {
}
type service struct {
client *resty.Client
wxId string
client *resty.Client
robotInfo *core.RobotInfo
}
func New(client *resty.Client, wxId string) API {
return &service{
client: client,
wxId: wxId,
}
func New(client *resty.Client, robotInfo *core.RobotInfo) API {
return &service{client: client, robotInfo: robotInfo}
}

View File

@ -14,7 +14,6 @@ import (
// @return err
func (s service) GetQRCode(deviceId, deviceName string) (resp GetQRCodeResponse, err error) {
var result base.Response[GetQRCodeResponse]
_, err = s.client.R().
SetResult(&result).
SetBody(map[string]any{
@ -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": s.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
}
@ -60,6 +63,10 @@ func (s service) CheckUuid(uuid string) (resp CheckUuidResponse, err error) {
return
}
resp = result.Data
// 这儿做一下处理在登录成功之后更新一下wxId的值免得再初始化一次client
if resp.AcctSectResp.Username != "" {
s.robotInfo.SetId(resp.AcctSectResp.Username)
}
return
}
@ -69,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": s.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
}
@ -81,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": s.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)
@ -98,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": s.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
}
@ -109,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": s.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
}
@ -121,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": s.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
}
@ -135,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": s.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,13 +22,10 @@ type API interface {
}
type service struct {
client *resty.Client
wxId string
client *resty.Client
robotInfo *core.RobotInfo
}
func New(client *resty.Client, wxId string) API {
return &service{
client: client,
wxId: wxId,
}
func New(client *resty.Client, robotInfo *core.RobotInfo) API {
return &service{client: client, robotInfo: robotInfo}
}

View File

@ -17,9 +17,10 @@ import (
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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"ClientMsgId": clientMsgId,
"CreateTime": createTime,
@ -42,9 +43,10 @@ func (s service) RevokeMsg(toWxId string, clientMsgId, createTime, newMsgId int)
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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Xml": xml,
"Type": appMsgType,
@ -70,9 +72,10 @@ func (s service) SendAppMsg(toWxId, xml string, appMsgType int) (clientMsgId, cr
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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Content": xml,
}).Post("/SendCDNFileMsg")
@ -97,9 +100,10 @@ func (s service) SendCDNFileMsg(toWxId, xml string) (clientMsgId, createTime, ne
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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Content": xml,
}).Post("/SendCDNImgMsg")
@ -124,9 +128,10 @@ func (s service) SendCDNImgMsg(toWxId, xml string) (clientMsgId, createTime, new
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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Content": xml,
}).Post("/SendCDNVideoMsg")
@ -153,9 +158,10 @@ func (s service) SendCDNVideoMsg(toWxId, xml string) (clientMsgId, createTime, n
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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"CardWxid": cardWxId,
"CardNickname": cardNickname,
@ -181,9 +187,10 @@ func (s service) SendCardMsg(toWxId, cardWxId, cardNickname, cardAlias string) (
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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Md5": md5,
"TotalLen": length,
@ -207,9 +214,10 @@ func (s service) SendEmojiMsg(toWxId, md5 string, length int) (resp any, err 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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Base64": imgBase64Str,
}).Post("/SendImageMsg")
@ -237,9 +245,10 @@ func (s service) SendImageMsg(toWxId, imgBase64Str string) (clientMsgId, createT
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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Url": url,
"Title": title,
@ -268,9 +277,10 @@ func (s service) SendShareLink(toWxId, url, title, description, thumbUrl string)
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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Content": content,
"Type": 1,
@ -299,9 +309,10 @@ func (s service) SendTextMsg(toWxId, content string, atUser []string) (clientMsg
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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Base64": video,
"ImageBase64": cover,
@ -336,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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"ToWxid": toWxId,
"Base64": voice,
"VoiceTime": duration,
@ -361,9 +373,10 @@ func (s service) SendVoiceMsg(toWxId, voice, format string, duration int) (clien
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": s.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,13 +17,10 @@ type API interface {
}
type service struct {
client *resty.Client
wxId string
client *resty.Client
robotInfo *core.RobotInfo
}
func New(client *resty.Client, wxId string) API {
return &service{
client: client,
wxId: wxId,
}
func New(client *resty.Client, robotInfo *core.RobotInfo) API {
return &service{client: client, robotInfo: robotInfo}
}

View File

@ -41,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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"AesKey": aesKey,
"Cdnmidimgurl": cdnMidImgUrl,
}).Post("/CdnDownloadImg")
@ -63,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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"AttachId": attachId,
}).Post("/DownloadAttach")
if err = result.CheckError(err); err != nil {
@ -82,11 +84,13 @@ func (s service) DownloadAttach(attachId string) (dataBase64Str string, err erro
// @return dataBase64Str
// @return err
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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"MsgId": msgId,
}).Post("/DownloadVideo")
if err = result.CheckError(err); err != nil {
@ -107,9 +111,10 @@ func (s service) DownloadVideo(msgId int) (dataBase64Str string, err error) {
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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"MsgId": msgId,
"Voiceurl": voiceUrl,
"Length": length,
@ -129,9 +134,10 @@ func (s service) DownloadVoice(msgId int, voiceUrl string, length int) (dataBase
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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"Proxy": map[string]any{
"ProxyIp": fmt.Sprintf("%s:%d", param.Ip, param.Port),
"ProxyUser": param.Username,
@ -153,9 +159,10 @@ func (s service) SetProxy(param SetProxyRequest) (err error) {
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": s.wxId,
"Wxid": s.robotInfo.GetId(),
"StepCount": stepCount,
}).Post("/SetStep")
if err = result.CheckError(err); err != nil {

View File

@ -1,20 +1,20 @@
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) // 获取个人二维码
GetProfile() (resp any, err error) // 获取个人信息
GetMyQRCode() (str string, err error) // 获取个人二维码
GetProfile() (resp GetProfileResponse, err error) // 获取个人信息
}
type service struct {
client *resty.Client
wxId string
client *resty.Client
robotInfo *core.RobotInfo
}
func New(client *resty.Client, wxId string) API {
return &service{
client: client,
wxId: wxId,
}
func New(client *resty.Client, robotInfo *core.RobotInfo) API {
return &service{client: client, robotInfo: robotInfo}
}

View File

@ -1,6 +1,8 @@
package user
import "gitee.ltd/lxh/xybot/base"
import (
"gitee.ltd/lxh/xybot/base"
)
// GetMyQRCode
// @description: 获取个人二维码
@ -10,8 +12,9 @@ import "gitee.ltd/lxh/xybot/base"
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": s.wxId, "Style": 0}).
SetBody(map[string]any{"Wxid": s.robotInfo.GetId(), "Style": 0}).
Post("/GetMyQRCode")
if err = result.CheckError(err); err != nil {
return
@ -25,6 +28,17 @@ func (s service) GetMyQRCode() (str string, err error) {
// @receiver s
// @return resp 用户信息
// @return err
func (s service) GetProfile() (resp any, err error) {
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": s.robotInfo.GetId()}).
Post("/GetProfile")
if err = result.CheckError(err); err != nil {
return
}
resp = result.Data
return
}