添加默认的uuid回调
This commit is contained in:
parent
1dab95b4ee
commit
6e683358ef
32
bot.go
32
bot.go
@ -24,7 +24,7 @@ type Bot struct {
|
||||
hotReloadStorage HotReloadStorage
|
||||
}
|
||||
|
||||
// 判断当前用户是否正常在线
|
||||
// Alive 判断当前用户是否正常在线
|
||||
func (b *Bot) Alive() bool {
|
||||
if b.self == nil {
|
||||
return false
|
||||
@ -37,7 +37,7 @@ func (b *Bot) Alive() bool {
|
||||
}
|
||||
}
|
||||
|
||||
// 获取当前的用户
|
||||
// GetCurrentUser 获取当前的用户
|
||||
// self, err := bot.GetCurrentUser()
|
||||
// if err != nil {
|
||||
// return
|
||||
@ -50,7 +50,7 @@ func (b *Bot) GetCurrentUser() (*Self, error) {
|
||||
return b.self, nil
|
||||
}
|
||||
|
||||
// 热登录,可实现重复登录,
|
||||
// HotLogin 热登录,可实现重复登录,
|
||||
// retry设置为true可在热登录失效后进行普通登录行为
|
||||
// storage := NewJsonFileHotReloadStorage("storage.json")
|
||||
// err := bot.HotLogin(storage, true)
|
||||
@ -98,7 +98,7 @@ func (b *Bot) hotLoginInit() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 用户登录
|
||||
// Login 用户登录
|
||||
// 该方法会一直阻塞,直到用户扫码登录,或者二维码过期
|
||||
func (b *Bot) Login() error {
|
||||
uuid, err := b.Caller.GetLoginUUID()
|
||||
@ -135,7 +135,7 @@ func (b *Bot) Login() error {
|
||||
}
|
||||
}
|
||||
|
||||
// 用户退出
|
||||
// Logout 用户退出
|
||||
func (b *Bot) Logout() error {
|
||||
if b.Alive() {
|
||||
if b.LogoutCallBack != nil {
|
||||
@ -267,7 +267,7 @@ func (b *Bot) getNewWechatMessage() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 当消息同步发生了错误或者用户主动在手机上退出,该方法会立即返回,否则会一直阻塞
|
||||
// Block 当消息同步发生了错误或者用户主动在手机上退出,该方法会立即返回,否则会一直阻塞
|
||||
func (b *Bot) Block() error {
|
||||
if b.self == nil {
|
||||
return errors.New("`Block` must be called after user login")
|
||||
@ -276,22 +276,22 @@ func (b *Bot) Block() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取当前Bot崩溃的原因
|
||||
// CrashReason 获取当前Bot崩溃的原因
|
||||
func (b *Bot) CrashReason() error {
|
||||
return b.err
|
||||
}
|
||||
|
||||
// setter for Bot.MessageHandler
|
||||
// MessageOnSuccess setter for Bot.MessageHandler
|
||||
func (b *Bot) MessageOnSuccess(h func(msg *Message)) {
|
||||
b.MessageHandler = h
|
||||
}
|
||||
|
||||
// setter for Bot.GetMessageErrorHandler
|
||||
// MessageOnError setter for Bot.GetMessageErrorHandler
|
||||
func (b *Bot) MessageOnError(h func(err error)) {
|
||||
b.GetMessageErrorHandler = h
|
||||
}
|
||||
|
||||
// 写入HotReloadStorage
|
||||
// DumpHotReloadStorage 写入HotReloadStorage
|
||||
func (b *Bot) DumpHotReloadStorage() error {
|
||||
if b.hotReloadStorage == nil {
|
||||
return errors.New("hotReloadStorage can be nil")
|
||||
@ -321,13 +321,13 @@ func (b *Bot) OnLogout(f func(bot *Bot)) {
|
||||
b.LogoutCallBack = f
|
||||
}
|
||||
|
||||
// Bot的构造方法,需要自己传入Caller
|
||||
// NewBot Bot的构造方法,需要自己传入Caller
|
||||
func NewBot(caller *Caller) *Bot {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
return &Bot{Caller: caller, storage: &Storage{}, context: ctx, cancel: cancel}
|
||||
}
|
||||
|
||||
// 默认的Bot的构造方法,
|
||||
// DefaultBot 默认的Bot的构造方法,
|
||||
// mode不传入默认为openwechat.Normal,详情见mode
|
||||
// bot := openwechat.DefaultBot(openwechat.Desktop)
|
||||
func DefaultBot(modes ...mode) *Bot {
|
||||
@ -339,15 +339,17 @@ func DefaultBot(modes ...mode) *Bot {
|
||||
}
|
||||
caller := DefaultCaller()
|
||||
caller.Client.mode = m
|
||||
return NewBot(caller)
|
||||
bot := NewBot(caller)
|
||||
bot.UUIDCallback = PrintlnQrcodeUrl
|
||||
return bot
|
||||
}
|
||||
|
||||
// 通过uuid获取登录二维码的url
|
||||
// GetQrcodeUrl 通过uuid获取登录二维码的url
|
||||
func GetQrcodeUrl(uuid string) string {
|
||||
return qrcode + uuid
|
||||
}
|
||||
|
||||
// 打印登录二维码
|
||||
// PrintlnQrcodeUrl 打印登录二维码
|
||||
func PrintlnQrcodeUrl(uuid string) {
|
||||
println("访问下面网址扫描二维码登录")
|
||||
println(GetQrcodeUrl(uuid))
|
||||
|
@ -208,6 +208,7 @@ func TestSendMessage(t *testing.T) {
|
||||
func TestAgreeFriendsAdd(t *testing.T) {
|
||||
bot := defaultBot()
|
||||
bot.MessageHandler = func(msg *Message) {
|
||||
msg.Sender()
|
||||
if msg.IsFriendAdd() {
|
||||
if err := msg.Agree(); err != nil {
|
||||
t.Error(err)
|
||||
|
42
caller.go
42
caller.go
@ -8,24 +8,24 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// 调用请求和解析请求
|
||||
// Caller 调用请求和解析请求
|
||||
// 上层模块可以直接获取封装后的请求结果
|
||||
type Caller struct {
|
||||
Client *Client
|
||||
path *url.URL
|
||||
}
|
||||
|
||||
// Constructor for Caller
|
||||
// NewCaller Constructor for Caller
|
||||
func NewCaller(client *Client) *Caller {
|
||||
return &Caller{Client: client}
|
||||
}
|
||||
|
||||
// Default Constructor for Caller
|
||||
// DefaultCaller Default Constructor for Caller
|
||||
func DefaultCaller() *Caller {
|
||||
return NewCaller(DefaultClient())
|
||||
}
|
||||
|
||||
// 获取登录的uuid
|
||||
// GetLoginUUID 获取登录的uuid
|
||||
func (c *Caller) GetLoginUUID() (string, error) {
|
||||
resp, err := c.Client.GetLoginUUID()
|
||||
if err != nil {
|
||||
@ -47,7 +47,7 @@ func (c *Caller) GetLoginUUID() (string, error) {
|
||||
return string(results[1]), nil
|
||||
}
|
||||
|
||||
// 检查是否登录成功
|
||||
// CheckLogin 检查是否登录成功
|
||||
func (c *Caller) CheckLogin(uuid string) (*CheckLoginResponse, error) {
|
||||
resp, err := c.Client.CheckLogin(uuid)
|
||||
if err != nil {
|
||||
@ -69,7 +69,7 @@ func (c *Caller) CheckLogin(uuid string) (*CheckLoginResponse, error) {
|
||||
return &CheckLoginResponse{Code: code, Raw: buffer.Bytes()}, nil
|
||||
}
|
||||
|
||||
// 获取登录信息
|
||||
// GetLoginInfo 获取登录信息
|
||||
func (c *Caller) GetLoginInfo(body []byte) (*LoginInfo, error) {
|
||||
// 从响应体里面获取需要跳转的url
|
||||
results := redirectUriRegexp.FindSubmatch(body)
|
||||
@ -102,7 +102,7 @@ func (c *Caller) GetLoginInfo(body []byte) (*LoginInfo, error) {
|
||||
return &loginInfo, nil
|
||||
}
|
||||
|
||||
// 获取初始化信息
|
||||
// WebInit 获取初始化信息
|
||||
func (c *Caller) WebInit(request *BaseRequest) (*WebInitResponse, error) {
|
||||
resp, err := c.Client.WebInit(request)
|
||||
if err != nil {
|
||||
@ -116,7 +116,7 @@ func (c *Caller) WebInit(request *BaseRequest) (*WebInitResponse, error) {
|
||||
return &webInitResponse, nil
|
||||
}
|
||||
|
||||
// 通知手机已登录
|
||||
// WebWxStatusNotify 通知手机已登录
|
||||
func (c *Caller) WebWxStatusNotify(request *BaseRequest, response *WebInitResponse, info *LoginInfo) error {
|
||||
resp, err := c.Client.WebWxStatusNotify(request, response, info)
|
||||
if err != nil {
|
||||
@ -133,7 +133,7 @@ func (c *Caller) WebWxStatusNotify(request *BaseRequest, response *WebInitRespon
|
||||
return nil
|
||||
}
|
||||
|
||||
// 异步获取是否有新的消息
|
||||
// SyncCheck 异步获取是否有新的消息
|
||||
func (c *Caller) SyncCheck(info *LoginInfo, response *WebInitResponse) (*SyncCheckResponse, error) {
|
||||
resp, err := c.Client.SyncCheck(info, response)
|
||||
if err != nil {
|
||||
@ -153,7 +153,7 @@ func (c *Caller) SyncCheck(info *LoginInfo, response *WebInitResponse) (*SyncChe
|
||||
return syncCheckResponse, nil
|
||||
}
|
||||
|
||||
// 获取所有的联系人
|
||||
// WebWxGetContact 获取所有的联系人
|
||||
func (c *Caller) WebWxGetContact(info *LoginInfo) (Members, error) {
|
||||
resp, err := c.Client.WebWxGetContact(info)
|
||||
if err != nil {
|
||||
@ -170,7 +170,7 @@ func (c *Caller) WebWxGetContact(info *LoginInfo) (Members, error) {
|
||||
return item.MemberList, nil
|
||||
}
|
||||
|
||||
// 获取联系人的详情
|
||||
// WebWxBatchGetContact 获取联系人的详情
|
||||
// 注: Members参数的长度不要大于50
|
||||
func (c *Caller) WebWxBatchGetContact(members Members, request *BaseRequest) (Members, error) {
|
||||
resp, err := c.Client.WebWxBatchGetContact(members, request)
|
||||
@ -188,7 +188,7 @@ func (c *Caller) WebWxBatchGetContact(members Members, request *BaseRequest) (Me
|
||||
return item.ContactList, nil
|
||||
}
|
||||
|
||||
// 获取新的消息接口
|
||||
// WebWxSync 获取新的消息接口
|
||||
func (c *Caller) WebWxSync(request *BaseRequest, response *WebInitResponse, info *LoginInfo) (*WebWxSyncResponse, error) {
|
||||
resp, err := c.Client.WebWxSync(request, response, info)
|
||||
if err != nil {
|
||||
@ -202,13 +202,13 @@ func (c *Caller) WebWxSync(request *BaseRequest, response *WebInitResponse, info
|
||||
return &webWxSyncResponse, nil
|
||||
}
|
||||
|
||||
// 发送消息接口
|
||||
// WebWxSendMsg 发送消息接口
|
||||
func (c *Caller) WebWxSendMsg(msg *SendMessage, info *LoginInfo, request *BaseRequest) (*SentMessage, error) {
|
||||
resp, err := c.Client.WebWxSendMsg(msg, info, request)
|
||||
return getSuccessSentMessage(msg, resp, err)
|
||||
}
|
||||
|
||||
// 修改用户备注接口
|
||||
// WebWxOplog 修改用户备注接口
|
||||
func (c *Caller) WebWxOplog(request *BaseRequest, remarkName, toUserName string) error {
|
||||
resp, err := c.Client.WebWxOplog(request, remarkName, toUserName)
|
||||
if err != nil {
|
||||
@ -240,7 +240,7 @@ func (c *Caller) UploadMedia(file *os.File, request *BaseRequest, info *LoginInf
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
// 发送图片消息接口
|
||||
// WebWxSendImageMsg 发送图片消息接口
|
||||
func (c *Caller) WebWxSendImageMsg(file *os.File, request *BaseRequest, info *LoginInfo, fromUserName, toUserName string) (*SentMessage, error) {
|
||||
// 首先尝试上传图片
|
||||
resp, err := c.UploadMedia(file, request, info, fromUserName, toUserName)
|
||||
@ -270,13 +270,13 @@ func (c *Caller) WebWxSendFile(file *os.File, req *BaseRequest, info *LoginInfo,
|
||||
return c.WebWxSendAppMsg(msg, req)
|
||||
}
|
||||
|
||||
// 发送媒体消息
|
||||
// WebWxSendAppMsg 发送媒体消息
|
||||
func (c *Caller) WebWxSendAppMsg(msg *SendMessage, req *BaseRequest) (*SentMessage, error) {
|
||||
resp, err := c.Client.WebWxSendAppMsg(msg, req)
|
||||
return getSuccessSentMessage(msg, resp, err)
|
||||
}
|
||||
|
||||
// 用户退出
|
||||
// Logout 用户退出
|
||||
func (c *Caller) Logout(info *LoginInfo) error {
|
||||
resp, err := c.Client.Logout(info)
|
||||
if err != nil {
|
||||
@ -285,7 +285,7 @@ func (c *Caller) Logout(info *LoginInfo) error {
|
||||
return parseBaseResponseError(resp)
|
||||
}
|
||||
|
||||
// 拉好友入群
|
||||
// AddFriendIntoChatRoom 拉好友入群
|
||||
func (c *Caller) AddFriendIntoChatRoom(req *BaseRequest, info *LoginInfo, group *Group, friends ...*Friend) error {
|
||||
if len(friends) == 0 {
|
||||
return errors.New("no friends found")
|
||||
@ -297,7 +297,7 @@ func (c *Caller) AddFriendIntoChatRoom(req *BaseRequest, info *LoginInfo, group
|
||||
return parseBaseResponseError(resp)
|
||||
}
|
||||
|
||||
// 从群聊中移除用户
|
||||
// RemoveFriendFromChatRoom 从群聊中移除用户
|
||||
func (c *Caller) RemoveFriendFromChatRoom(req *BaseRequest, info *LoginInfo, group *Group, users ...*User) error {
|
||||
if len(users) == 0 {
|
||||
return errors.New("no users found")
|
||||
@ -309,7 +309,7 @@ func (c *Caller) RemoveFriendFromChatRoom(req *BaseRequest, info *LoginInfo, gro
|
||||
return parseBaseResponseError(resp)
|
||||
}
|
||||
|
||||
// 同意加好友请求
|
||||
// WebWxVerifyUser 同意加好友请求
|
||||
func (c *Caller) WebWxVerifyUser(storage *Storage, info RecommendInfo, verifyContent string) error {
|
||||
resp, err := c.Client.WebWxVerifyUser(storage, info, verifyContent)
|
||||
if err != nil {
|
||||
@ -318,7 +318,7 @@ func (c *Caller) WebWxVerifyUser(storage *Storage, info RecommendInfo, verifyCon
|
||||
return parseBaseResponseError(resp)
|
||||
}
|
||||
|
||||
// 撤回消息操作
|
||||
// WebWxRevokeMsg 撤回消息操作
|
||||
func (c *Caller) WebWxRevokeMsg(msg *SentMessage, request *BaseRequest) error {
|
||||
resp, err := c.Client.WebWxRevokeMsg(msg, request)
|
||||
if err != nil {
|
||||
|
56
client.go
56
client.go
@ -18,7 +18,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// 请求上下文钩子
|
||||
// HttpHook 请求上下文钩子
|
||||
type HttpHook interface {
|
||||
BeforeRequest(req *http.Request)
|
||||
AfterRequest(response *http.Response, err error)
|
||||
@ -34,7 +34,7 @@ func (u UserAgentHook) BeforeRequest(req *http.Request) {
|
||||
|
||||
func (u UserAgentHook) AfterRequest(response *http.Response, err error) {}
|
||||
|
||||
// http请求客户端
|
||||
// Client http请求客户端
|
||||
// 客户端需要维持Session会话
|
||||
// 并且客户端不允许跳转
|
||||
type Client struct {
|
||||
@ -50,7 +50,7 @@ func NewClient(client *http.Client) *Client {
|
||||
return &Client{Client: client}
|
||||
}
|
||||
|
||||
// 自动存储cookie
|
||||
// DefaultClient 自动存储cookie
|
||||
// 设置客户端不自动跳转
|
||||
func DefaultClient() *Client {
|
||||
jar, _ := cookiejar.New(nil)
|
||||
@ -91,7 +91,7 @@ func (c *Client) setCookie(resp *http.Response) {
|
||||
c.cookies[path] = cookies
|
||||
}
|
||||
|
||||
// 抽象Do方法,将所有的有效的cookie存入Client.cookies
|
||||
// Do 抽象Do方法,将所有的有效的cookie存入Client.cookies
|
||||
// 方便热登陆时获取
|
||||
func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
||||
resp, err := c.do(req)
|
||||
@ -101,12 +101,12 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// 获取当前client的所有的有效的client
|
||||
// GetCookieMap 获取当前client的所有的有效的client
|
||||
func (c *Client) GetCookieMap() map[string][]*http.Cookie {
|
||||
return c.cookies
|
||||
}
|
||||
|
||||
// 获取登录的uuid
|
||||
// GetLoginUUID 获取登录的uuid
|
||||
func (c *Client) GetLoginUUID() (*http.Response, error) {
|
||||
path, _ := url.Parse(jslogin)
|
||||
params := url.Values{}
|
||||
@ -126,13 +126,13 @@ func (c *Client) GetLoginUUID() (*http.Response, error) {
|
||||
return c.Do(req)
|
||||
}
|
||||
|
||||
// 获取登录的二维吗
|
||||
// GetLoginQrcode 获取登录的二维吗
|
||||
func (c *Client) GetLoginQrcode(uuid string) (*http.Response, error) {
|
||||
path := qrcode + uuid
|
||||
return c.Get(path)
|
||||
}
|
||||
|
||||
// 检查是否登录
|
||||
// CheckLogin 检查是否登录
|
||||
func (c *Client) CheckLogin(uuid string) (*http.Response, error) {
|
||||
path, _ := url.Parse(login)
|
||||
now := time.Now().Unix()
|
||||
@ -157,7 +157,7 @@ func (c *Client) GetLoginInfo(path string) (*http.Response, error) {
|
||||
return c.Do(req)
|
||||
}
|
||||
|
||||
// 请求获取初始化信息
|
||||
// WebInit 请求获取初始化信息
|
||||
func (c *Client) WebInit(request *BaseRequest) (*http.Response, error) {
|
||||
path, _ := url.Parse(c.domain.BaseHost() + webwxinit)
|
||||
params := url.Values{}
|
||||
@ -173,7 +173,7 @@ func (c *Client) WebInit(request *BaseRequest) (*http.Response, error) {
|
||||
return c.Do(req)
|
||||
}
|
||||
|
||||
// 通知手机已登录
|
||||
// WebWxStatusNotify 通知手机已登录
|
||||
func (c *Client) WebWxStatusNotify(request *BaseRequest, response *WebInitResponse, info *LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(c.domain.BaseHost() + webwxstatusnotify)
|
||||
params := url.Values{}
|
||||
@ -194,7 +194,7 @@ func (c *Client) WebWxStatusNotify(request *BaseRequest, response *WebInitRespon
|
||||
return c.Do(req)
|
||||
}
|
||||
|
||||
// 异步检查是否有新的消息返回
|
||||
// SyncCheck 异步检查是否有新的消息返回
|
||||
func (c *Client) SyncCheck(info *LoginInfo, response *WebInitResponse) (*http.Response, error) {
|
||||
path, _ := url.Parse(c.domain.SyncHost() + synccheck)
|
||||
params := url.Values{}
|
||||
@ -217,7 +217,7 @@ func (c *Client) SyncCheck(info *LoginInfo, response *WebInitResponse) (*http.Re
|
||||
return c.Do(req)
|
||||
}
|
||||
|
||||
// 获取联系人信息
|
||||
// WebWxGetContact 获取联系人信息
|
||||
func (c *Client) WebWxGetContact(info *LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(c.domain.BaseHost() + webwxgetcontact)
|
||||
params := url.Values{}
|
||||
@ -229,7 +229,7 @@ func (c *Client) WebWxGetContact(info *LoginInfo) (*http.Response, error) {
|
||||
return c.Do(req)
|
||||
}
|
||||
|
||||
// 获取联系人详情
|
||||
// WebWxBatchGetContact 获取联系人详情
|
||||
func (c *Client) WebWxBatchGetContact(members Members, request *BaseRequest) (*http.Response, error) {
|
||||
path, _ := url.Parse(c.domain.BaseHost() + webwxbatchgetcontact)
|
||||
params := url.Values{}
|
||||
@ -248,7 +248,7 @@ func (c *Client) WebWxBatchGetContact(members Members, request *BaseRequest) (*h
|
||||
return c.Do(req)
|
||||
}
|
||||
|
||||
// 获取消息接口
|
||||
// WebWxSync 获取消息接口
|
||||
func (c *Client) WebWxSync(request *BaseRequest, response *WebInitResponse, info *LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(c.domain.BaseHost() + webwxsync)
|
||||
params := url.Values{}
|
||||
@ -281,7 +281,7 @@ func (c *Client) sendMessage(request *BaseRequest, url string, msg *SendMessage)
|
||||
return c.Do(req)
|
||||
}
|
||||
|
||||
// 发送文本消息
|
||||
// WebWxSendMsg 发送文本消息
|
||||
func (c *Client) WebWxSendMsg(msg *SendMessage, info *LoginInfo, request *BaseRequest) (*http.Response, error) {
|
||||
msg.Type = TextMessage
|
||||
path, _ := url.Parse(c.domain.BaseHost() + webwxsendmsg)
|
||||
@ -292,7 +292,7 @@ func (c *Client) WebWxSendMsg(msg *SendMessage, info *LoginInfo, request *BaseRe
|
||||
return c.sendMessage(request, path.String(), msg)
|
||||
}
|
||||
|
||||
// 获取用户的头像
|
||||
// WebWxGetHeadImg 获取用户的头像
|
||||
func (c *Client) WebWxGetHeadImg(headImageUrl string) (*http.Response, error) {
|
||||
path := c.domain.BaseHost() + headImageUrl
|
||||
req, _ := http.NewRequest(http.MethodGet, path, nil)
|
||||
@ -446,7 +446,7 @@ func (c *Client) WebWxUploadMediaByChunk(file *os.File, request *BaseRequest, in
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// 发送图片
|
||||
// WebWxSendMsgImg 发送图片
|
||||
// 这个接口依赖上传文件的接口
|
||||
// 发送的图片必须是已经成功上传的图片
|
||||
func (c *Client) WebWxSendMsgImg(msg *SendMessage, request *BaseRequest, info *LoginInfo) (*http.Response, error) {
|
||||
@ -461,7 +461,7 @@ func (c *Client) WebWxSendMsgImg(msg *SendMessage, request *BaseRequest, info *L
|
||||
return c.sendMessage(request, path.String(), msg)
|
||||
}
|
||||
|
||||
// 发送文件信息
|
||||
// WebWxSendAppMsg 发送文件信息
|
||||
func (c *Client) WebWxSendAppMsg(msg *SendMessage, request *BaseRequest) (*http.Response, error) {
|
||||
msg.Type = AppMessage
|
||||
path, _ := url.Parse(c.domain.BaseHost() + webwxsendappmsg)
|
||||
@ -472,7 +472,7 @@ func (c *Client) WebWxSendAppMsg(msg *SendMessage, request *BaseRequest) (*http.
|
||||
return c.sendMessage(request, path.String(), msg)
|
||||
}
|
||||
|
||||
// 用户重命名接口
|
||||
// WebWxOplog 用户重命名接口
|
||||
func (c *Client) WebWxOplog(request *BaseRequest, remarkName, userName string) (*http.Response, error) {
|
||||
path, _ := url.Parse(c.domain.BaseHost() + webwxoplog)
|
||||
params := url.Values{}
|
||||
@ -490,7 +490,7 @@ func (c *Client) WebWxOplog(request *BaseRequest, remarkName, userName string) (
|
||||
return c.Do(req)
|
||||
}
|
||||
|
||||
// 添加用户为好友接口
|
||||
// WebWxVerifyUser 添加用户为好友接口
|
||||
func (c *Client) WebWxVerifyUser(storage *Storage, info RecommendInfo, verifyContent string) (*http.Response, error) {
|
||||
loginInfo := storage.LoginInfo
|
||||
path, _ := url.Parse(c.domain.BaseHost() + webwxverifyuser)
|
||||
@ -518,7 +518,7 @@ func (c *Client) WebWxVerifyUser(storage *Storage, info RecommendInfo, verifyCon
|
||||
return c.Do(req)
|
||||
}
|
||||
|
||||
// 获取图片消息的图片响应
|
||||
// WebWxGetMsgImg 获取图片消息的图片响应
|
||||
func (c *Client) WebWxGetMsgImg(msg *Message, info *LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(c.domain.BaseHost() + webwxgetmsgimg)
|
||||
params := url.Values{}
|
||||
@ -530,7 +530,7 @@ func (c *Client) WebWxGetMsgImg(msg *Message, info *LoginInfo) (*http.Response,
|
||||
return c.Do(req)
|
||||
}
|
||||
|
||||
// 获取语音消息的语音响应
|
||||
// WebWxGetVoice 获取语音消息的语音响应
|
||||
func (c *Client) WebWxGetVoice(msg *Message, info *LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(c.domain.BaseHost() + webwxgetvoice)
|
||||
params := url.Values{}
|
||||
@ -541,7 +541,7 @@ func (c *Client) WebWxGetVoice(msg *Message, info *LoginInfo) (*http.Response, e
|
||||
return c.Do(req)
|
||||
}
|
||||
|
||||
// 获取视频消息的视频响应
|
||||
// WebWxGetVideo 获取视频消息的视频响应
|
||||
func (c *Client) WebWxGetVideo(msg *Message, info *LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(c.domain.BaseHost() + webwxgetvideo)
|
||||
params := url.Values{}
|
||||
@ -552,7 +552,7 @@ func (c *Client) WebWxGetVideo(msg *Message, info *LoginInfo) (*http.Response, e
|
||||
return c.Do(req)
|
||||
}
|
||||
|
||||
// 获取文件消息的文件响应
|
||||
// WebWxGetMedia 获取文件消息的文件响应
|
||||
func (c *Client) WebWxGetMedia(msg *Message, info *LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(c.domain.FileHost() + webwxgetmedia)
|
||||
params := url.Values{}
|
||||
@ -567,7 +567,7 @@ func (c *Client) WebWxGetMedia(msg *Message, info *LoginInfo) (*http.Response, e
|
||||
return c.Do(req)
|
||||
}
|
||||
|
||||
// 用户退出
|
||||
// Logout 用户退出
|
||||
func (c *Client) Logout(info *LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(c.domain.BaseHost() + webwxlogout)
|
||||
params := url.Values{}
|
||||
@ -579,7 +579,7 @@ func (c *Client) Logout(info *LoginInfo) (*http.Response, error) {
|
||||
return c.Do(req)
|
||||
}
|
||||
|
||||
// 添加用户进群聊
|
||||
// AddMemberIntoChatRoom 添加用户进群聊
|
||||
func (c *Client) AddMemberIntoChatRoom(req *BaseRequest, info *LoginInfo, group *Group, friends ...*Friend) (*http.Response, error) {
|
||||
path, _ := url.Parse(c.domain.BaseHost() + webwxupdatechatroom)
|
||||
params := url.Values{}
|
||||
@ -602,7 +602,7 @@ func (c *Client) AddMemberIntoChatRoom(req *BaseRequest, info *LoginInfo, group
|
||||
return c.Do(requ)
|
||||
}
|
||||
|
||||
// 从群聊中移除用户
|
||||
// RemoveMemberFromChatRoom 从群聊中移除用户
|
||||
func (c *Client) RemoveMemberFromChatRoom(req *BaseRequest, info *LoginInfo, group *Group, friends ...*User) (*http.Response, error) {
|
||||
path, _ := url.Parse(c.domain.BaseHost() + webwxupdatechatroom)
|
||||
params := url.Values{}
|
||||
@ -624,7 +624,7 @@ func (c *Client) RemoveMemberFromChatRoom(req *BaseRequest, info *LoginInfo, gro
|
||||
return c.Do(requ)
|
||||
}
|
||||
|
||||
// 撤回消息
|
||||
// WebWxRevokeMsg 撤回消息
|
||||
func (c *Client) WebWxRevokeMsg(msg *SentMessage, request *BaseRequest) (*http.Response, error) {
|
||||
content := map[string]interface{}{
|
||||
"BaseRequest": request,
|
||||
|
@ -77,7 +77,7 @@ var (
|
||||
loginForbiddenError = errors.New("login forbidden")
|
||||
)
|
||||
|
||||
// ALL跟search函数搭配
|
||||
// ALL 跟search函数搭配
|
||||
// friends.Search(openwechat.ALL, )
|
||||
const ALL = 0
|
||||
|
||||
|
10
items.go
10
items.go
@ -9,7 +9,7 @@ import (
|
||||
一些网络返回信息的封装
|
||||
*/
|
||||
|
||||
// 登录信息
|
||||
// LoginInfo 登录信息
|
||||
type LoginInfo struct {
|
||||
Ret int `xml:"ret"`
|
||||
WxUin int `xml:"wxuin"`
|
||||
@ -28,14 +28,14 @@ func (l LoginInfo) Error() string {
|
||||
return l.Message
|
||||
}
|
||||
|
||||
// 初始的请求信息
|
||||
// BaseRequest 初始的请求信息
|
||||
// 几乎所有的请求都要携带该参数
|
||||
type BaseRequest struct {
|
||||
Uin int
|
||||
Sid, Skey, DeviceID string
|
||||
}
|
||||
|
||||
// 大部分返回对象都携带该信息
|
||||
// BaseResponse 大部分返回对象都携带该信息
|
||||
type BaseResponse struct {
|
||||
Ret int
|
||||
ErrMsg string
|
||||
@ -80,7 +80,7 @@ type SyncKey struct {
|
||||
List []struct{ Key, Val int64 }
|
||||
}
|
||||
|
||||
// 初始化的相应信息
|
||||
// WebInitResponse 初始化的相应信息
|
||||
type WebInitResponse struct {
|
||||
Count int
|
||||
ClientVersion int
|
||||
@ -98,7 +98,7 @@ type WebInitResponse struct {
|
||||
ContactList []User
|
||||
}
|
||||
|
||||
// 公众号的订阅信息
|
||||
// MPSubscribeMsg 公众号的订阅信息
|
||||
type MPSubscribeMsg struct {
|
||||
MPArticleCount int
|
||||
Time int64
|
||||
|
70
message.go
70
message.go
@ -56,7 +56,7 @@ type Message struct {
|
||||
item map[string]interface{}
|
||||
}
|
||||
|
||||
// 获取消息的发送者
|
||||
// Sender 获取消息的发送者
|
||||
func (m *Message) Sender() (*User, error) {
|
||||
if m.FromUserName == m.Bot.self.User.UserName {
|
||||
return m.Bot.self.User, nil
|
||||
@ -65,7 +65,7 @@ func (m *Message) Sender() (*User, error) {
|
||||
return user.Detail()
|
||||
}
|
||||
|
||||
// 获取消息在群里面的发送者
|
||||
// SenderInGroup 获取消息在群里面的发送者
|
||||
func (m *Message) SenderInGroup() (*User, error) {
|
||||
if !m.IsSendByGroup() {
|
||||
return nil, errors.New("message is not from group")
|
||||
@ -85,7 +85,7 @@ func (m *Message) SenderInGroup() (*User, error) {
|
||||
return users.First(), nil
|
||||
}
|
||||
|
||||
// 获取消息的接收者
|
||||
// Receiver 获取消息的接收者
|
||||
func (m *Message) Receiver() (*User, error) {
|
||||
if m.IsSendByGroup() {
|
||||
if sender, err := m.Sender(); err != nil {
|
||||
@ -106,22 +106,22 @@ func (m *Message) Receiver() (*User, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// 判断消息是否由自己发送
|
||||
// IsSendBySelf 判断消息是否由自己发送
|
||||
func (m *Message) IsSendBySelf() bool {
|
||||
return m.FromUserName == m.Bot.self.User.UserName
|
||||
}
|
||||
|
||||
// 判断消息是否由好友发送
|
||||
// IsSendByFriend 判断消息是否由好友发送
|
||||
func (m *Message) IsSendByFriend() bool {
|
||||
return !m.IsSendByGroup() && strings.HasPrefix(m.FromUserName, "@") && !m.IsSendBySelf()
|
||||
}
|
||||
|
||||
// 判断消息是否由群组发送
|
||||
// IsSendByGroup 判断消息是否由群组发送
|
||||
func (m *Message) IsSendByGroup() bool {
|
||||
return strings.HasPrefix(m.FromUserName, "@@")
|
||||
}
|
||||
|
||||
// 回复消息
|
||||
// Reply 回复消息
|
||||
func (m *Message) Reply(msgType int, content, mediaId string) (*SentMessage, error) {
|
||||
msg := NewSendMessage(msgType, content, m.Bot.self.User.UserName, m.FromUserName, mediaId)
|
||||
info := m.Bot.storage.LoginInfo
|
||||
@ -129,19 +129,19 @@ func (m *Message) Reply(msgType int, content, mediaId string) (*SentMessage, err
|
||||
return m.Bot.Caller.WebWxSendMsg(msg, info, request)
|
||||
}
|
||||
|
||||
// 回复文本消息
|
||||
// ReplyText 回复文本消息
|
||||
func (m *Message) ReplyText(content string) (*SentMessage, error) {
|
||||
return m.Reply(TextMessage, content, "")
|
||||
}
|
||||
|
||||
// 回复图片消息
|
||||
// ReplyImage 回复图片消息
|
||||
func (m *Message) ReplyImage(file *os.File) (*SentMessage, error) {
|
||||
info := m.Bot.storage.LoginInfo
|
||||
request := m.Bot.storage.Request
|
||||
return m.Bot.Caller.WebWxSendImageMsg(file, request, info, m.Bot.self.UserName, m.FromUserName)
|
||||
}
|
||||
|
||||
// 回复文件消息
|
||||
// ReplyFile 回复文件消息
|
||||
func (m *Message) ReplyFile(file *os.File) (*SentMessage, error) {
|
||||
info := m.Bot.storage.LoginInfo
|
||||
request := m.Bot.storage.Request
|
||||
@ -180,7 +180,7 @@ func (m *Message) IsMedia() bool {
|
||||
return m.MsgType == 49
|
||||
}
|
||||
|
||||
// 判断是否撤回
|
||||
// IsRecalled 判断是否撤回
|
||||
func (m *Message) IsRecalled() bool {
|
||||
return m.MsgType == 10002
|
||||
}
|
||||
@ -193,17 +193,17 @@ func (m *Message) IsNotify() bool {
|
||||
return m.MsgType == 51 && m.StatusNotifyCode != 0
|
||||
}
|
||||
|
||||
// 判断当前的消息是不是微信转账
|
||||
// IsTransferAccounts 判断当前的消息是不是微信转账
|
||||
func (m *Message) IsTransferAccounts() bool {
|
||||
return m.IsMedia() && m.FileName == "微信转账"
|
||||
}
|
||||
|
||||
// 否发出红包判断当前是
|
||||
// IsSendRedPacket 否发出红包判断当前是
|
||||
func (m *Message) IsSendRedPacket() bool {
|
||||
return m.IsSystem() && m.Content == "发出红包,请在手机上查看"
|
||||
}
|
||||
|
||||
// 判断当前是否收到红包
|
||||
// IsReceiveRedPacket 判断当前是否收到红包
|
||||
func (m *Message) IsReceiveRedPacket() bool {
|
||||
return m.IsSystem() && m.Content == "收到红包,请在手机上查看"
|
||||
}
|
||||
@ -212,17 +212,17 @@ func (m *Message) IsSysNotice() bool {
|
||||
return m.MsgType == 9999
|
||||
}
|
||||
|
||||
// 判断是否为操作通知消息
|
||||
// StatusNotify 判断是否为操作通知消息
|
||||
func (m *Message) StatusNotify() bool {
|
||||
return m.MsgType == 51
|
||||
}
|
||||
|
||||
// 判断消息是否为文件类型的消息
|
||||
// HasFile 判断消息是否为文件类型的消息
|
||||
func (m *Message) HasFile() bool {
|
||||
return m.IsPicture() || m.IsVoice() || m.IsVideo() || m.IsMedia()
|
||||
}
|
||||
|
||||
// 获取文件消息的文件
|
||||
// GetFile 获取文件消息的文件
|
||||
func (m *Message) GetFile() (*http.Response, error) {
|
||||
if !m.HasFile() {
|
||||
return nil, errors.New("invalid message type")
|
||||
@ -242,7 +242,7 @@ func (m *Message) GetFile() (*http.Response, error) {
|
||||
return nil, errors.New("unsupported type")
|
||||
}
|
||||
|
||||
// 获取card类型
|
||||
// Card 获取card类型
|
||||
func (m *Message) Card() (*Card, error) {
|
||||
if !m.IsCard() {
|
||||
return nil, errors.New("card message required")
|
||||
@ -253,7 +253,7 @@ func (m *Message) Card() (*Card, error) {
|
||||
return &card, err
|
||||
}
|
||||
|
||||
// 获取FriendAddMessageContent内容
|
||||
// FriendAddMessageContent 获取FriendAddMessageContent内容
|
||||
func (m *Message) FriendAddMessageContent() (*FriendAddMessage, error) {
|
||||
if !m.IsFriendAdd() {
|
||||
return nil, errors.New("friend add message required")
|
||||
@ -264,7 +264,7 @@ func (m *Message) FriendAddMessageContent() (*FriendAddMessage, error) {
|
||||
return &f, err
|
||||
}
|
||||
|
||||
// 获取撤回消息的内容
|
||||
// RevokeMsg 获取撤回消息的内容
|
||||
func (m *Message) RevokeMsg() (*RevokeMsg, error) {
|
||||
if !m.IsRecalled() {
|
||||
return nil, errors.New("recalled message required")
|
||||
@ -275,7 +275,7 @@ func (m *Message) RevokeMsg() (*RevokeMsg, error) {
|
||||
return &r, err
|
||||
}
|
||||
|
||||
// 同意好友的请求
|
||||
// Agree 同意好友的请求
|
||||
func (m *Message) Agree(verifyContents ...string) error {
|
||||
if !m.IsFriendAdd() {
|
||||
return fmt.Errorf("friend add message required")
|
||||
@ -287,7 +287,7 @@ func (m *Message) Agree(verifyContents ...string) error {
|
||||
return m.Bot.Caller.WebWxVerifyUser(m.Bot.storage, m.RecommendInfo, builder.String())
|
||||
}
|
||||
|
||||
// 往消息上下文中设置值
|
||||
// Set 往消息上下文中设置值
|
||||
// goroutine safe
|
||||
func (m *Message) Set(key string, value interface{}) {
|
||||
m.mu.Lock()
|
||||
@ -298,7 +298,7 @@ func (m *Message) Set(key string, value interface{}) {
|
||||
m.item[key] = value
|
||||
}
|
||||
|
||||
// 从消息上下文中获取值
|
||||
// Get 从消息上下文中获取值
|
||||
// goroutine safe
|
||||
func (m *Message) Get(key string) (value interface{}, exist bool) {
|
||||
m.mu.RLock()
|
||||
@ -346,7 +346,7 @@ func (m *Message) init(bot *Bot) {
|
||||
}
|
||||
}
|
||||
|
||||
// 发送消息的结构体
|
||||
// SendMessage 发送消息的结构体
|
||||
type SendMessage struct {
|
||||
Type int
|
||||
Content string
|
||||
@ -357,7 +357,7 @@ type SendMessage struct {
|
||||
MediaId string `json:"MediaId,omitempty"`
|
||||
}
|
||||
|
||||
// SendMessage的构造方法
|
||||
// NewSendMessage SendMessage的构造方法
|
||||
func NewSendMessage(msgType int, content, fromUserName, toUserName, mediaId string) *SendMessage {
|
||||
id := strconv.FormatInt(time.Now().UnixNano()/1e2, 10)
|
||||
return &SendMessage{
|
||||
@ -371,17 +371,17 @@ func NewSendMessage(msgType int, content, fromUserName, toUserName, mediaId stri
|
||||
}
|
||||
}
|
||||
|
||||
// 文本消息的构造方法
|
||||
// NewTextSendMessage 文本消息的构造方法
|
||||
func NewTextSendMessage(content, fromUserName, toUserName string) *SendMessage {
|
||||
return NewSendMessage(TextMessage, content, fromUserName, toUserName, "")
|
||||
}
|
||||
|
||||
// 媒体消息的构造方法
|
||||
// NewMediaSendMessage 媒体消息的构造方法
|
||||
func NewMediaSendMessage(msgType int, fromUserName, toUserName, mediaId string) *SendMessage {
|
||||
return NewSendMessage(msgType, "", fromUserName, toUserName, mediaId)
|
||||
}
|
||||
|
||||
// 一些特殊类型的消息会携带该结构体信息
|
||||
// RecommendInfo 一些特殊类型的消息会携带该结构体信息
|
||||
type RecommendInfo struct {
|
||||
OpCode int
|
||||
Scene int
|
||||
@ -399,7 +399,7 @@ type RecommendInfo struct {
|
||||
UserName string
|
||||
}
|
||||
|
||||
// 名片消息内容
|
||||
// Card 名片消息内容
|
||||
type Card struct {
|
||||
XMLName xml.Name `xml:"msg"`
|
||||
ImageStatus int `xml:"imagestatus,attr"`
|
||||
@ -423,7 +423,7 @@ type Card struct {
|
||||
RegionCode string `xml:"regionCode,attr"`
|
||||
}
|
||||
|
||||
// 好友添加消息信息内容
|
||||
// FriendAddMessage 好友添加消息信息内容
|
||||
type FriendAddMessage struct {
|
||||
XMLName xml.Name `xml:"msg"`
|
||||
Shortpy int `xml:"shortpy,attr"`
|
||||
@ -466,7 +466,7 @@ type FriendAddMessage struct {
|
||||
} `xml:"brandlist"`
|
||||
}
|
||||
|
||||
// 撤回消息Content
|
||||
// RevokeMsg 撤回消息Content
|
||||
type RevokeMsg struct {
|
||||
SysMsg xml.Name `xml:"sysmsg"`
|
||||
Type string `xml:"type,attr"`
|
||||
@ -478,24 +478,24 @@ type RevokeMsg struct {
|
||||
} `xml:"revokemsg"`
|
||||
}
|
||||
|
||||
// 已发送的信息
|
||||
// SentMessage 已发送的信息
|
||||
type SentMessage struct {
|
||||
*SendMessage
|
||||
Self *Self
|
||||
MsgId string
|
||||
}
|
||||
|
||||
// 撤回该消息
|
||||
// Revoke 撤回该消息
|
||||
func (s *SentMessage) Revoke() error {
|
||||
return s.Self.RevokeMessage(s)
|
||||
}
|
||||
|
||||
// 转发该消息给好友
|
||||
// ForwardToFriends 转发该消息给好友
|
||||
func (s *SentMessage) ForwardToFriends(friends ...*Friend) error {
|
||||
return s.Self.ForwardMessageToFriends(s, friends...)
|
||||
}
|
||||
|
||||
// 转发该消息给群组
|
||||
// ForwardToGroups 转发该消息给群组
|
||||
func (s *SentMessage) ForwardToGroups(groups ...*Group) error {
|
||||
return s.Self.ForwardMessageToGroups(s, groups...)
|
||||
}
|
||||
|
@ -1,22 +1,22 @@
|
||||
package openwechat
|
||||
|
||||
// 消息处理函数
|
||||
// MessageHandler 消息处理函数
|
||||
type MessageHandler func(msg *Message)
|
||||
|
||||
// 消息分发处理接口
|
||||
// MessageDispatcher 消息分发处理接口
|
||||
// 跟 DispatchMessage 结合封装成 MessageHandler
|
||||
type MessageDispatcher interface {
|
||||
Dispatch(msg *Message)
|
||||
}
|
||||
|
||||
// 跟 MessageDispatcher 结合封装成 MessageHandler
|
||||
// DispatchMessage 跟 MessageDispatcher 结合封装成 MessageHandler
|
||||
func DispatchMessage(dispatcher MessageDispatcher) func(msg *Message) {
|
||||
return func(msg *Message) { dispatcher.Dispatch(msg) }
|
||||
}
|
||||
|
||||
// MessageDispatcher impl
|
||||
|
||||
// MessageMatchDispatcher 消息处理函数
|
||||
// MessageContextHandler 消息处理函数
|
||||
type MessageContextHandler func(ctx *MessageContext)
|
||||
|
||||
type MessageContextHandlerGroup []MessageContextHandler
|
||||
@ -28,7 +28,7 @@ type MessageContext struct {
|
||||
*Message
|
||||
}
|
||||
|
||||
// 主动调用下一个消息处理函数(或开始调用)
|
||||
// Next 主动调用下一个消息处理函数(或开始调用)
|
||||
func (c *MessageContext) Next() {
|
||||
c.index++
|
||||
for c.index <= len(c.messageHandlers) {
|
||||
@ -60,12 +60,12 @@ type MessageMatchDispatcher struct {
|
||||
matchNodes matchNodes
|
||||
}
|
||||
|
||||
// Constructor for MessageMatchDispatcher
|
||||
// NewMessageMatchDispatcher Constructor
|
||||
func NewMessageMatchDispatcher() *MessageMatchDispatcher {
|
||||
return &MessageMatchDispatcher{}
|
||||
}
|
||||
|
||||
// 设置是否异步处理
|
||||
// SetAsync 设置是否异步处理
|
||||
func (m *MessageMatchDispatcher) SetAsync(async bool) {
|
||||
m.async = async
|
||||
}
|
||||
@ -93,7 +93,7 @@ func (m *MessageMatchDispatcher) do(ctx *MessageContext) {
|
||||
ctx.Next()
|
||||
}
|
||||
|
||||
// 注册消息处理函数, 根据自己的需求自定义
|
||||
// RegisterHandler 注册消息处理函数, 根据自己的需求自定义
|
||||
// matchFunc返回true则表示处理对应的handlers
|
||||
func (m *MessageMatchDispatcher) RegisterHandler(matchFunc matchFunc, handlers ...MessageContextHandler) {
|
||||
if matchFunc == nil {
|
||||
@ -103,32 +103,32 @@ func (m *MessageMatchDispatcher) RegisterHandler(matchFunc matchFunc, handlers .
|
||||
m.matchNodes = append(m.matchNodes, node)
|
||||
}
|
||||
|
||||
// 注册处理消息类型为Text的处理函数
|
||||
// OnText 注册处理消息类型为Text的处理函数
|
||||
func (m *MessageMatchDispatcher) OnText(handlers ...MessageContextHandler) {
|
||||
m.RegisterHandler(func(message *Message) bool { return message.IsText() }, handlers...)
|
||||
}
|
||||
|
||||
// 注册处理消息类型为Image的处理函数
|
||||
// OnImage 注册处理消息类型为Image的处理函数
|
||||
func (m *MessageMatchDispatcher) OnImage(handlers ...MessageContextHandler) {
|
||||
m.RegisterHandler(func(message *Message) bool { return message.IsPicture() }, handlers...)
|
||||
}
|
||||
|
||||
// 注册处理消息类型为Voice的处理函数
|
||||
// OnVoice 注册处理消息类型为Voice的处理函数
|
||||
func (m *MessageMatchDispatcher) OnVoice(handlers ...MessageContextHandler) {
|
||||
m.RegisterHandler(func(message *Message) bool { return message.IsVoice() }, handlers...)
|
||||
}
|
||||
|
||||
// 注册处理消息类型为FriendAdd的处理函数
|
||||
// OnFriendAdd 注册处理消息类型为FriendAdd的处理函数
|
||||
func (m *MessageMatchDispatcher) OnFriendAdd(handlers ...MessageContextHandler) {
|
||||
m.RegisterHandler(func(message *Message) bool { return message.IsFriendAdd() }, handlers...)
|
||||
}
|
||||
|
||||
// 注册处理消息类型为Card的处理函数
|
||||
// OnCard 注册处理消息类型为Card的处理函数
|
||||
func (m *MessageMatchDispatcher) OnCard(handlers ...MessageContextHandler) {
|
||||
m.RegisterHandler(func(message *Message) bool { return message.IsCard() }, handlers...)
|
||||
}
|
||||
|
||||
// 注册根据好友昵称是否匹配的消息处理函数
|
||||
// OnFriendByNickName 注册根据好友昵称是否匹配的消息处理函数
|
||||
func (m *MessageMatchDispatcher) OnFriendByNickName(nickName string, handlers ...MessageContextHandler) {
|
||||
matchFunc := func(message *Message) bool {
|
||||
if message.IsSendByFriend() {
|
||||
@ -140,17 +140,17 @@ func (m *MessageMatchDispatcher) OnFriendByNickName(nickName string, handlers ..
|
||||
m.RegisterHandler(matchFunc, handlers...)
|
||||
}
|
||||
|
||||
// 注册发送者为好友的处理函数
|
||||
// OnFriend 注册发送者为好友的处理函数
|
||||
func (m *MessageMatchDispatcher) OnFriend(handlers ...MessageContextHandler) {
|
||||
m.RegisterHandler(func(message *Message) bool { return message.IsSendByFriend() }, handlers...)
|
||||
}
|
||||
|
||||
// 注册发送者为群组的处理函数
|
||||
// OnGroup 注册发送者为群组的处理函数
|
||||
func (m *MessageMatchDispatcher) OnGroup(handlers ...MessageContextHandler) {
|
||||
m.RegisterHandler(func(message *Message) bool { return message.IsSendByGroup() }, handlers...)
|
||||
}
|
||||
|
||||
// 注册根据消息发送者的行为是否匹配的消息处理函数
|
||||
// OnUser 注册根据消息发送者的行为是否匹配的消息处理函数
|
||||
func (m *MessageMatchDispatcher) OnUser(f func(user *User) bool, handlers ...MessageContextHandler) {
|
||||
mf := func(message *Message) bool {
|
||||
sender, err := message.Sender()
|
||||
@ -162,7 +162,7 @@ func (m *MessageMatchDispatcher) OnUser(f func(user *User) bool, handlers ...Mes
|
||||
m.RegisterHandler(mf, handlers...)
|
||||
}
|
||||
|
||||
// 注册根据好友备注是否匹配的消息处理函数
|
||||
// OnFriendByRemarkName 注册根据好友备注是否匹配的消息处理函数
|
||||
func (m *MessageMatchDispatcher) OnFriendByRemarkName(remarkName string, handlers ...MessageContextHandler) {
|
||||
f := func(user *User) bool {
|
||||
return user.IsFriend() && user.RemarkName == remarkName
|
||||
@ -170,7 +170,7 @@ func (m *MessageMatchDispatcher) OnFriendByRemarkName(remarkName string, handler
|
||||
m.OnUser(f, handlers...)
|
||||
}
|
||||
|
||||
// 注册根据群名是否匹配的消息处理函数
|
||||
// OnGroupByGroupName 注册根据群名是否匹配的消息处理函数
|
||||
func (m *MessageMatchDispatcher) OnGroupByGroupName(groupName string, handlers ...MessageContextHandler) {
|
||||
f := func(user *User) bool {
|
||||
return user.IsGroup() && user.NickName == groupName
|
||||
|
@ -21,7 +21,7 @@ func ToBuffer(v interface{}) (*bytes.Buffer, error) {
|
||||
return &buffer, err
|
||||
}
|
||||
|
||||
// 获取随机设备id
|
||||
// GetRandomDeviceId 获取随机设备id
|
||||
func GetRandomDeviceId() string {
|
||||
rand.Seed(time.Now().Unix())
|
||||
var builder strings.Builder
|
||||
@ -42,7 +42,7 @@ func getWebWxDataTicket(cookies []*http.Cookie) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Form Xml 格式化
|
||||
// XmlFormString Form Xml 格式化
|
||||
func XmlFormString(text string) string {
|
||||
lt := strings.ReplaceAll(text, "<", "<")
|
||||
gt := strings.ReplaceAll(lt, ">", ">")
|
||||
@ -58,7 +58,7 @@ func getTotalDuration(delay ...time.Duration) time.Duration {
|
||||
return total
|
||||
}
|
||||
|
||||
// 获取文件上传的类型
|
||||
// GetFileContentType 获取文件上传的类型
|
||||
func GetFileContentType(file multipart.File) (string, error) {
|
||||
data := make([]byte, 512)
|
||||
if _, err := file.Read(data); err != nil {
|
||||
|
80
relations.go
80
relations.go
@ -13,44 +13,44 @@ func (f Friend) String() string {
|
||||
return fmt.Sprintf("<Friend:%s>", f.NickName)
|
||||
}
|
||||
|
||||
// 重命名当前好友
|
||||
// SetRemarkName 重命名当前好友
|
||||
func (f *Friend) SetRemarkName(name string) error {
|
||||
return f.Self.SetRemarkNameToFriend(f, name)
|
||||
}
|
||||
|
||||
// 发送自定义消息
|
||||
// SendMsg 发送自定义消息
|
||||
func (f *Friend) SendMsg(msg *SendMessage) (*SentMessage, error) {
|
||||
return f.Self.SendMessageToFriend(f, msg)
|
||||
}
|
||||
|
||||
// 发送文本消息
|
||||
// SendText 发送文本消息
|
||||
func (f *Friend) SendText(content string) (*SentMessage, error) {
|
||||
return f.Self.SendTextToFriend(f, content)
|
||||
}
|
||||
|
||||
// 发送图片消息
|
||||
// SendImage 发送图片消息
|
||||
func (f *Friend) SendImage(file *os.File) (*SentMessage, error) {
|
||||
return f.Self.SendImageToFriend(f, file)
|
||||
}
|
||||
|
||||
// 发送文件消息
|
||||
// SendFile 发送文件消息
|
||||
func (f *Friend) SendFile(file *os.File) (*SentMessage, error) {
|
||||
return f.Self.SendFileToFriend(f, file)
|
||||
}
|
||||
|
||||
// 拉该好友入群
|
||||
// AddIntoGroup 拉该好友入群
|
||||
func (f *Friend) AddIntoGroup(groups ...*Group) error {
|
||||
return f.Self.AddFriendIntoManyGroups(f, groups...)
|
||||
}
|
||||
|
||||
type Friends []*Friend
|
||||
|
||||
// 获取好友的数量
|
||||
// Count 获取好友的数量
|
||||
func (f Friends) Count() int {
|
||||
return len(f)
|
||||
}
|
||||
|
||||
// 获取第一个好友
|
||||
// First 获取第一个好友
|
||||
func (f Friends) First() *Friend {
|
||||
if f.Count() > 0 {
|
||||
return f[0]
|
||||
@ -58,7 +58,7 @@ func (f Friends) First() *Friend {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取最后一个好友
|
||||
// Last 获取最后一个好友
|
||||
func (f Friends) Last() *Friend {
|
||||
if f.Count() > 0 {
|
||||
return f[f.Count()-1]
|
||||
@ -66,22 +66,22 @@ func (f Friends) Last() *Friend {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 根据用户名查找好友
|
||||
// SearchByUserName 根据用户名查找好友
|
||||
func (f Friends) SearchByUserName(limit int, username string) (results Friends) {
|
||||
return f.Search(limit, func(friend *Friend) bool { return friend.User.UserName == username })
|
||||
}
|
||||
|
||||
// 根据昵称查找好友
|
||||
// SearchByNickName 根据昵称查找好友
|
||||
func (f Friends) SearchByNickName(limit int, nickName string) (results Friends) {
|
||||
return f.Search(limit, func(friend *Friend) bool { return friend.User.NickName == nickName })
|
||||
}
|
||||
|
||||
// 根据备注查找好友
|
||||
// SearchByRemarkName 根据备注查找好友
|
||||
func (f Friends) SearchByRemarkName(limit int, remarkName string) (results Friends) {
|
||||
return f.Search(limit, func(friend *Friend) bool { return friend.User.RemarkName == remarkName })
|
||||
}
|
||||
|
||||
// 根据自定义条件查找好友
|
||||
// Search 根据自定义条件查找好友
|
||||
func (f Friends) Search(limit int, condFuncList ...func(friend *Friend) bool) (results Friends) {
|
||||
if condFuncList == nil {
|
||||
return f
|
||||
@ -106,7 +106,7 @@ func (f Friends) Search(limit int, condFuncList ...func(friend *Friend) bool) (r
|
||||
return
|
||||
}
|
||||
|
||||
// 向slice的好友依次发送消息
|
||||
// SendMsg 向slice的好友依次发送消息
|
||||
func (f Friends) SendMsg(msg *SendMessage, delay ...time.Duration) error {
|
||||
total := getTotalDuration(delay...)
|
||||
var (
|
||||
@ -128,7 +128,7 @@ func (f Friends) SendMsg(msg *SendMessage, delay ...time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 向slice的好友依次发送文本消息
|
||||
// SendText 向slice的好友依次发送文本消息
|
||||
func (f Friends) SendText(text string, delay ...time.Duration) error {
|
||||
total := getTotalDuration(delay...)
|
||||
var (
|
||||
@ -150,7 +150,7 @@ func (f Friends) SendText(text string, delay ...time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 向slice的好友依次发送图片消息
|
||||
// SendImage 向slice的好友依次发送图片消息
|
||||
func (f Friends) SendImage(file *os.File, delay ...time.Duration) error {
|
||||
total := getTotalDuration(delay...)
|
||||
var (
|
||||
@ -172,7 +172,7 @@ func (f Friends) SendImage(file *os.File, delay ...time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 群发文件
|
||||
// SendFile 群发文件
|
||||
func (f Friends)SendFile(file *os.File, delay ...time.Duration) error {
|
||||
total := getTotalDuration(delay...)
|
||||
var (
|
||||
@ -201,27 +201,27 @@ func (g Group) String() string {
|
||||
return fmt.Sprintf("<Group:%s>", g.NickName)
|
||||
}
|
||||
|
||||
// 发行消息给当前的群组
|
||||
// SendMsg 发行消息给当前的群组
|
||||
func (g *Group) SendMsg(msg *SendMessage) (*SentMessage, error) {
|
||||
return g.Self.SendMessageToGroup(g, msg)
|
||||
}
|
||||
|
||||
// 发行文本消息给当前的群组
|
||||
// SendText 发行文本消息给当前的群组
|
||||
func (g *Group) SendText(content string) (*SentMessage, error) {
|
||||
return g.Self.SendTextToGroup(g, content)
|
||||
}
|
||||
|
||||
// 发行图片消息给当前的群组
|
||||
// SendImage 发行图片消息给当前的群组
|
||||
func (g *Group) SendImage(file *os.File) (*SentMessage, error) {
|
||||
return g.Self.SendImageToGroup(g, file)
|
||||
}
|
||||
|
||||
// 发送文件给当前的群组
|
||||
// SendFile 发送文件给当前的群组
|
||||
func (g *Group) SendFile(file *os.File) (*SentMessage, error) {
|
||||
return g.Self.SendFileToGroup(g, file)
|
||||
}
|
||||
|
||||
// 获取所有的群成员
|
||||
// Members 获取所有的群成员
|
||||
func (g *Group) Members() (Members, error) {
|
||||
group, err := g.Detail()
|
||||
if err != nil {
|
||||
@ -230,12 +230,12 @@ func (g *Group) Members() (Members, error) {
|
||||
return group.MemberList, nil
|
||||
}
|
||||
|
||||
// 拉好友入群
|
||||
// AddFriendsIn 拉好友入群
|
||||
func (g *Group) AddFriendsIn(friends ...*Friend) error {
|
||||
return g.Self.AddFriendsIntoGroup(g, friends...)
|
||||
}
|
||||
|
||||
// 从群聊中移除用户
|
||||
// RemoveMembers 从群聊中移除用户
|
||||
// Deprecated
|
||||
// 无论是网页版,还是程序上都不起作用
|
||||
func (g *Group) RemoveMembers(members Members) error {
|
||||
@ -244,12 +244,12 @@ func (g *Group) RemoveMembers(members Members) error {
|
||||
|
||||
type Groups []*Group
|
||||
|
||||
// 获取群组数量
|
||||
// Count 获取群组数量
|
||||
func (g Groups) Count() int {
|
||||
return len(g)
|
||||
}
|
||||
|
||||
// 获取第一个群组
|
||||
// First 获取第一个群组
|
||||
func (g Groups) First() *Group {
|
||||
if g.Count() > 0 {
|
||||
return g[0]
|
||||
@ -257,7 +257,7 @@ func (g Groups) First() *Group {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取最后一个群组
|
||||
// Last 获取最后一个群组
|
||||
func (g Groups) Last() *Group {
|
||||
if g.Count() > 0 {
|
||||
return g[g.Count()-1]
|
||||
@ -265,7 +265,7 @@ func (g Groups) Last() *Group {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 向群组依次发送消息, 支持发送延迟
|
||||
// SendMsg 向群组依次发送消息, 支持发送延迟
|
||||
func (g Groups) SendMsg(msg *SendMessage, delay ...time.Duration) error {
|
||||
total := getTotalDuration(delay...)
|
||||
var (
|
||||
@ -287,7 +287,7 @@ func (g Groups) SendMsg(msg *SendMessage, delay ...time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 向群组依次发送文本消息, 支持发送延迟
|
||||
// SendText 向群组依次发送文本消息, 支持发送延迟
|
||||
func (g Groups) SendText(text string, delay ...time.Duration) error {
|
||||
total := getTotalDuration(delay...)
|
||||
var (
|
||||
@ -309,7 +309,7 @@ func (g Groups) SendText(text string, delay ...time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 向群组依次发送图片消息, 支持发送延迟
|
||||
// SendImage 向群组依次发送图片消息, 支持发送延迟
|
||||
func (g Groups) SendImage(file *os.File, delay ...time.Duration) error {
|
||||
total := getTotalDuration(delay...)
|
||||
var (
|
||||
@ -331,22 +331,22 @@ func (g Groups) SendImage(file *os.File, delay ...time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 根据用户名查找群组
|
||||
// SearchByUserName 根据用户名查找群组
|
||||
func (g Groups) SearchByUserName(limit int, username string) (results Groups) {
|
||||
return g.Search(limit, func(group *Group) bool { return group.UserName == username })
|
||||
}
|
||||
|
||||
// 根据昵称查找群组
|
||||
// SearchByNickName 根据昵称查找群组
|
||||
func (g Groups) SearchByNickName(limit int, nickName string) (results Groups) {
|
||||
return g.Search(limit, func(group *Group) bool { return group.NickName == nickName })
|
||||
}
|
||||
|
||||
// 根据备注查找群组
|
||||
// SearchByRemarkName 根据备注查找群组
|
||||
func (g Groups) SearchByRemarkName(limit int, remarkName string) (results Groups) {
|
||||
return g.Search(limit, func(group *Group) bool { return group.RemarkName == remarkName })
|
||||
}
|
||||
|
||||
// 根据自定义条件查找群组
|
||||
// Search 根据自定义条件查找群组
|
||||
func (g Groups) Search(limit int, condFuncList ...func(group *Group) bool) (results Groups) {
|
||||
if condFuncList == nil {
|
||||
return g
|
||||
@ -371,22 +371,22 @@ func (g Groups) Search(limit int, condFuncList ...func(group *Group) bool) (resu
|
||||
return
|
||||
}
|
||||
|
||||
// 公众号对象
|
||||
// Mp 公众号对象
|
||||
type Mp struct{ *User }
|
||||
|
||||
func (m Mp) String() string {
|
||||
return fmt.Sprintf("<Mp:%s>", m.NickName)
|
||||
}
|
||||
|
||||
// 公众号组对象
|
||||
// Mps 公众号组对象
|
||||
type Mps []*Mp
|
||||
|
||||
// 数量统计
|
||||
// Count 数量统计
|
||||
func (m Mps) Count() int {
|
||||
return len(m)
|
||||
}
|
||||
|
||||
// 获取第一个
|
||||
// First 获取第一个
|
||||
func (m Mps) First() *Mp {
|
||||
if m.Count() > 0 {
|
||||
return m[0]
|
||||
@ -394,7 +394,7 @@ func (m Mps) First() *Mp {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取最后一个
|
||||
// Last 获取最后一个
|
||||
func (m Mps) Last() *Mp {
|
||||
if m.Count() > 0 {
|
||||
return m[m.Count()-1]
|
||||
@ -402,7 +402,7 @@ func (m Mps) Last() *Mp {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 根据自定义条件查找
|
||||
// Search 根据自定义条件查找
|
||||
func (m Mps) Search(limit int, condFuncList ...func(group *Mp) bool) (results Mps) {
|
||||
if condFuncList == nil {
|
||||
return m
|
||||
|
10
stroage.go
10
stroage.go
@ -7,7 +7,7 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// 身份信息, 维持整个登陆的Session会话
|
||||
// Storage 身份信息, 维持整个登陆的Session会话
|
||||
type Storage struct {
|
||||
LoginInfo *LoginInfo
|
||||
Request *BaseRequest
|
||||
@ -21,21 +21,21 @@ type HotReloadStorageItem struct {
|
||||
WechatDomain WechatDomain
|
||||
}
|
||||
|
||||
// 热登陆存储接口
|
||||
// HotReloadStorage 热登陆存储接口
|
||||
type HotReloadStorage interface {
|
||||
GetHotReloadStorageItem() HotReloadStorageItem // 获取HotReloadStorageItem
|
||||
Dump(item HotReloadStorageItem) error // 实现该方法, 将必要信息进行序列化
|
||||
Load() error // 实现该方法, 将存储媒介的内容反序列化
|
||||
}
|
||||
|
||||
// 实现HotReloadStorage接口
|
||||
// JsonFileHotReloadStorage 实现HotReloadStorage接口
|
||||
// 默认以json文件的形式存储
|
||||
type JsonFileHotReloadStorage struct {
|
||||
item HotReloadStorageItem
|
||||
filename string
|
||||
}
|
||||
|
||||
// 将信息写入json文件
|
||||
// Dump 将信息写入json文件
|
||||
func (f *JsonFileHotReloadStorage) Dump(item HotReloadStorageItem) error {
|
||||
|
||||
file, err := os.OpenFile(f.filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.ModePerm)
|
||||
@ -56,7 +56,7 @@ func (f *JsonFileHotReloadStorage) Dump(item HotReloadStorageItem) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// 从文件中读取信息
|
||||
// Load 从文件中读取信息
|
||||
func (f *JsonFileHotReloadStorage) Load() error {
|
||||
file, err := os.Open(f.filename)
|
||||
|
||||
|
78
user.go
78
user.go
@ -9,7 +9,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 抽象的用户结构: 好友 群组 公众号
|
||||
// User 抽象的用户结构: 好友 群组 公众号
|
||||
type User struct {
|
||||
Uin int
|
||||
HideInputBarFlag int
|
||||
@ -54,12 +54,12 @@ func (u *User) String() string {
|
||||
return fmt.Sprintf("<User:%s>", u.NickName)
|
||||
}
|
||||
|
||||
// 获取用户头像
|
||||
// GetAvatarResponse 获取用户头像
|
||||
func (u *User) GetAvatarResponse() (*http.Response, error) {
|
||||
return u.Self.Bot.Caller.Client.WebWxGetHeadImg(u.HeadImgUrl)
|
||||
}
|
||||
|
||||
// 下载用户头像
|
||||
// SaveAvatar 下载用户头像
|
||||
func (u *User) SaveAvatar(filename string) error {
|
||||
resp, err := u.GetAvatarResponse()
|
||||
if err != nil {
|
||||
@ -79,7 +79,7 @@ func (u *User) SaveAvatar(filename string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// 获取用户的详情
|
||||
// Detail 获取用户的详情
|
||||
func (u *User) Detail() (*User, error) {
|
||||
if u.UserName == u.Self.UserName {
|
||||
return u.Self.User, nil
|
||||
@ -95,22 +95,22 @@ func (u *User) Detail() (*User, error) {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// 判断是否为好友
|
||||
// IsFriend 判断是否为好友
|
||||
func (u *User) IsFriend() bool {
|
||||
return !u.IsGroup() && strings.HasPrefix(u.UserName, "@") && u.VerifyFlag == 0
|
||||
}
|
||||
|
||||
// 判断是否为群组
|
||||
// IsGroup 判断是否为群组
|
||||
func (u *User) IsGroup() bool {
|
||||
return strings.HasPrefix(u.UserName, "@@") && u.VerifyFlag == 0
|
||||
}
|
||||
|
||||
// 判断是否为公众号
|
||||
// IsMP 判断是否为公众号
|
||||
func (u *User) IsMP() bool {
|
||||
return u.VerifyFlag == 8 || u.VerifyFlag == 24 || u.VerifyFlag == 136
|
||||
}
|
||||
|
||||
// 自己,当前登录用户对象
|
||||
// Self 自己,当前登录用户对象
|
||||
type Self struct {
|
||||
*User
|
||||
Bot *Bot
|
||||
@ -121,7 +121,7 @@ type Self struct {
|
||||
mps Mps
|
||||
}
|
||||
|
||||
// 获取所有的好友、群组、公众号信息
|
||||
// Members 获取所有的好友、群组、公众号信息
|
||||
func (s *Self) Members(update ...bool) (Members, error) {
|
||||
// 首先判断缓存里有没有,如果没有则去更新缓存
|
||||
// 判断是否需要更新,如果传入的参数不为nil,则取第一个
|
||||
@ -150,7 +150,7 @@ func (s *Self) updateMembers() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取文件传输助手对象,封装成Friend返回
|
||||
// FileHelper 获取文件传输助手对象,封装成Friend返回
|
||||
// fh, err := self.FileHelper() // or fh := openwechat.NewFriendHelper(self)
|
||||
func (s *Self) FileHelper() (*Friend, error) {
|
||||
// 如果缓存里有,直接返回,否则去联系人里面找
|
||||
@ -170,7 +170,7 @@ func (s *Self) FileHelper() (*Friend, error) {
|
||||
return s.fileHelper, nil
|
||||
}
|
||||
|
||||
// 获取所有的好友
|
||||
// Friends 获取所有的好友
|
||||
func (s *Self) Friends(update ...bool) (Friends, error) {
|
||||
if s.friends == nil || (len(update) > 0 && update[0]) {
|
||||
if _, err := s.Members(true); err != nil {
|
||||
@ -181,7 +181,7 @@ func (s *Self) Friends(update ...bool) (Friends, error) {
|
||||
return s.friends, nil
|
||||
}
|
||||
|
||||
// 获取所有的群组
|
||||
// Groups 获取所有的群组
|
||||
func (s *Self) Groups(update ...bool) (Groups, error) {
|
||||
if s.groups == nil || (len(update) > 0 && update[0]) {
|
||||
if _, err := s.Members(true); err != nil {
|
||||
@ -192,7 +192,7 @@ func (s *Self) Groups(update ...bool) (Groups, error) {
|
||||
return s.groups, nil
|
||||
}
|
||||
|
||||
// 获取所有的公众号
|
||||
// Mps 获取所有的公众号
|
||||
func (s *Self) Mps(update ...bool) (Mps, error) {
|
||||
if s.mps == nil || (len(update) > 0 && update[0]) {
|
||||
if _, err := s.Members(true); err != nil {
|
||||
@ -203,7 +203,7 @@ func (s *Self) Mps(update ...bool) (Mps, error) {
|
||||
return s.mps, nil
|
||||
}
|
||||
|
||||
// 更新所有的联系人信息
|
||||
// UpdateMembersDetail 更新所有的联系人信息
|
||||
func (s *Self) UpdateMembersDetail() error {
|
||||
// 先获取所有的联系人
|
||||
members, err := s.Members()
|
||||
@ -227,39 +227,39 @@ func (s *Self) sendMessageToUser(user *User, msg *SendMessage) (*SentMessage, er
|
||||
return successSendMessage, nil
|
||||
}
|
||||
|
||||
// 发送消息给好友
|
||||
// SendMessageToFriend 发送消息给好友
|
||||
func (s *Self) SendMessageToFriend(friend *Friend, msg *SendMessage) (*SentMessage, error) {
|
||||
return s.sendMessageToUser(friend.User, msg)
|
||||
}
|
||||
|
||||
// 发送文本消息给好友
|
||||
// SendTextToFriend 发送文本消息给好友
|
||||
func (s *Self) SendTextToFriend(friend *Friend, text string) (*SentMessage, error) {
|
||||
msg := NewTextSendMessage(text, s.UserName, friend.UserName)
|
||||
return s.SendMessageToFriend(friend, msg)
|
||||
}
|
||||
|
||||
// 发送图片消息给好友
|
||||
// SendImageToFriend 发送图片消息给好友
|
||||
func (s *Self) SendImageToFriend(friend *Friend, file *os.File) (*SentMessage, error) {
|
||||
req := s.Bot.storage.Request
|
||||
info := s.Bot.storage.LoginInfo
|
||||
return s.Bot.Caller.WebWxSendImageMsg(file, req, info, s.UserName, friend.UserName)
|
||||
}
|
||||
|
||||
// 发送文件给好友
|
||||
// SendFileToFriend 发送文件给好友
|
||||
func (s *Self) SendFileToFriend(friend *Friend, file *os.File) (*SentMessage, error) {
|
||||
req := s.Bot.storage.Request
|
||||
info := s.Bot.storage.LoginInfo
|
||||
return s.Bot.Caller.WebWxSendFile(file, req, info, s.UserName, friend.UserName)
|
||||
}
|
||||
|
||||
// 设置好友备注
|
||||
// SetRemarkNameToFriend 设置好友备注
|
||||
// self.SetRemarkNameToFriend(friend, "remark") // or friend.SetRemarkName("remark")
|
||||
func (s *Self) SetRemarkNameToFriend(friend *Friend, remarkName string) error {
|
||||
req := s.Bot.storage.Request
|
||||
return s.Bot.Caller.WebWxOplog(req, remarkName, friend.UserName)
|
||||
}
|
||||
|
||||
// 拉多名好友进群
|
||||
// AddFriendsIntoGroup 拉多名好友进群
|
||||
// 最好自己是群主,成功率高一点,因为有的群允许非群组拉人,而有的群不允许
|
||||
func (s *Self) AddFriendsIntoGroup(group *Group, friends ...*Friend) error {
|
||||
if len(friends) == 0 {
|
||||
@ -283,7 +283,7 @@ func (s *Self) AddFriendsIntoGroup(group *Group, friends ...*Friend) error {
|
||||
return s.Bot.Caller.AddFriendIntoChatRoom(req, info, group, friends...)
|
||||
}
|
||||
|
||||
// 从群聊中移除用户
|
||||
// RemoveMemberFromGroup 从群聊中移除用户
|
||||
// Deprecated
|
||||
// 无论是网页版,还是程序上都不起作用
|
||||
func (s *Self) RemoveMemberFromGroup(group *Group, members Members) error {
|
||||
@ -314,7 +314,7 @@ func (s *Self) RemoveMemberFromGroup(group *Group, members Members) error {
|
||||
return s.Bot.Caller.RemoveFriendFromChatRoom(req, info, group, members...)
|
||||
}
|
||||
|
||||
// 拉好友进多个群聊
|
||||
// AddFriendIntoManyGroups 拉好友进多个群聊
|
||||
// AddFriendIntoGroups, 名字和上面的有点像
|
||||
func (s *Self) AddFriendIntoManyGroups(friend *Friend, groups ...*Group) error {
|
||||
for _, group := range groups {
|
||||
@ -325,32 +325,32 @@ func (s *Self) AddFriendIntoManyGroups(friend *Friend, groups ...*Group) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 发送消息给群组
|
||||
// SendMessageToGroup 发送消息给群组
|
||||
func (s *Self) SendMessageToGroup(group *Group, msg *SendMessage) (*SentMessage, error) {
|
||||
return s.sendMessageToUser(group.User, msg)
|
||||
}
|
||||
|
||||
// 发送文本消息给群组
|
||||
// SendTextToGroup 发送文本消息给群组
|
||||
func (s *Self) SendTextToGroup(group *Group, text string) (*SentMessage, error) {
|
||||
msg := NewTextSendMessage(text, s.UserName, group.UserName)
|
||||
return s.SendMessageToGroup(group, msg)
|
||||
}
|
||||
|
||||
// 发送图片消息给群组
|
||||
// SendImageToGroup 发送图片消息给群组
|
||||
func (s *Self) SendImageToGroup(group *Group, file *os.File) (*SentMessage, error) {
|
||||
req := s.Bot.storage.Request
|
||||
info := s.Bot.storage.LoginInfo
|
||||
return s.Bot.Caller.WebWxSendImageMsg(file, req, info, s.UserName, group.UserName)
|
||||
}
|
||||
|
||||
// 发送文件给群组
|
||||
// SendFileToGroup 发送文件给群组
|
||||
func (s *Self) SendFileToGroup(group *Group, file *os.File) (*SentMessage, error) {
|
||||
req := s.Bot.storage.Request
|
||||
info := s.Bot.storage.LoginInfo
|
||||
return s.Bot.Caller.WebWxSendFile(file, req, info, s.UserName, group.UserName)
|
||||
}
|
||||
|
||||
// 撤回消息
|
||||
// RevokeMessage 撤回消息
|
||||
// sentMessage, err := friend.SendText("message")
|
||||
// if err == nil {
|
||||
// self.RevokeMessage(sentMessage) // or sentMessage.Revoke()
|
||||
@ -392,7 +392,7 @@ func (s *Self) forwardMessage(msg *SentMessage, users ...*User) error {
|
||||
return errors.New("unsupport message")
|
||||
}
|
||||
|
||||
// 转发给好友
|
||||
// ForwardMessageToFriends 转发给好友
|
||||
func (s *Self) ForwardMessageToFriends(msg *SentMessage, friends ...*Friend) error {
|
||||
var users = make([]*User, len(friends))
|
||||
for index, friend := range friends {
|
||||
@ -401,7 +401,7 @@ func (s *Self) ForwardMessageToFriends(msg *SentMessage, friends ...*Friend) err
|
||||
return s.forwardMessage(msg, users...)
|
||||
}
|
||||
|
||||
// 转发给群组
|
||||
// ForwardMessageToGroups 转发给群组
|
||||
func (s *Self) ForwardMessageToGroups(msg *SentMessage, groups ...*Group) error {
|
||||
var users = make([]*User, len(groups))
|
||||
for index, group := range groups {
|
||||
@ -410,15 +410,15 @@ func (s *Self) ForwardMessageToGroups(msg *SentMessage, groups ...*Group) error
|
||||
return s.forwardMessage(msg, users...)
|
||||
}
|
||||
|
||||
// 抽象的用户组
|
||||
// Members 抽象的用户组
|
||||
type Members []*User
|
||||
|
||||
// 统计数量
|
||||
// Count 统计数量
|
||||
func (m Members) Count() int {
|
||||
return len(m)
|
||||
}
|
||||
|
||||
// 获取第一个
|
||||
// First 获取第一个
|
||||
func (m Members) First() *User {
|
||||
if m.Count() > 0 {
|
||||
u := m[0]
|
||||
@ -427,7 +427,7 @@ func (m Members) First() *User {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取最后一个
|
||||
// Last 获取最后一个
|
||||
func (m Members) Last() *User {
|
||||
if m.Count() > 0 {
|
||||
u := m[m.Count()-1]
|
||||
@ -436,7 +436,7 @@ func (m Members) Last() *User {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 设置owner
|
||||
// SetOwner 设置owner
|
||||
// 请不要随意设置
|
||||
func (m Members) SetOwner(s *Self) {
|
||||
for _, member := range m {
|
||||
@ -444,22 +444,22 @@ func (m Members) SetOwner(s *Self) {
|
||||
}
|
||||
}
|
||||
|
||||
// 根据用户名查找
|
||||
// SearchByUserName 根据用户名查找
|
||||
func (m Members) SearchByUserName(limit int, username string) (results Members) {
|
||||
return m.Search(limit, func(user *User) bool { return user.UserName == username })
|
||||
}
|
||||
|
||||
// 根据昵称查找
|
||||
// SearchByNickName 根据昵称查找
|
||||
func (m Members) SearchByNickName(limit int, nickName string) (results Members) {
|
||||
return m.Search(limit, func(user *User) bool { return user.NickName == nickName })
|
||||
}
|
||||
|
||||
// 根据备注查找
|
||||
// SearchByRemarkName 根据备注查找
|
||||
func (m Members) SearchByRemarkName(limit int, remarkName string) (results Members) {
|
||||
return m.Search(limit, func(user *User) bool { return user.RemarkName == remarkName })
|
||||
}
|
||||
|
||||
// 根据自定义条件查找
|
||||
// Search 根据自定义条件查找
|
||||
func (m Members) Search(limit int, condFuncList ...func(user *User) bool) (results Members) {
|
||||
if condFuncList == nil {
|
||||
return m
|
||||
@ -569,7 +569,7 @@ func (m Members) detail(self *Self) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 这里为了兼容Desktop版本找不到文件传输助手的问题
|
||||
// NewFriendHelper 这里为了兼容Desktop版本找不到文件传输助手的问题
|
||||
// 文件传输助手的微信身份标识符永远是filehelper
|
||||
// 这种形式的对象可能缺少一些其他属性
|
||||
// 但是不影响发送信息的功能
|
||||
|
Loading…
x
Reference in New Issue
Block a user