删除storage接口的依赖
This commit is contained in:
parent
1c885a9182
commit
f80cb2ebd2
45
bot.go
45
bot.go
@ -8,7 +8,7 @@ import (
|
||||
type Bot struct {
|
||||
Caller *Caller
|
||||
self *Self
|
||||
storage WechatStorage
|
||||
storage *Storage
|
||||
ScanCallBack func(body []byte)
|
||||
LoginCallBack func(body []byte)
|
||||
UUIDCallback func(uuid string)
|
||||
@ -41,7 +41,6 @@ func (b *Bot) GetCurrentUser() (*Self, error) {
|
||||
// 用户登录
|
||||
// 该方法会一直阻塞,直到用户扫码登录,或者二维码过期
|
||||
func (b *Bot) Login() error {
|
||||
b.prepare()
|
||||
uuid, err := b.Caller.GetLoginUUID()
|
||||
if err != nil {
|
||||
return err
|
||||
@ -71,7 +70,7 @@ func (b *Bot) Login() error {
|
||||
|
||||
func (b *Bot) Logout() error {
|
||||
if b.Alive() {
|
||||
info := b.storage.GetLoginInfo()
|
||||
info := b.storage.LoginInfo
|
||||
if err := b.Caller.Logout(info); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -94,10 +93,10 @@ func (b *Bot) login(data []byte) error {
|
||||
}
|
||||
|
||||
// 将LoginInfo存到storage里面
|
||||
b.storage.SetLoginInfo(*info)
|
||||
b.storage.LoginInfo = info
|
||||
|
||||
// 构建BaseRequest
|
||||
request := BaseRequest{
|
||||
request := &BaseRequest{
|
||||
Uin: info.WxUin,
|
||||
Sid: info.WxSid,
|
||||
Skey: info.SKey,
|
||||
@ -105,7 +104,7 @@ func (b *Bot) login(data []byte) error {
|
||||
}
|
||||
|
||||
// 将BaseRequest存到storage里面方便后续调用
|
||||
b.storage.SetBaseRequest(request)
|
||||
b.storage.Request = request
|
||||
// 获取初始化的用户信息和一些必要的参数
|
||||
resp, err := b.Caller.WebInit(request)
|
||||
if err != nil {
|
||||
@ -114,10 +113,10 @@ func (b *Bot) login(data []byte) error {
|
||||
// 设置当前的用户
|
||||
b.self = &Self{Bot: b, User: &resp.User}
|
||||
b.self.Self = b.self
|
||||
b.storage.SetWebInitResponse(*resp)
|
||||
b.storage.Response = resp
|
||||
|
||||
// 通知手机客户端已经登录
|
||||
if err = b.Caller.WebWxStatusNotify(request, *resp, *info); err != nil {
|
||||
if err = b.Caller.WebWxStatusNotify(request, resp, info); err != nil {
|
||||
return err
|
||||
}
|
||||
// 开启协程,轮训获取是否有新的消息返回
|
||||
@ -135,8 +134,8 @@ func (b *Bot) asyncCall() error {
|
||||
resp *SyncCheckResponse
|
||||
)
|
||||
for b.Alive() {
|
||||
info := b.storage.GetLoginInfo()
|
||||
response := b.storage.GetWebInitResponse()
|
||||
info := b.storage.LoginInfo
|
||||
response := b.storage.Response
|
||||
resp, err = b.Caller.SyncCheck(info, response)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -163,16 +162,12 @@ func (b *Bot) stopAsyncCALL(err error) {
|
||||
|
||||
// 获取新的消息
|
||||
func (b *Bot) getMessage() error {
|
||||
info := b.storage.GetLoginInfo()
|
||||
response := b.storage.GetWebInitResponse()
|
||||
request := b.storage.GetBaseRequest()
|
||||
resp, err := b.Caller.WebWxSync(request, response, info)
|
||||
resp, err := b.Caller.WebWxSync(b.storage.Request, b.storage.Response, b.storage.LoginInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 更新SyncKey并且重新存入storage
|
||||
response.SyncKey = resp.SyncKey
|
||||
b.storage.SetWebInitResponse(response)
|
||||
b.storage.Response.SyncKey = resp.SyncKey
|
||||
// 遍历所有的新的消息,依次处理
|
||||
for _, message := range resp.AddMsgList {
|
||||
// 根据不同的消息类型来进行处理,方便后续统一调用
|
||||
@ -185,12 +180,6 @@ func (b *Bot) getMessage() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bot) prepare() {
|
||||
if b.storage == nil {
|
||||
b.storage = NewSimpleWechatStorage()
|
||||
}
|
||||
}
|
||||
|
||||
// 当消息同步发生了错误或者用户主动在手机上退出,该方法会立即返回,否则会一直阻塞
|
||||
func (b *Bot) Block() error {
|
||||
if b.self == nil {
|
||||
@ -203,12 +192,18 @@ func (b *Bot) Block() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewBot(caller *Caller, storage WechatStorage) *Bot {
|
||||
return &Bot{Caller: caller, storage: storage, exit: make(chan bool)}
|
||||
func NewBot(caller *Caller) *Bot {
|
||||
return &Bot{Caller: caller, storage: &Storage{}, exit: make(chan bool)}
|
||||
}
|
||||
|
||||
func DefaultBot() *Bot {
|
||||
return NewBot(DefaultCaller(), NewSimpleWechatStorage())
|
||||
return NewBot(DefaultCaller())
|
||||
}
|
||||
|
||||
type Storage struct {
|
||||
LoginInfo *LoginInfo
|
||||
Request *BaseRequest
|
||||
Response *WebInitResponse
|
||||
}
|
||||
|
||||
func GetQrcodeUrl(uuid string) string {
|
||||
|
@ -19,6 +19,7 @@ func TestDefaultBot(t *testing.T) {
|
||||
return
|
||||
}
|
||||
self, _ := bot.GetCurrentUser()
|
||||
self.SaveAvatar("2.png")
|
||||
fileHelper, _ := self.FileHelper()
|
||||
fileHelper.SendText("6666")
|
||||
group, _ := self.Groups()
|
||||
|
20
caller.go
20
caller.go
@ -84,7 +84,7 @@ func (c *Caller) GetLoginInfo(body []byte) (*LoginInfo, error) {
|
||||
}
|
||||
|
||||
// 获取初始化信息
|
||||
func (c *Caller) WebInit(request BaseRequest) (*WebInitResponse, error) {
|
||||
func (c *Caller) WebInit(request *BaseRequest) (*WebInitResponse, error) {
|
||||
resp := NewReturnResponse(c.Client.WebInit(request))
|
||||
if resp.Err() != nil {
|
||||
return nil, resp.Err()
|
||||
@ -98,7 +98,7 @@ func (c *Caller) WebInit(request BaseRequest) (*WebInitResponse, error) {
|
||||
}
|
||||
|
||||
// 通知手机已登录
|
||||
func (c *Caller) WebWxStatusNotify(request BaseRequest, response WebInitResponse, info LoginInfo) error {
|
||||
func (c *Caller) WebWxStatusNotify(request *BaseRequest, response *WebInitResponse, info *LoginInfo) error {
|
||||
resp := NewReturnResponse(c.Client.WebWxStatusNotify(request, response, info))
|
||||
if resp.Err() != nil {
|
||||
return resp.Err()
|
||||
@ -115,7 +115,7 @@ func (c *Caller) WebWxStatusNotify(request BaseRequest, response WebInitResponse
|
||||
}
|
||||
|
||||
// 异步获取是否有新的消息
|
||||
func (c *Caller) SyncCheck(info LoginInfo, response WebInitResponse) (*SyncCheckResponse, error) {
|
||||
func (c *Caller) SyncCheck(info *LoginInfo, response *WebInitResponse) (*SyncCheckResponse, error) {
|
||||
resp := NewReturnResponse(c.Client.SyncCheck(info, response))
|
||||
if resp.Err() != nil {
|
||||
return nil, resp.Err()
|
||||
@ -135,7 +135,7 @@ func (c *Caller) SyncCheck(info LoginInfo, response WebInitResponse) (*SyncCheck
|
||||
}
|
||||
|
||||
// 获取所有的联系人
|
||||
func (c *Caller) WebWxGetContact(info LoginInfo) (Members, error) {
|
||||
func (c *Caller) WebWxGetContact(info *LoginInfo) (Members, error) {
|
||||
resp := NewReturnResponse(c.Client.WebWxGetContact(info))
|
||||
if resp.Err() != nil {
|
||||
return nil, resp.Err()
|
||||
@ -153,7 +153,7 @@ func (c *Caller) WebWxGetContact(info LoginInfo) (Members, error) {
|
||||
|
||||
// 获取联系人的详情
|
||||
// 注: Members参数的长度不要大于50
|
||||
func (c *Caller) WebWxBatchGetContact(members Members, request BaseRequest) (Members, error) {
|
||||
func (c *Caller) WebWxBatchGetContact(members Members, request *BaseRequest) (Members, error) {
|
||||
resp := NewReturnResponse(c.Client.WebWxBatchGetContact(members, request))
|
||||
if resp.Err() != nil {
|
||||
return nil, resp.Err()
|
||||
@ -170,7 +170,7 @@ func (c *Caller) WebWxBatchGetContact(members Members, request BaseRequest) (Mem
|
||||
}
|
||||
|
||||
// 获取新的消息接口
|
||||
func (c *Caller) WebWxSync(request BaseRequest, response WebInitResponse, info LoginInfo) (*WebWxSyncResponse, error) {
|
||||
func (c *Caller) WebWxSync(request *BaseRequest, response *WebInitResponse, info *LoginInfo) (*WebWxSyncResponse, error) {
|
||||
resp := NewReturnResponse(c.Client.WebWxSync(request, response, info))
|
||||
if resp.Err() != nil {
|
||||
return nil, resp.Err()
|
||||
@ -184,19 +184,19 @@ func (c *Caller) WebWxSync(request BaseRequest, response WebInitResponse, info L
|
||||
}
|
||||
|
||||
// 发送消息接口
|
||||
func (c *Caller) WebWxSendMsg(msg *SendMessage, info LoginInfo, request BaseRequest) error {
|
||||
func (c *Caller) WebWxSendMsg(msg *SendMessage, info *LoginInfo, request *BaseRequest) error {
|
||||
resp := NewReturnResponse(c.Client.WebWxSendMsg(msg, info, request))
|
||||
return parseBaseResponseError(resp)
|
||||
}
|
||||
|
||||
// 修改用户备注接口
|
||||
func (c *Caller) WebWxOplog(request BaseRequest, remarkName, toUserName string) error {
|
||||
func (c *Caller) WebWxOplog(request *BaseRequest, remarkName, toUserName string) error {
|
||||
resp := NewReturnResponse(c.Client.WebWxOplog(request, remarkName, toUserName))
|
||||
return parseBaseResponseError(resp)
|
||||
}
|
||||
|
||||
// 发送图片消息接口
|
||||
func (c *Caller) WebWxSendImageMsg(file *os.File, request BaseRequest, info LoginInfo, fromUserName, toUserName string) error {
|
||||
func (c *Caller) WebWxSendImageMsg(file *os.File, request *BaseRequest, info *LoginInfo, fromUserName, toUserName string) error {
|
||||
// 首先尝试上传图片
|
||||
resp := NewReturnResponse(c.Client.WebWxUploadMedia(file, request, info, fromUserName, toUserName, "image/jpeg", "pic"))
|
||||
// 无错误上传成功之后获取请求结果,判断结果是否正常
|
||||
@ -222,7 +222,7 @@ func (c *Caller) WebWxSendImageMsg(file *os.File, request BaseRequest, info Logi
|
||||
}
|
||||
|
||||
// 用户退出
|
||||
func (c *Caller) Logout(info LoginInfo) error {
|
||||
func (c *Caller) Logout(info *LoginInfo) error {
|
||||
resp := NewReturnResponse(c.Client.Logout(info))
|
||||
return parseBaseResponseError(resp)
|
||||
}
|
||||
|
42
client.go
42
client.go
@ -76,12 +76,12 @@ func (c *Client) GetLoginInfo(path string) (*http.Response, error) {
|
||||
}
|
||||
|
||||
// 请求获取初始化信息
|
||||
func (c *Client) WebInit(request BaseRequest) (*http.Response, error) {
|
||||
func (c *Client) WebInit(request *BaseRequest) (*http.Response, error) {
|
||||
path, _ := url.Parse(webWxInitUrl)
|
||||
params := url.Values{}
|
||||
params.Add("_", fmt.Sprintf("%d", time.Now().Unix()))
|
||||
path.RawQuery = params.Encode()
|
||||
content := struct{ BaseRequest BaseRequest }{BaseRequest: request}
|
||||
content := struct{ BaseRequest *BaseRequest }{BaseRequest: request}
|
||||
body, err := ToBuffer(content)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -90,7 +90,7 @@ func (c *Client) WebInit(request BaseRequest) (*http.Response, error) {
|
||||
}
|
||||
|
||||
// 通知手机已登录
|
||||
func (c *Client) WebWxStatusNotify(request BaseRequest, response WebInitResponse, info LoginInfo) (*http.Response, error) {
|
||||
func (c *Client) WebWxStatusNotify(request *BaseRequest, response *WebInitResponse, info *LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(webWxStatusNotifyUrl)
|
||||
params := url.Values{}
|
||||
params.Add("lang", "zh_CN")
|
||||
@ -111,7 +111,7 @@ func (c *Client) WebWxStatusNotify(request BaseRequest, response WebInitResponse
|
||||
}
|
||||
|
||||
// 异步检查是否有新的消息返回
|
||||
func (c *Client) SyncCheck(info LoginInfo, response WebInitResponse) (*http.Response, error) {
|
||||
func (c *Client) SyncCheck(info *LoginInfo, response *WebInitResponse) (*http.Response, error) {
|
||||
path, _ := url.Parse(syncCheckUrl)
|
||||
params := url.Values{}
|
||||
params.Add("r", strconv.FormatInt(time.Now().Unix(), 10))
|
||||
@ -134,7 +134,7 @@ func (c *Client) SyncCheck(info LoginInfo, response WebInitResponse) (*http.Resp
|
||||
}
|
||||
|
||||
// 获取联系人信息
|
||||
func (c *Client) WebWxGetContact(info LoginInfo) (*http.Response, error) {
|
||||
func (c *Client) WebWxGetContact(info *LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(webWxGetContactUrl)
|
||||
params := url.Values{}
|
||||
params.Add("r", strconv.FormatInt(time.Now().Unix(), 10))
|
||||
@ -145,7 +145,7 @@ func (c *Client) WebWxGetContact(info LoginInfo) (*http.Response, error) {
|
||||
}
|
||||
|
||||
// 获取联系人详情
|
||||
func (c *Client) WebWxBatchGetContact(members Members, request BaseRequest) (*http.Response, error) {
|
||||
func (c *Client) WebWxBatchGetContact(members Members, request *BaseRequest) (*http.Response, error) {
|
||||
path, _ := url.Parse(webWxBatchGetContactUrl)
|
||||
params := url.Values{}
|
||||
params.Add("type", "ex")
|
||||
@ -164,7 +164,7 @@ func (c *Client) WebWxBatchGetContact(members Members, request BaseRequest) (*ht
|
||||
}
|
||||
|
||||
// 获取消息接口
|
||||
func (c *Client) WebWxSync(request BaseRequest, response WebInitResponse, info LoginInfo) (*http.Response, error) {
|
||||
func (c *Client) WebWxSync(request *BaseRequest, response *WebInitResponse, info *LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(webWxSyncUrl)
|
||||
params := url.Values{}
|
||||
params.Add("sid", info.WxSid)
|
||||
@ -184,7 +184,7 @@ func (c *Client) WebWxSync(request BaseRequest, response WebInitResponse, info L
|
||||
}
|
||||
|
||||
// 发送消息
|
||||
func (c *Client) sendMessage(request BaseRequest, url string, msg *SendMessage) (*http.Response, error) {
|
||||
func (c *Client) sendMessage(request *BaseRequest, url string, msg *SendMessage) (*http.Response, error) {
|
||||
content := map[string]interface{}{
|
||||
"BaseRequest": request,
|
||||
"Msg": msg,
|
||||
@ -197,7 +197,7 @@ func (c *Client) sendMessage(request BaseRequest, url string, msg *SendMessage)
|
||||
}
|
||||
|
||||
// 发送文本消息
|
||||
func (c *Client) WebWxSendMsg(msg *SendMessage, info LoginInfo, request BaseRequest) (*http.Response, error) {
|
||||
func (c *Client) WebWxSendMsg(msg *SendMessage, info *LoginInfo, request *BaseRequest) (*http.Response, error) {
|
||||
msg.Type = TextMessage
|
||||
path, _ := url.Parse(webWxSendMsgUrl)
|
||||
params := url.Values{}
|
||||
@ -214,7 +214,7 @@ func (c *Client) WebWxGetHeadImg(headImageUrl string) (*http.Response, error) {
|
||||
}
|
||||
|
||||
// 上传文件
|
||||
func (c *Client) WebWxUploadMedia(file *os.File, request BaseRequest, info LoginInfo, forUserName, toUserName, contentType, mediaType string) (*http.Response, error) {
|
||||
func (c *Client) WebWxUploadMedia(file *os.File, request *BaseRequest, info *LoginInfo, forUserName, toUserName, contentType, mediaType string) (*http.Response, error) {
|
||||
path, _ := url.Parse(webWxUpLoadMediaUrl)
|
||||
params := url.Values{}
|
||||
params.Add("f", "json")
|
||||
@ -281,7 +281,7 @@ func (c *Client) WebWxUploadMedia(file *os.File, request BaseRequest, info Login
|
||||
// 发送图片
|
||||
// 这个接口依赖上传文件的接口
|
||||
// 发送的图片必须是已经成功上传的图片
|
||||
func (c *Client) WebWxSendMsgImg(msg *SendMessage, request BaseRequest, info LoginInfo) (*http.Response, error) {
|
||||
func (c *Client) WebWxSendMsgImg(msg *SendMessage, request *BaseRequest, info *LoginInfo) (*http.Response, error) {
|
||||
msg.Type = ImageMessage
|
||||
path, _ := url.Parse(webWxSendMsgImgUrl)
|
||||
params := url.Values{}
|
||||
@ -294,7 +294,7 @@ func (c *Client) WebWxSendMsgImg(msg *SendMessage, request BaseRequest, info Log
|
||||
}
|
||||
|
||||
// 发送文件信息
|
||||
func (c *Client) WebWxSendAppMsg(msg *SendMessage, request BaseRequest) (*http.Response, error) {
|
||||
func (c *Client) WebWxSendAppMsg(msg *SendMessage, request *BaseRequest) (*http.Response, error) {
|
||||
msg.Type = AppMessage
|
||||
path, _ := url.Parse(webWxSendAppMsgUrl)
|
||||
params := url.Values{}
|
||||
@ -305,7 +305,7 @@ func (c *Client) WebWxSendAppMsg(msg *SendMessage, request BaseRequest) (*http.R
|
||||
}
|
||||
|
||||
// 用户重命名接口
|
||||
func (c *Client) WebWxOplog(request BaseRequest, remarkName, userName string, ) (*http.Response, error) {
|
||||
func (c *Client) WebWxOplog(request *BaseRequest, remarkName, userName string, ) (*http.Response, error) {
|
||||
path, _ := url.Parse(webWxOplogUrl)
|
||||
params := url.Values{}
|
||||
params.Add("lang", "zh_CN")
|
||||
@ -323,8 +323,8 @@ func (c *Client) WebWxOplog(request BaseRequest, remarkName, userName string, )
|
||||
}
|
||||
|
||||
// 添加用户为好友接口
|
||||
func (c *Client) WebWxVerifyUser(storage WechatStorage, info RecommendInfo, verifyContent string) (*http.Response, error) {
|
||||
loginInfo := storage.GetLoginInfo()
|
||||
func (c *Client) WebWxVerifyUser(storage *Storage, info RecommendInfo, verifyContent string) (*http.Response, error) {
|
||||
loginInfo := storage.LoginInfo
|
||||
path, _ := url.Parse(webWxVerifyUserUrl)
|
||||
params := url.Values{}
|
||||
params.Add("r", strconv.FormatInt(time.Now().Unix(), 10))
|
||||
@ -332,7 +332,7 @@ func (c *Client) WebWxVerifyUser(storage WechatStorage, info RecommendInfo, veri
|
||||
params.Add("pass_ticket", loginInfo.PassTicket)
|
||||
path.RawQuery = params.Encode()
|
||||
content := map[string]interface{}{
|
||||
"BaseRequest": storage.GetBaseRequest(),
|
||||
"BaseRequest": storage.Request,
|
||||
"Opcode": 3,
|
||||
"SceneList": []int{33},
|
||||
"SceneListCount": 1,
|
||||
@ -351,7 +351,7 @@ func (c *Client) WebWxVerifyUser(storage WechatStorage, info RecommendInfo, veri
|
||||
}
|
||||
|
||||
// 获取图片消息的图片响应
|
||||
func (c *Client) WebWxGetMsgImg(msg *Message, info LoginInfo) (*http.Response, error) {
|
||||
func (c *Client) WebWxGetMsgImg(msg *Message, info *LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(webWxGetMsgImgUrl)
|
||||
params := url.Values{}
|
||||
params.Add("MsgID", msg.MsgId)
|
||||
@ -362,7 +362,7 @@ func (c *Client) WebWxGetMsgImg(msg *Message, info LoginInfo) (*http.Response, e
|
||||
}
|
||||
|
||||
// 获取语音消息的语音响应
|
||||
func (c *Client) WebWxGetVoice(msg *Message, info LoginInfo) (*http.Response, error) {
|
||||
func (c *Client) WebWxGetVoice(msg *Message, info *LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(webWxGetVoiceUrl)
|
||||
params := url.Values{}
|
||||
params.Add("msgid", msg.MsgId)
|
||||
@ -372,7 +372,7 @@ func (c *Client) WebWxGetVoice(msg *Message, info LoginInfo) (*http.Response, er
|
||||
}
|
||||
|
||||
// 获取视频消息的视频响应
|
||||
func (c *Client) WebWxGetVideo(msg *Message, info LoginInfo) (*http.Response, error) {
|
||||
func (c *Client) WebWxGetVideo(msg *Message, info *LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(webWxGetVideoUrl)
|
||||
params := url.Values{}
|
||||
params.Add("msgid", msg.MsgId)
|
||||
@ -382,7 +382,7 @@ func (c *Client) WebWxGetVideo(msg *Message, info LoginInfo) (*http.Response, er
|
||||
}
|
||||
|
||||
// 获取文件消息的文件响应
|
||||
func (c *Client) WebWxGetMedia(msg *Message, info LoginInfo) (*http.Response, error) {
|
||||
func (c *Client) WebWxGetMedia(msg *Message, info *LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(webWxGetMediaUrl)
|
||||
params := url.Values{}
|
||||
params.Add("sender", msg.FromUserName)
|
||||
@ -396,7 +396,7 @@ func (c *Client) WebWxGetMedia(msg *Message, info LoginInfo) (*http.Response, er
|
||||
}
|
||||
|
||||
// 用户退出
|
||||
func (c *Client) Logout(info LoginInfo) (*http.Response, error) {
|
||||
func (c *Client) Logout(info *LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(webWxLogoutUrl)
|
||||
params := url.Values{}
|
||||
params.Add("redirect", "1")
|
||||
|
16
message.go
16
message.go
@ -127,8 +127,8 @@ func (m *Message) IsSendByGroup() bool {
|
||||
// 回复消息
|
||||
func (m *Message) Reply(msgType int, content, mediaId string) error {
|
||||
msg := NewSendMessage(msgType, content, m.Bot.self.User.UserName, m.FromUserName, mediaId)
|
||||
info := m.Bot.storage.GetLoginInfo()
|
||||
request := m.Bot.storage.GetBaseRequest()
|
||||
info := m.Bot.storage.LoginInfo
|
||||
request := m.Bot.storage.Request
|
||||
return m.Bot.Caller.WebWxSendMsg(msg, info, request)
|
||||
}
|
||||
|
||||
@ -139,8 +139,8 @@ func (m *Message) ReplyText(content string) error {
|
||||
|
||||
// 回复图片消息
|
||||
func (m *Message) ReplyImage(file *os.File) error {
|
||||
info := m.Bot.storage.GetLoginInfo()
|
||||
request := m.Bot.storage.GetBaseRequest()
|
||||
info := m.Bot.storage.LoginInfo
|
||||
request := m.Bot.storage.Request
|
||||
return m.Bot.Caller.WebWxSendImageMsg(file, request, info, m.Bot.self.UserName, m.FromUserName)
|
||||
}
|
||||
|
||||
@ -199,16 +199,16 @@ func (m *Message) GetFile() (*http.Response, error) {
|
||||
return nil, errors.New("invalid message type")
|
||||
}
|
||||
if m.IsPicture() {
|
||||
return m.Bot.Caller.Client.WebWxGetMsgImg(m, m.Bot.storage.GetLoginInfo())
|
||||
return m.Bot.Caller.Client.WebWxGetMsgImg(m, m.Bot.storage.LoginInfo)
|
||||
}
|
||||
if m.IsVoice() {
|
||||
return m.Bot.Caller.Client.WebWxGetVoice(m, m.Bot.storage.GetLoginInfo())
|
||||
return m.Bot.Caller.Client.WebWxGetVoice(m, m.Bot.storage.LoginInfo)
|
||||
}
|
||||
if m.IsVideo() {
|
||||
return m.Bot.Caller.Client.WebWxGetVideo(m, m.Bot.storage.GetLoginInfo())
|
||||
return m.Bot.Caller.Client.WebWxGetVideo(m, m.Bot.storage.LoginInfo)
|
||||
}
|
||||
if m.IsMedia() {
|
||||
return m.Bot.Caller.Client.WebWxGetMedia(m, m.Bot.storage.GetLoginInfo())
|
||||
return m.Bot.Caller.Client.WebWxGetMedia(m, m.Bot.storage.LoginInfo)
|
||||
}
|
||||
return nil, errors.New("unsupported type")
|
||||
}
|
||||
|
46
stroage.go
46
stroage.go
@ -1,49 +1,3 @@
|
||||
|
||||
package openwechat
|
||||
|
||||
// WechatStorage
|
||||
// 可以根据自己的情况来实现该接口
|
||||
type WechatStorage interface {
|
||||
SetLoginInfo(loginInfo LoginInfo)
|
||||
SetBaseRequest(baseRequest BaseRequest)
|
||||
SetWebInitResponse(webInitResponse WebInitResponse)
|
||||
GetLoginInfo() LoginInfo
|
||||
GetBaseRequest() BaseRequest
|
||||
GetWebInitResponse() WebInitResponse
|
||||
}
|
||||
|
||||
// implement WechatStorage
|
||||
// WechatStorage接口的实现
|
||||
type SimpleWechatStorage struct {
|
||||
loginInfo LoginInfo
|
||||
baseRequest BaseRequest
|
||||
webInitResponse WebInitResponse
|
||||
}
|
||||
|
||||
func NewSimpleWechatStorage() *SimpleWechatStorage {
|
||||
return &SimpleWechatStorage{}
|
||||
}
|
||||
|
||||
func (s *SimpleWechatStorage) SetLoginInfo(loginInfo LoginInfo) {
|
||||
s.loginInfo = loginInfo
|
||||
}
|
||||
|
||||
func (s *SimpleWechatStorage) SetBaseRequest(baseRequest BaseRequest) {
|
||||
s.baseRequest = baseRequest
|
||||
}
|
||||
|
||||
func (s *SimpleWechatStorage) SetWebInitResponse(webInitResponse WebInitResponse) {
|
||||
s.webInitResponse = webInitResponse
|
||||
}
|
||||
|
||||
func (s *SimpleWechatStorage) GetLoginInfo() LoginInfo {
|
||||
return s.loginInfo
|
||||
}
|
||||
|
||||
func (s *SimpleWechatStorage) GetBaseRequest() BaseRequest {
|
||||
return s.baseRequest
|
||||
}
|
||||
|
||||
func (s *SimpleWechatStorage) GetWebInitResponse() WebInitResponse {
|
||||
return s.webInitResponse
|
||||
}
|
||||
|
24
user.go
24
user.go
@ -66,14 +66,20 @@ func (u *User) SaveAvatar(filename string) error {
|
||||
if _, err := buffer.ReadFrom(resp.Body); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(filename, buffer.Bytes(), os.ModePerm)
|
||||
file, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
_, err = file.Write(buffer.Bytes())
|
||||
return err
|
||||
}
|
||||
|
||||
func (u *User) sendMsg(msg *SendMessage) error {
|
||||
msg.FromUserName = u.Self.UserName
|
||||
msg.ToUserName = u.UserName
|
||||
info := u.Self.Bot.storage.GetLoginInfo()
|
||||
request := u.Self.Bot.storage.GetBaseRequest()
|
||||
info := u.Self.Bot.storage.LoginInfo
|
||||
request := u.Self.Bot.storage.Request
|
||||
return u.Self.Bot.Caller.WebWxSendMsg(msg, info, request)
|
||||
}
|
||||
|
||||
@ -83,20 +89,20 @@ func (u *User) sendText(content string) error {
|
||||
}
|
||||
|
||||
func (u *User) sendImage(file *os.File) error {
|
||||
request := u.Self.Bot.storage.GetBaseRequest()
|
||||
info := u.Self.Bot.storage.GetLoginInfo()
|
||||
request := u.Self.Bot.storage.Request
|
||||
info := u.Self.Bot.storage.LoginInfo
|
||||
return u.Self.Bot.Caller.WebWxSendImageMsg(file, request, info, u.Self.UserName, u.UserName)
|
||||
}
|
||||
|
||||
func (u *User) setRemarkName(remarkName string) error {
|
||||
request := u.Self.Bot.storage.GetBaseRequest()
|
||||
request := u.Self.Bot.storage.Request
|
||||
return u.Self.Bot.Caller.WebWxOplog(request, remarkName, u.UserName)
|
||||
}
|
||||
|
||||
// 获取用户的详情
|
||||
func (u *User) Detail() (*User, error) {
|
||||
members := Members{u}
|
||||
request := u.Self.Bot.storage.GetBaseRequest()
|
||||
request := u.Self.Bot.storage.Request
|
||||
newMembers, err := u.Self.Bot.Caller.WebWxBatchGetContact(members, request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -140,7 +146,7 @@ func (s *Self) Members(update ...bool) (Members, error) {
|
||||
|
||||
// 更新联系人处理
|
||||
func (s *Self) updateMembers() error {
|
||||
info := s.Bot.storage.GetLoginInfo()
|
||||
info := s.Bot.storage.LoginInfo
|
||||
members, err := s.Bot.Caller.WebWxGetContact(info)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -252,7 +258,7 @@ func (s *Self) UpdateMembersDetail() error {
|
||||
times = count / 50
|
||||
}
|
||||
var newMembers Members
|
||||
request := s.Bot.storage.GetBaseRequest()
|
||||
request := s.Bot.storage.Request
|
||||
var pMembers Members
|
||||
// 分情况依次更新
|
||||
for i := 1; i <= times; i++ {
|
||||
|
Loading…
x
Reference in New Issue
Block a user