Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.www.
wechaty
提交
ffd0f7b3
W
wechaty
项目概览
.www.
/
wechaty
与 Fork 源项目一致
Fork自
wechaty / wechaty
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
wechaty
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ffd0f7b3
编写于
7月 26, 2016
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
enhance io support
上级
562f2eaf
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
95 addition
and
33 deletion
+95
-33
README.md
README.md
+1
-1
src/io.js
src/io.js
+83
-32
src/wechaty.js
src/wechaty.js
+11
-0
未找到文件。
README.md
浏览文件 @
ffd0f7b3
...
@@ -26,7 +26,7 @@ It supports [linux](https://travis-ci.org/zixia/wechaty), [win32](https://ci.app
...
@@ -26,7 +26,7 @@ It supports [linux](https://travis-ci.org/zixia/wechaty), [win32](https://ci.app
Wechaty is dead easy to use: 9 lines javascript for your 1st wechat bot.
Wechaty is dead easy to use: 9 lines javascript for your 1st wechat bot.
## 1. Basic: 9 lines
## 1. Basic: 9 lines
The following 9 lines of code implement a bot log
every
message to console:
The following 9 lines of code implement a bot log
all
message to console:
```
javascript
```
javascript
const
Wechaty
=
require
(
'
wechaty
'
)
const
Wechaty
=
require
(
'
wechaty
'
)
...
...
src/io.js
浏览文件 @
ffd0f7b3
...
@@ -14,6 +14,7 @@ const WebSocket = require('ws')
...
@@ -14,6 +14,7 @@ const WebSocket = require('ws')
const
co
=
require
(
'
co
'
)
const
co
=
require
(
'
co
'
)
const
log
=
require
(
'
./npmlog-env
'
)
const
log
=
require
(
'
./npmlog-env
'
)
const
Contact
=
require
(
'
./contact
'
)
class
Io
{
class
Io
{
...
@@ -41,7 +42,7 @@ class Io {
...
@@ -41,7 +42,7 @@ class Io {
log
.
verbose
(
'
Io
'
,
'
init()
'
)
log
.
verbose
(
'
Io
'
,
'
init()
'
)
return
co
.
call
(
this
,
function
*
()
{
return
co
.
call
(
this
,
function
*
()
{
yield
this
.
init
Wechaty
()
yield
this
.
init
EventHook
()
yield
this
.
initWebSocket
()
yield
this
.
initWebSocket
()
return
this
return
this
...
@@ -52,6 +53,7 @@ class Io {
...
@@ -52,6 +53,7 @@ class Io {
}
}
initWebSocket
()
{
initWebSocket
()
{
log
.
verbose
(
'
Io
'
,
'
initWebSocket()
'
)
// const auth = 'Basic ' + new Buffer(this.token + ':X').toString('base64')
// const auth = 'Basic ' + new Buffer(this.token + ':X').toString('base64')
const
auth
=
'
Token
'
+
this
.
token
const
auth
=
'
Token
'
+
this
.
token
const
headers
=
{
'
Authorization
'
:
auth
}
const
headers
=
{
'
Authorization
'
:
auth
}
...
@@ -72,19 +74,61 @@ class Io {
...
@@ -72,19 +74,61 @@ class Io {
}.
bind
(
this
))
}.
bind
(
this
))
ws
.
on
(
'
message
'
,
(
data
,
flags
)
=>
{
ws
.
on
(
'
message
'
,
(
data
,
flags
)
=>
{
log
.
verbose
(
'
Io
'
,
'
WebSocket got message
'
)
log
.
silly
(
'
Io
'
,
'
initWebSocket() ws.on(message): %s
'
,
data
)
// flags.binary will be set if a binary data is received.
// flags.binary will be set if a binary data is received.
// flags.masked will be set if the data was masked.
// flags.masked will be set if the data was masked.
log
.
verbose
(
'
Io
'
,
'
onMessage: %s
'
,
data
)
if
(
data
.
onMessage
)
{
const
ioEvent
=
{
const
script
=
data
.
script
name
:
'
raw
'
,
payload
:
data
}
try
{
const
obj
=
JSON
.
parse
(
data
)
ioEvent
.
name
=
obj
.
name
ioEvent
.
payload
=
obj
.
payload
}
catch
(
e
)
{
log
.
verbose
(
'
Io
'
,
'
on(message) recv a non IoEvent data[%s]
'
,
data
)
}
switch
(
ioEvent
.
name
)
{
case
'
botie
'
:
const
payload
=
ioEvent
.
payload
if
(
payload
.
onMessage
)
{
const
script
=
payload
.
script
const
fn
=
eval
(
script
)
const
fn
=
eval
(
script
)
if
(
typeof
fn
===
'
function
'
)
{
if
(
typeof
fn
===
'
function
'
)
{
this
.
onMessage
=
fn
this
.
onMessage
=
fn
}
else
{
}
else
{
log
.
warn
(
'
Io
'
,
'
onMessage server push function invalid
'
)
log
.
warn
(
'
Io
'
,
'
server pushed function is invalid
'
)
}
}
}
break
case
'
reset
'
:
log
.
verbose
(
'
Io
'
,
'
on(reset): %s
'
,
ioEvent
.
payload
)
this
.
wechaty
.
reset
()
break
case
'
update
'
:
log
.
verbose
(
'
Io
'
,
'
on(report): %s
'
,
ioEvent
.
payload
)
const
user
=
this
.
wechaty
.
user
()
if
(
user
)
{
const
loginEvent
=
{
name
:
'
login
'
,
payload
:
user
.
obj
}
this
.
send
(
loginEvent
)
}
break
case
'
sys
'
:
// do nothing
break
default
:
log
.
warn
(
'
Io
'
,
'
UNKNOWN on(%s): %s
'
,
ioEvent
.
name
,
ioEvent
.
payload
)
break
}
}
})
})
...
@@ -124,22 +168,22 @@ class Io {
...
@@ -124,22 +168,22 @@ class Io {
}
}
}
}
init
Wechaty
()
{
init
EventHook
()
{
const
wechaty
=
this
.
wechaty
const
wechaty
=
this
.
wechaty
wechaty
.
on
(
'
message
'
,
this
.
ioMessage
)
wechaty
.
on
(
'
message
'
,
this
.
ioMessage
)
const
io
Events
=
[
const
hook
Events
=
[
'
scan
'
'
scan
'
,
'
login
'
,
'
login
'
,
'
logout
'
,
'
logout
'
,
'
error
'
,
'
error
'
,
'
heartbeat
'
,
'
heartbeat
'
]
]
io
Events
.
map
(
event
=>
{
hook
Events
.
map
(
event
=>
{
wechaty
.
on
(
event
,
data
=>
{
wechaty
.
on
(
event
,
data
=>
{
if
(
!
this
.
connected
())
{
if
(
!
this
.
connected
())
{
log
.
verbose
(
'
Io
'
,
'
init
Wechaty
() on event[%s] without a connected websocket
'
,
event
)
log
.
verbose
(
'
Io
'
,
'
init
EventHook
() on event[%s] without a connected websocket
'
,
event
)
return
return
}
}
const
ioEvent
=
{
const
ioEvent
=
{
...
@@ -147,16 +191,32 @@ class Io {
...
@@ -147,16 +191,32 @@ class Io {
,
payload
:
data
,
payload
:
data
}
}
log
.
verbose
(
'
Io
'
,
'
initWechaty() on(event) send ioEvent[%s:%s]
'
,
event
,
data
)
switch
(
event
)
{
case
'
login
'
:
case
'
logout
'
:
if
(
data
instanceof
Contact
)
{
ioEvent
.
payload
=
data
.
obj
}
break
default
:
break
}
this
.
send
(
ioEvent
)
})
})
return
Promise
.
resolve
()
}
send
(
ioEvent
)
{
log
.
silly
(
'
Io
'
,
'
send(%s: %s)
'
,
ioEvent
.
name
,
ioEvent
.
payload
)
this
.
ws
.
send
(
this
.
ws
.
send
(
JSON
.
stringify
(
JSON
.
stringify
(
ioEvent
ioEvent
)
)
)
)
})
})
return
Promise
.
resolve
()
}
}
close
()
{
close
()
{
...
@@ -178,12 +238,3 @@ class Io {
...
@@ -178,12 +238,3 @@ class Io {
* Expose `Wechaty`.
* Expose `Wechaty`.
*/
*/
module
.
exports
=
Io
.
default
=
Io
.
Io
=
Io
module
.
exports
=
Io
.
default
=
Io
.
Io
=
Io
/*
www.wechaty.io
www
api
test
*/
src/wechaty.js
浏览文件 @
ffd0f7b3
...
@@ -43,6 +43,17 @@ class Wechaty extends EventEmitter {
...
@@ -43,6 +43,17 @@ class Wechaty extends EventEmitter {
version
()
{
return
this
.
npmVersion
}
version
()
{
return
this
.
npmVersion
}
user
()
{
return
this
.
puppet
&&
this
.
puppet
.
user
}
reset
(
reason
)
{
log
.
verbose
(
'
Wechaty
'
,
'
reset() because %s
'
,
reason
)
if
(
this
.
puppet
&&
this
.
puppet
.
browser
)
{
this
.
puppet
.
browser
.
dead
(
'
restart required by wechaty reset()
'
)
}
else
{
log
.
warn
(
'
Wechaty
'
,
'
reset() without browser
'
)
}
}
init
()
{
init
()
{
log
.
info
(
'
Wechaty
'
,
'
v%s initializing...
'
,
this
.
npmVersion
)
log
.
info
(
'
Wechaty
'
,
'
v%s initializing...
'
,
this
.
npmVersion
)
log
.
verbose
(
'
Wechaty
'
,
'
puppet: %s
'
,
this
.
type
)
log
.
verbose
(
'
Wechaty
'
,
'
puppet: %s
'
,
this
.
type
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录