Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wechaty
wechaty
提交
91ee6440
W
wechaty
项目概览
wechaty
/
wechaty
上一次同步 接近 3 年
通知
298
Star
10499
Fork
6
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
152
列表
看板
标记
里程碑
合并请求
1
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
wechaty
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
152
Issue
152
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
91ee6440
编写于
10月 28, 2017
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add logonoff() method (
#926
)
上级
fa966db0
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
50 addition
and
12 deletion
+50
-12
example/ding-dong-bot.ts
example/ding-dong-bot.ts
+11
-7
src/puppet-web/bridge.ts
src/puppet-web/bridge.ts
+1
-1
src/puppet-web/puppet-web.ts
src/puppet-web/puppet-web.ts
+7
-1
src/puppet.ts
src/puppet.ts
+10
-0
src/wechaty.ts
src/wechaty.ts
+18
-0
test/puppet-web/puppet-web.spec.ts
test/puppet-web/puppet-web.spec.ts
+3
-3
未找到文件。
example/ding-dong-bot.ts
浏览文件 @
91ee6440
...
@@ -69,11 +69,7 @@ bot
...
@@ -69,11 +69,7 @@ bot
.
on
(
'
logout
'
,
user
=>
log
.
info
(
'
Bot
'
,
`
${
user
.
name
()}
logouted`
))
.
on
(
'
logout
'
,
user
=>
log
.
info
(
'
Bot
'
,
`
${
user
.
name
()}
logouted`
))
.
on
(
'
login
'
,
user
=>
{
.
on
(
'
login
'
,
user
=>
{
log
.
info
(
'
Bot
'
,
`
${
user
.
name
()}
logined`
)
log
.
info
(
'
Bot
'
,
`
${
user
.
name
()}
logined`
)
bot
.
say
(
'
Wechaty login
'
)
bot
.
say
(
'
Wechaty login
'
).
catch
(
console
.
error
)
})
.
on
(
'
error
'
,
e
=>
{
log
.
info
(
'
Bot
'
,
'
error: %s
'
,
e
)
bot
.
say
(
'
Wechaty error:
'
+
e
.
message
)
})
})
.
on
(
'
scan
'
,
(
url
,
code
)
=>
{
.
on
(
'
scan
'
,
(
url
,
code
)
=>
{
if
(
!
/201|200/
.
test
(
String
(
code
)))
{
if
(
!
/201|200/
.
test
(
String
(
code
)))
{
...
@@ -111,12 +107,20 @@ bot
...
@@ -111,12 +107,20 @@ bot
bot
.
start
()
bot
.
start
()
.
catch
(
e
=>
{
.
catch
(
e
=>
{
log
.
error
(
'
Bot
'
,
'
init() fail: %s
'
,
e
)
log
.
error
(
'
Bot
'
,
'
init() fail: %s
'
,
e
)
bot
.
quit
()
bot
.
stop
()
process
.
exit
(
-
1
)
process
.
exit
(
-
1
)
})
})
bot
.
on
(
'
error
'
,
async
e
=>
{
log
.
error
(
'
Bot
'
,
'
error: %s
'
,
e
)
await
bot
.
say
(
'
Wechaty error:
'
+
e
.
message
).
catch
(
console
.
error
)
await
bot
.
stop
()
})
finis
((
code
,
signal
)
=>
{
finis
((
code
,
signal
)
=>
{
const
exitMsg
=
`Wechaty exit
${
code
}
because of
${
signal
}
`
const
exitMsg
=
`Wechaty exit
${
code
}
because of
${
signal
}
`
console
.
log
(
exitMsg
)
console
.
log
(
exitMsg
)
bot
.
say
(
exitMsg
)
if
(
bot
.
logonoff
())
{
bot
.
say
(
exitMsg
).
catch
(
console
.
error
)
}
})
})
src/puppet-web/bridge.ts
浏览文件 @
91ee6440
...
@@ -287,7 +287,7 @@ export class Bridge extends EventEmitter {
...
@@ -287,7 +287,7 @@ export class Bridge extends EventEmitter {
log
.
silly
(
'
PuppetWebBridge
'
,
'
quit() browser.close()-ed
'
)
log
.
silly
(
'
PuppetWebBridge
'
,
'
quit() browser.close()-ed
'
)
}
catch
(
e
)
{
}
catch
(
e
)
{
log
.
warn
(
'
PuppetWebBridge
'
,
'
quit() exception: %s
'
,
e
)
log
.
warn
(
'
PuppetWebBridge
'
,
'
quit() exception: %s
'
,
e
)
this
.
emit
(
'
error
'
,
e
)
//
this.emit('error', e)
}
finally
{
}
finally
{
this
.
state
.
off
(
true
)
this
.
state
.
off
(
true
)
}
}
...
...
src/puppet-web/puppet-web.ts
浏览文件 @
91ee6440
...
@@ -263,6 +263,7 @@ export class PuppetWeb extends Puppet {
...
@@ -263,6 +263,7 @@ export class PuppetWeb extends Puppet {
})
})
this
.
bridge
.
on
(
'
ding
'
,
Event
.
onDing
.
bind
(
this
))
this
.
bridge
.
on
(
'
ding
'
,
Event
.
onDing
.
bind
(
this
))
this
.
bridge
.
on
(
'
error
'
,
e
=>
this
.
emit
(
'
error
'
,
e
))
this
.
bridge
.
on
(
'
log
'
,
Event
.
onLog
.
bind
(
this
))
this
.
bridge
.
on
(
'
log
'
,
Event
.
onLog
.
bind
(
this
))
this
.
bridge
.
on
(
'
login
'
,
Event
.
onLogin
.
bind
(
this
))
this
.
bridge
.
on
(
'
login
'
,
Event
.
onLogin
.
bind
(
this
))
this
.
bridge
.
on
(
'
logout
'
,
Event
.
onLogout
.
bind
(
this
))
this
.
bridge
.
on
(
'
logout
'
,
Event
.
onLogout
.
bind
(
this
))
...
@@ -301,6 +302,11 @@ export class PuppetWeb extends Puppet {
...
@@ -301,6 +302,11 @@ export class PuppetWeb extends Puppet {
}
}
public
logined
():
boolean
{
public
logined
():
boolean
{
log
.
warn
(
'
PuppetWeb
'
,
'
logined() DEPRECATED. use logonoff() instead.
'
)
return
this
.
logonoff
()
}
public
logonoff
():
boolean
{
return
!!
(
this
.
user
)
return
!!
(
this
.
user
)
}
}
...
@@ -722,7 +728,7 @@ export class PuppetWeb extends Puppet {
...
@@ -722,7 +728,7 @@ export class PuppetWeb extends Puppet {
* send to `filehelper` for notice / log
* send to `filehelper` for notice / log
*/
*/
public
async
say
(
content
:
string
):
Promise
<
boolean
>
{
public
async
say
(
content
:
string
):
Promise
<
boolean
>
{
if
(
!
this
.
log
ined
())
{
if
(
!
this
.
log
onoff
())
{
throw
new
Error
(
'
can not say before login
'
)
throw
new
Error
(
'
can not say before login
'
)
}
}
...
...
src/puppet.ts
浏览文件 @
91ee6440
...
@@ -110,14 +110,24 @@ export abstract class Puppet extends EventEmitter implements Sayable {
...
@@ -110,14 +110,24 @@ export abstract class Puppet extends EventEmitter implements Sayable {
public
abstract
self
()
:
Contact
public
abstract
self
()
:
Contact
/**
* Message
*/
public
abstract
forward
(
message
:
MediaMessage
,
contact
:
Contact
|
Room
)
:
Promise
<
boolean
>
public
abstract
forward
(
message
:
MediaMessage
,
contact
:
Contact
|
Room
)
:
Promise
<
boolean
>
public
abstract
say
(
content
:
string
)
:
Promise
<
boolean
>
public
abstract
say
(
content
:
string
)
:
Promise
<
boolean
>
public
abstract
send
(
message
:
Message
|
MediaMessage
)
:
Promise
<
boolean
>
public
abstract
send
(
message
:
Message
|
MediaMessage
)
:
Promise
<
boolean
>
/**
* Login / Logout
*/
public
abstract
logonoff
()
:
boolean
public
abstract
reset
(
reason
?:
string
)
:
void
public
abstract
reset
(
reason
?:
string
)
:
void
public
abstract
logout
()
:
Promise
<
void
>
public
abstract
logout
()
:
Promise
<
void
>
public
abstract
quit
()
:
Promise
<
void
>
public
abstract
quit
()
:
Promise
<
void
>
/**
* Misc
*/
public
abstract
ding
()
:
Promise
<
string
>
public
abstract
ding
()
:
Promise
<
string
>
/**
/**
...
...
src/wechaty.ts
浏览文件 @
91ee6440
...
@@ -523,6 +523,24 @@ export class Wechaty extends EventEmitter implements Sayable {
...
@@ -523,6 +523,24 @@ export class Wechaty extends EventEmitter implements Sayable {
return
return
}
}
/**
* Get the logon / logoff state
*
* @returns {boolean}
* @example
* if (bot.logonoff()) {
* console.log('Bot logined')
* } else {
* console.log('Bot not logined')
* }
*/
public
logonoff
():
Boolean
{
if
(
!
this
.
puppet
)
{
return
false
}
return
this
.
puppet
.
logonoff
()
}
/**
/**
* Get current user
* Get current user
*
*
...
...
test/puppet-web/puppet-web.spec.ts
浏览文件 @
91ee6440
...
@@ -64,13 +64,13 @@ test('login/logout events', sinonTest(async function (t: test.Test) {
...
@@ -64,13 +64,13 @@ test('login/logout events', sinonTest(async function (t: test.Test) {
await
pw
.
init
()
await
pw
.
init
()
t
.
pass
(
'
should be inited
'
)
t
.
pass
(
'
should be inited
'
)
t
.
is
(
pw
.
log
ined
()
,
false
,
'
should be not logined
'
)
t
.
is
(
pw
.
log
onoff
()
,
false
,
'
should be not logined
'
)
const
EXPECTED_CHIPER
=
'
loginFired
'
const
EXPECTED_CHIPER
=
'
loginFired
'
const
loginPromise
=
new
Promise
(
r
=>
pw
.
once
(
'
login
'
,
_
=>
r
(
EXPECTED_CHIPER
)))
const
loginPromise
=
new
Promise
(
r
=>
pw
.
once
(
'
login
'
,
_
=>
r
(
EXPECTED_CHIPER
)))
pw
.
bridge
.
emit
(
'
login
'
,
'
TestPuppetWeb
'
)
pw
.
bridge
.
emit
(
'
login
'
,
'
TestPuppetWeb
'
)
t
.
is
(
await
loginPromise
,
EXPECTED_CHIPER
,
'
should fired login event
'
)
t
.
is
(
await
loginPromise
,
EXPECTED_CHIPER
,
'
should fired login event
'
)
t
.
is
(
pw
.
log
ined
(),
true
,
'
should be logined
'
)
t
.
is
(
pw
.
log
onoff
(),
true
,
'
should be logined
'
)
t
.
ok
((
pw
.
bridge
.
getUserName
as
any
).
called
,
'
bridge.getUserName should be called
'
)
t
.
ok
((
pw
.
bridge
.
getUserName
as
any
).
called
,
'
bridge.getUserName should be called
'
)
t
.
ok
((
pw
.
getContact
as
any
).
called
,
'
pw.getContact should be called
'
)
t
.
ok
((
pw
.
getContact
as
any
).
called
,
'
pw.getContact should be called
'
)
...
@@ -81,7 +81,7 @@ test('login/logout events', sinonTest(async function (t: test.Test) {
...
@@ -81,7 +81,7 @@ test('login/logout events', sinonTest(async function (t: test.Test) {
const
logoutPromise
=
new
Promise
((
res
,
rej
)
=>
pw
.
once
(
'
logout
'
,
_
=>
res
(
'
logoutFired
'
)))
const
logoutPromise
=
new
Promise
((
res
,
rej
)
=>
pw
.
once
(
'
logout
'
,
_
=>
res
(
'
logoutFired
'
)))
pw
.
bridge
.
emit
(
'
logout
'
)
pw
.
bridge
.
emit
(
'
logout
'
)
t
.
is
(
await
logoutPromise
,
'
logoutFired
'
,
'
should fire logout event
'
)
t
.
is
(
await
logoutPromise
,
'
logoutFired
'
,
'
should fire logout event
'
)
t
.
is
(
pw
.
log
ined
(),
false
,
'
should be logouted
'
)
t
.
is
(
pw
.
log
onoff
(),
false
,
'
should be logouted
'
)
await
pw
.
quit
()
await
pw
.
quit
()
profile
.
destroy
()
profile
.
destroy
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录