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

local ci

上级 b9ba006e
......@@ -24,14 +24,14 @@ So a tireless bot working for me 24x7 on wechat, moniting/filtering the most imp
Wechaty is super easy to use: 10 lines of javascript is enough for your first wechat robot.
## 1. Basic: 10 lines
The following 10 lines of code will implement a bot that will reply a message automatically to you:
The following 10 lines of code implement a bot who reply "roger" for every message received:
```javascript
const Wechaty = require('wechaty')
const bot = new Wechaty()
bot.init()
.then(bot.getLoginQrImgUrl.bind(bot.puppet))
.then(bot.getLoginQrImgUrl.bind(bot))
.then(url => console.log(`Scan qrcode in url to login: \n${url}`))
bot.on('message', m => {
......@@ -58,7 +58,7 @@ To Be Written.
Plan to glue with Machine Learning/Deep Learning/Neural Network/Natural Language Processing.
# Installation & Usage
# Installation
Use NPM is recommended to install Wechaty for you:
```bash
$ npm install --save wechaty
......@@ -77,7 +77,7 @@ NodeJS Version 6.0 & above is required.
Use `git` to checkout Wechaty source code from [Github.com](https://github.com)
```shell
git clone git@github.com:zixia/wechaty.git
# git clone https://github.com/zixia/wechaty.git
# or use https: git clone https://github.com/zixia/wechaty.git
```
## 3. Install Dependents
......@@ -89,7 +89,7 @@ npm install
## 4. Run Demo Bot
```shell
npm start
# node example/ding-dong-bot.js
# this will run: node example/ding-dong-bot.js
```
# Trouble Shooting
......
/**
*
*
* Wechaty bot use a Tuling123.com brain
*
* Apply your own tuling123.com API_KEY
......@@ -43,17 +43,19 @@ bot.on('message', m => {
const msg = yield m.ready()
log.info('Bot', 'recv: %s' , msg)
if (!m.inGroup()) {
const r = new Wechaty.Message()
.set('to', m.get('from'))
if (m.inGroup()) {
return
}
const r = new Wechaty.Message()
.set('to', m.get('from'))
const content = m.get('content').toString()
const answer = brain.ask(content, {userid: msg.get('from')})
r.set('content', answer)
const content = m.get('content')
const {code, text} = yield brain.ask(content, {userid: msg.get('from')})
r.set('content', text)
yield bot.send(r)
log.info('Bot', `REPLY: ${answer}`)
}
yield bot.send(r)
log.info('Bot', `REPLY: {code:${code}, text:${text}}`)
})
.catch(e => log.error('Bot', 'on message rejected: %s' , e))
})
......
......@@ -22,22 +22,25 @@ class Message {
// Transform rawObj to local m
parse(rawObj) {
return {
id: rawObj.MsgId
, type: rawObj.MsgType
, from: Contact.load(rawObj.MMActualSender)
, to: Contact.load(rawObj.ToUserName)
, group: rawObj.MMIsChatRoom ? new Group(rawObj.FromUserName) : null // MMPeerUserName always eq FromUserName ?
, content: rawObj.MMActualContent // Content has @id prefix added by wx
, status: rawObj.Status
, digest: rawObj.MMDigest
, date: new Date(rawObj.MMDisplayTime*1000)
id: rawObj.MsgId
, type: rawObj.MsgType
, from: rawObj.MMActualSender
, to: rawObj.ToUserName
, group: !!(rawObj.MMIsChatRoom) // MMPeerUserName always eq FromUserName ?
, content: rawObj.MMActualContent // Content has @id prefix added by wx
, status: rawObj.Status
, digest: rawObj.MMDigest
, date: new Date(rawObj.MMDisplayTime*1000)
, fromContact: Contact.load(rawObj.MMActualSender)
, toContact: Contact.load(rawObj.ToUserName)
, inGroup: rawObj.MMIsChatRoom ? Group.load(rawObj.FromUserName) : null
}
}
toString() {
const name = html2str(this.obj.from.get('name'))
const group = this.obj.group
const group = this.obj.inGroup
let content = html2str(this.obj.content)
if (content.length > 20) content = content.substring(0,17) + '...';
let groupStr = group ? html2str(group) : ''
......@@ -57,11 +60,14 @@ class Message {
ready() {
return new Promise((resolve, reject) => {
this.obj.from.ready() // Contact from
.then(r => this.obj.to.ready()) // Contact to
.then(r => this.obj.group && this.obj.group.ready()) // Group member list
this.obj.fromContact.ready() // Contact from
.then(r => this.obj.toContact.ready()) // Contact to
.then(r => this.obj.inGroup && this.obj.inGroup.ready()) // Group member list
.then(r => resolve(this)) // RESOLVE
.catch(e => reject(e)) // REJECT
.catch(e => { // REJECT
log.error('Message', 'ready() rejected:' + e)
reject(e)
})
})
}
......@@ -80,13 +86,13 @@ class Message {
return this
}
dump() {
console.error('======= dump message =======')
Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj[k]}`))
dump() {
console.error('======= dump message =======')
Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj[k]}`))
}
dumpRaw() {
dumpRaw() {
console.error('======= dump raw message =======')
Object.keys(this.rawObj).forEach(k => console.error(`${k}: ${this.rawObj[k]}`))
Object.keys(this.rawObj).forEach(k => console.error(`${k}: ${this.rawObj[k]}`))
}
getCount() { return Message.counter }
......
......@@ -38,6 +38,8 @@ class PuppetWeb extends Puppet {
this.on('login' , () => this.logined = true)
this.on('logout', () => this.logined = false)
this.userId = null
return Promise.all([
this.initServer()
, this.initBrowser()
......@@ -53,10 +55,7 @@ class PuppetWeb extends Puppet {
].map(event =>
this.server.on(event, data => this.emit(event, data))
)
this.server.on('message', data => {
const m = new Message(data)
this.emit('message', m)
})
this.server.on('message', data => this.forwardMessage(data))
/**
* `unload` event is sent from js@browser to webserver via socketio
* after received `unload`, we re-inject the Wechaty js code into browser.
......@@ -82,11 +81,19 @@ class PuppetWeb extends Puppet {
return this.browser.init()
}
forwardMessage(data) {
const m = new Message(data)
const fromId = m.get('from')
if (this.userId)
this.emit('message', m)
}
send(message) {
const toContact = message.get('to')
const content = message.get('content')
const toContact = message.get('to')
const toContactId = toContact.getId()
const content = message.get('content')
return this.proxyWechaty('send', toContact.getId(), content)
log.silly('PuppetWeb', `send(${toContactId}, ${content})`)
return this.proxyWechaty('send', toContactId, content)
}
logout() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册