提交 1bec6e51 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

enhanced tuling123 bot

上级 eac49618
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
const log = require('npmlog') const log = require('npmlog')
const co = require('co') const co = require('co')
const Tuling123 = require('tuling123-client') const Tuling123 = require('tuling123-client')
const EventEmitter2 = require('eventemitter2')
const Wechaty = require('../src/wechaty') const Wechaty = require('../src/wechaty')
//log.level = 'verbose' //log.level = 'verbose'
...@@ -56,17 +57,75 @@ bot.init() ...@@ -56,17 +57,75 @@ bot.init()
process.exit(-1) process.exit(-1)
}) })
function talk(m) { class Talker extends EventEmitter2 {
co(function* () { constructor(thinker) {
const fromId = m.from().id log.verbose('Talker()')
const content = m.content() super()
const {code, text} = yield brain.ask(content, {userid: fromId}) 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 minDelayTime = 5000
const maxDelayTime = 15000 const maxDelayTime = 15000
const delayTime = Math.floor(Math.random() * (maxDelayTime - minDelayTime)) + minDelayTime const delayTime = Math.floor(Math.random() * (maxDelayTime - minDelayTime)) + minDelayTime
return delayTime
}
}
var Talkers = []
log.info('Bot', `REPLY(after ${Math.floor(delayTime/1000)}s): {code:${code}, text:${text}}`) function talk(m) {
setTimeout(r => { bot.reply(m, text) }, delayTime) 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)
} }
...@@ -10,19 +10,14 @@ const log = require('npmlog') ...@@ -10,19 +10,14 @@ const log = require('npmlog')
class Contact { class Contact {
constructor(id) { constructor(id) {
if (!Contact.puppet) {
throw new Error('no puppet attached to Contact')
}
log.silly('Contact', `constructor(${id})`) log.silly('Contact', `constructor(${id})`)
if (!Contact.puppet) { throw new Error('no puppet attached to Contact') }
this.id = id this.id = id
this.obj = {} this.obj = {}
} }
toString() { toString() { return `Contact(${this.obj.name}[${this.id}])` }
var name = this.obj.name ? `${this.obj.name}@${this.id}` : this.id
return `Contact(${name})`
}
parse(rawObj) { parse(rawObj) {
return !rawObj ? {} : { return !rawObj ? {} : {
......
...@@ -18,11 +18,7 @@ class Group { ...@@ -18,11 +18,7 @@ class Group {
throw new Error('no puppet attached to Group') throw new Error('no puppet attached to Group')
} }
} }
toString() { return `Group(${this.obj.name}[${this.id}])` }
toString() {
var name = this.obj.name ? `[${this.obj.name}]${this.id}` : this.id
return `Group(${name})`
}
ready(contactGetter) { ready(contactGetter) {
log.silly('Group', `ready(${contactGetter})`) log.silly('Group', `ready(${contactGetter})`)
......
...@@ -70,6 +70,7 @@ class Message { ...@@ -70,6 +70,7 @@ class Message {
group() { return this.obj.group } group() { return this.obj.group }
ready() { ready() {
log.silly('Message', 'ready()')
return this.obj.from.ready() // Contact from return this.obj.from.ready() // Contact from
.then(r => this.obj.to.ready()) // Contact to .then(r => this.obj.to.ready()) // Contact to
.then(r => this.obj.group && this.obj.group.ready()) // Group member list .then(r => this.obj.group && this.obj.group.ready()) // Group member list
......
...@@ -12,7 +12,6 @@ const fs = require('fs') ...@@ -12,7 +12,6 @@ const fs = require('fs')
const path = require('path') const path = require('path')
const WebDriver = require('selenium-webdriver') const WebDriver = require('selenium-webdriver')
const log = require('npmlog') const log = require('npmlog')
//log.disableColor()
class Browser { class Browser {
constructor(options) { constructor(options) {
......
...@@ -68,6 +68,10 @@ class Wechaty extends EventEmitter { ...@@ -68,6 +68,10 @@ class Wechaty extends EventEmitter {
return 'dong' return 'dong'
} }
/**
* @deprecated
* use on('scan', ({code, url}) => {}) instead.
*/
getLoginQrImgUrl() { return this.puppet.getLoginQrImgUrl() } getLoginQrImgUrl() { return this.puppet.getLoginQrImgUrl() }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册