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

session save & reload all ok, and docs for environment variables

上级 f17d153f
......@@ -43,4 +43,5 @@ coverage
0
t.js
t
tmp-chatbot-name-for-unit-testing.wechaty
ding-dong-bot.json
......@@ -136,6 +136,28 @@ If wechaty is not run as expected, run unit test maybe help to find some useful
npm test
```
## DEBUG output
Wechaty use [npmlog](https://www.npmjs.com/package/npmlog) to output debug message. You can set log level by environment variable `WECHATY_DEBUG` to show debug message.
environment variable `WECHATY_DEBUG` values:
1. `silly`
1. `verbose`
1. `info`
1. `warn`
1. `error`
Linux/OSX(Mac):
```bash
$ export WECHATY_DEBUG=verbose
```
Win32:
```shell
set WECHATY_DEBUG=verbose
```
# Requirement
ECMAScript2015(ES6). I develop and test wechaty with Node.js v6.0.
......@@ -200,11 +222,17 @@ To be support.
Main bot class.
```javascript
const bot = new Wechaty()
const bot = new Wechaty(options)
```
options:
1. `session`: session name. if a session name is provided, the login status will be saved to it, and automatically restored on next time of wechaty start(restart).
* can be set by environment variable: `WECHATY_SESSION`
### Wechaty.init()
Initialize the bot, return Promise.
```javascript
bot.init()
.then(() => {
......@@ -379,7 +407,7 @@ Know more about TAP: [Why I use Tape Instead of Mocha & So Should You](https://m
1. `message.recv.*`
- [ ] Message
- [ ] Send/Reply image message
- [ ] Session save/load
- [x] Session save/load
Everybody is welcome to issue your needs.
......@@ -441,4 +469,4 @@ Copyright & License
* Docs released under Creative Commons
[downloads-image]: http://img.shields.io/npm/dm/wechaty.svg?style=flat-square
[downloads-url]: https://npmjs.org/package/wechaty
\ No newline at end of file
[downloads-url]: https://npmjs.org/package/wechaty
const log = require('npmlog')
log.level = 'verbose'
log.level = 'silly'
const Wechaty = require('../src/wechaty')
......@@ -28,14 +26,11 @@ Please wait... I'm trying to login in...
`
console.log(welcome)
const bot = new Wechaty({
head: 'chrome'
, session: 'ding-dong-bot.wechaty'
})
const bot = new Wechaty({ session: 'ding-dong-bot.json' })
bot
.on('login' , user => log.info('Bot', 'logined'))
.on('logout' , () => log.info('Bot', 'logouted'))
.on('login' , user => log.info('Bot', `${user.name()} logined`))
.on('logout' , user => log.info('Bot', `${user.name()} logouted`))
.on('scan', ({url, code}) => {
console.log(`Scan QR Code in url to login: ${code}\n${url}`)
})
......
......@@ -276,20 +276,13 @@ class PuppetWeb extends Puppet {
logout() { return this.bridge.logout() }
getContact(id) { return this.bridge.getContact(id) }
getLoginQrImgUrl() {
if (!this.bridge) {
log.error('PuppetWeb', 'bridge not found')
return
}
return this.bridge.getLoginQrImgUrl()
}
logined() { return !!(this.user) }
checkSession() {
log.verbose('PuppetWeb', `checkSession(${this.session})`)
return this.browser.driver.manage().getCookies()
.then(cookies => {
log.verbose('PuppetWeb', 'checkSession %s', require('util').inspect(cookies.map(c => { return {name: c.name, value: c.value} })))
log.silly('PuppetWeb', 'checkSession %s', require('util').inspect(cookies.map(c => { return {name: c.name, value: c.value} })))
return cookies
})
}
......@@ -305,7 +298,7 @@ class PuppetWeb extends Puppet {
if (/ChromeDriver/i.test(c.name)) { return false }
else { return true }
})
log.verbose('PuppetWeb', 'saving %d cookies for session: %s', cookies.length
log.silly('PuppetWeb', 'saving %d cookies for session: %s', cookies.length
, util.inspect(filteredCookies.map(c => c.name))
)
......@@ -329,8 +322,8 @@ class PuppetWeb extends Puppet {
return new Promise((resolve, reject) => {
fs.readFile(filename, (err, jsonStr) => {
if (err) {
if (err) { log.verbose('PuppetWeb', 'loadSession(%s) skipped because: %s', this.session, err) }
return reject(err.toString())
if (err) { log.silly('PuppetWeb', 'loadSession(%s) skipped because error code: %s', this.session, err.code) }
return reject('error code:' + err.code)
}
const cookies = JSON.parse(jsonStr)
log.verbose('PuppetWeb', 'loading %d cookies for session', cookies.length )
......
......@@ -31,12 +31,11 @@ class Wechaty extends EventEmitter {
}
toString() { return 'Class Wechaty(' + this.puppet + ')'}
init() {
log.info('Wechaty', 'init() with version: %s, puppet: %s, head: %s, session: %s'
, this.VERSION
, this.options.puppet
, this.options.head
, this.options.session
)
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)
this.initPuppet()
this.initEventHook()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册