From 1bec6e517f6273fc9c43ce8f6a28cef3f10f3437 Mon Sep 17 00:00:00 2001 From: Zhuohuan LI Date: Tue, 24 May 2016 17:08:58 +0000 Subject: [PATCH] enhanced tuling123 bot --- example/tuling123-bot.js | 77 ++++++++++++++++++++++++++++++++++----- package.json | 1 + src/contact.js | 9 +---- src/group.js | 6 +-- src/message.js | 1 + src/puppet-web-browser.js | 1 - src/wechaty.js | 4 ++ 7 files changed, 77 insertions(+), 22 deletions(-) diff --git a/example/tuling123-bot.js b/example/tuling123-bot.js index 6929f04f..c5576314 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 8748e805..d5f73081 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 531c902f..0d8c9851 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 ada15bda..51249b66 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 abd050e3..ee722923 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 30f422fb..31c01e3b 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 a43fbef6..050a7a63 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() } } -- GitLab