add message file download support
This commit is contained in:
parent
526431f400
commit
96d14f39f6
2
bot.go
2
bot.go
@ -193,7 +193,7 @@ func (b *Bot) Block() error {
|
||||
if b.self == nil {
|
||||
return errors.New("`Block` must be called after user login")
|
||||
}
|
||||
if _, closed := <-b.exit; closed {
|
||||
if _, closed := <-b.exit; !closed {
|
||||
return errors.New("can not call `Block` after user logout")
|
||||
}
|
||||
close(b.exit)
|
||||
|
15
bot_test.go
15
bot_test.go
@ -2,22 +2,31 @@ package openwechat
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDefaultBot(t *testing.T) {
|
||||
bot := DefaultBot()
|
||||
messageHandler := func(message *Message) {
|
||||
fmt.Println(message.Content)
|
||||
if message.HasFile() {
|
||||
if message.IsMedia() {
|
||||
resp, err := message.GetFile()
|
||||
if err == nil {
|
||||
data, _ := ioutil.ReadAll(resp.Body)
|
||||
ioutil.WriteFile(message.EncryFileName, data, 0x777)
|
||||
resp.Body.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
bot.RegisterMessageHandler(messageHandler)
|
||||
bot.UUIDCallback = PrintlnQrcodeUrl
|
||||
if err := bot.Login(); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
|
||||
}
|
||||
bot.Block()
|
||||
fmt.Println(bot.Block())
|
||||
}
|
||||
|
||||
func TestBotMessageHandler(t *testing.T) {
|
||||
|
31
client.go
31
client.go
@ -359,3 +359,34 @@ func (c *Client) WebWxGetMsgImg(msg *Message, info LoginInfo) (*http.Response, e
|
||||
path.RawQuery = params.Encode()
|
||||
return c.Get(path.String())
|
||||
}
|
||||
|
||||
func (c *Client) WebWxGetVoice(msg *Message, info LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(webWxGetVoiceUrl)
|
||||
params := url.Values{}
|
||||
params.Add("msgid", msg.MsgId)
|
||||
params.Add("skey", info.SKey)
|
||||
path.RawQuery = params.Encode()
|
||||
return c.Get(path.String())
|
||||
}
|
||||
|
||||
func (c *Client) WebWxGetVideo(msg *Message, info LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(webWxGetVideoUrl)
|
||||
params := url.Values{}
|
||||
params.Add("msgid", msg.MsgId)
|
||||
params.Add("skey", info.SKey)
|
||||
path.RawQuery = params.Encode()
|
||||
return c.Get(path.String())
|
||||
}
|
||||
|
||||
func (c *Client) WebWxGetMedia(msg *Message, info LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(webWxGetMediaUrl)
|
||||
params := url.Values{}
|
||||
params.Add("sender", msg.FromUserName)
|
||||
params.Add("mediaid", msg.MediaId)
|
||||
params.Add("encryfilename", msg.EncryFileName)
|
||||
params.Add("fromuser", fmt.Sprintf("%d", info.WxUin))
|
||||
params.Add("pass_ticket", info.PassTicket)
|
||||
params.Add("webwx_data_ticket", getWebWxDataTicket(c.Jar.Cookies(path)))
|
||||
path.RawQuery = params.Encode()
|
||||
return c.Get(path.String())
|
||||
}
|
||||
|
@ -29,7 +29,10 @@ const (
|
||||
webWxVerifyUserUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxverifyuser"
|
||||
syncCheckUrl = "https://webpush.wx2.qq.com/cgi-bin/mmwebwx-bin/synccheck"
|
||||
webWxUpLoadMediaUrl = "https://file.wx2.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia"
|
||||
webWxGetMsgImgUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg"
|
||||
webWxGetMsgImgUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg"
|
||||
webWxGetVoiceUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetvoice"
|
||||
webWxGetVideoUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetvideo"
|
||||
webWxGetMediaUrl = "https://file.wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetmedia"
|
||||
|
||||
jsonContentType = "application/json; charset=utf-8"
|
||||
)
|
||||
|
26
message.go
26
message.go
@ -159,7 +159,7 @@ func (m *Message) IsVideo() bool {
|
||||
return m.MsgType == 43 || m.MsgType == 62
|
||||
}
|
||||
|
||||
func (m *Message) IsSharing() bool {
|
||||
func (m *Message) IsMedia() bool {
|
||||
return m.MsgType == 49
|
||||
}
|
||||
|
||||
@ -175,11 +175,27 @@ func (m *Message) IsNotify() bool {
|
||||
return m.MsgType == 51 && m.StatusNotifyCode != 0
|
||||
}
|
||||
|
||||
func (m *Message) GetMsgImageResponse() (*http.Response, error) {
|
||||
if !m.IsPicture() {
|
||||
return nil, errors.New("Picture type message required")
|
||||
func (m *Message) HasFile() bool {
|
||||
return m.IsPicture() || m.IsVoice() || m.IsVideo() || m.IsMedia()
|
||||
}
|
||||
|
||||
func (m *Message) GetFile() (*http.Response, error) {
|
||||
if !m.HasFile() {
|
||||
return nil, errors.New("invalid message type")
|
||||
}
|
||||
return m.Bot.Caller.Client.WebWxGetMsgImg(m, m.Bot.storage.GetLoginInfo())
|
||||
if m.IsPicture() {
|
||||
return m.Bot.Caller.Client.WebWxGetMsgImg(m, m.Bot.storage.GetLoginInfo())
|
||||
}
|
||||
if m.IsVoice() {
|
||||
return m.Bot.Caller.Client.WebWxGetVoice(m, m.Bot.storage.GetLoginInfo())
|
||||
}
|
||||
if m.IsVideo() {
|
||||
return m.Bot.Caller.Client.WebWxGetVideo(m, m.Bot.storage.GetLoginInfo())
|
||||
}
|
||||
if m.IsMedia() {
|
||||
return m.Bot.Caller.Client.WebWxGetMedia(m, m.Bot.storage.GetLoginInfo())
|
||||
}
|
||||
return nil, errors.New("unsupported type")
|
||||
}
|
||||
|
||||
// 用在多个messageHandler之间传递信息
|
||||
|
Loading…
x
Reference in New Issue
Block a user