Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_46011646
wechaty
提交
29b0ab10
W
wechaty
项目概览
weixin_46011646
/
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,发现更多精彩内容 >>
提交
29b0ab10
编写于
10月 05, 2016
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
#32 add Firer Class to match message with event
上级
2546084f
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
254 addition
and
97 deletion
+254
-97
src/puppet-web/event.js
src/puppet-web/event.js
+11
-97
src/puppet-web/firer.js
src/puppet-web/firer.js
+162
-0
src/puppet-web/firer.spec.js
src/puppet-web/firer.spec.js
+81
-0
未找到文件。
src/puppet-web/event.js
浏览文件 @
29b0ab10
...
...
@@ -22,11 +22,13 @@ const util = require('util')
const
fs
=
require
(
'
fs
'
)
const
co
=
require
(
'
co
'
)
const
log
=
require
(
'
../brolog-env
'
)
const
Contact
=
require
(
'
../contact
'
)
const
Message
=
require
(
'
../message
'
)
const
MediaMessage
=
require
(
'
../message-media
'
)
const
log
=
require
(
'
../brolog-env
'
)
const
Contact
=
require
(
'
../contact
'
)
const
Message
=
require
(
'
../message
'
)
const
MediaMessage
=
require
(
'
../message-media
'
)
const
FriendRequest
=
require
(
'
./friend-request
'
)
const
Firer
=
require
(
'
./firer
'
)
const
PuppetWebEvent
=
{
onBrowserDead
...
...
@@ -338,16 +340,17 @@ function onServerMessage(data) {
* Fire Events if match message type & content
*/
switch
(
m
.
type
())
{
// data.MsgType
case
Message
.
Type
.
VERIFYMSG
:
fireFriendRequest
(
m
)
Firer
.
fireFriendRequest
.
call
(
this
,
m
)
break
case
Message
.
Type
.
SYS
:
if
(
m
.
room
())
{
fireRoomJoin
(
m
)
fireRoomLeave
(
m
)
Firer
.
fireRoomJoin
.
call
(
this
,
m
)
Firer
.
fireRoomLeave
.
call
(
this
,
m
)
}
else
{
fireFriendConfirm
(
m
.
content
()
)
Firer
.
fireFriendConfirm
.
call
(
this
,
m
)
}
break
}
...
...
@@ -382,93 +385,4 @@ function onServerMessage(data) {
})
}
function
fireFriendRequest
(
m
)
{
const
info
=
m
.
rawObj
.
RecommendInfo
log
.
verbose
(
'
PuppetWebEvent
'
,
'
fireFriendRequest(%s)
'
,
info
)
const
request
=
new
FriendRequest
()
request
.
receive
(
info
)
this
.
emit
(
'
friend
'
,
request
.
contact
,
request
)
}
function
fireFriendConfirm
(
m
)
{
const
content
=
m
.
content
()
log
.
silly
(
'
PuppetWebEvent
'
,
'
fireFriendConfirm(%s)
'
,
content
)
/**
* try to find FriendRequest Confirmation Message
*/
if
(
!
/^You have added
(
.+
)
as your WeChat contact. Start chatting!$/
.
test
(
content
))
{
return
}
const
request
=
new
FriendRequest
()
const
contact
=
Contact
.
load
(
m
.
get
(
'
from
'
))
request
.
confirm
(
contact
)
this
.
emit
(
'
friend
'
,
contact
)
}
/**
* try to find 'join' event for Room
*
1.
You've invited "李卓桓" to the group chat
You've invited "李卓桓.PreAngel、Bruce LEE" to the group chat
2.
"李卓桓.PreAngel" invited "Bruce LEE" to the group chat
*/
function
fireRoomJoin
(
m
)
{
const
room
=
m
.
room
()
const
content
=
m
.
content
()
const
inviteRe
=
/^"
?(
.+
)
"
?
invited "
(
.+
)
" to the group chat$/
const
found
=
content
.
match
(
inviteRe
)
if
(
!
found
)
{
return
}
const
[
_
,
inviter
,
invitee
]
=
found
let
inviterContact
,
inviteeContact
if
(
inviter
===
"
You've
"
)
{
inviterContact
=
Contact
.
load
(
this
.
userId
)
}
else
{
inviterContact
=
room
.
member
(
inviter
)
}
inviteeContact
=
room
.
member
(
invitee
)
if
(
!
inviterContact
||
!
inviteeContact
)
{
log
.
error
(
'
PuppetWebEvent
'
,
'
inivter or invitee not found for %s, %s
'
,
inviter
,
invitee
)
return
}
room
.
emit
(
'
join
'
,
inviteeContact
,
inviterContact
)
}
/**
* You removed "Bruce LEE" from the group chat
*/
function
fireRoomLeave
(
m
)
{
const
room
=
m
.
room
()
const
content
=
m
.
content
()
const
removeRe
=
/^You removed "
(
.+
)
" from the group chat$/
const
found
=
content
.
match
(
removeRe
)
if
(
!
found
)
{
return
}
let
[
_
,
leaver
]
=
found
leaverContact
=
m
.
room
().
member
(
leaver
)
if
(
!
leaverContact
)
{
log
.
error
(
'
PuppetWebEvent
'
,
'
leaver not found for %s
'
,
leaver
)
return
}
room
.
emit
(
'
leave
'
,
leaverContact
)
}
module
.
exports
=
PuppetWebEvent
src/puppet-web/firer.js
0 → 100644
浏览文件 @
29b0ab10
/**
*
* Wechaty: Wechat for Bot. Connecting ChatBots
*
* Class PuppetWeb Firer
*
* Process the Message to find which event to FIRE
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
/**************************************
*
* Firer for Class PuppetWeb
*
* here `this` is a PuppetWeb Instance
*
***************************************/
const
util
=
require
(
'
util
'
)
const
fs
=
require
(
'
fs
'
)
const
co
=
require
(
'
co
'
)
const
log
=
require
(
'
../brolog-env
'
)
const
Contact
=
require
(
'
../contact
'
)
const
Message
=
require
(
'
../message
'
)
const
FriendRequest
=
require
(
'
./friend-request
'
)
const
PuppetWebFirer
=
{
fireFriendConfirm
,
fireFriendRequest
,
fireRoomJoin
,
fireRoomLeave
// for testing
,
checkFriendConfirm
,
checkRoomJoin
,
checkRoomLeave
}
const
regexConfig
=
{
friendConfirm
:
/^You have added
(
.+
)
as your WeChat contact. Start chatting!$/
,
roomJoin
:
/^"
?(
.+
?)
"
?
invited "
(
.+
)
" to the group chat$/
,
roomLeave
:
/^You removed "
(
.+
)
" from the group chat$/
}
function
fireFriendRequest
(
m
)
{
const
info
=
m
.
rawObj
.
RecommendInfo
log
.
verbose
(
'
PuppetWebEvent
'
,
'
fireFriendRequest(%s)
'
,
info
)
const
request
=
new
FriendRequest
()
request
.
receive
(
info
)
this
.
emit
(
'
friend
'
,
request
.
contact
,
request
)
}
/**
* try to find FriendRequest Confirmation Message
*/
function
checkFriendConfirm
(
content
)
{
const
re
=
regexConfig
.
friendConfirm
if
(
re
.
test
(
content
))
{
return
true
}
else
{
return
false
}
}
function
fireFriendConfirm
(
m
)
{
const
content
=
m
.
content
()
log
.
silly
(
'
PuppetWebEvent
'
,
'
fireFriendConfirm(%s)
'
,
content
)
if
(
!
checkFriendConfirm
(
content
))
{
return
}
const
request
=
new
FriendRequest
()
const
contact
=
Contact
.
load
(
m
.
get
(
'
from
'
))
request
.
confirm
(
contact
)
this
.
emit
(
'
friend
'
,
contact
)
}
/**
* try to find 'join' event for Room
*
1.
You've invited "李卓桓" to the group chat
You've invited "李卓桓.PreAngel、Bruce LEE" to the group chat
2.
"李卓桓.PreAngel" invited "Bruce LEE" to the group chat
*/
function
checkRoomJoin
(
content
)
{
const
re
=
regexConfig
.
roomJoin
const
found
=
content
.
match
(
re
)
if
(
!
found
)
{
return
false
}
const
[
_
,
inviter
,
invitee
]
=
found
return
[
invitee
,
inviter
]
}
function
fireRoomJoin
(
m
)
{
const
room
=
m
.
room
()
const
content
=
m
.
content
()
const
[
invitee
,
inviter
]
=
checkRoomJoin
(
cotent
)
if
(
!
invitee
||
!
inviter
)
{
return
}
let
inviterContact
,
inviteeContact
if
(
inviter
===
"
You've
"
)
{
inviterContact
=
Contact
.
load
(
this
.
userId
)
}
else
{
inviterContact
=
room
.
member
(
inviter
)
}
inviteeContact
=
room
.
member
(
invitee
)
if
(
!
inviterContact
||
!
inviteeContact
)
{
log
.
error
(
'
PuppetWebEvent
'
,
'
inivter or invitee not found for %s, %s
'
,
inviter
,
invitee
)
return
}
room
.
emit
(
'
join
'
,
inviteeContact
,
inviterContact
)
}
function
checkRoomLeave
(
content
)
{
const
re
=
regexConfig
.
roomLeave
const
found
=
content
.
match
(
re
)
if
(
!
found
)
{
return
false
}
const
[
_
,
leaver
]
=
found
return
leaver
}
/**
* You removed "Bruce LEE" from the group chat
*/
function
fireRoomLeave
(
m
)
{
const
leaver
=
checkRoomLeave
(
m
.
content
())
if
(
!
leaver
)
{
return
}
const
room
=
m
.
room
()
leaverContact
=
room
.
member
(
leaver
)
if
(
!
leaverContact
)
{
log
.
error
(
'
PuppetWebEvent
'
,
'
leaver not found for %s
'
,
leaver
)
return
}
room
.
emit
(
'
leave
'
,
leaverContact
)
}
module
.
exports
=
PuppetWebFirer
src/puppet-web/firer.spec.js
0 → 100755
浏览文件 @
29b0ab10
/**
*
* Wechaty: Wechat for Bot. Connecting ChatBots
*
* Class PuppetWeb Firer
*
* Process the Message to find which event to FIRE
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import
{
test
}
from
'
ava
'
import
Contact
from
'
../contact
'
import
Message
from
'
../message
'
import
FriendRequest
from
'
./friend-request
'
import
Firer
from
'
./firer
'
test
(
'
Firer smoking test
'
,
t
=>
{
t
.
true
(
true
,
'
should be true
'
)
})
test
(
'
Firer.checkFriendConfirm
'
,
t
=>
{
const
content
=
'
You have added 李卓桓 as your WeChat contact. Start chatting!
'
let
result
result
=
Firer
.
checkFriendConfirm
(
content
)
t
.
truthy
(
result
,
'
should be truthy for confirm msg
'
)
result
=
Firer
.
checkFriendConfirm
(
'
fsdfsdfasdfasdfadsa
'
)
t
.
falsy
(
result
,
'
should be falsy for other msg
'
)
})
test
(
'
Firer.checkRoomJoin
'
,
t
=>
{
const
contentList
=
[
[
`You've invited "李卓桓" to the group chat`
,
`You've`
,
`李卓桓`
]
,
[
`You've invited "李卓桓.PreAngel、Bruce LEE" to the group chat`
,
`You've`
,
`李卓桓.PreAngel、Bruce LEE`
]
,
[
`"李卓桓.PreAngel" invited "Bruce LEE" to the group chat`
,
`李卓桓.PreAngel`
,
`Bruce LEE`
]
]
let
result
contentList
.
forEach
(([
content
,
inviter
,
invitee
])
=>
{
result
=
Firer
.
checkRoomJoin
(
content
)
t
.
truthy
(
result
,
'
should check room join message right for
'
+
content
)
t
.
is
(
result
[
0
],
invitee
,
'
should get invitee right
'
)
t
.
is
(
result
[
1
],
inviter
,
'
should get inviter right
'
)
})
result
=
Firer
.
checkRoomJoin
(
'
fsadfsadfsdfsdfs
'
)
t
.
false
(
result
,
'
should get false if message is not expected
'
)
})
test
(
'
Firer.checkRoomLeave
'
,
t
=>
{
const
data
=
[
`You removed "Bruce LEE" from the group chat`
,
`Bruce LEE`
]
let
leaver
leaver
=
Firer
.
checkRoomLeave
(
data
[
0
])
t
.
truthy
(
leaver
,
'
should get leaver for leave message
'
)
t
.
is
(
leaver
,
data
[
1
],
'
should get leaver name right
'
)
leaver
=
Firer
.
checkRoomLeave
(
'
fafdsfsdfafa
'
)
t
.
false
(
leaver
,
'
should get false if message is not expected
'
)
})
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录