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

enhanced tuling123 bot

上级 eac49618
......@@ -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)
}
......@@ -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 ? {} : {
......
......@@ -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})`)
......
......@@ -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
......
......@@ -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) {
......
......@@ -68,6 +68,10 @@ class Wechaty extends EventEmitter {
return 'dong'
}
/**
* @deprecated
* use on('scan', ({code, url}) => {}) instead.
*/
getLoginQrImgUrl() { return this.puppet.getLoginQrImgUrl() }
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册