Skip to content

Commit

Permalink
Merge branch 'master' of github.com:zixia/wechaty
Browse files Browse the repository at this point in the history
  • Loading branch information
huan committed Jun 14, 2016
2 parents 09c14df + 6109f05 commit 2400fd0
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
"微信",
"weixin",
"personal",
"account",
"bot",
"robot",
"chatbot",
"framework",
"library",
"api",
"wechaty",
"微信控"
],
Expand Down
8 changes: 2 additions & 6 deletions src/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 }
Expand Down
10 changes: 6 additions & 4 deletions src/html-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ function digestEmoji(html) {
}

function plainText(html) {
return unescapeHtml(
stripHtml(
digestEmoji(
html
return stripHtml(
unescapeHtml(
stripHtml(
digestEmoji(
html
)
)
)
)
Expand Down
2 changes: 1 addition & 1 deletion src/puppet-web-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/puppet-web-injectio.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions src/puppet-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 2 additions & 6 deletions src/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
27 changes: 22 additions & 5 deletions src/wechaty.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
})
Expand Down Expand Up @@ -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
Expand All @@ -112,7 +126,6 @@ class Wechaty extends EventEmitter {
log.error('Wechaty', 'logout() exception: %s', e.message)
throw e
})

}

send(message) {
Expand All @@ -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)
Expand Down

0 comments on commit 2400fd0

Please sign in to comment.