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

modulized IoBot

上级 85372df8
#!/usr/bin/env node #!/usr/bin/env node
const Wechaty = require('..') const log = require('../src/npmlog-env')
const IoBot = require('../src/io-bot')
const log = Wechaty.log
const welcome = ` const welcome = `
| __ __ _ _ | __ __ _ _
...@@ -35,45 +35,20 @@ if (!token) { ...@@ -35,45 +35,20 @@ if (!token) {
log.info('Bot', 'Starting for WECHATY_TOKEN: %s', token) log.info('Bot', 'Starting for WECHATY_TOKEN: %s', token)
} }
const bot = new Wechaty({ const ioBot = new IoBot({
profile profile
, token // token for wechaty.io auth , token
, log
}) })
bot ioBot.init()
.on('login' , user => log.info('Bot', `${user.name()} logined`))
.on('logout' , user => log.info('Bot', `${user.name()} logouted`))
.on('scan' , ({url, code}) => log.info('Bot', '[%d] %s <-- Scan QR Code in url to login', code, url))
.on('message', m => {
m.ready()
.then(onMessage)
.catch(e => log.error('Bot', 'ready: %s' , e))
})
bot.init()
.catch(e => { .catch(e => {
log.error('Bot', 'init() fail: %s', e) log.error('Bot', 'init() fail: %s', e)
bot.quit() bot.quit()
process.exit(-1) process.exit(-1)
}) })
const onMessage = function(m) {
const from = m.from()
const to = m.to()
const content = m.toString()
const room = m.room()
// log.info('Bot', '%s<%s>:%s'
// , (room ? '['+room.name()+']' : '')
// , from.name()
// , m.toStringDigest()
// )
if (/^wechaty|botie/i.test(m.get('content')) && !bot.self(m)) {
bot.reply(m, 'https://www.wechaty.io')
.then(_ => log.info('Bot', 'REPLIED to magic word "wechaty"'))
}
}
/** /**
* *
* To make heroku happy * To make heroku happy
...@@ -89,4 +64,4 @@ if (process.env.DYNO) { ...@@ -89,4 +64,4 @@ if (process.env.DYNO) {
app.listen(process.env.PORT || 8080, function () { app.listen(process.env.PORT || 8080, function () {
console.log('Wechaty IO Bot listening on port ' + process.env.PORT + '!') console.log('Wechaty IO Bot listening on port ' + process.env.PORT + '!')
}) })
} }
\ No newline at end of file
/**
*
* wechaty: Wechat for Bot. and for human who talk to bot/robot
*
* Class Io
* http://www.wechaty.io
*
* Licenst: ISC
* https://github.com/zixia/wechaty
*
*/
const Wechaty = require('./wechaty')
class IoBot {
constructor({
token = 'EPP'
, profile = 'wechaty-epp'
, log = null
}) {
if (!log) {
const e = new Error('constructor() log(npmlog/brolog) must be set')
throw e
}
this.log = log
this.log.verbose('IoBot', 'constructor() with token:[%s]', token)
if (!token) {
const e = new Error('constructor() token must be set')
this.log.error('IoBot', e)
throw e
}
this.token = token
this.profile = profile
}
init() {
this.log.verbose('IoBot', 'init()')
this.wechaty = new Wechaty({
profile: this.profile
, token: this.token
})
this.wechaty
.on('login' , user => this.log.info('IoBot', `${user.name} logined`))
.on('logout' , user => this.log.info('IoBot', `${user} logouted`))
.on('scan', ({url, code}) => this.log.info('IoBot', `[${code}] ${url}`))
.on('message' , message => {
message.ready()
.then(this.onMessage.bind(this))
.catch(e => this.log.error('IoBot', 'message.ready() %s' , e))
})
return this.wechaty.init()
.then(_ => this)
.catch(e => {
this.log.error('IoBot', 'init() init fail: %s', e)
bot.quit()
return e
})
}
onMessage(m) {
const from = m.from()
const to = m.to()
const content = m.toString()
const room = m.room()
// log.info('Bot', '%s<%s>:%s'
// , (room ? '['+room.name()+']' : '')
// , from.name()
// , m.toStringDigest()
// )
if (/^wechaty|botie/i.test(m.get('content')) && !bot.self(m)) {
bot.reply(m, 'https://www.wechaty.io')
.then(_ => log.info('Bot', 'REPLIED to magic word "wechaty"'))
}
}
}
module.exports = IoBot.default = IoBot.IoBot = IoBot
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册