diff --git a/package.json b/package.json index 6c325b1d534bda20150dbec564b38a4dc30aacc7..034304c8037f28a2f9e4e07e0cca1eb8596bd41c 100644 --- a/package.json +++ b/package.json @@ -5,10 +5,8 @@ "main": "dist/src/index.js", "typings": "dist/src/index.d.ts", "wechaty": { - "DEFAULT_HEAD": 0, "DEFAULT_PORT": 8080, "DEFAULT_PROTOCOL": "io|0.0.1", - "DEFAULT_TOKEN": "WECHATY_IO_TOKEN", "DEFAULT_APIHOST": "api.chatie.io" }, "scripts": { @@ -52,6 +50,9 @@ }, "keywords": [ "wechat", + "rpa", + "conversational", + "ai", "微信", "weixin", "personal", @@ -73,7 +74,7 @@ "bugs": { "url": "https://github.com/wechaty/wechaty/issues" }, - "homepage": "https://chatie.io/wechaty/", + "homepage": "https://github.com/wechaty/", "engines": { "node": ">= 10" }, @@ -92,12 +93,12 @@ "pkg-dir": "^4.0.0", "portfinder": "^1.0.17", "promise-retry": "^1.1.1", - "qr-image": "^3.2.0", "raven": "^2.6.2", "read-pkg-up": "^7.0.0", "state-switch": "^0.6.2", "watchdog": "^0.8.1", "wechaty-puppet": "^0.19.6", + "wechaty-puppet-hostie": "^0.3.8", "ws": "^7.0.0" }, "devDependencies": { @@ -117,7 +118,6 @@ "@types/node": "^12.12.3", "@types/open-graph": "^0.2.0", "@types/promise-retry": "^1.1.3", - "@types/qr-image": "^3.2.1", "@types/raven": "^2.1.0", "@types/request": "^2.47.1", "@types/retry": "^0.12.0", diff --git a/src/io-client.ts b/src/io-client.ts index c743f231b274a6cbcbe99c1e136892ebfb53bdbe..4c6c9d5b74ed87dc25ac1e03e1dbd4b56f463607 100644 --- a/src/io-client.ts +++ b/src/io-client.ts @@ -22,6 +22,9 @@ */ import { StateSwitch } from 'state-switch' +import { PuppetHostieServer } from 'wechaty-puppet-hostie' +import { PuppetHostieGrpcServerOptions } from 'wechaty-puppet-hostie/dist/src/grpc/puppet-server' + import { Message } from './user' import { @@ -37,7 +40,12 @@ export interface IoClientOptions { export class IoClient { - private io: Io // XXX keep io `null-able` or not? 20161026 + /** + * Huan(20161026): keep io `null-able` or not? + * Huan(202002): make it optional. + */ + private io?: Io + private hostieServer?: PuppetHostieServer private state: StateSwitch @@ -47,12 +55,33 @@ export class IoClient { log.verbose('IoClient', 'constructor(%s)', JSON.stringify(options)) this.state = new StateSwitch('IoClient', log) + } - this.io = new Io({ - token : this.options.token, - wechaty : this.options.wechaty, - }) + private async startHostie () { + log.verbose('IoClient', 'startHostie()') + + if (this.hostieServer) { + throw new Error('hostie server exists') + } + + const options: PuppetHostieGrpcServerOptions = { + endpoint : '0.0.0.0:8788', + puppet : this.options.wechaty.puppet, + token : this.options.token, + } + this.hostieServer = new PuppetHostieServer(options) + await this.hostieServer.start() + } + + private async stopHostie () { + log.verbose('IoClient', 'stopHostie()') + + if (!this.hostieServer) { + throw new Error('hostie server does not exist') + } + await this.hostieServer.stop() + this.hostieServer = undefined } public async start (): Promise { @@ -67,9 +96,10 @@ export class IoClient { this.state.on('pending') try { + await this.hookWechaty(this.options.wechaty) await this.startIo() - await this.hookWechaty(this.options.wechaty) + await this.startHostie() this.state.on(true) @@ -105,6 +135,15 @@ export class IoClient { throw e } + if (this.io) { + throw new Error('io exists') + } + + this.io = new Io({ + token : this.options.token, + wechaty : this.options.wechaty, + }) + try { await this.io.start() } catch (e) { @@ -113,6 +152,18 @@ export class IoClient { } } + private async stopIo () { + log.verbose('IoClient', 'stopIo()') + + if (!this.io) { + log.warn('IoClient', 'stopIo() io does not exist') + return + } + + await this.io.stop() + this.io = undefined + } + private async onMessage (msg: Message) { log.verbose('IoClient', 'onMessage(%s)', msg) @@ -138,14 +189,9 @@ export class IoClient { this.state.off('pending') - // XXX - if (!this.io) { - log.warn('IoClient', 'stop() without this.io') - this.state.off(true) - return - } + await this.stopIo() + await this.stopHostie() - await this.io.stop() this.state.off(true) // XXX 20161026 diff --git a/src/puppet-config.ts b/src/puppet-config.ts index c4353ee3cf930fdf48590f90e589878355fa5150..76bed2f27d25154fd4c293b688125825f244d8e6 100644 --- a/src/puppet-config.ts +++ b/src/puppet-config.ts @@ -14,6 +14,7 @@ export const PUPPET_DEPENDENCIES = { // 'wechaty-puppet-hostie' : '^0.0.1', // 'wechaty-puppet-dll' : '^0.3.1', // https://www.npmjs.com/package/wechaty-puppet-dll + 'wechaty-puppet-hostie' : '^0.3.8', // https://www.npmjs.com/package/wechaty-puppet-hostie // 'wechaty-puppet-ioscat' : '^0.5.22', // https://www.npmjs.com/package/wechaty-puppet-ioscat 'wechaty-puppet-mock' : '^0.16.1', // https://www.npmjs.com/package/wechaty-puppet-mock // 'wechaty-puppet-padchat' : '^0.19.3', // https://www.npmjs.com/package/wechaty-puppet-padchat