diff --git a/.gitignore b/.gitignore index a12afed6ce8404657f9b0f090a0616d71684945f..9c1875ffa95c73ccff05c603e114cb8f82fad7a9 100644 --- a/.gitignore +++ b/.gitignore @@ -43,5 +43,4 @@ coverage 0 t.js t -ding-dong-bot.json -unit-test-session.json +*.wechaty.json diff --git a/README.md b/README.md index 05ce4e850b39bd9c67441e69d3ae29690a563ce1..482501ea84ac480d8135a22bbc5a8d2d74812bbe 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Can you image that? I'm dying... So a tireless bot working for me 24x7 on wechat, moniting/filtering the most important message is badly needed. For example: highlights discusstion which contains the KEYWORDS I want to follow up(especially in a noisy room). ;-) -At last, It's also built for my personal Automatically Testing study purpose. +At last, It's built for my personal study purpose of Automatically Testing. # Examples Wechaty is super easy to use: 10 lines of javascript is enough for your first wechat robot. diff --git a/example/ding-dong-bot.js b/example/ding-dong-bot.js index 620105622634a1ac5994d41f460c5e661e21f248..0945f0abeec899fa5da8301cc9dfaedf1215040d 100644 --- a/example/ding-dong-bot.js +++ b/example/ding-dong-bot.js @@ -26,7 +26,7 @@ Please wait... I'm trying to login in... ` console.log(welcome) -const bot = new Wechaty({ session: 'ding-dong-bot.json' }) +const bot = new Wechaty({ session: 'ding-dong-bot.wechaty.json' }) bot .on('login' , user => log.info('Bot', `${user.name()} logined`)) diff --git a/src/message.js b/src/message.js index 576e5161a6bb4be6dcd57c63131814b35189cd59..a7d4c8e8a23c19d05e56a327a2a41e7345045a04 100644 --- a/src/message.js +++ b/src/message.js @@ -38,7 +38,13 @@ class Message { , self: undefined // to store the logined user id } } - toString() { return this.obj.content } + toString() { + const text = this.digestEmoji(this.obj.digest) + const from = this.unescapeHtml(this.digestEmoji(Contact.load(this.obj.from).name())) + const room = this.obj.room ? this.unescapeHtml(this.digestEmoji(Room.load(this.obj.room).name())) : '' + return '<' + from + (room ? ('@'+room) : '') + '>: ' + '{' + this.type() + '}' + text + } + toStringEx() { var s = `${this.constructor.name}#${Message.counter}` s += '(' + this.getSenderString() @@ -64,6 +70,12 @@ class Message { .replace(/</g, '<') .replace(/&/g, '&') } + digestEmoji(str) { + // + return str + .replace(/]+>/g + , '($1$2)') + } from() { return this.obj.from } to() { return this.obj.to } diff --git a/src/puppet-web.js b/src/puppet-web.js index 1de6308744dab657c02f63da6d8d4173ee1513e9..7c17202ec4a66f17b4280199540fe39f0e4641b4 100644 --- a/src/puppet-web.js +++ b/src/puppet-web.js @@ -318,12 +318,12 @@ class PuppetWeb extends Puppet { log.error('PuppetWeb', 'onServerUnload() found browser dead. wait it to restore itself') return } - // re-init bridge - return process.nextTick(() => { + // re-init bridge after 1 second XXX: better method to confirm unload/reload finished? + return setTimeout(() => { this.bridge.init() .then(r => log.verbose('PuppetWeb', 'onServerUnload() bridge.init() done: %s', r)) .catch(e => log.error('PuppetWeb', 'onServerUnload() bridge.init() exceptoin: %s', e.message)) - }) + }, 1000) } onServerLog(data) { log.verbose('PuppetWeb', 'onServerLog: %s', data) @@ -377,7 +377,7 @@ class PuppetWeb extends Puppet { m.ready() // TODO: EventEmitter2 for video/audio/app/sys.... .then(() => this.emit('message', m)) .catch(e => { - log.error('PuppetWeb', 'onServerMessage() message ready exception: %s', e.message) + log.error('PuppetWeb', 'onServerMessage() message ready exception: %s', e) }) } diff --git a/src/wechaty.js b/src/wechaty.js index 8ad2388ae9a9f5caa34e48a6b72f4dca4b25b4bf..de3462fad951c3511300119e7d4419988a798e3d 100644 --- a/src/wechaty.js +++ b/src/wechaty.js @@ -24,9 +24,10 @@ class Wechaty extends EventEmitter { constructor(options) { super() this.options = options || {} - this.options.puppet = this.options.puppet || process.env.WECHATY_PUPPET || 'web' - this.options.head = this.options.head || process.env.WECHATY_HEAD || false - this.options.session = this.options.session || process.env.WECHATY_SESSION // no session, no session save/estore + this.options.puppet = this.options.puppet || process.env.WECHATY_PUPPET || 'web' + this.options.head = this.options.head || process.env.WECHATY_HEAD || false + this.options.port = this.options.port || process.env.WECHATY_PORT || 8788 // W(87) X(88), ascii char code ;-] + this.options.session = this.options.session || process.env.WECHATY_SESSION // no session, no session save/restore this.VERSION = require('../package.json').version } @@ -38,6 +39,15 @@ class Wechaty extends EventEmitter { log.verbose('Wechaty', 'session: %s', this.options.session) return co.call(this, function* () { + const okPort = yield this.getPort(this.options.port) + + if (okPort != this.options.port) { + log.verbose('Wechaty', 'port: %d not available, changed to %d', this.options.port, okPort) + this.options.port = okPort + } else { + log.verbose('Wechaty', 'port: %d', this.options.port) + } + yield this.initPuppet() yield this.initEventHook() yield this.puppet.init() @@ -126,6 +136,30 @@ class Wechaty extends EventEmitter { throw e }) } + + getPort(port) { + return new Promise((resolve, reject) => { + port = port || 8788 + // https://gist.github.com/mikeal/1840641 + function getPort (cb) { + var tryPort = port + port += 1 + var server = require('net').createServer() + server.on('error', function (err) { + if (err) {} + getPort(cb) + }) + server.listen(tryPort, function (err) { + if (err) {} + server.once('close', function () { + cb(tryPort) + }) + server.close() + }) + } + getPort(okPort => resolve(okPort)) + }) + } } Puppet.Web = PuppetWeb diff --git a/test/puppet-web-browser-spec.js b/test/puppet-web-browser-spec.js index 92a1c08e57b417a57a3f8230312c61baa60d7a8e..661b5d287b3fa98feff035d1a3176d731efec82e 100644 --- a/test/puppet-web-browser-spec.js +++ b/test/puppet-web-browser-spec.js @@ -6,7 +6,7 @@ const log = require('../src/npmlog-env') const Browser = require('../src/puppet-web-browser') const PORT = process.env.WECHATY_PORT || 58788 const HEAD = process.env.WECHATY_HEAD || false -const SESSION = 'unit-test-session.json' +const SESSION = 'unit-test-session.wechaty.json' test('Browser class cookie smoking tests', function(t) { const b = new Browser({port: PORT, head: HEAD}) diff --git a/test/puppet-web-spec.js b/test/puppet-web-spec.js index 05308e74ef2399dcc2fbc665e4837e8c9f36fe59..9a519f63566c897d94cfafbcf6788a4afc0bb8fe 100644 --- a/test/puppet-web-spec.js +++ b/test/puppet-web-spec.js @@ -7,7 +7,7 @@ const log = require('../src/npmlog-env') const PORT = process.env.WECHATY_PORT || 58788 const HEAD = process.env.WECHATY_HEAD || false -const SESSION = 'unit-test-session.json' +const SESSION = 'unit-test-session.wechaty.json' const PuppetWeb = require('../src/puppet-web')