Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.www.
wechaty
提交
6f302769
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,体验更适合开发者的 AI 搜索 >>
提交
6f302769
编写于
9月 05, 2016
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
rename io-bot to io-client
上级
f423fc55
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
176 addition
and
58 deletion
+176
-58
bin/client.js
bin/client.js
+10
-10
index.js
index.js
+2
-2
package.json
package.json
+5
-5
src/io-client.js
src/io-client.js
+148
-0
src/io.js
src/io.js
+1
-1
src/wechaty.js
src/wechaty.js
+9
-39
test/wechaty.spec.js
test/wechaty.spec.js
+1
-1
未找到文件。
bin/
io-bo
t.js
→
bin/
clien
t.js
浏览文件 @
6f302769
#!/usr/bin/env node
const
{
Io
Bo
t
Io
Clien
t
,
Config
,
log
}
=
require
(
'
../
'
)
...
...
@@ -27,28 +27,28 @@ __________________________________________________
let
token
=
Config
.
token
if
(
!
token
)
{
log
.
error
(
'
Bot
'
,
'
token not found: please set WECHATY_TOKEN in environment before run io-bo
t
'
)
log
.
error
(
'
Client
'
,
'
token not found: please set WECHATY_TOKEN in environment before run io-clien
t
'
)
// process.exit(-1)
token
=
Config
.
DEFAULT_TOKEN
log
.
warn
(
'
Bo
t
'
,
`set token to "
${
token
}
" for demo purpose`
)
log
.
warn
(
'
Clien
t
'
,
`set token to "
${
token
}
" for demo purpose`
)
}
console
.
log
(
welcome
)
log
.
info
(
'
Bo
t
'
,
'
Starting for WECHATY_TOKEN: %s
'
,
token
)
log
.
info
(
'
Clien
t
'
,
'
Starting for WECHATY_TOKEN: %s
'
,
token
)
const
ioBot
=
new
IoBo
t
({
const
client
=
new
IoClien
t
({
token
,
log
})
ioBo
t
.
init
()
.
catch
(
onError
.
bind
(
ioBo
t
))
clien
t
.
init
()
.
catch
(
onError
.
bind
(
clien
t
))
ioBo
t
.
initWeb
()
.
catch
(
onError
.
bind
(
ioBo
t
))
clien
t
.
initWeb
()
.
catch
(
onError
.
bind
(
clien
t
))
function
onError
(
e
)
{
log
.
error
(
'
Bo
t
'
,
'
initWeb() fail: %s
'
,
e
)
log
.
error
(
'
Clien
t
'
,
'
initWeb() fail: %s
'
,
e
)
this
.
quit
()
process
.
exit
(
-
1
)
}
...
...
index.js
浏览文件 @
6f302769
...
...
@@ -8,7 +8,7 @@ const Room = require('./src/room')
const
Puppet
=
require
(
'
./src/puppet
'
)
const
PuppetWeb
=
require
(
'
./src/puppet-web
'
)
const
Io
Bot
=
require
(
'
./src/io-bo
t
'
)
const
Io
Client
=
require
(
'
./src/io-clien
t
'
)
const
log
=
require
(
'
./src/brolog-env
'
)
...
...
@@ -26,7 +26,7 @@ Object.assign(Wechaty, {
,
Puppet
,
PuppetWeb
,
Io
Bo
t
,
Io
Clien
t
,
version
:
require
(
'
./package.json
'
).
version
,
log
// for convenionce use npmlog with environment variable LEVEL
...
...
package.json
浏览文件 @
6f302769
...
...
@@ -10,21 +10,21 @@
"DEFAULT_PORT"
:
8788
,
"DEFAULT_PUPPET_PORT"
:
18788
,
"DEFAULT_PROTOCOL"
:
"io|0.0.1"
,
"DEFAULT_TOKEN"
:
"
WECHATY-
TOKEN"
,
"DEFAULT_TOKEN"
:
"
TEST_
TOKEN"
,
"ENDPOINT"
:
"wss://api.wechaty.io/v0/websocket"
,
"BINARY_CHROMIUM"
:
"/wechaty/bin/xvfb-chromium"
},
"scripts"
:
{
"lint"
:
"npm run eslint && npm run tslint"
,
"eslint"
:
"eslint
\"
{src,test}/**/*.js
\"
"
,
"tslint"
:
"tslint
\"
{src,test}/**/*.ts
\"
"
,
"eslint"
:
"eslint
\"
{src,test
,bin
}/**/*.js
\"
"
,
"tslint"
:
"tslint
\"
{src,test
,bin
}/**/*.ts
\"
"
,
"pretest"
:
"npm run lint"
,
"test"
:
"npm run test:phantomjs && npm run test:chrome"
,
"test:phantomjs"
:
"cross-env LC_ALL=C WECHATY_LOG=info WECHATY_HEAD=phantomjs ava --timeout=10m
\"
{src,test}/**/*.spec.js
\"
"
,
"test:chrome"
:
"cross-env LC_ALL=C WECHATY_LOG=silly WECHATY_HEAD=chrome ava --timeout=10m
\"
{src,test}/**/*.spec.js
\"
"
,
"testdev"
:
"cross-env LC_ALL=C WECHATY_LOG=silly ava --serial --verbose --fail-fast --timeout=3m"
,
"ava"
:
"cross-env LC_ALL=C WECHATY_LOG=verbose ava
\"
{src,test}/**/*.spec.js
\"
"
,
"start"
:
"node bin/
io-bo
t"
,
"start"
:
"node bin/
clien
t"
,
"demo"
:
"node example/ding-dong-bot.js"
},
"repository"
:
{
...
...
@@ -32,7 +32,7 @@
"url"
:
"git+https://github.com/wechaty/wechaty.git"
},
"bin"
:
{
"
io-bot"
:
"bin/io-bo
t.js"
,
"
wechaty-client"
:
"bin/clien
t.js"
,
"wechaty-version"
:
"bin/version.js"
},
"keywords"
:
[
...
...
src/io-
bo
t.js
→
src/io-
clien
t.js
100644 → 100755
浏览文件 @
6f302769
...
...
@@ -2,70 +2,109 @@
*
* wechaty: Wechat for Bot. and for human who talk to bot/robot
*
* Class Io
* Class Io
Client
* http://www.wechaty.io
*
* Licenst: ISC
* https://github.com/
zixia
/wechaty
* https://github.com/
wechaty
/wechaty
*
*/
const
co
=
require
(
'
co
'
)
/**
* DO NOT use `require('../')` here!
* because it will casue a LOOP require ERROR
*/
const
Wechaty
=
require
(
'
./wechaty
'
)
const
Config
=
require
(
'
./config
'
)
const
Io
=
require
(
'
./io
'
)
const
brolog
=
require
(
'
./brolog-env
'
)
class
Io
Bo
t
{
class
Io
Clien
t
{
constructor
({
token
,
log
=
Wechaty
.
log
})
{
token
=
Config
.
token
||
Config
.
DEFAULT_TOKEN
,
endpoint
=
Config
.
endpoint
,
log
=
brolog
}
=
{})
{
if
(
!
log
)
{
const
e
=
new
Error
(
'
constructor() log(npmlog/brolog) must be set
'
)
throw
e
}
this
.
log
=
log
this
.
log
.
verbose
(
'
Io
Bo
t
'
,
'
constructor() with token: %s
'
,
token
)
this
.
log
.
verbose
(
'
Io
Clien
t
'
,
'
constructor() with token: %s
'
,
token
)
if
(
!
token
)
{
const
e
=
new
Error
(
'
constructor() token must be set
'
)
this
.
log
.
error
(
'
Io
Bot
'
,
e
)
this
.
log
.
error
(
'
Io
Client
'
,
e
.
messag
e
)
throw
e
}
this
.
token
=
token
}
init
()
{
this
.
log
.
verbose
(
'
IoBot
'
,
'
init()
'
)
this
.
log
.
verbose
(
'
IoClient
'
,
'
init()
'
)
return
co
.
call
(
this
,
function
*
()
{
this
.
wechaty
=
yield
this
.
initWechaty
()
this
.
io
=
yield
this
.
initIo
()
return
this
}).
catch
(
e
=>
{
this
.
log
.
error
(
'
IoClient
'
,
'
init() exception: %s
'
,
e
.
message
)
throw
e
})
}
initWechaty
()
{
this
.
log
.
verbose
(
'
IoClient
'
,
'
initWechaty()
'
)
const
wechaty
=
this
.
wechaty
=
new
Wechaty
({
const
wechaty
=
new
Wechaty
({
profile
:
Config
.
DEFAULT_PROFILE
,
token
:
this
.
token
})
wechaty
.
on
(
'
login
'
,
user
=>
this
.
log
.
info
(
'
IoBot
'
,
`
${
user
.
name
()}
logined`
))
.
on
(
'
logout
'
,
user
=>
this
.
log
.
info
(
'
IoBot
'
,
`
${
user
.
name
()}
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
))
})
.
on
(
'
login
'
,
user
=>
this
.
log
.
info
(
'
IoClient
'
,
`
${
user
.
name
()}
logined`
))
.
on
(
'
logout
'
,
user
=>
this
.
log
.
info
(
'
IoClient
'
,
`
${
user
.
name
()}
logouted`
))
.
on
(
'
scan
'
,
({
url
,
code
})
=>
this
.
log
.
info
(
'
IoClient
'
,
`[
${
code
}
]
${
url
}
`
))
.
on
(
'
message
'
,
message
=>
{
message
.
ready
()
.
then
(
this
.
onMessage
.
bind
(
this
))
.
catch
(
e
=>
this
.
log
.
error
(
'
IoClient
'
,
'
message.ready() %s
'
,
e
))
})
return
wechaty
.
init
()
.
then
(
_
=>
{
this
.
log
.
verbose
(
'
IoBot
'
,
'
wechaty.init() succ
'
)
return
this
})
.
catch
(
e
=>
{
this
.
log
.
error
(
'
IoBot
'
,
'
init() init fail: %s
'
,
e
)
wechaty
.
quit
()
throw
e
})
.
then
(
_
=>
{
this
.
log
.
verbose
(
'
IoClient
'
,
'
wechaty.init() succ
'
)
return
this
})
.
catch
(
e
=>
{
this
.
log
.
error
(
'
IoClient
'
,
'
init() init fail: %s
'
,
e
)
wechaty
.
quit
()
throw
e
})
}
initIo
()
{
this
.
log
.
verbose
(
'
IoClient
'
,
'
initIo()
'
)
if
(
!
this
.
token
)
{
log
.
verbose
(
'
IoClient
'
,
'
initIo() skiped for no token set
'
)
return
Promise
.
resolve
(
'
no token
'
)
}
else
{
log
.
verbose
(
'
IoClient
'
,
'
initIo(%s)
'
,
this
.
token
)
}
const
io
=
new
Io
({
wechaty
:
this
.
wechaty
,
token
:
this
.
token
,
endpoint
:
this
.
endpoint
})
return
io
.
init
()
.
catch
(
e
=>
{
log
.
verbose
(
'
IoClient
'
,
'
initIo() init fail: %s
'
,
e
.
message
)
throw
e
})
}
initWeb
(
port
=
Config
.
httpPort
)
{
...
...
@@ -79,7 +118,7 @@ class IoBot {
return
new
Promise
((
resolve
,
reject
)
=>
{
app
.
listen
(
port
,
()
=>
{
this
.
log
.
verbose
(
'
Io
Bo
t
'
,
'
initWeb() Wechaty IO Bot listening on port
'
+
port
+
'
!
'
)
this
.
log
.
verbose
(
'
Io
Clien
t
'
,
'
initWeb() Wechaty IO Bot listening on port
'
+
port
+
'
!
'
)
return
resolve
(
this
)
...
...
@@ -106,4 +145,4 @@ class IoBot {
}
}
module
.
exports
=
Io
Bot
.
default
=
IoBot
.
IoBot
=
IoBo
t
module
.
exports
=
Io
Client
.
default
=
IoClient
.
IoClient
=
IoClien
t
src/io.js
浏览文件 @
6f302769
...
...
@@ -24,7 +24,7 @@ class Io {
,
token
=
null
,
endpoint
=
Config
.
endpoint
,
protocol
=
Config
.
DEFAULT_PROTOCOL
})
{
}
=
{}
)
{
if
(
!
wechaty
||
!
token
)
{
throw
new
Error
(
'
Io must has wechaty & token set
'
)
}
...
...
src/wechaty.js
浏览文件 @
6f302769
...
...
@@ -26,8 +26,6 @@ class Wechaty extends EventEmitter {
type
=
Config
.
puppet
,
head
=
Config
.
head
,
port
=
Config
.
port
,
endpoint
=
Config
.
endpoint
,
token
=
Config
.
token
// token for wechaty.io auth. will connect to wechaty.io cloud management if token is set
,
profile
=
Config
.
profile
// no profile, no session save/restore
}
=
{})
{
log
.
verbose
(
'
Wechaty
'
,
'
contructor()
'
)
...
...
@@ -36,8 +34,6 @@ class Wechaty extends EventEmitter {
this
.
type
=
type
this
.
head
=
head
this
.
port
=
port
this
.
token
=
token
this
.
endpoint
=
endpoint
this
.
profile
=
/
\.
wechaty
\.
json$/i
.
test
(
profile
)
?
profile
...
...
@@ -59,7 +55,10 @@ class Wechaty extends EventEmitter {
if
(
!
forceNpm
)
{
try
{
// Synchronous version of fs.access(). This throws if any accessibility checks fail, and does nothing otherwise.
/**
* Synchronous version of fs.access().
* This throws if any accessibility checks fail, and does nothing otherwise.
*/
fs
.
accessSync
(
dotGitPath
,
fs
.
F_OK
)
const
ss
=
require
(
'
child_process
'
)
...
...
@@ -96,12 +95,11 @@ class Wechaty extends EventEmitter {
}
init
()
{
log
.
info
(
'
Wechaty
'
,
'
v%s initializing...
'
,
this
.
version
())
log
.
verbose
(
'
Wechaty
'
,
'
puppet: %s
'
,
this
.
type
)
log
.
verbose
(
'
Wechaty
'
,
'
head: %s
'
,
this
.
head
)
log
.
verbose
(
'
Wechaty
'
,
'
profile: %s
'
,
this
.
profile
)
log
.
verbose
(
'
Wechaty
'
,
'
uuid: %s
'
,
this
.
uuid
)
log
.
info
(
'
Wechaty
'
,
'
v%s initializing...
'
,
this
.
version
())
log
.
verbose
(
'
Wechaty
'
,
'
puppet: %s
'
,
this
.
type
)
log
.
verbose
(
'
Wechaty
'
,
'
head: %s
'
,
this
.
head
)
log
.
verbose
(
'
Wechaty
'
,
'
profile: %s
'
,
this
.
profile
)
log
.
verbose
(
'
Wechaty
'
,
'
uuid: %s
'
,
this
.
uuid
)
if
(
this
.
inited
)
{
log
.
error
(
'
Wechaty
'
,
'
init() already inited. return and do nothing.
'
)
...
...
@@ -109,12 +107,6 @@ class Wechaty extends EventEmitter {
}
return
co
.
call
(
this
,
function
*
()
{
this
.
io
=
yield
this
.
initIo
()
.
catch
(
e
=>
{
// fail safe
log
.
error
(
'
Wechaty
'
,
'
initIo failed: %s
'
,
e
.
message
)
this
.
emit
(
'
error
'
,
e
)
})
this
.
puppet
=
yield
this
.
initPuppet
()
this
.
inited
=
true
...
...
@@ -126,28 +118,6 @@ class Wechaty extends EventEmitter {
})
}
initIo
()
{
if
(
!
this
.
token
)
{
log
.
verbose
(
'
Wechaty
'
,
'
initIo() skiped for no token set
'
)
return
Promise
.
resolve
(
'
no token
'
)
}
else
{
log
.
verbose
(
'
Wechaty
'
,
'
initIo(%s)
'
,
this
.
token
)
}
const
Io
=
require
(
'
./io
'
)
const
io
=
new
Io
({
wechaty
:
this
,
token
:
this
.
token
,
endpoint
:
this
.
endpoint
})
return
io
.
init
()
.
catch
(
e
=>
{
log
.
verbose
(
'
Wechaty
'
,
'
Wechaty.IO init fail: %s
'
,
e
.
message
)
throw
e
})
}
initPuppet
()
{
let
puppet
switch
(
this
.
type
)
{
...
...
test/wechaty.spec.js
浏览文件 @
6f302769
...
...
@@ -7,7 +7,7 @@ test('Wechaty Framework', t => {
t
.
truthy
(
Wechaty
.
Contact
,
'
should export Wechaty.Contact
'
)
t
.
truthy
(
Wechaty
.
Room
,
'
should export Wechaty.Room
'
)
t
.
truthy
(
Wechaty
.
Io
Bot
,
'
should export Wechaty.IoBo
t
'
)
t
.
truthy
(
Wechaty
.
Io
Client
,
'
should export Wechaty.IoClien
t
'
)
t
.
truthy
(
Wechaty
.
log
,
'
should export Wechaty.log
'
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录