From 0064056ca6a812fb849faa02ad20a995a16a16e8 Mon Sep 17 00:00:00 2001 From: Zhuohuan LI Date: Sat, 18 Jun 2016 13:13:28 +0800 Subject: [PATCH] add: Wechaty.self() to test if a message is from self. del: Message.self() --- example/api-ai-bot.js | 2 +- example/ding-dong-bot.js | 4 ++-- example/roger-bot.js | 2 +- example/tuling123-bot.js | 2 +- src/message.js | 10 ---------- src/puppet-web-event.js | 12 ++++++------ src/puppet-web.js | 18 ++++++++++++++---- src/wechaty.js | 9 ++++++++- 8 files changed, 33 insertions(+), 26 deletions(-) diff --git a/example/api-ai-bot.js b/example/api-ai-bot.js index 08b392a1..360d922d 100644 --- a/example/api-ai-bot.js +++ b/example/api-ai-bot.js @@ -46,7 +46,7 @@ bot .on('login' , user => log.info('Bot', `bot login: ${user}`)) .on('logout' , e => log.info('Bot', 'bot logout.')) .on('message', m => { - if (m.self()) { return } + if (bot.self(m)) { return } co(function* () { const msg = yield m.ready() diff --git a/example/ding-dong-bot.js b/example/ding-dong-bot.js index 07da4a56..ffc1b39a 100644 --- a/example/ding-dong-bot.js +++ b/example/ding-dong-bot.js @@ -32,11 +32,11 @@ bot .on('login' , user => log.info('Bot', `${user.name()} logined`)) .on('logout' , user => log.info('Bot', `${user.name()} logouted`)) .on('scan', ({url, code}) => { - console.log(`Scan QR Code in url to login: ${code}\n${url}`) if (!/201|200/.test(code)) { let loginUrl = url.replace(/\/qrcode\//, '/l/') require('qrcode-terminal').generate(loginUrl) } + console.log(`${url}\n[${code}] Scan QR Code in above url to login: `) }) .on('message', m => { m.ready() @@ -44,7 +44,7 @@ bot log.info('Bot', 'recv: %s', msg.toStringEx()) // logToFile(JSON.stringify(msg.rawObj)) - if (/^(ding|ping|bing)$/i.test(m.get('content')) && !m.self()) { + if (/^(ding|ping|bing)$/i.test(m.get('content')) && !bot.self(m)) { bot.reply(m, 'dong') .then(() => { log.warn('Bot', 'REPLY: dong') }) } diff --git a/example/roger-bot.js b/example/roger-bot.js index 7d0a480c..5b5ffd47 100644 --- a/example/roger-bot.js +++ b/example/roger-bot.js @@ -6,7 +6,7 @@ bot console.log(`Use Wechat to Scan QR Code in url to login: ${code}\n${url}`) }) .on('message', m => { - (!m.self()) && bot.reply(m, 'roger') // 1. reply others' msg + (!bot.self(m)) && bot.reply(m, 'roger') // 1. reply others' msg .then(() => console.log(`RECV: ${m}, REPLY: "roger"`)) // 2. log message .catch(e => console.error(e)) // 3. catch exception }) diff --git a/example/tuling123-bot.js b/example/tuling123-bot.js index 6758c6de..561c9ecf 100644 --- a/example/tuling123-bot.js +++ b/example/tuling123-bot.js @@ -47,7 +47,7 @@ bot console.log(`Scan QR Code in url to login: w${code}\n${url}`) }) .on('message', m => { - if (m.self()) return + if (bot.self(m)) return co(function* () { const msg = yield m.ready() diff --git a/src/message.js b/src/message.js index 42bb3601..191ab1f0 100644 --- a/src/message.js +++ b/src/message.js @@ -34,8 +34,6 @@ class Message { , status: rawObj.Status , digest: rawObj.MMDigest , date: rawObj.MMDisplayTime // Javascript timestamp of milliseconds - - , self: undefined // to store the logined user id } // FIXME: has ther any better method to know the room ID? @@ -87,14 +85,6 @@ class Message { typeEx() { return Message.Type[this.obj.type] } count() { return Message.counter } - self() { - if (!this.obj.self) { - log.warn('Message', 'self not set') - return false - } - return this.obj.self === this.obj.from - } - ready() { log.silly('Message', 'ready()') diff --git a/src/puppet-web-event.js b/src/puppet-web-event.js index 2276a7d7..6389414e 100644 --- a/src/puppet-web-event.js +++ b/src/puppet-web-event.js @@ -210,16 +210,16 @@ function onServerLogin(data) { /** * save login user id to this.userId */ - const userName = this.userId = yield this.bridge.getUserName() + this.userId = yield this.bridge.getUserName() - if (!userName) { + if (!this.userId) { log.verbose('PuppetWebEvent', 'onServerLogin: browser not full loaded, retry later.') setTimeout(onServerLogin.bind(this), 500) return } - log.verbose('PuppetWebEvent', 'bridge.getUserName: %s', userName) - this.user = yield Contact.load(userName).ready() + log.verbose('PuppetWebEvent', 'bridge.getUserName: %s', this.userId) + this.user = yield Contact.load(this.userId).ready() log.verbose('PuppetWebEvent', `onServerLogin() user ${this.user.name()} logined`) this.emit('login', this.user) @@ -263,8 +263,8 @@ function onServerMessage(data) { break; } - if (this.user) { - m.set('self', this.user.id) + if (this.userId) { + m.set('self', this.userId) } else { log.warn('PuppetWebEvent', 'onServerMessage() without this.user') } diff --git a/src/puppet-web.js b/src/puppet-web.js index 10a38e39..65bcd101 100644 --- a/src/puppet-web.js +++ b/src/puppet-web.js @@ -42,7 +42,8 @@ class PuppetWeb extends Puppet { this.head = options.head this.session = options.session // if not set session, then dont store session. - this.user = null // of user self + this.userId = null // user id + this.user = null // of user self } toString() { return `Class PuppetWeb({browser:${this.browser},port:${this.port}})` } @@ -235,6 +236,17 @@ class PuppetWeb extends Puppet { return Event.onBrowserDead.call(this, e) } + self(message) { + if (!this.userId) { + log.verbose('PuppetWeb', 'self() got no this.userId') + return false + } + if (!message || !message.id) { + log.verbose('PuppetWeb', 'self() got no message') + return false + } + return this.userId == message.get('from') + } send(message) { const to = message.get('to') const room = message.get('room') @@ -268,10 +280,8 @@ class PuppetWeb extends Puppet { .set('to' , message.obj.from) .set('room' , message.obj.room) - if (this.user) { + if (this.userId) { // FIXME: find a alternate way to check a message create by `self` - m.set('self', this.user.id) - } else if (this.userId) { m.set('self', this.userId) } else { diff --git a/src/wechaty.js b/src/wechaty.js index 20f5a6be..17dd4987 100644 --- a/src/wechaty.js +++ b/src/wechaty.js @@ -129,7 +129,14 @@ class Wechaty extends EventEmitter { }) } - send(message) { + self(message) { + return this.puppet.self(message) + .catch(e => { + log.error('Wechaty', 'self() exception: %s', e.message) + throw e + }) + } + send(message) { return this.puppet.send(message) .catch(e => { log.error('Wechaty', 'send() exception: %s', e.message) -- GitLab