Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_46011646
wechaty
提交
cfe64fe3
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,体验更适合开发者的 AI 搜索 >>
提交
cfe64fe3
编写于
5月 11, 2016
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ding-dong-bot show group name right
上级
d51bcc3d
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
69 addition
and
90 deletion
+69
-90
examples/ding-dong-bot.js
examples/ding-dong-bot.js
+6
-8
src/contact.js
src/contact.js
+10
-24
src/group.js
src/group.js
+15
-36
src/message.js
src/message.js
+28
-12
src/puppet-web-server.js
src/puppet-web-server.js
+6
-6
tests/contact-tests.js
tests/contact-tests.js
+2
-2
tests/group-tests.js
tests/group-tests.js
+2
-2
未找到文件。
examples/ding-dong-bot.js
浏览文件 @
cfe64fe3
const
Wechaty
=
require
(
'
../src/wechaty
'
)
const
log
=
require
(
'
npmlog
'
)
log
.
level
=
'
verbose
'
log
.
level
=
'
silly
'
//
log.level = 'silly'
const
welcome
=
`
| __ __ _ _
...
...
@@ -33,18 +33,16 @@ bot.init()
.
then
(
login
)
bot
.
on
(
'
message
'
,
(
m
)
=>
{
m
.
ready
()
.
then
(
msg
=>
log
.
warn
(
'
Bot
'
,
'
recv: %s
'
,
msg
))
.
catch
(
e
=>
log
.
err
(
'
Bot
'
,
'
ready:
'
+
e
))
m
.
ready
()
.
then
(
msg
=>
log
.
info
(
'
Bot
'
,
'
recv: %s
'
,
msg
))
.
catch
(
e
=>
log
.
error
(
'
Bot
'
,
'
ready: %s
'
,
e
))
log
.
info
(
'
Bot
'
,
'
recv: %s
'
,
m
)
if
(
/^ding|ping$/
.
test
(
m
.
get
(
'
content
'
)))
{
if
(
/^ding|ping$/i
.
test
(
m
.
get
(
'
content
'
)))
{
const
r
=
new
Wechaty
.
Message
()
r
.
set
(
'
to
'
,
m
.
get
(
'
from
'
))
r
.
set
(
'
content
'
,
'
dong
'
)
bot
.
send
(
r
)
.
then
(()
=>
{
log
.
info
(
'
Bot
'
,
'
REPLY: dong
'
)
})
.
then
(()
=>
{
log
.
warn
(
'
Bot
'
,
'
REPLY: dong
'
)
})
}
})
...
...
src/contact.js
浏览文件 @
cfe64fe3
...
...
@@ -16,38 +16,24 @@ class Contact {
this
.
id
=
id
this
.
obj
=
{}
this
.
loading
=
Contact
.
puppet
.
getContact
(
id
)
}
ready
(
contactGetter
)
{
if
(
this
.
obj
.
id
)
return
Promise
.
resolve
(
this
);
contactGetter
=
contactGetter
||
Contact
.
puppet
.
getContact
.
bind
(
Contact
.
puppet
)
return
contactGetter
(
this
.
id
)
.
then
(
data
=>
{
log
.
silly
(
'
Contact
'
,
`Contact.puppet.getContact(
${
id
}
) resolved`
)
log
.
silly
(
'
Contact
'
,
`Contact.puppet.getContact(
${
this
.
id
}
) resolved`
)
this
.
rawObj
=
data
this
.
obj
=
this
.
parse
(
data
)
return
new
Promise
(
r
=>
r
())
return
this
}).
catch
(
e
=>
{
log
.
error
(
'
Contact
'
,
`Contact.puppet.getContact(
${
id
}
) rejected: `
+
e
)
log
.
error
(
'
Contact
'
,
`Contact.puppet.getContact(
${
this
.
id
}
) rejected: `
+
e
)
throw
new
Error
(
'
getContact:
'
+
e
)
})
}
ready
()
{
const
timeout
=
1
*
1000
// 1 seconds
const
sleepTime
=
500
// 100 ms
let
spentTime
=
0
return
new
Promise
((
resolve
,
reject
)
=>
{
return
readyChecker
.
apply
(
this
)
function
readyChecker
()
{
log
.
verbose
(
'
Contact
'
,
`readyChecker(
${
spentTime
}
)`
)
if
(
this
.
obj
.
id
)
return
resolve
(
this
);
spentTime
+=
sleepTime
if
(
spentTime
>
timeout
)
return
reject
(
'
Contact.ready() timeout after
'
+
timeout
+
'
ms
'
);
return
setTimeout
(
readyChecker
.
bind
(
this
),
sleepTime
)
}
})
}
parse
(
rawObj
)
{
return
!
rawObj
?
{}
:
{
id
:
rawObj
.
UserName
...
...
src/group.js
浏览文件 @
cfe64fe3
...
...
@@ -12,52 +12,35 @@ const Contact = require('./contact')
class
Group
{
constructor
(
id
)
{
this
.
id
=
id
if
(
!
Group
.
puppet
)
throw
new
Error
(
'
no puppet attached to Group
'
);
this
.
obj
=
{}
log
.
silly
(
'
Group
'
,
`constructor(
${
id
}
)`
)
if
(
!
Group
.
puppet
)
throw
new
Error
(
'
no puppet attached to Group
'
);
}
this
.
id
=
id
this
.
obj
=
{}
toString
()
{
return
this
.
obj
.
name
?
this
.
obj
.
name
:
this
.
id
}
getId
()
{
return
this
.
id
}
ready
(
contactGetter
)
{
log
.
silly
(
'
Group
'
,
'
ready()
'
)
if
(
this
.
obj
.
id
)
return
resolve
(
this
);
this
.
loading
=
Group
.
puppet
.
getContact
(
id
)
contactGetter
=
contactGetter
||
Group
.
puppet
.
getContact
.
bind
(
Group
.
puppet
)
return
Group
.
puppet
.
getContact
(
this
.
id
)
.
then
(
data
=>
{
log
.
silly
(
'
Group
'
,
`Group.puppet.getContact(
${
id
}
) resolved`
)
log
.
silly
(
'
Group
'
,
`Group.puppet.getContact(
${
this
.
id
}
) resolved`
)
this
.
rawObj
=
data
this
.
obj
=
this
.
parse
(
data
)
return
this
}).
catch
(
e
=>
{
log
.
error
(
'
Group
'
,
`Group.puppet.getContact(
${
id
}
) rejected: `
+
e
)
log
.
error
(
'
Group
'
,
`Group.puppet.getContact(
${
this
.
id
}
) rejected: `
+
e
)
throw
new
Error
(
'
getContact:
'
+
e
)
})
}
toString
()
{
return
this
.
obj
.
name
?
this
.
obj
.
name
:
this
.
id
}
getId
()
{
return
this
.
id
}
ready
()
{
const
timeout
=
1
*
1000
// 1 seconds
const
sleepTime
=
100
// 100 ms
let
spentTime
=
0
return
new
Promise
((
resolve
,
reject
)
=>
{
return
readyChecker
.
apply
(
this
)
function
readyChecker
()
{
log
.
verbose
(
'
Group
'
,
`readyChecker(
${
spentTime
}
)`
)
if
(
this
.
obj
.
id
)
return
resolve
(
this
);
spentTime
+=
sleepTime
if
(
spentTime
>
timeout
)
return
reject
(
'
Group.ready() timeout after
'
+
timeout
+
'
ms
'
);
return
setTimeout
(
readyChecker
.
bind
(
this
),
sleepTime
)
}
})
}
parse
(
rawObj
)
{
return
!
rawObj
?
{}
:
{
id
:
rawObj
.
UserName
,
encryId
:
rawObj
.
EncryChatRoomId
// ???
,
name
:
rawObj
.
NickName
,
members
:
rawObj
.
MemberList
.
map
(
m
=>
{
return
{
...
...
@@ -77,10 +60,6 @@ class Group {
Object
.
keys
(
this
.
obj
).
forEach
(
k
=>
console
.
error
(
`
${
k
}
:
${
this
.
obj
[
k
]}
`
))
}
toString
()
{
return
`Group(
${
this
.
id
}
)`
}
getId
()
{
return
this
.
id
}
get
(
prop
)
{
return
this
.
obj
[
prop
]
}
...
...
src/message.js
浏览文件 @
cfe64fe3
...
...
@@ -13,12 +13,15 @@ const log = require('npmlog')
class
Message
{
constructor
(
rawObj
)
{
this
.
rawObj
=
rawObj
=
rawObj
||
{}
Message
.
counter
++
;
Message
.
counter
++
this
.
rawObj
=
rawObj
=
rawObj
||
{}
this
.
obj
=
this
.
parse
(
rawObj
)
}
// Transform rawObj to local m
this
.
obj
=
{
// Transform rawObj to local m
parse
(
rawObj
)
{
return
{
id
:
rawObj
.
MsgId
,
type
:
rawObj
.
MsgType
,
from
:
Contact
.
load
(
rawObj
.
MMActualSender
)
...
...
@@ -31,26 +34,39 @@ class Message {
,
actual_content
:
rawObj
.
MMActualContent
,
date
:
new
Date
(
rawObj
.
MMDisplayTime
*
1000
)
}
}
}
toString
()
{
const
name
=
this
.
obj
.
from
.
get
(
'
name
'
)
const
name
=
html2str
(
this
.
obj
.
from
.
get
(
'
name
'
)
)
const
group
=
this
.
obj
.
group
let
content
=
this
.
obj
.
content
let
content
=
html2str
(
this
.
obj
.
content
)
if
(
content
.
length
>
20
)
content
=
content
.
substring
(
0
,
17
)
+
'
...
'
;
if
(
group
)
return
`Message(
${
name
}
@
${
group
}
:
${
content
}
)`
else
return
`Message(
${
name
}
:
${
content
}
)`
}
let
groupStr
=
group
?
html2str
(
group
)
:
''
let
fromStr
=
'
<
'
+
name
+
(
groupStr
?
`@[
${
groupStr
}
]`
:
''
)
+
'
>
'
return
`Message#
${
Message
.
counter
}
(
${
fromStr
}
:
${
content
}
)`
function
html2str
(
html
)
{
return
html
.
replace
(
/
(
<
([^
>
]
+
)
>
)
/ig
,
''
)
.
replace
(
/'/g
,
"
'
"
)
.
replace
(
/"/g
,
'
"
'
)
.
replace
(
/>/g
,
'
>
'
)
.
replace
(
/</g
,
'
<
'
)
.
replace
(
/&/g
,
'
&
'
)
}
}
ready
()
{
return
new
Promise
((
resolve
,
reject
)
=>
{
this
.
obj
.
from
.
ready
()
// Contact from
.
then
(
r
=>
this
.
obj
.
to
.
ready
())
// Contact to
.
then
(
r
=>
resolve
(
this
))
.
catch
(
e
=>
reject
(
e
))
.
then
(
r
=>
this
.
obj
.
group
&&
this
.
obj
.
group
.
ready
())
// Group member list
.
then
(
r
=>
resolve
(
this
))
// RESOLVE
.
catch
(
e
=>
reject
(
e
))
// REJECT
})
}
inGroup
()
{
return
!!
(
this
.
obj
.
group
)
}
get
(
prop
)
{
if
(
!
prop
||
!
(
prop
in
this
.
obj
))
{
const
s
=
'
[
'
+
Object
.
keys
(
this
.
obj
).
join
(
'
,
'
)
+
'
]
'
...
...
src/puppet-web-server.js
浏览文件 @
cfe64fe3
...
...
@@ -54,9 +54,9 @@ class Server extends EventEmitter {
* after received `unload`, we re-inject the Wechaty js code into browser.
*/
this
.
on
(
'
unload
'
,
()
=>
{
log
.
verbose
(
'
Server
'
,
'
server received unload event
'
)
log
.
warn
(
'
Server
'
,
'
server received unload event
'
)
this
.
browser
.
inject
()
.
then
(()
=>
log
.
verbose
(
'
Server
'
,
'
re-injected
'
))
.
then
(()
=>
log
.
warn
(
'
Server
'
,
'
re-injected
'
))
.
catch
((
e
)
=>
log
.
error
(
'
Server
'
,
'
inject err:
'
+
e
))
})
...
...
@@ -93,7 +93,7 @@ class Server extends EventEmitter {
})
app
.
get
(
'
/ding
'
,
function
(
req
,
res
)
{
log
.
verbose
(
'
Server
'
,
'
%s GET /ding
'
,
new
Date
())
log
.
silly
(
'
Server
'
,
'
%s GET /ding
'
,
new
Date
())
res
.
end
(
'
dong
'
)
})
...
...
@@ -116,7 +116,7 @@ class Server extends EventEmitter {
this
.
socketClient
=
s
s
.
on
(
'
disconnect
'
,
function
()
{
log
.
verbose
(
'
Server
'
,
'
socket.io disconnected
'
)
log
.
warn
(
'
Server
'
,
'
socket.io disconnected
'
)
/**
* Possible conditions:
* 1. Browser reload
...
...
@@ -134,7 +134,7 @@ class Server extends EventEmitter {
,
'
unload
'
].
map
(
e
=>
{
s
.
on
(
e
,
data
=>
{
log
.
verbose
(
'
Server
'
,
`recv event[
${
e
}
] from browser`
)
log
.
silly
(
'
Server
'
,
`recv event[
${
e
}
] from browser`
)
this
.
emit
(
e
,
data
)
})
})
...
...
@@ -192,7 +192,7 @@ class Server extends EventEmitter {
const
argsJson
=
JSON
.
stringify
(
args
)
const
wechatyScript
=
`return (Wechaty && Wechaty.
${
wechatyFunc
}
.apply(undefined, JSON.parse('
${
argsJson
}
')))`
log
.
verbose
(
'
Server
'
,
'
proxyWechaty:
'
+
wechatyScript
)
log
.
silly
(
'
Server
'
,
'
proxyWechaty:
'
+
wechatyScript
)
return
this
.
browserExecute
(
wechatyScript
)
}
}
...
...
tests/contact-tests.js
浏览文件 @
cfe64fe3
...
...
@@ -11,7 +11,7 @@ test('Contact smoke testing', t => {
const
NickName
=
'
Nick Name Test
'
// Mock
Contact
.
puppet
.
getContact
=
function
(
id
)
{
const
mockContactGetter
=
function
(
id
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
if
(
id
!=
UserName
)
return
resolve
({});
setTimeout
(()
=>
{
...
...
@@ -26,7 +26,7 @@ test('Contact smoke testing', t => {
const
c
=
new
Contact
(
UserName
)
t
.
equal
(
c
.
getId
(),
UserName
,
'
id/UserName right
'
)
c
.
ready
()
c
.
ready
(
mockContactGetter
)
.
then
(
r
=>
{
t
.
equal
(
c
.
get
(
'
id
'
)
,
UserName
,
'
UserName set
'
)
t
.
equal
(
c
.
get
(
'
name
'
)
,
NickName
,
'
NickName set
'
)
...
...
tests/group-tests.js
浏览文件 @
cfe64fe3
...
...
@@ -37,7 +37,7 @@ false && test('Message ready() promise testing', t => {
Contact
.
init
()
// Mock
Contact
.
puppet
.
getContact
=
function
(
id
)
{
const
mockContactGetter
=
function
(
id
)
{
log
.
silly
(
'
MessageTesting
'
,
`mocked getContact(
${
id
}
)`
)
return
new
Promise
((
resolve
,
reject
)
=>
{
let
obj
=
{}
...
...
@@ -70,7 +70,7 @@ false && test('Message ready() promise testing', t => {
t
.
equal
(
m
.
get
(
'
id
'
),
expectedMsgId
,
'
id/MsgId right
'
)
m
.
ready
()
m
.
ready
(
mockContactGetter
)
.
then
(
r
=>
{
/*
const fromC = m.get('from')
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录