提交 c6d5ed39 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

#33 add verify user interface to bridge, and created FriendRequest class

上级 4a46be78
/**
* Wechat for Bot. Connecting ChatBots
*
* Interface for puppet
*
* Class FriendRequest
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
const Wechaty = require('./wechaty')
class FriendRequest {
constructor() {
if (!Wechaty.puppet) {
throw new Error('no Wechaty.puppet instanciated')
}
}
send(contact, message) { throw new Error('pure virtual implement') }
accept() { throw new Error('pure virtual implement') }
}
module.exports = FriendRequest.default = FriendRequest.FriendRequest = FriendRequest
......@@ -200,6 +200,28 @@ class Bridge {
})
}
verifyUserRequest(contactId, message) {
if (!contactId) {
throw new Error('no valid contactId')
}
return this.proxyWechaty('verifyUserRequest', contactId, message)
.catch(e => {
log.error('PuppetWebBridge', 'verifyUserRequest(%s, %s) exception: %s', contactId, message, e.message)
throw e
})
}
verifyUserOk(contactId, ticket) {
if (!contactId || !ticket) {
throw new Error('no valid contactId or ticket')
}
return this.proxyWechaty('verifyUserOk', contactId, ticket)
.catch(e => {
log.error('PuppetWebBridge', 'verifyUserOk(%s, %s) exception: %s', contactId, ticket, e.message)
throw e
})
}
send(toUserName, content) {
return this.proxyWechaty('send', toUserName, content)
.catch(e => {
......
......@@ -14,7 +14,7 @@
/**************************************
*
* Events for Class PuppetWeb
*
*
* here `this` is a PuppetWeb Instance
*
***************************************/
......@@ -26,6 +26,7 @@ const log = require('../brolog-env')
const Contact = require('../contact')
const Message = require('../message')
const MediaMessage = require('../message-media')
const FriendRequest = require('./friend-request')
const PuppetWebEvent = {
onBrowserDead
......@@ -64,7 +65,7 @@ function onBrowserDead(e) {
}
this.scan = null
return co.call(this, function* () {
// log.verbose('PuppetWebEvent', 'onBrowserDead() co() set isBrowserBirthing true')
// this.isBrowserBirthing = true
......@@ -77,7 +78,7 @@ function onBrowserDead(e) {
})
if (!this.browser || !this.bridge) {
const e = new Error('no browser or no bridge')
const e = new Error('no browser or no bridge')
log.error('PuppetWebEvent', 'onBrowserDead() %s', e.message)
throw e
}
......@@ -138,7 +139,7 @@ function onServerScan(data) {
log.verbose('PuppetWebEvent', 'onServerScan(%d)', data && data.code)
this.scan = data // ScanInfo
/**
* When wx.qq.com push a new QRCode to Scan, there will be cookie updates(?)
*/
......@@ -186,7 +187,7 @@ function onServerDisconnect(data) {
log.error('PuppetWebEvent', '%s', e.message)
throw e
}
/**
* conditions:
* 1. browser crash(i.e.: be killed)
......@@ -223,7 +224,7 @@ function onServerDisconnect(data) {
*
* @depreciated 20160825 zixia
* when `unload` there should always be a `disconnect` event?
*
*
* `unload` event is sent from js@browser to webserver via socketio
* after received `unload`, we should fix bridge by re-inject the Wechaty js code into browser.
* possible conditions:
......@@ -250,7 +251,7 @@ function onServerUnload(data) {
log.warn('PuppetWebEvent', 'onServerUnload() %s', e.message)
throw e
}
if (this.browser.dead()) {
log.error('PuppetWebEvent', 'onServerUnload() found browser dead. wait it to restore itself')
return
......@@ -276,7 +277,7 @@ function onServerLogin(data, attempt = 0) {
log.verbose('PuppetWebEvent', 'onServerLogin(%s, %d)', data, attempt)
this.scan = null
if (this.userId) {
log.verbose('PuppetWebEvent', 'onServerLogin() be called but with userId set?')
}
......@@ -308,7 +309,8 @@ function onServerLogin(data, attempt = 0) {
this.emit('login', this.user)
}).catch(e => {
log.error('PuppetWebEvent', 'onServerLogin() exception: %s', e.message)
log.error('PuppetWebEvent', 'onServerLogin() exception: %s', e)
console.log(e.stack)
throw e
})
}
......@@ -333,6 +335,34 @@ function onServerMessage(data) {
let m
// log.warn('PuppetWebEvent', 'MsgType: %s', data.MsgType)
switch (data.MsgType) {
case Message.Type.VERIFYMSG:
log.silly('PuppetWebEvent', 'onServerMessage() received VERIFYMSG')
m = new Message(data)
const request = new FriendRequest()
request.receive(data.RecommendInfo)
this.emit('friend', request.contact, request)
break
case Message.Type.SYS:
log.silly('PuppetWebEvent', 'onServerMessage() received SYSMSG')
m = new Message(data)
/**
* try to find FriendRequest Confirmation Message
*/
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)
}
break
case Message.Type.IMAGE:
// log.verbose('PuppetWebEvent', 'onServerMessage() IMAGE message')
m = new MediaMessage(data)
......
/**
* Wechat for Bot. Connecting ChatBots
*
* Interface for puppet
*
* Class FriendRequest
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
* request/accept: https://github.com/wechaty/wechaty/issues/33
*
* 1. send request
* 2. receive request(in friend event)
* 3. confirmation friendship(friend event)
*
*/
const Wechaty = require('../wechaty')
const Contact = require('../contact')
const FriendRequest = require('../friend-request')
const log = require('../brolog-env')
class PuppetWebFriendRequest extends FriendRequest {
constructor() {
super()
this.type = '' // enum('send', 'receive', 'confirm')
}
receive(info) {
log.verbose('PuppetWebFriendRequest', 'receive(%s)', info)
if (!info || !info.UserName) {
throw new Error('not valid RecommendInfo: ' + info)
}
this.info = info
this.message = info.Content
this.contact = Contact.load(info.UserName)
// ??? this.nick = info.NickName
this.type = 'receive'
}
confirm(contact) {
log.verbose('PuppetWebFriendRequest', 'confirm(%s)', contact)
if (!contact instanceof Contact) {
contact = Contact.load(contact)
}
this.contact = contact
this.type = 'confirm'
}
send(contact, message = 'Hi') {
log.verbose('PuppetWebFriendRequest', 'send(%s)', contact)
if (!contact instanceof Contact) {
contact = Contact.load(contact)
}
this.contact = contact
this.type = 'send'
if (message) {
this.message = message
}
return Wechaty.puppet.friendRequestSend(contact, message)
}
accept() {
log.verbose('FriendRequest', 'accept() %s', this.contact)
if (this.type !== 'receive') {
throw new Error('request on a ' + this.type + ' type')
}
return Wechaty.puppet.friendRequestAccept(contact, message)
}
}
module.exports = PuppetWebFriendRequest.default = PuppetWebFriendRequest.PuppetWebFriendRequest = PuppetWebFriendRequest
import { test } from 'ava'
import { PuppetWebFriendRequest } from './friend-request'
import { Wechaty } from '../wechaty'
import { Contact } from '../contact'
import { Message } from '../message'
Wechaty.puppet = {}
test('PuppetWebFriendRequest.receive smoking test', t => {
const rawMessageData = `
{"MsgId":"3225371967511173931","FromUserName":"fmessage","ToUserName":"@f7321198e0349f1b38c9f2ef158f70eb","MsgType":37,"Content":"<msg fromusername=\\"wxid_a8d806dzznm822\\" encryptusername=\\"v1_c1e03a32c60dd9a9e14f1092132808a2de0ad363f79b303693654282954fbe4d3e12481166f4b841f28de3dd58b0bd54@stranger\\" fromnickname=\\"李卓桓.PreAngel\\" content=\\"我是群聊"Wechaty"的李卓桓.PreAngel\\" shortpy=\\"LZHPREANGEL\\" imagestatus=\\"3\\" scene=\\"14\\" country=\\"CN\\" province=\\"Beijing\\" city=\\"Haidian\\" sign=\\"投资人中最会飞的程序员。好友请加 918999 ,因为本号好友已满。\\" percard=\\"1\\" sex=\\"1\\" alias=\\"zixia008\\" weibo=\\"\\" weibonickname=\\"\\" albumflag=\\"0\\" albumstyle=\\"0\\" albumbgimgid=\\"911623988445184_911623988445184\\" snsflag=\\"49\\" snsbgimgid=\\"http://mmsns.qpic.cn/mmsns/zZSYtpeVianSQYekFNbuiajROicLficBzzeGuvQjnWdGDZ4budZovamibQnoKWba7D2LeuQRPffS8aeE/0\\" snsbgobjectid=\\"12183966160653848744\\" mhash=\\"\\" mfullhash=\\"\\" bigheadimgurl=\\"http://wx.qlogo.cn/mmhead/ver_1/xct7OPTbuU6iaS8gTaK2VibhRs3rATwnU1rCUwWu8ic89EGOynaic2Y4MUdKr66khhAplcfFlm7xbXhum5reania3fXDXH6CI9c3Bb4BODmYAh04/0\\" smallheadimgurl=\\"http://wx.qlogo.cn/mmhead/ver_1/xct7OPTbuU6iaS8gTaK2VibhRs3rATwnU1rCUwWu8ic89EGOynaic2Y4MUdKr66khhAplcfFlm7xbXhum5reania3fXDXH6CI9c3Bb4BODmYAh04/132\\" ticket=\\"v2_ba70dfbdb1b10168d61c1ab491be19e219db11ed5c28701f605efb4dccbf132f664d8a4c9ef6e852b2a4e8d8638be81d125c2e641f01903669539c53f1e582b2@stranger\\" opcode=\\"2\\" googlecontact=\\"\\" qrticket=\\"\\" chatroomusername=\\"2332413729@chatroom\\" sourceusername=\\"\\" sourcenickname=\\"\\"><brandlist count=\\"0\\" ver=\\"670564024\\"></brandlist></msg>","Status":3,"ImgStatus":1,"CreateTime":1475567560,"VoiceLength":0,"PlayLength":0,"FileName":"","FileSize":"","MediaId":"","Url":"","AppMsgType":0,"StatusNotifyCode":0,"StatusNotifyUserName":"","RecommendInfo":{"UserName":"@04a0fa314d0d8d50dc54e2ec908744ebf46b87404d143fd9a6692182dd90bd49","NickName":"李卓桓.PreAngel","Province":"北京","City":"海淀","Content":"我是群聊\\"Wechaty\\"的李卓桓.PreAngel","Signature":"投资人中最会飞的程序员。好友请加 918999 ,因为本号好友已满。","Alias":"zixia008","Scene":14,"AttrStatus":233251,"Sex":1,"Ticket":"v2_ba70dfbdb1b10168d61c1ab491be19e219db11ed5c28701f605efb4dccbf132f664d8a4c9ef6e852b2a4e8d8638be81d125c2e641f01903669539c53f1e582b2@stranger","OpCode":2,"HeadImgUrl":"/cgi-bin/mmwebwx-bin/webwxgeticon?seq=0&username=@04a0fa314d0d8d50dc54e2ec908744ebf46b87404d143fd9a6692182dd90bd49&skey=@crypt_f9cec94b_5b073dca472bd5e41771d309bb8c37bd&msgid=3225371967511173931","MMFromVerifyMsg":true},"ForwardFlag":0,"AppInfo":{"AppID":"","Type":0},"HasProductId":0,"Ticket":"","ImgHeight":0,"ImgWidth":0,"SubMsgType":0,"NewMsgId":3225371967511174000,"MMPeerUserName":"fmessage","MMDigest":"李卓桓.PreAngel想要将你加为朋友","MMIsSend":false,"MMIsChatRoom":false,"MMUnread":true,"LocalID":"3225371967511173931","ClientMsgId":"3225371967511173931","MMActualContent":"<msg fromusername=\\"wxid_a8d806dzznm822\\" encryptusername=\\"v1_c1e03a32c60dd9a9e14f1092132808a2de0ad363f79b303693654282954fbe4d3e12481166f4b841f28de3dd58b0bd54@stranger\\" fromnickname=\\"李卓桓.PreAngel\\" content=\\"我是群聊"Wechaty"的李卓桓.PreAngel\\" shortpy=\\"LZHPREANGEL\\" imagestatus=\\"3\\" scene=\\"14\\" country=\\"CN\\" province=\\"Beijing\\" city=\\"Haidian\\" sign=\\"投资人中最会飞的程序员。好友请加 918999 ,因为本号好友已满。\\" percard=\\"1\\" sex=\\"1\\" alias=\\"zixia008\\" weibo=\\"\\" weibonickname=\\"\\" albumflag=\\"0\\" albumstyle=\\"0\\" albumbgimgid=\\"911623988445184_911623988445184\\" snsflag=\\"49\\" snsbgimgid=\\"http://mmsns.qpic.cn/mmsns/zZSYtpeVianSQYekFNbuiajROicLficBzzeGuvQjnWdGDZ4budZovamibQnoKWba7D2LeuQRPffS8aeE/0\\" snsbgobjectid=\\"12183966160653848744\\" mhash=\\"\\" mfullhash=\\"\\" bigheadimgurl=\\"http://wx.qlogo.cn/mmhead/ver_1/xct7OPTbuU6iaS8gTaK2VibhRs3rATwnU1rCUwWu8ic89EGOynaic2Y4MUdKr66khhAplcfFlm7xbXhum5reania3fXDXH6CI9c3Bb4BODmYAh04/0\\" smallheadimgurl=\\"http://wx.qlogo.cn/mmhead/ver_1/xct7OPTbuU6iaS8gTaK2VibhRs3rATwnU1rCUwWu8ic89EGOynaic2Y4MUdKr66khhAplcfFlm7xbXhum5reania3fXDXH6CI9c3Bb4BODmYAh04/132\\" ticket=\\"v2_ba70dfbdb1b10168d61c1ab491be19e219db11ed5c28701f605efb4dccbf132f664d8a4c9ef6e852b2a4e8d8638be81d125c2e641f01903669539c53f1e582b2@stranger\\" opcode=\\"2\\" googlecontact=\\"\\" qrticket=\\"\\" chatroomusername=\\"2332413729@chatroom\\" sourceusername=\\"\\" sourcenickname=\\"\\"><brandlist count=\\"0\\" ver=\\"670564024\\"></brandlist></msg>","MMActualSender":"fmessage","MMDigestTime":"15:52","MMDisplayTime":1475567560,"MMTime":"15:52"}
`
const m = new Message(rawMessageData)
const fr = new PuppetWebFriendRequest()
fr.receive(m.rawObj.RecommendInfo)
t.true(typeof fr.info === 'object', 'should has info object')
t.is(fr.message, '我是群聊"Wechaty"的李卓桓.PreAngel', 'should has right request message')
t.true(fr.contact instanceof Contact, 'should have a Contact instance')
t.is(fr.type, 'receive', 'should be receive type')
})
test('PuppetWebFriendRequest.confirm smoking test', t => {
const rawMessageData = `
{"MsgId":"3382012679535022763","FromUserName":"@04a0fa314d0d8d50dc54e2ec908744ebf46b87404d143fd9a6692182dd90bd49","ToUserName":"@f7321198e0349f1b38c9f2ef158f70eb","MsgType":10000,"Content":"You have added 李卓桓.PreAngel as your WeChat contact. Start chatting!","Status":4,"ImgStatus":1,"CreateTime":1475569920,"VoiceLength":0,"PlayLength":0,"FileName":"","FileSize":"","MediaId":"","Url":"","AppMsgType":0,"StatusNotifyCode":0,"StatusNotifyUserName":"","RecommendInfo":{"UserName":"","NickName":"","QQNum":0,"Province":"","City":"","Content":"","Signature":"","Alias":"","Scene":0,"VerifyFlag":0,"AttrStatus":0,"Sex":0,"Ticket":"","OpCode":0},"ForwardFlag":0,"AppInfo":{"AppID":"","Type":0},"HasProductId":0,"Ticket":"","ImgHeight":0,"ImgWidth":0,"SubMsgType":0,"NewMsgId":3382012679535022600,"MMPeerUserName":"@04a0fa314d0d8d50dc54e2ec908744ebf46b87404d143fd9a6692182dd90bd49","MMDigest":"You have added 李卓桓.PreAngel as your WeChat contact. Start chatting!","MMIsSend":false,"MMIsChatRoom":false,"LocalID":"3382012679535022763","ClientMsgId":"3382012679535022763","MMActualContent":"You have added 李卓桓.PreAngel as your WeChat contact. Start chatting!","MMActualSender":"@04a0fa314d0d8d50dc54e2ec908744ebf46b87404d143fd9a6692182dd90bd49","MMDigestTime":"16:32","MMDisplayTime":1475569920,"MMTime":"16:32"}
`
const m = new Message(rawMessageData)
t.true(/^You have added (.+) as your WeChat contact. Start chatting!$/.test(m.get('content')), 'should match confirm message')
const fr = new PuppetWebFriendRequest()
const contact = Contact.load(m.get('from'))
fr.confirm(contact)
t.true(fr.contact instanceof Contact, 'should have a Contact instance')
t.is(fr.type, 'confirm', 'should be confirm type')
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册