添加同意好友请求支持

This commit is contained in:
eatMoreApple 2021-04-27 15:17:18 +08:00
parent 72338d99bb
commit 2d4d7e7d78
4 changed files with 491 additions and 408 deletions

View File

@ -196,3 +196,20 @@ func TestSendMessage(t *testing.T) {
return return
} }
} }
func TestAgreeFriendsAdd(t *testing.T) {
bot := defaultBot()
bot.MessageHandler = func(msg *Message) {
if msg.IsFriendAdd() {
if err := msg.Agree(); err != nil {
t.Error(err)
}
bot.Logout()
}
}
if err := bot.Login(); err != nil {
t.Error(err)
return
}
bot.Block()
}

View File

@ -245,6 +245,12 @@ func (c *Caller) RemoveFriendFromChatRoom(req *BaseRequest, info *LoginInfo, gro
return parseBaseResponseError(resp) return parseBaseResponseError(resp)
} }
// 同意加好友请求
func (c *Caller) WebWxVerifyUser(storage *Storage, info RecommendInfo, verifyContent string) error {
resp := NewReturnResponse(c.Client.WebWxVerifyUser(storage, info, verifyContent))
return parseBaseResponseError(resp)
}
// 处理响应返回的结果是否正常 // 处理响应返回的结果是否正常
func parseBaseResponseError(resp *ReturnResponse) error { func parseBaseResponseError(resp *ReturnResponse) error {
if resp.Err() != nil { if resp.Err() != nil {

View File

@ -340,7 +340,7 @@ func (c *Client) WebWxVerifyUser(storage *Storage, info RecommendInfo, verifyCon
content := map[string]interface{}{ content := map[string]interface{}{
"BaseRequest": storage.Request, "BaseRequest": storage.Request,
"Opcode": 3, "Opcode": 3,
"SceneList": []int{33}, "SceneList": [1]int{33},
"SceneListCount": 1, "SceneListCount": 1,
"VerifyContent": verifyContent, "VerifyContent": verifyContent,
"VerifyUserList": []interface{}{map[string]string{ "VerifyUserList": []interface{}{map[string]string{

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/xml" "encoding/xml"
"errors" "errors"
"fmt"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
@ -162,7 +163,7 @@ func (m *Message) IsVoice() bool {
} }
func (m *Message) IsFriendAdd() bool { func (m *Message) IsFriendAdd() bool {
return m.MsgType == 37 return m.MsgType == 37 && m.FromUserName == "fmessage"
} }
func (m *Message) IsCard() bool { func (m *Message) IsCard() bool {
@ -225,6 +226,29 @@ func (m *Message) Card() (*Card, error) {
return &card, err return &card, err
} }
// 获取FriendAddMessageContent内容
func (m *Message) FriendAddMessageContent() (*FriendAddMessageContent, error) {
if !m.IsFriendAdd() {
return nil, errors.New("friend add message required")
}
var f FriendAddMessageContent
content := XmlFormString(m.Content)
err := xml.Unmarshal([]byte(content), &f)
return &f, err
}
// 同意好友的请求
func (m *Message) Agree(verifyContents ...string) error {
if !m.IsFriendAdd() {
return fmt.Errorf("friend add message required")
}
var builder strings.Builder
for _, v := range verifyContents {
builder.WriteString(v)
}
return m.Bot.Caller.WebWxVerifyUser(m.Bot.storage, m.RecommendInfo, builder.String())
}
// 往消息上下文中设置值 // 往消息上下文中设置值
// goroutine safe // goroutine safe
func (m *Message) Set(key string, value interface{}) { func (m *Message) Set(key string, value interface{}) {
@ -268,13 +292,6 @@ func (m *Message) init(bot *Bot) {
} }
} }
//func (m *Message) Agree() error {
// if !m.IsFriendAdd() {
// return fmt.Errorf("the excepted message type is 37, but got %d", m.MsgType)
// }
// return m.Bot.Caller.Client.WebWxVerifyUser(m.Bot.storage, m.RecommendInfo, "")
//}
// 发送消息的结构体 // 发送消息的结构体
type SendMessage struct { type SendMessage struct {
Type int Type int
@ -351,3 +368,46 @@ type Card struct {
BrandFlags string `xml:"brandFlags,attr"` BrandFlags string `xml:"brandFlags,attr"`
RegionCode string `xml:"regionCode,attr"` RegionCode string `xml:"regionCode,attr"`
} }
// 好友添加消息信息内容
type FriendAddMessageContent struct {
XMLName xml.Name `xml:"msg"`
Shortpy int `xml:"shortpy,attr"`
ImageStatus int `xml:"imagestatus,attr"`
Scene int `xml:"scene,attr"`
PerCard int `xml:"percard,attr"`
Sex int `xml:"sex,attr"`
AlbumFlag int `xml:"albumflag,attr"`
AlbumStyle int `xml:"albumstyle,attr"`
SnsFlag int `xml:"snsflag,attr"`
Opcode int `xml:"opcode,attr"`
FromUserName string `xml:"fromusername,attr"`
EncryptUserName string `xml:"encryptusername,attr"`
FromNickName string `xml:"fromnickname,attr"`
Content string `xml:"content,attr"`
Country string `xml:"country,attr"`
Province string `xml:"province,attr"`
City string `xml:"city,attr"`
Sign string `xml:"sign,attr"`
Alias string `xml:"alias,attr"`
WeiBo string `xml:"weibo,attr"`
AlbumBgImgId string `xml:"albumbgimgid,attr"`
SnsBgImgId string `xml:"snsbgimgid,attr"`
SnsBgObjectId string `xml:"snsbgobjectid,attr"`
MHash string `xml:"mhash,attr"`
MFullHash string `xml:"mfullhash,attr"`
BigHeadImgUrl string `xml:"bigheadimgurl,attr"`
SmallHeadImgUrl string `xml:"smallheadimgurl,attr"`
Ticket string `xml:"ticket,attr"`
GoogleContact string `xml:"googlecontact,attr"`
QrTicket string `xml:"qrticket,attr"`
ChatRoomUserName string `xml:"chatroomusername,attr"`
SourceUserName string `xml:"sourceusername,attr"`
ShareCardUserName string `xml:"sharecardusername,attr"`
ShareCardNickName string `xml:"sharecardnickname,attr"`
CardVersion string `xml:"cardversion,attr"`
BrandList struct {
Count int `xml:"count,attr"`
Ver int64 `xml:"ver,attr"`
} `xml:"brandlist"`
}