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

ding-dong-bot show group name right

上级 d51bcc3d
const Wechaty = require('../src/wechaty')
const log = require('npmlog')
log.level = 'verbose'
log.level = 'silly'
//log.level = 'silly'
const welcome = `
| __ __ _ _
......@@ -33,18 +33,16 @@ bot.init()
.then(login)
bot.on('message', (m) => {
m
.ready()
.then(msg => log.warn('Bot', 'recv: %s', msg))
.catch(e => log.err('Bot', 'ready: ' + e))
m.ready()
.then (msg => log.info ('Bot', 'recv: %s' , msg))
.catch(e => log.error('Bot', 'ready: %s' , e))
log.info('Bot', 'recv: %s', m)
if (/^ding|ping$/.test(m.get('content'))) {
if (/^ding|ping$/i.test(m.get('content'))) {
const r = new Wechaty.Message()
r.set('to', m.get('from'))
r.set('content', 'dong')
bot.send(r)
.then(() => { log.info('Bot', 'REPLY: dong') })
.then(() => { log.warn('Bot', 'REPLY: dong') })
}
})
......
......@@ -16,38 +16,24 @@ class Contact {
this.id = id
this.obj = {}
this.loading = Contact.puppet.getContact(id)
}
ready(contactGetter) {
if (this.obj.id) return Promise.resolve(this);
contactGetter = contactGetter || Contact.puppet.getContact.bind(Contact.puppet)
return contactGetter(this.id)
.then(data => {
log.silly('Contact', `Contact.puppet.getContact(${id}) resolved`)
log.silly('Contact', `Contact.puppet.getContact(${this.id}) resolved`)
this.rawObj = data
this.obj = this.parse(data)
return new Promise(r => r())
return this
}).catch(e => {
log.error('Contact', `Contact.puppet.getContact(${id}) rejected: ` + e)
log.error('Contact', `Contact.puppet.getContact(${this.id}) rejected: ` + e)
throw new Error('getContact: ' + e)
})
}
ready() {
const timeout = 1 * 1000 // 1 seconds
const sleepTime = 500 // 100 ms
let spentTime = 0
return new Promise((resolve, reject) => {
return readyChecker.apply(this)
function readyChecker() {
log.verbose('Contact', `readyChecker(${spentTime})`)
if (this.obj.id) return resolve(this);
spentTime += sleepTime
if (spentTime > timeout)
return reject('Contact.ready() timeout after ' + timeout + ' ms');
return setTimeout(readyChecker.bind(this), sleepTime)
}
})
}
parse(rawObj) {
return !rawObj ? {} : {
id: rawObj.UserName
......
......@@ -12,52 +12,35 @@ const Contact = require('./contact')
class Group {
constructor(id) {
this.id = id
if (!Group.puppet) throw new Error('no puppet attached to Group');
this.obj = {}
log.silly('Group', `constructor(${id})`)
if (!Group.puppet) throw new Error('no puppet attached to Group');
}
this.id = id
this.obj = {}
toString() { return this.obj.name ? this.obj.name : this.id }
getId() { return this.id }
ready(contactGetter) {
log.silly('Group', 'ready()')
if (this.obj.id) return resolve(this);
this.loading = Group.puppet.getContact(id)
contactGetter = contactGetter || Group.puppet.getContact.bind(Group.puppet)
return Group.puppet.getContact(this.id)
.then(data => {
log.silly('Group', `Group.puppet.getContact(${id}) resolved`)
log.silly('Group', `Group.puppet.getContact(${this.id}) resolved`)
this.rawObj = data
this.obj = this.parse(data)
return this
}).catch(e => {
log.error('Group', `Group.puppet.getContact(${id}) rejected: ` + e)
log.error('Group', `Group.puppet.getContact(${this.id}) rejected: ` + e)
throw new Error('getContact: ' + e)
})
}
toString() { return this.obj.name ? this.obj.name : this.id }
getId() { return this.id }
ready() {
const timeout = 1 * 1000 // 1 seconds
const sleepTime = 100 // 100 ms
let spentTime = 0
return new Promise((resolve, reject) => {
return readyChecker.apply(this)
function readyChecker() {
log.verbose('Group', `readyChecker(${spentTime})`)
if (this.obj.id) return resolve(this);
spentTime += sleepTime
if (spentTime > timeout)
return reject('Group.ready() timeout after ' + timeout + ' ms');
return setTimeout(readyChecker.bind(this), sleepTime)
}
})
}
parse(rawObj) {
return !rawObj ? {} : {
id: rawObj.UserName
, encryId: rawObj.EncryChatRoomId // ???
, name: rawObj.NickName
, members: rawObj.MemberList.map(m => {
return {
......@@ -77,10 +60,6 @@ class Group {
Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj[k]}`))
}
toString() {
return `Group(${this.id})`
}
getId() { return this.id }
get(prop) { return this.obj[prop] }
......
......@@ -13,12 +13,15 @@ const log = require('npmlog')
class Message {
constructor(rawObj) {
this.rawObj = rawObj = rawObj || {}
Message.counter++;
Message.counter++
this.rawObj = rawObj = rawObj || {}
this.obj = this.parse(rawObj)
}
// Transform rawObj to local m
this.obj = {
// Transform rawObj to local m
parse(rawObj) {
return {
id: rawObj.MsgId
, type: rawObj.MsgType
, from: Contact.load(rawObj.MMActualSender)
......@@ -31,26 +34,39 @@ class Message {
, actual_content: rawObj.MMActualContent
, date: new Date(rawObj.MMDisplayTime*1000)
}
}
}
toString() {
const name = this.obj.from.get('name')
const name = html2str(this.obj.from.get('name'))
const group = this.obj.group
let content = this.obj.content
let content = html2str(this.obj.content)
if (content.length > 20) content = content.substring(0,17) + '...';
if (group) return `Message(${name}@${group}: ${content})`
else return `Message(${name}: ${content})`
}
let groupStr = group ? html2str(group) : ''
let fromStr = '<' + name + (groupStr ? `@[${groupStr}]` : '') + '>'
return `Message#${Message.counter}(${fromStr}: ${content})`
function html2str(html) {
return html.replace(/(<([^>]+)>)/ig,'')
.replace(/&apos;/g, "'")
.replace(/&quot;/g, '"')
.replace(/&gt;/g, '>')
.replace(/&lt;/g, '<')
.replace(/&amp;/g, '&')
}
}
ready() {
return new Promise((resolve, reject) => {
this.obj.from.ready() // Contact from
.then(r => this.obj.to.ready()) // Contact to
.then(r => resolve(this))
.catch(e => reject(e))
.then(r => this.obj.group && this.obj.group.ready()) // Group member list
.then(r => resolve(this)) // RESOLVE
.catch(e => reject(e)) // REJECT
})
}
inGroup() { return !!(this.obj.group) }
get(prop) {
if (!prop || !(prop in this.obj)) {
const s = '[' + Object.keys(this.obj).join(',') + ']'
......
......@@ -54,9 +54,9 @@ class Server extends EventEmitter {
* after received `unload`, we re-inject the Wechaty js code into browser.
*/
this.on('unload', () => {
log.verbose('Server', 'server received unload event')
log.warn('Server', 'server received unload event')
this.browser.inject()
.then(() => log.verbose('Server', 're-injected'))
.then(() => log.warn('Server', 're-injected'))
.catch((e) => log.error('Server', 'inject err: ' + e))
})
......@@ -93,7 +93,7 @@ class Server extends EventEmitter {
})
app.get('/ding', function (req, res) {
log.verbose('Server', '%s GET /ding', new Date())
log.silly('Server', '%s GET /ding', new Date())
res.end('dong')
})
......@@ -116,7 +116,7 @@ class Server extends EventEmitter {
this.socketClient = s
s.on('disconnect', function() {
log.verbose('Server', 'socket.io disconnected')
log.warn('Server', 'socket.io disconnected')
/**
* Possible conditions:
* 1. Browser reload
......@@ -134,7 +134,7 @@ class Server extends EventEmitter {
, 'unload'
].map(e => {
s.on(e, data => {
log.verbose('Server', `recv event[${e}] from browser`)
log.silly('Server', `recv event[${e}] from browser`)
this.emit(e, data)
})
})
......@@ -192,7 +192,7 @@ class Server extends EventEmitter {
const argsJson = JSON.stringify(args)
const wechatyScript = `return (Wechaty && Wechaty.${wechatyFunc}.apply(undefined, JSON.parse('${argsJson}')))`
log.verbose('Server', 'proxyWechaty: ' + wechatyScript)
log.silly('Server', 'proxyWechaty: ' + wechatyScript)
return this.browserExecute(wechatyScript)
}
}
......
......@@ -11,7 +11,7 @@ test('Contact smoke testing', t => {
const NickName = 'Nick Name Test'
// Mock
Contact.puppet.getContact = function (id) {
const mockContactGetter = function (id) {
return new Promise((resolve,reject) => {
if (id!=UserName) return resolve({});
setTimeout(() => {
......@@ -26,7 +26,7 @@ test('Contact smoke testing', t => {
const c = new Contact(UserName)
t.equal(c.getId(), UserName, 'id/UserName right')
c.ready()
c.ready(mockContactGetter)
.then(r => {
t.equal(c.get('id') , UserName, 'UserName set')
t.equal(c.get('name') , NickName, 'NickName set')
......
......@@ -37,7 +37,7 @@ false && test('Message ready() promise testing', t => {
Contact.init()
// Mock
Contact.puppet.getContact = function (id) {
const mockContactGetter = function (id) {
log.silly('MessageTesting', `mocked getContact(${id})`)
return new Promise((resolve,reject) => {
let obj = {}
......@@ -70,7 +70,7 @@ false && test('Message ready() promise testing', t => {
t.equal(m.get('id'), expectedMsgId, 'id/MsgId right')
m.ready()
m.ready(mockContactGetter)
.then(r => {
/*
const fromC = m.get('from')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册