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

1. make Message.toString() clear

2. add environment variable WECHATY_PORT support
3. if PORT is not available, pick a new one near PORT
4. rename unit test session name
上级 a09fb59d
......@@ -43,5 +43,4 @@ coverage
0
t.js
t
ding-dong-bot.json
unit-test-session.json
*.wechaty.json
......@@ -25,7 +25,7 @@ Can you image that? I'm dying...
So a tireless bot working for me 24x7 on wechat, moniting/filtering the most important message is badly needed. For example: highlights discusstion which contains the KEYWORDS I want to follow up(especially in a noisy room). ;-)
At last, It's also built for my personal Automatically Testing study purpose.
At last, It's built for my personal study purpose of Automatically Testing.
# Examples
Wechaty is super easy to use: 10 lines of javascript is enough for your first wechat robot.
......
......@@ -26,7 +26,7 @@ Please wait... I'm trying to login in...
`
console.log(welcome)
const bot = new Wechaty({ session: 'ding-dong-bot.json' })
const bot = new Wechaty({ session: 'ding-dong-bot.wechaty.json' })
bot
.on('login' , user => log.info('Bot', `${user.name()} logined`))
......
......@@ -38,7 +38,13 @@ class Message {
, self: undefined // to store the logined user id
}
}
toString() { return this.obj.content }
toString() {
const text = this.digestEmoji(this.obj.digest)
const from = this.unescapeHtml(this.digestEmoji(Contact.load(this.obj.from).name()))
const room = this.obj.room ? this.unescapeHtml(this.digestEmoji(Room.load(this.obj.room).name())) : ''
return '<' + from + (room ? ('@'+room) : '') + '>: ' + '{' + this.type() + '}' + text
}
toStringEx() {
var s = `${this.constructor.name}#${Message.counter}`
s += '(' + this.getSenderString()
......@@ -64,6 +70,12 @@ class Message {
.replace(/&lt;/g, '<')
.replace(/&amp;/g, '&')
}
digestEmoji(str) {
// <img class="emoji emoji1f4a4" text="[流汗]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />
return str
.replace(/<img class="\w*?emoji (\w*?emoji[^"]+?)" text="(.*?)_web" src=[^>]+>/g
, '($1$2)')
}
from() { return this.obj.from }
to() { return this.obj.to }
......
......@@ -318,12 +318,12 @@ class PuppetWeb extends Puppet {
log.error('PuppetWeb', 'onServerUnload() found browser dead. wait it to restore itself')
return
}
// re-init bridge
return process.nextTick(() => {
// re-init bridge after 1 second XXX: better method to confirm unload/reload finished?
return setTimeout(() => {
this.bridge.init()
.then(r => log.verbose('PuppetWeb', 'onServerUnload() bridge.init() done: %s', r))
.catch(e => log.error('PuppetWeb', 'onServerUnload() bridge.init() exceptoin: %s', e.message))
})
}, 1000)
}
onServerLog(data) {
log.verbose('PuppetWeb', 'onServerLog: %s', data)
......@@ -377,7 +377,7 @@ class PuppetWeb extends Puppet {
m.ready() // TODO: EventEmitter2 for video/audio/app/sys....
.then(() => this.emit('message', m))
.catch(e => {
log.error('PuppetWeb', 'onServerMessage() message ready exception: %s', e.message)
log.error('PuppetWeb', 'onServerMessage() message ready exception: %s', e)
})
}
......
......@@ -24,9 +24,10 @@ class Wechaty extends EventEmitter {
constructor(options) {
super()
this.options = options || {}
this.options.puppet = this.options.puppet || process.env.WECHATY_PUPPET || 'web'
this.options.head = this.options.head || process.env.WECHATY_HEAD || false
this.options.session = this.options.session || process.env.WECHATY_SESSION // no session, no session save/estore
this.options.puppet = this.options.puppet || process.env.WECHATY_PUPPET || 'web'
this.options.head = this.options.head || process.env.WECHATY_HEAD || false
this.options.port = this.options.port || process.env.WECHATY_PORT || 8788 // W(87) X(88), ascii char code ;-]
this.options.session = this.options.session || process.env.WECHATY_SESSION // no session, no session save/restore
this.VERSION = require('../package.json').version
}
......@@ -38,6 +39,15 @@ class Wechaty extends EventEmitter {
log.verbose('Wechaty', 'session: %s', this.options.session)
return co.call(this, function* () {
const okPort = yield this.getPort(this.options.port)
if (okPort != this.options.port) {
log.verbose('Wechaty', 'port: %d not available, changed to %d', this.options.port, okPort)
this.options.port = okPort
} else {
log.verbose('Wechaty', 'port: %d', this.options.port)
}
yield this.initPuppet()
yield this.initEventHook()
yield this.puppet.init()
......@@ -126,6 +136,30 @@ class Wechaty extends EventEmitter {
throw e
})
}
getPort(port) {
return new Promise((resolve, reject) => {
port = port || 8788
// https://gist.github.com/mikeal/1840641
function getPort (cb) {
var tryPort = port
port += 1
var server = require('net').createServer()
server.on('error', function (err) {
if (err) {}
getPort(cb)
})
server.listen(tryPort, function (err) {
if (err) {}
server.once('close', function () {
cb(tryPort)
})
server.close()
})
}
getPort(okPort => resolve(okPort))
})
}
}
Puppet.Web = PuppetWeb
......
......@@ -6,7 +6,7 @@ const log = require('../src/npmlog-env')
const Browser = require('../src/puppet-web-browser')
const PORT = process.env.WECHATY_PORT || 58788
const HEAD = process.env.WECHATY_HEAD || false
const SESSION = 'unit-test-session.json'
const SESSION = 'unit-test-session.wechaty.json'
test('Browser class cookie smoking tests', function(t) {
const b = new Browser({port: PORT, head: HEAD})
......
......@@ -7,7 +7,7 @@ const log = require('../src/npmlog-env')
const PORT = process.env.WECHATY_PORT || 58788
const HEAD = process.env.WECHATY_HEAD || false
const SESSION = 'unit-test-session.json'
const SESSION = 'unit-test-session.wechaty.json'
const PuppetWeb = require('../src/puppet-web')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册