diff --git a/src/contact.js b/src/contact.js index baabc6af318d2338929fef9e6ca331b07c8c8e89..0d4940630646bd930e60c4ef5b4d661f0975ce45 100644 --- a/src/contact.js +++ b/src/contact.js @@ -1,15 +1,60 @@ +/** + * + * wechaty: Wechat for Bot. and for human who talk to bot/robot + * + * Licenst: ISC + * https://github.com/zixia/wechaty + * + */ class Contact { constructor(id) { + if (!Contact.puppet) throw new Error('no puppet attached to Contact'); + this.id = id + this.obj = {} + + Contact.puppet.getContact(id) + .then(data => { + this.rawObj = data + this.obj = this.parse(this.rawObj) + }).catch(e => { + throw new Error('getContact: ' + e) + }) + } + + parse(rawObj) { + return !rawObj ? {} : { + id: rawObj.UserName + , weixin: rawObj.Alias + , name: rawObj.NickName + , remark: rawObj.RemarkName + , sex: rawObj.Sex + , province: rawObj.Province + , city: rawObj.City + , signature: rawObj.Signature + } + } + + dumpRaw() { + console.error('======= dump raw contact =======') + Object.keys(this.rawObj).forEach(k => console.error(`${k}: ${this.rawObj[k]}`)) + } + dump() { + console.error('======= dump contact =======') + Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj[k]}`)) } toString() { return `Contact({id:${this.id})` } + getId() { return this.id } + get(prop) { return this.obj[prop] } + + send(message) { } @@ -21,4 +66,14 @@ class Contact { } } +Contact.pool = {} +Contact.load = function (id) { + if (id in Contact.pool) { + return Contact.pool[id] + } + return Contact.pool[id] = new Contact(id) +} + +Contact.attach = function (puppet) { Contact.puppet = puppet } + module.exports = Contact diff --git a/src/group.js b/src/group.js index 124f9b0e7ea56ee495f8432f7a695bd65f8a49be..f2e6670f161eba14bc74cc0adf6b734d7a1c7293 100644 --- a/src/group.js +++ b/src/group.js @@ -1,3 +1,12 @@ +/** + * + * wechaty: Wechat for Bot. and for human who talk to bot/robot + * + * Licenst: ISC + * https://github.com/zixia/wechaty + * + */ + class Group { constructor(id) { this.id = id diff --git a/src/message.js b/src/message.js index b7e39fed71fb5a68b677f76e919078d27a154771..0cdf1b2ad23e0b4c80f594c7bd67a24866aa2cad 100644 --- a/src/message.js +++ b/src/message.js @@ -1,4 +1,12 @@ -//const EventEmitter = require('events') +/** + * + * wechaty: Wechat for Bot. and for human who talk to bot/robot + * + * Licenst: ISC + * https://github.com/zixia/wechaty + * + */ + const Contact = require('./contact') const Group = require('./group') @@ -7,11 +15,11 @@ class Message { this.rawObj = rawObj = rawObj || {} // Transform rawObj to local m - this.m = { + this.obj = { id: rawObj.MsgId , type: rawObj.MsgType - , from: new Contact(rawObj.MMActualSender) - , to: new Contact(rawObj.MMPeerUserName) + , from: Contact.load(rawObj.MMActualSender) + , to: Contact.load(rawObj.MMPeerUserName) , group: rawObj.MMIsChatRoom ? new Group(rawObj.FromUserName) : null , content: rawObj.MMActualContent , status: rawObj.Status @@ -23,31 +31,27 @@ class Message { } toString() { - const id = this.m.id - // Contact - const from = this.m.from.getId() - const to = this.m.to.getId() - //return `Message({id:${id}, from:${from}, to:${to})` - let content = this.m.content + const name = this.obj.from.get('name') + let content = this.obj.content if (content.length > 20) content = content.substring(0,17) + '...'; - return `Message("${content}")` + return `Message("${name}: ${content}")` } get(prop) { - if (!prop || !(prop in this.m)) { - const s = '[' + Object.keys(this.m).join(',') + ']' + if (!prop || !(prop in this.obj)) { + const s = '[' + Object.keys(this.obj).join(',') + ']' throw new Error(`Message.get(${prop}) must be in: ${s}`) } - return this.m[prop] + return this.obj[prop] } set(prop, value) { - this.m[prop] = value + this.obj[prop] = value } dump() { console.error('======= dump message =======') - Object.keys(this.m).forEach(k => console.error(`${k}: ${this.m[k]}`)) + Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj[k]}`)) } dumpRaw() { console.error('======= dump raw message =======') diff --git a/src/puppet-web-injectio.js b/src/puppet-web-injectio.js index 092d538aff4783cbcc2686fcbe26a80512e4e95e..ec46b5566f120d7d8bd5497d8ce0de89093b922e 100644 --- a/src/puppet-web-injectio.js +++ b/src/puppet-web-injectio.js @@ -57,16 +57,14 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?'; , slog: slog // log throw Socket IO , ding: ding , quit: quit + + , getContact: getContact } function isReady() { return !!((typeof angular)!=='undefined' && angular.element && angular.element("body")) } function init() { - // XXX - // return 'init skiped in browser' - - if (!isReady()) { clog('angular not ready. wait 500ms...') setTimeout(init, 500) @@ -87,8 +85,9 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?'; var injector = angular.element(document).injector() var rootScope = injector.get("$rootScope") var http = injector.get("$http") - var chatFactory = injector.get("chatFactory") - var confFactory = injector.get("confFactory") + var chatFactory = injector.get("chatFactory") + var confFactory = injector.get("confFactory") + var contactFactory = injector.get('contactFactory') var loginScope = angular.element(".login_box").scope() // get all we need from wx in browser(angularjs) @@ -96,8 +95,9 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?'; injector: injector , rootScope: rootScope , http: http - , chatFactory: chatFactory - , confFactory: confFactory + , chatFactory: chatFactory + , confFactory: confFactory + , contactFactory: contactFactory , loginScope: loginScope } } @@ -122,6 +122,7 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?'; chat.appendMessage(m) return chat.sendMessage(m) } + function getContact(id) { return Wechaty.glue.contactFactory.getContact(id) } function hookMessage() { var rootScope = Wechaty.glue.rootScope rootScope.$on("message:add:success", function (event, data) { @@ -174,7 +175,7 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?'; var callback = arguments[arguments.length - 1] if (typeof callback==='function') - callback('Wechaty') + return callback('Wechaty') return 'Wechaty' diff --git a/src/puppet-web.js b/src/puppet-web.js index 55377c10c91012a99c5e20ac2e8acf1a61d8187f..ec93ab21b68f15c7e76f5731b211df9be7ec07d3 100644 --- a/src/puppet-web.js +++ b/src/puppet-web.js @@ -96,12 +96,9 @@ class PuppetWeb extends Puppet { * Public Methods * */ - getLoginQrImgUrl() { - return this.server.proxyWechaty('getLoginQrImgUrl') - } - getLoginStatusCode() { - return this.server.proxyWechaty('getLoginStatusCode') - } + getLoginQrImgUrl() { return this.server.proxyWechaty('getLoginQrImgUrl') } + getLoginStatusCode() { return this.server.proxyWechaty('getLoginStatusCode') } + getContact(id) { return this.server.proxyWechaty('getContact', id) } } module.exports = PuppetWeb diff --git a/src/puppet.js b/src/puppet.js index de85c97fd0f0b631fbbf3c1739ea368c719a3ede..a7960395019644b5f42d2ef51ed5b6eea08aae68 100644 --- a/src/puppet.js +++ b/src/puppet.js @@ -32,6 +32,8 @@ class Puppet extends EventEmitter { logout() { throw new Error('To Be Implementsd') } alive() { throw new Error('To Be Implementsd') } + getContact() { throw new Error('To Be Implementsd') } + // () { throw new Error('To Be Implemented') } /** diff --git a/src/wechaty.js b/src/wechaty.js index b5a21387f1098247c879f3dbb588df3f99522cfe..1887be1835a38abe8954cacc1a8bd4619ed046a8 100644 --- a/src/wechaty.js +++ b/src/wechaty.js @@ -1,3 +1,12 @@ +/** + * + * wechaty: Wechat for Bot. and for human who talk to bot/robot + * + * Licenst: ISC + * https://github.com/zixia/wechaty + * + */ + const EventEmitter = require('events') //const Util = require('util'); @@ -23,6 +32,7 @@ class Wechaty extends EventEmitter { throw new Error('Puppet unknown: ' + puppet) break } + Contact.attach(this.puppet) this.puppet.on('message', (e) => { this.emit('message', e)