diff --git a/example/tuling123-bot.js b/example/tuling123-bot.js index 6929f04f96270906bcdc04b49037ca780c4425fd..c55763149090f0a5e6e30d7aece108ff314cbdb2 100644 --- a/example/tuling123-bot.js +++ b/example/tuling123-bot.js @@ -11,8 +11,9 @@ * */ const log = require('npmlog') -const co = require('co') +const co = require('co') const Tuling123 = require('tuling123-client') +const EventEmitter2 = require('eventemitter2') const Wechaty = require('../src/wechaty') //log.level = 'verbose' @@ -56,17 +57,75 @@ bot.init() process.exit(-1) }) -function talk(m) { - co(function* () { - const fromId = m.from().id - const content = m.content() - const {code, text} = yield brain.ask(content, {userid: fromId}) +class Talker extends EventEmitter2 { + constructor(thinker) { + log.verbose('Talker()') + super() + this.thinker = thinker + this.obj = { + text: [] + , time: [] + } + this.timer = null + } + save(text) { + log.verbose('Talker', 'save(%s)', text) + this.obj.text.push(text) + this.obj.time.push(Date.now()) + } + load() { + const text = this.obj.text.join(', ') + log.verbose('Talker', 'load(%s)', text) + this.obj.text = [] + this.obj.time = [] + return text + } + + updateTimer(delayTime) { + delayTime = delayTime || this.delayTime() + log.verbose('Talker', 'updateTimer(%s)', delayTime) + + if (this.timer) { clearTimeout(this.timer) } + this.timer = setTimeout(this.say.bind(this), delayTime) + } + + hear(text) { + log.verbose('Talker', `hear(${text})`) + this.save(text) + this.updateTimer() + } + say() { + log.verbose('Talker', 'say()') + const text = this.load() + this.thinker(text) + .then(reply => this.emit('say', reply)) + this.timer = null + } + + delayTime() { const minDelayTime = 5000 const maxDelayTime = 15000 const delayTime = Math.floor(Math.random() * (maxDelayTime - minDelayTime)) + minDelayTime + return delayTime + } +} - log.info('Bot', `REPLY(after ${Math.floor(delayTime/1000)}s): {code:${code}, text:${text}}`) - setTimeout(r => { bot.reply(m, text) }, delayTime) - }) +var Talkers = [] + +function talk(m) { + const fromId = m.from().id + const groupId = m.group().id + const content = m.content() + + const talkerName = fromId + groupId + if (!Talkers[talkerName]) { + Talkers[talkerName] = new Talker(function(text) { + log.info('Tuling123', 'Talker is thinking how to reply: %s', text) + return brain.ask(text, {userid: talkerName}) + .then(r => r.text) + }) + Talkers[talkerName].on('say', reply => bot.reply(m, reply)) + } + Talkers[talkerName].hear(content) } diff --git a/package.json b/package.json index 8748e805ec37093b57f974e32ea73456024c0858..d5f73081a5d6b5098753aa14dde22f841a6ec196 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "body-parser": "^1.15.0", "chromedriver": "^2.21.2", "co": "^4.6.0", + "eventemitter2": "^1.0.3", "express": "^4.13.4", "npmlog": "^2.0.3", "phantomjs-prebuilt": "^2.1.7", diff --git a/src/contact.js b/src/contact.js index 531c902f3ad66dca823a039a5075db6cadac5b5a..0d8c985118847763d80d545cb4110514a2f49432 100644 --- a/src/contact.js +++ b/src/contact.js @@ -10,19 +10,14 @@ const log = require('npmlog') class Contact { constructor(id) { - if (!Contact.puppet) { - throw new Error('no puppet attached to Contact') - } log.silly('Contact', `constructor(${id})`) + if (!Contact.puppet) { throw new Error('no puppet attached to Contact') } this.id = id this.obj = {} } - toString() { - var name = this.obj.name ? `${this.obj.name}@${this.id}` : this.id - return `Contact(${name})` - } + toString() { return `Contact(${this.obj.name}[${this.id}])` } parse(rawObj) { return !rawObj ? {} : { diff --git a/src/group.js b/src/group.js index ada15bda2d4df286a3beb9adce4b5702a4e16012..51249b66e170c1c07f3aea844083a7e963368be3 100644 --- a/src/group.js +++ b/src/group.js @@ -18,11 +18,7 @@ class Group { throw new Error('no puppet attached to Group') } } - - toString() { - var name = this.obj.name ? `[${this.obj.name}]${this.id}` : this.id - return `Group(${name})` - } + toString() { return `Group(${this.obj.name}[${this.id}])` } ready(contactGetter) { log.silly('Group', `ready(${contactGetter})`) diff --git a/src/message.js b/src/message.js index abd050e367c828fdb19d373b7d6745acb686fde7..ee7229234e13e248630c5cb2de98d0a1f05cb134 100644 --- a/src/message.js +++ b/src/message.js @@ -70,6 +70,7 @@ class Message { group() { return this.obj.group } ready() { + log.silly('Message', 'ready()') return this.obj.from.ready() // Contact from .then(r => this.obj.to.ready()) // Contact to .then(r => this.obj.group && this.obj.group.ready()) // Group member list diff --git a/src/puppet-web-browser.js b/src/puppet-web-browser.js index 30f422fbcd96b427a8e78399d2511a938724f32c..31c01e3bb09a5ca4b1ad2590e2a1ffd9d5a44c4d 100644 --- a/src/puppet-web-browser.js +++ b/src/puppet-web-browser.js @@ -12,7 +12,6 @@ const fs = require('fs') const path = require('path') const WebDriver = require('selenium-webdriver') const log = require('npmlog') -//log.disableColor() class Browser { constructor(options) { diff --git a/src/wechaty.js b/src/wechaty.js index a43fbef69054878310e6fea2762116a247175a72..050a7a6334051a8385e17f8ae2497d6d516e8490 100644 --- a/src/wechaty.js +++ b/src/wechaty.js @@ -68,6 +68,10 @@ class Wechaty extends EventEmitter { return 'dong' } + /** + * @deprecated + * use on('scan', ({code, url}) => {}) instead. + */ getLoginQrImgUrl() { return this.puppet.getLoginQrImgUrl() } }