diff --git a/bin/io-bot.js b/bin/client.js similarity index 65% rename from bin/io-bot.js rename to bin/client.js index e0902526db78c354b23ba0665f917caad2a0a4f3..a9f9732777705c2df7d36a0cee82cd573bb92203 100755 --- a/bin/io-bot.js +++ b/bin/client.js @@ -1,7 +1,7 @@ #!/usr/bin/env node const { - IoBot + IoClient , Config , log } = require('../') @@ -27,28 +27,28 @@ __________________________________________________ let token = Config.token if (!token) { - log.error('Bot', 'token not found: please set WECHATY_TOKEN in environment before run io-bot') + log.error('Client', 'token not found: please set WECHATY_TOKEN in environment before run io-client') // process.exit(-1) token = Config.DEFAULT_TOKEN - log.warn('Bot', `set token to "${token}" for demo purpose`) + log.warn('Client', `set token to "${token}" for demo purpose`) } console.log(welcome) -log.info('Bot', 'Starting for WECHATY_TOKEN: %s', token) +log.info('Client', 'Starting for WECHATY_TOKEN: %s', token) -const ioBot = new IoBot({ +const client = new IoClient({ token , log }) -ioBot.init() - .catch(onError.bind(ioBot)) +client.init() + .catch(onError.bind(client)) -ioBot.initWeb() - .catch(onError.bind(ioBot)) +client.initWeb() + .catch(onError.bind(client)) function onError(e) { - log.error('Bot', 'initWeb() fail: %s', e) + log.error('Client', 'initWeb() fail: %s', e) this.quit() process.exit(-1) } diff --git a/index.js b/index.js index e9cae5fb1927b773447b0d57c4e3f7d7b2bddce3..0e1f0b267cf41040ad9d77456fec8f72f8743cf4 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ const Room = require('./src/room') const Puppet = require('./src/puppet') const PuppetWeb = require('./src/puppet-web') -const IoBot = require('./src/io-bot') +const IoClient = require('./src/io-client') const log = require('./src/brolog-env') @@ -26,7 +26,7 @@ Object.assign(Wechaty, { , Puppet , PuppetWeb - , IoBot + , IoClient , version: require('./package.json').version , log // for convenionce use npmlog with environment variable LEVEL diff --git a/package.json b/package.json index 107c0362fb1a9979e008e1e0f19fd762a19c178f..0fb6979ab67f45881518dc138bfcabf0e9545c0f 100644 --- a/package.json +++ b/package.json @@ -10,21 +10,21 @@ "DEFAULT_PORT": 8788, "DEFAULT_PUPPET_PORT": 18788, "DEFAULT_PROTOCOL": "io|0.0.1", - "DEFAULT_TOKEN": "WECHATY-TOKEN", + "DEFAULT_TOKEN": "TEST_TOKEN", "ENDPOINT": "wss://api.wechaty.io/v0/websocket", "BINARY_CHROMIUM": "/wechaty/bin/xvfb-chromium" }, "scripts": { "lint": "npm run eslint && npm run tslint", - "eslint": "eslint \"{src,test}/**/*.js\"", - "tslint": "tslint \"{src,test}/**/*.ts\"", + "eslint": "eslint \"{src,test,bin}/**/*.js\"", + "tslint": "tslint \"{src,test,bin}/**/*.ts\"", "pretest": "npm run lint", "test": "npm run test:phantomjs && npm run test:chrome", "test:phantomjs": "cross-env LC_ALL=C WECHATY_LOG=info WECHATY_HEAD=phantomjs ava --timeout=10m \"{src,test}/**/*.spec.js\"", "test:chrome": "cross-env LC_ALL=C WECHATY_LOG=silly WECHATY_HEAD=chrome ava --timeout=10m \"{src,test}/**/*.spec.js\"", "testdev": "cross-env LC_ALL=C WECHATY_LOG=silly ava --serial --verbose --fail-fast --timeout=3m", "ava": "cross-env LC_ALL=C WECHATY_LOG=verbose ava \"{src,test}/**/*.spec.js\"", - "start": "node bin/io-bot", + "start": "node bin/client", "demo": "node example/ding-dong-bot.js" }, "repository": { @@ -32,7 +32,7 @@ "url": "git+https://github.com/wechaty/wechaty.git" }, "bin": { - "io-bot": "bin/io-bot.js", + "wechaty-client": "bin/client.js", "wechaty-version": "bin/version.js" }, "keywords": [ diff --git a/src/io-bot.js b/src/io-bot.js deleted file mode 100644 index 94b478a60d61a906a5d4a13df26cfaa0781523e1..0000000000000000000000000000000000000000 --- a/src/io-bot.js +++ /dev/null @@ -1,109 +0,0 @@ -/** - * - * wechaty: Wechat for Bot. and for human who talk to bot/robot - * - * Class Io - * http://www.wechaty.io - * - * Licenst: ISC - * https://github.com/zixia/wechaty - * - */ - -/** - * DO NOT use `require('../')` here! - * because it will casue a LOOP require ERROR - */ -const Wechaty = require('./wechaty') -const Config = require('./config') - -class IoBot { - constructor({ - token - , log = Wechaty.log - }) { - if (!log) { - const e = new Error('constructor() log(npmlog/brolog) must be set') - throw e - } - this.log = log - this.log.verbose('IoBot', 'constructor() with token: %s', token) - - if (!token) { - const e = new Error('constructor() token must be set') - this.log.error('IoBot', e) - throw e - } - this.token = token - } - - init() { - this.log.verbose('IoBot', 'init()') - - const wechaty = this.wechaty = new Wechaty({ - profile: Config.DEFAULT_PROFILE - , token: this.token - }) - - wechaty - .on('login' , user => this.log.info('IoBot', `${user.name()} logined`)) - .on('logout' , user => this.log.info('IoBot', `${user.name()} logouted`)) - .on('scan', ({url, code}) => this.log.info('IoBot', `[${code}] ${url}`)) - .on('message' , message => { - message.ready() - .then(this.onMessage.bind(this)) - .catch(e => this.log.error('IoBot', 'message.ready() %s' , e)) - }) - - - return wechaty.init() - .then(_ => { - this.log.verbose('IoBot', 'wechaty.init() succ') - return this - }) - .catch(e => { - this.log.error('IoBot', 'init() init fail: %s', e) - wechaty.quit() - throw e - }) - } - - initWeb(port = Config.httpPort) { -// if (process.env.DYNO) { -// } - const app = require('express')() - - app.get('/', function (req, res) { - res.send('Wechaty IO Bot Alive!') - }) - - return new Promise((resolve, reject) => { - app.listen(port, () => { - this.log.verbose('IoBot', 'initWeb() Wechaty IO Bot listening on port ' + port + '!') - - return resolve(this) - - }) - }) - } - - onMessage(m) { - const from = m.from() - const to = m.to() - const content = m.toString() - const room = m.room() - - // log.info('Bot', '%s<%s>:%s' - // , (room ? '['+room.name()+']' : '') - // , from.name() - // , m.toStringDigest() - // ) - - if (/^wechaty|botie/i.test(m.get('content')) && !bot.self(m)) { - bot.reply(m, 'https://www.wechaty.io') - .then(_ => log.info('Bot', 'REPLIED to magic word "wechaty"')) - } - } -} - -module.exports = IoBot.default = IoBot.IoBot = IoBot diff --git a/src/io-client.js b/src/io-client.js new file mode 100755 index 0000000000000000000000000000000000000000..4573881ae94bf980c0b5b3c56ee563f2bfe51b4a --- /dev/null +++ b/src/io-client.js @@ -0,0 +1,148 @@ +/** + * + * wechaty: Wechat for Bot. and for human who talk to bot/robot + * + * Class IoClient + * http://www.wechaty.io + * + * Licenst: ISC + * https://github.com/wechaty/wechaty + * + */ + +const co = require('co') + +/** + * DO NOT use `require('../')` here! + * because it will casue a LOOP require ERROR + */ +const Wechaty = require('./wechaty') +const Config = require('./config') +const Io = require('./io') +const brolog = require('./brolog-env') + +class IoClient { + constructor({ + token = Config.token || Config.DEFAULT_TOKEN + , endpoint = Config.endpoint + , log = brolog + } = {}) { + if (!log) { + const e = new Error('constructor() log(npmlog/brolog) must be set') + throw e + } + this.log = log + this.log.verbose('IoClient', 'constructor() with token: %s', token) + + if (!token) { + const e = new Error('constructor() token must be set') + this.log.error('IoClient', e.message) + throw e + } + this.token = token + } + + init() { + this.log.verbose('IoClient', 'init()') + + return co.call(this, function* () { + this.wechaty = yield this.initWechaty() + this.io = yield this.initIo() + return this + }).catch(e => { + this.log.error('IoClient', 'init() exception: %s', e.message) + throw e + }) + } + + initWechaty() { + this.log.verbose('IoClient', 'initWechaty()') + + const wechaty = new Wechaty({ + profile: Config.DEFAULT_PROFILE + }) + + wechaty + .on('login' , user => this.log.info('IoClient', `${user.name()} logined`)) + .on('logout' , user => this.log.info('IoClient', `${user.name()} logouted`)) + .on('scan', ({url, code}) => this.log.info('IoClient', `[${code}] ${url}`)) + .on('message' , message => { + message.ready() + .then(this.onMessage.bind(this)) + .catch(e => this.log.error('IoClient', 'message.ready() %s' , e)) + }) + + return wechaty.init() + .then(_ => { + this.log.verbose('IoClient', 'wechaty.init() succ') + return this + }) + .catch(e => { + this.log.error('IoClient', 'init() init fail: %s', e) + wechaty.quit() + throw e + }) + } + + initIo() { + this.log.verbose('IoClient', 'initIo()') + + if (!this.token) { + log.verbose('IoClient', 'initIo() skiped for no token set') + return Promise.resolve('no token') + } else { + log.verbose('IoClient', 'initIo(%s)', this.token) + } + + const io = new Io({ + wechaty: this.wechaty + , token: this.token + , endpoint: this.endpoint + }) + + return io.init() + .catch(e => { + log.verbose('IoClient', 'initIo() init fail: %s', e.message) + throw e + }) + } + + initWeb(port = Config.httpPort) { +// if (process.env.DYNO) { +// } + const app = require('express')() + + app.get('/', function (req, res) { + res.send('Wechaty IO Bot Alive!') + }) + + return new Promise((resolve, reject) => { + app.listen(port, () => { + this.log.verbose('IoClient', 'initWeb() Wechaty IO Bot listening on port ' + port + '!') + + return resolve(this) + + }) + }) + } + + onMessage(m) { + const from = m.from() + const to = m.to() + const content = m.toString() + const room = m.room() + + // log.info('Bot', '%s<%s>:%s' + // , (room ? '['+room.name()+']' : '') + // , from.name() + // , m.toStringDigest() + // ) + + if (/^wechaty|botie/i.test(m.get('content')) && !bot.self(m)) { + bot.reply(m, 'https://www.wechaty.io') + .then(_ => log.info('Bot', 'REPLIED to magic word "wechaty"')) + } + } +} + +module.exports = IoClient.default = IoClient.IoClient = IoClient diff --git a/src/io.js b/src/io.js index c6b03a3a4fbed767a6687bce58f0d9044fd61953..96844bc2602c047e2e250e9d551a3b0e1c46ac51 100644 --- a/src/io.js +++ b/src/io.js @@ -24,7 +24,7 @@ class Io { , token = null , endpoint = Config.endpoint , protocol = Config.DEFAULT_PROTOCOL - }) { + } = {}) { if (!wechaty || !token) { throw new Error('Io must has wechaty & token set') } diff --git a/src/wechaty.js b/src/wechaty.js index e9f78204b40f8f67b5e78cf9bc4decd230c47ba7..184a897492a47d7be60b7ca559b82bc7ea8b5bbc 100644 --- a/src/wechaty.js +++ b/src/wechaty.js @@ -26,8 +26,6 @@ class Wechaty extends EventEmitter { type = Config.puppet , head = Config.head , port = Config.port - , endpoint = Config.endpoint - , token = Config.token // token for wechaty.io auth. will connect to wechaty.io cloud management if token is set , profile = Config.profile // no profile, no session save/restore } = {}) { log.verbose('Wechaty', 'contructor()') @@ -36,8 +34,6 @@ class Wechaty extends EventEmitter { this.type = type this.head = head this.port = port - this.token = token - this.endpoint = endpoint this.profile = /\.wechaty\.json$/i.test(profile) ? profile @@ -59,7 +55,10 @@ class Wechaty extends EventEmitter { if (!forceNpm) { try { - // Synchronous version of fs.access(). This throws if any accessibility checks fail, and does nothing otherwise. + /** + * Synchronous version of fs.access(). + * This throws if any accessibility checks fail, and does nothing otherwise. + */ fs.accessSync(dotGitPath, fs.F_OK) const ss = require('child_process') @@ -96,12 +95,11 @@ class Wechaty extends EventEmitter { } init() { - - log.info('Wechaty', 'v%s initializing...', this.version()) - log.verbose('Wechaty', 'puppet: %s' , this.type) - log.verbose('Wechaty', 'head: %s' , this.head) - log.verbose('Wechaty', 'profile: %s', this.profile) - log.verbose('Wechaty', 'uuid: %s' , this.uuid) + log.info('Wechaty', 'v%s initializing...' , this.version()) + log.verbose('Wechaty', 'puppet: %s' , this.type) + log.verbose('Wechaty', 'head: %s' , this.head) + log.verbose('Wechaty', 'profile: %s' , this.profile) + log.verbose('Wechaty', 'uuid: %s' , this.uuid) if (this.inited) { log.error('Wechaty', 'init() already inited. return and do nothing.') @@ -109,12 +107,6 @@ class Wechaty extends EventEmitter { } return co.call(this, function* () { - this.io = yield this.initIo() - .catch(e => { // fail safe - log.error('Wechaty', 'initIo failed: %s', e.message) - this.emit('error', e) - }) - this.puppet = yield this.initPuppet() this.inited = true @@ -126,28 +118,6 @@ class Wechaty extends EventEmitter { }) } - initIo() { - if (!this.token) { - log.verbose('Wechaty', 'initIo() skiped for no token set') - return Promise.resolve('no token') - } else { - log.verbose('Wechaty', 'initIo(%s)', this.token) - } - - const Io = require('./io') - const io = new Io({ - wechaty: this - , token: this.token - , endpoint: this.endpoint - }) - - return io.init() - .catch(e => { - log.verbose('Wechaty', 'Wechaty.IO init fail: %s', e.message) - throw e - }) - } - initPuppet() { let puppet switch (this.type) { diff --git a/test/wechaty.spec.js b/test/wechaty.spec.js index 79c2a4646bcf23bbcab4f9f640e212aac7859811..5e51cf0d6c76de3f74cd3f3ecbd906065c3e7693 100644 --- a/test/wechaty.spec.js +++ b/test/wechaty.spec.js @@ -7,7 +7,7 @@ test('Wechaty Framework', t => { t.truthy(Wechaty.Contact , 'should export Wechaty.Contact') t.truthy(Wechaty.Room , 'should export Wechaty.Room') - t.truthy(Wechaty.IoBot , 'should export Wechaty.IoBot') + t.truthy(Wechaty.IoClient , 'should export Wechaty.IoClient') t.truthy(Wechaty.log , 'should export Wechaty.log')