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

Merge branch 'master' of github.com:zixia/wechaty

......@@ -17,7 +17,7 @@ Supports [linux](https://travis-ci.org/zixia/wechaty), [win32](https://ci.appvey
[![Downloads][downloads-image]][downloads-url]
# Examples
Wechaty is super easy to use: 10 lines of javascript is enough for your first wechat robot.
Wechaty is super easy to use: 9 lines of javascript is enough for your 1st wechat bot.
## 1. Basic: 9 lines
The following 9 lines of code implement a bot who reply "roger" for every message received:
......
......@@ -18,13 +18,10 @@
"微信",
"weixin",
"personal",
"account",
"bot",
"robot",
"chatbot",
"framework",
"library",
"api",
"wechaty",
"微信控"
],
......
......@@ -19,11 +19,7 @@ class Contact {
this.obj = {}
}
toString() {
return this.obj.name
? htmlUtil.plainText(this.obj.name)
: this.obj.id
}
toString() { return this.id }
toStringEx() { return `Contact(${this.obj.name}[${this.id}])` }
parse(rawObj) {
......@@ -45,7 +41,7 @@ class Contact {
}
}
name() { return this.obj.name }
name() { return htmlUtil.plainText(this.obj.name) }
remark() { return this.obj.remark }
stranger() { return this.obj.stranger }
star() { return this.obj.star }
......
......@@ -29,10 +29,12 @@ function digestEmoji(html) {
}
function plainText(html) {
return unescapeHtml(
stripHtml(
digestEmoji(
html
return stripHtml(
unescapeHtml(
stripHtml(
digestEmoji(
html
)
)
)
)
......
......@@ -145,7 +145,7 @@ class Bridge {
.catch (e => {
log.warn('PuppetWebBridge', 'inject() exception: %s', e.message)
attempt = attempt || 0
if (attempt++ < 3) {
if (attempt++ < 9) { // if init fail, retry 9 times
log.warn('PuppetWebBridge', 'inject(%d) retry after 1 second: %s', attempt, e.message)
setTimeout(() => this.inject(attempt), 1000)
return
......
......@@ -343,6 +343,12 @@ return (function(port) {
var d = new Date()
s = d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds() + ' <Wechaty> ' + s
/**
* FIXME: WARN PuppetWebBridge inject() exception: {"errorMessage":"null is not an object (evaluating 'document.body.appendChild')"
* when will document.createElement('iframe') return null?
* this will cause the bridge init fail, and retry.
* should it be ignored? or keep this exception to retry is better?
*/
var i = document.createElement('iframe')
i.style.display = 'none'
document.body.appendChild(i)
......
......@@ -457,8 +457,13 @@ class PuppetWeb extends Puppet {
.set('from' , message.obj.to)
.set('to' , message.obj.from)
.set('room' , message.obj.room)
// FIXME: find a alternate way to check a message create by `self`
.set('self' , this.user.id)
if (this.user) {
// FIXME: find a alternate way to check a message create by `self`
m.set('self', this.user.id)
} else {
log.warn('PuppetWeb', 'reply() too early before logined user be set')
}
// log.verbose('PuppetWeb', 'reply() by message: %s', util.inspect(m))
return this.send(m)
......
......@@ -19,11 +19,7 @@ class Room {
}
}
toString() {
return this.obj.name
? htmlUtil.plainText(this.obj.name)
: this.obj.id
}
toString() { return this.id }
toStringEx() { return `Room(${this.obj.name}[${this.id}])` }
ready(contactGetter) {
......@@ -50,7 +46,7 @@ class Room {
})
}
name() { return this.obj.name }
name() { return htmlUtil.plainText(this.obj.name) }
get(prop) { return this.obj[prop] }
parse(rawObj) {
......
......@@ -30,14 +30,22 @@ class Wechaty extends EventEmitter {
this.options.session = this.options.session || process.env.WECHATY_SESSION // no session, no session save/restore
this.VERSION = require('../package.json').version
this.inited = false
}
toString() { return 'Class Wechaty(' + this.puppet + ')'}
init() {
log.info('Wechaty', 'v%s initializing...', this.VERSION)
log.verbose('Wechaty', 'puppet: %s' , this.options.puppet)
log.verbose('Wechaty', 'head: %s' , this.options.head)
log.verbose('Wechaty', 'session: %s', this.options.session)
if (this.inited) {
log.error('Wechaty', 'init() already inited. return and do nothing.')
return Promise.resolve(this)
}
return co.call(this, function* () {
const okPort = yield this.getPort(this.options.port)
......@@ -52,9 +60,11 @@ class Wechaty extends EventEmitter {
yield this.initEventHook()
yield this.puppet.init()
this.inited = true
return this // for chaining
}).catch(e => {
})
.catch(e => {
log.error('Wechaty', 'init() exception: %s', e.message)
throw e
})
......@@ -99,8 +109,12 @@ class Wechaty extends EventEmitter {
return Promise.resolve()
}
quit() {
return this.puppet.quit()
quit() {
const puppetBeforeDie = this.puppet
this.puppet = null
this.inited = false
return puppetBeforeDie.quit()
.catch(e => {
log.error('Wechaty', 'quit() exception: %s', e.message)
throw e
......@@ -112,7 +126,6 @@ class Wechaty extends EventEmitter {
log.error('Wechaty', 'logout() exception: %s', e.message)
throw e
})
}
send(message) {
......@@ -129,7 +142,11 @@ class Wechaty extends EventEmitter {
throw e
})
}
ding(data) {
ding(data) {
if (!this.puppet) {
return Promise.reject(new Error('wechaty cant ding coz no puppet'))
}
return this.puppet.ding(data)
.catch(e => {
log.error('Wechaty', 'ding() exception: %s', e.message)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册