[New Feature] FriendRequest class and event
Created by: huan
according to #14 (closed)
WxApp
class contactFactory {
verifyUser({
UserName: string
, Opcode: confFactory.VERIFYUSER_OPCODE_VERIFYOK
, Scene: confFactory.ADDSCENE_PF_WEB
, Ticket: Ticket
, VerifyContent: string
})
}
type RecommendInfo = {
UserName: string
NickName: string
Content: string // request message
Ticket: string // a pass token
VerifyFlag: number
}
interface Message = {
RecommendInfo: RecommendInfo
}
FriendRequest
Wechaty Class - send request to other
- receive request from other(in
friend
event)
class FriendRequest {
private info: RecommentInfo
private hello: string
private ticket: string
private contact: Contact
constructor(): FriendRequest
send(contact: string|Contact, hello?: string): Promise<any> // 1. send friend request to other
receive(info?: RecommendInfo): FriendRequest // 2. receive request from other
accept(): Promise<any>
}
Interface Wechaty {
on('friend', (contact: Contact, request: FriendRequest) => void): void
}
Add Friend / Send Friend Request
accept friend requirest interface should be like this:
const sender : Contact = message.get('from')
const fr = new FriendRequest()
fr.send(sender, 'hello~')
Event: Friend Request
- Receive New Friend Request
- Confirmation of FriendShip
friend request is a message from SP_CONTACT_RECOMMEND_HELPER: "fmessage"
, with type Message.Type.VERIFYMSG
in PuppetWeb.onServerMessage()
:
if (message.MsgType === MSGTYPE_VERIFYMSG) {
const request = new FriendRequest()
request.receive(message.RecommendInfo)
emit('friend', request.contact, request)
}
// Be Accepted: I've accepted your friend request. Now let's chat!
// Accept: You have added ${NickName} as your WeChat contact. Start chatting!
case Message.Type.SYS:
if (/^You have added (.+) as your WeChat contact. Start chatting!$/.test(m.get('content'))) {
const request = new FriendRequest()
const contact = Contact.load(m.get('from'))
request.confirm(contact)
this.emit('friend', contact)
}
then we can:
wechaty.on('friend', (contact, request) => {
if (request) { // 1. we got new request
if (/secret/.test(request.hello)) {
request.accept()
}
} else { // 2. we got new friend
console.log('got new friend: ' + contact)
}
})