xybot/client.go

63 lines
1.5 KiB
Go

package xybot
import (
"encoding/json"
"errors"
"gitee.ltd/lxh/xybot/friend"
"gitee.ltd/lxh/xybot/group"
"gitee.ltd/lxh/xybot/hongbao"
"gitee.ltd/lxh/xybot/login"
"gitee.ltd/lxh/xybot/message"
"gitee.ltd/lxh/xybot/tool"
"gitee.ltd/lxh/xybot/user"
"github.com/go-resty/resty/v2"
)
type Client struct {
// 基础URL
wxId string
// HTTP客户端
httpClient *resty.Client
// 功能模块
Login login.API
Friend friend.API
Group group.API
Message message.API
User user.API
HongBao hongbao.API
Tool tool.API
}
// NewClient
// @description: 创建新的微信API客户端
// @param wxId string 机器人微信ID
// @param baseUrl string 机器人接口基础URL
// @param debug bool 是否开启调试模式
// @return cli
// @return err
func NewClient(wxId, baseUrl string, debug bool) (cli *Client, err error) {
if baseUrl == "" {
err = errors.New("baseUrl 不得为空,格式为: http://10.0.0.11:9001")
return
}
cli = &Client{}
cli.httpClient = resty.New().
SetBaseURL(baseUrl).
SetDebug(debug).
SetHeader("User-Agent", "lxh-xybot-client").
SetJSONMarshaler(json.Marshal).
SetJSONUnmarshaler(json.Unmarshal)
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)
return
}