From fede82cb5e3193b74010b2d905cb95614712424b Mon Sep 17 00:00:00 2001 From: Zhuohuan LI Date: Sat, 28 May 2016 14:47:20 +0000 Subject: [PATCH] v0.0.9 --- example/api-ai-bot.js | 9 +++------ example/ding-dong-bot.js | 2 +- example/wit-ai-bot.js | 5 ----- package.json | 2 +- src/puppet-web.js | 41 ++++++++++++++++++++++++++++------------ src/wechaty.js | 3 ++- test/puppet-web-spec.js | 2 +- test/webdriver-spec.js | 32 +++++++++++++++---------------- 8 files changed, 53 insertions(+), 43 deletions(-) delete mode 100644 example/wit-ai-bot.js diff --git a/example/api-ai-bot.js b/example/api-ai-bot.js index 3158cbe6..f489ec18 100644 --- a/example/api-ai-bot.js +++ b/example/api-ai-bot.js @@ -2,8 +2,8 @@ * * Wechaty bot use a ApiAi.com brain * - * Apply your own tuling123.com API_KEY - * at: http://www.tuling123.com/html/doc/api.html + * Apply Your Own ApiAi Developer API_KEY at: + * http://www.api.ai * * Enjoy! * @@ -20,9 +20,6 @@ const Wechaty = require('../src/wechaty') // log.level = 'silly' /** - * - * Apply Your Own ApiAi Developer API_KEY at: - * http://www.api.ai * * `7217d7bce18c4bcfbe04ba7bdfaf9c08` for Wechaty demo * @@ -33,7 +30,7 @@ const brainApiAi = ApiAi(APIAI_API_KEY) const bot = new Wechaty({head: false}) console.log(` -Welcome to Tuling Wechaty Bot. +Welcome to api.AI Wechaty Bot. Api.AI Doc: https://docs.api.ai/v16/docs/get-started Notice: This bot will only active in the group which name contains 'wechaty'. diff --git a/example/ding-dong-bot.js b/example/ding-dong-bot.js index c83b4abb..8bc43cc7 100644 --- a/example/ding-dong-bot.js +++ b/example/ding-dong-bot.js @@ -28,7 +28,7 @@ Please wait... I'm trying to login in... ` console.log(welcome) -const bot = new Wechaty({head: true}) +const bot = new Wechaty({head: false}) bot .on('login' , () => log.info('Bot', 'logined')) diff --git a/example/wit-ai-bot.js b/example/wit-ai-bot.js deleted file mode 100644 index e99cae2f..00000000 --- a/example/wit-ai-bot.js +++ /dev/null @@ -1,5 +0,0 @@ -/* -To be writen - -https://wit.ai/ -*/ diff --git a/package.json b/package.json index 7f9d4c39..3137ba3b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wechaty", - "version": "0.0.8", + "version": "0.0.9", "description": "Wechat for Bot. (Personal Account, NOT Official Account)", "main": "index.js", "scripts": { diff --git a/src/puppet-web.js b/src/puppet-web.js index 62372fca..4f304cf5 100644 --- a/src/puppet-web.js +++ b/src/puppet-web.js @@ -166,16 +166,21 @@ class PuppetWeb extends Puppet { */ log.verbose('PuppetWeb', 'server received unload event') this.emit('logout', data) // XXX: should emit event[logout] from browser - this.bridge.inject() - .then(r => log.verbose('PuppetWeb', 're-injected:' + r)) - .catch(e => log.error('PuppetWeb', 'inject err: ' + e)) + + if (this.bridge) { + this.bridge.inject() + .then(r => log.verbose('PuppetWeb', 're-injected:' + r)) + .catch(e => log.error('PuppetWeb', 'inject err: ' + e)) + } else { + log.verbose('PuppetWeb', 'bridge gone, should be quiting now') + } } send(message) { const userName = message.to().id const content = message.content() - log.silly('PuppetWeb', `say(${userName}, ${content})`) + log.silly('PuppetWeb', `send(${userName}, ${content})`) return this.bridge.send(userName, content) } reply(recvMsg, replyMsg) { @@ -204,14 +209,26 @@ class PuppetWeb extends Puppet { log.verbose('PuppetWeb', 'quit()') let p = Promise.resolve(true) - if (this.server) { p.then(this.server.quit.bind(this)) } - else { log.warn('PuppetWeb', 'quit() without server') } - - if (this.bridge) { p.then(this.bridge.quit.bind(this)) } - else { log.warn('PuppetWeb', 'quit() without bridge') } - - if (this.browser) { p.then(this.browser.quit.bind(this)) } - else { log.warn('PuppetWeb', 'quit() without browser') } + if (this.bridge) { + p.then(this.bridge.quit.bind(this.bridge)) + this.bridge = null + } else { + log.warn('PuppetWeb', 'quit() without bridge') + } + + if (this.browser) { + p.then(this.browser.quit.bind(this.browser)) + this.browser = null + } else { + log.warn('PuppetWeb', 'quit() without browser') + } + + if (this.server) { + p.then(this.server.quit.bind(this.server)) + this.server = null + } else { + log.warn('PuppetWeb', 'quit() without server') + } return p // return Promise } diff --git a/src/wechaty.js b/src/wechaty.js index 82640e4c..a3b2a86a 100644 --- a/src/wechaty.js +++ b/src/wechaty.js @@ -66,7 +66,8 @@ class Wechaty extends EventEmitter { currentUser() { return this.puppet.currentUser() } quit() { return this.puppet.quit() } - send(message) { return this.puppet.say(message) } + + send(message) { return this.puppet.send(message) } reply(message, reply) { return this.puppet.reply(message, reply) } ding() { diff --git a/test/puppet-web-spec.js b/test/puppet-web-spec.js index b8d08036..015be459 100644 --- a/test/puppet-web-spec.js +++ b/test/puppet-web-spec.js @@ -2,7 +2,7 @@ const co = require('co') const test = require('tap').test const log = require('npmlog') log.level = 'verbose' -// log.level = 'silly' +log.level = 'silly' const PuppetWeb = require('../src/puppet-web') const PORT = 58788 diff --git a/test/webdriver-spec.js b/test/webdriver-spec.js index 536d2ea3..4dd738a4 100644 --- a/test/webdriver-spec.js +++ b/test/webdriver-spec.js @@ -18,24 +18,24 @@ const PORT = 58788 function driverProcessNum() { return new Promise((resolve, reject) => { - // require('ps-tree')(process.pid, (err, data) => { - // if (err) { return reject(err) } - // data.forEach(c => console.log(c)) - // const num = data.filter(obj => /phantomjs/i.test(obj.COMMAND)).length - // return resolve(num) - // }) + require('ps-tree')(process.pid, (err, children) => { + if (err) { return reject(err) } + children.forEach(child => log.silly('TestingWebDriver', 'ps-tree: %s %s', child.PID, child.COMMAND)) + const num = children.filter(child => /phantomjs/i.test(child.COMMAND)).length + return resolve(num) + }) - const exec = require('child_process').exec - exec('ps axf >> /tmp/ps.log', r=>r) - exec('ps axf | grep phantomjs | grep -v grep | wc -l >> /tmp/ps.log', r=>r) + // const exec = require('child_process').exec + // exec('ps axf >> /tmp/ps.log', r=>r) + // exec('ps axf | grep phantomjs | grep -v grep | wc -l >> /tmp/ps.log', r=>r) - exec('ps axf | grep phantomjs | grep -v grep | wc -l', function(err, stdout, stderr) { - if (err) { - return reject(err) - } - return resolve(parseInt(stdout[0])) - }) + // exec('ps axf | grep phantomjs | grep -v grep | wc -l', function(err, stdout, stderr) { + // if (err) { + // return reject(err) + // } + // return resolve(parseInt(stdout[0])) + // }) }) } @@ -88,7 +88,7 @@ test('WebDriver smoke testing', function(t) { // XXX: if get rid of this dummy, // driver.get() will fail due to cant start phantomjs process - // yield Promise.resolve() + yield Promise.resolve() yield driver.get('https://wx.qq.com/') t.pass('driver url opened') -- GitLab