Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.Veneno.
wechaty
提交
a117dfc9
W
wechaty
项目概览
.Veneno.
/
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,发现更多精彩内容 >>
提交
a117dfc9
编写于
10月 06, 2016
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
new room class methods demo
上级
b587668e
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
155 addition
and
0 deletion
+155
-0
example/room-bot.js
example/room-bot.js
+155
-0
未找到文件。
example/room-bot.js
0 → 100644
浏览文件 @
a117dfc9
const
{
Wechaty
,
Config
,
Room
,
Contact
,
Message
,
log
}
=
require
(
'
.
'
)
const
welcome
=
`
=============== Powered by Wechaty ===============
-------- https://github.com/wechaty/wechaty --------
Hello,
I'm a Wechaty Botie with the following super powers:
1. Find a room
2. Add people to room
3. Del people from room
4. Change room topic
5. Monitor room events
6. etc...
If you send a message of magic word 'ding',
you will get a invitation to join my own room!
__________________________________________________
Hope you like it, and you are very welcome to
upgrade me for more super powers!
Please wait... I'm trying to login in...
`
console
.
log
(
welcome
)
const
bot
=
new
Wechaty
({
profile
:
Config
.
DEFAULT_PROFILE
})
bot
.
on
(
'
scan
'
,
({
url
,
code
})
=>
{
console
.
log
(
`Use Wechat to Scan QR Code in url to login:
${
code
}
\n
${
url
}
`
)
})
.
on
(
'
login
'
,
user
=>
{
log
.
info
(
'
Bot
'
,
`
${
user
.
name
()}
logined`
)
setTimeout
(
_
=>
{
Room
.
find
({
name
:
/^ding/i
})
.
then
(
room
=>
{
log
.
info
(
'
Bot
'
,
'
start monitor "ding" room join/leave event
'
)
room
.
on
(
'
join
'
,
(
invitee
,
inviter
)
=>
{
try
{
log
.
info
(
'
Bot
'
,
'
room event: %s join, invited by %s
'
,
invitee
.
name
(),
inviter
.
name
())
const
m
=
new
Message
()
m
.
set
(
'
room
'
,
room
.
id
)
if
(
inviter
.
id
!==
bot
.
user
().
id
)
{
m
.
set
(
'
to
'
,
inviter
.
id
)
m
.
set
(
'
content
'
,
`@
${
inviter
.
name
()}
RULE1: Invitation is limited to me, the owner only. Please do not invit people without notify me.`
)
bot
.
send
(
m
)
m
.
set
(
'
to
'
,
invitee
.
id
)
m
.
set
(
'
content
'
,
`@
${
invitee
.
name
()}
Please contact me: by send "ding" to me, I will re-sed you a invitation. Now I will remove you out, sorry.`
)
bot
.
send
(
m
)
room
.
topic
(
'
ding - warn
'
+
inviter
.
name
())
room
.
del
(
invitee
)
}
else
{
m
.
set
(
'
to
'
,
invitee
.
id
)
m
.
set
(
'
content
'
,
`@
${
invitee
.
name
()}
Welcome to my room! :)`
)
bot
.
send
(
m
)
room
.
topic
(
'
ding - welcome
'
+
invitee
.
name
())
}
}
catch
(
e
)
{
log
.
error
(
'
room join event exception: %s
'
,
e
.
stack
)
}
})
room
.
on
(
'
leave
'
,
(
leaver
)
=>
{
log
.
info
(
'
Bot
'
,
'
room event: %s leave, byebye
'
,
leaver
.
name
())
})
})
.
catch
(
e
=>
{
log
.
warn
(
'
Bot
'
,
'
there is no room named ding(yet)
'
)
})
},
3000
)
})
.
on
(
'
room-join
'
,
(
room
,
invitee
,
inviter
)
=>
{
log
.
info
(
'
Bot
'
,
'
room-join event: Room %s got new member %s, invited by %s
'
,
room
.
toic
()
,
invitee
.
name
()
,
inviter
.
name
()
)
})
.
on
(
'
room-leave
'
,
(
room
,
leaver
)
=>
{
log
.
info
(
'
Bot
'
,
'
room-leave event: Room %s lost member %s
'
,
room
.
topic
()
,
leaver
.
name
()
)
})
.
on
(
'
logout
'
,
user
=>
log
.
info
(
'
Bot
'
,
`
${
user
.
name
()}
logouted`
))
.
on
(
'
error
'
,
e
=>
log
.
info
(
'
Bot
'
,
'
error: %s
'
,
e
))
.
on
(
'
message
'
,
m
=>
{
const
room
=
m
.
room
()
const
from
=
m
.
from
()
console
.
log
((
room
?
'
[
'
+
room
.
topic
()
+
'
]
'
:
''
)
+
'
<
'
+
from
.
name
()
+
'
>
'
+
'
:
'
+
m
.
toStringDigest
()
)
if
(
bot
.
self
(
m
))
{
return
}
const
noticeMessage
=
new
Message
()
noticeMessage
.
set
(
'
to
'
,
from
)
/**
* `ding` will be the magic(toggle) word:
* 1. say ding first time, will got a room invitation
* 2. say ding in room, will be removed out
*/
if
(
'
ding
'
===
m
.
get
(
'
content
'
))
{
if
(
m
.
room
())
{
if
(
/^ding/i
.
test
(
room
.
topic
()))
{
noticeMessage
.
set
(
'
content
'
,
'
You said "ding" in my room, I will remove you out.
'
)
bot
.
send
(
noticeMessage
)
room
.
del
(
m
.
from
())
}
}
else
{
var
contact
=
m
.
from
()
Room
.
find
({
name
:
/^ding/i
})
.
then
(
room
=>
{
if
(
room
)
{
room
.
add
(
contact
)
.
catch
(
e
=>
{
log
.
error
(
'
Bot
'
,
'
room.add() exception: %s
'
,
e
.
stack
)
})
}
else
{
Contact
.
find
({
name
:
'
Bruce LEE
'
})
.
then
(
c
=>
{
Room
.
create
([
contact
,
c
],
'
ding
'
)
.
then
(
_
=>
{
log
.
info
(
'
Bot
'
,
'
ding room not found, created one
'
)
})
.
catch
(
e
=>
{
log
.
error
(
'
Bot
'
,
'
ding room create fail: %s
'
,
e
.
stack
)
})
})
.
catch
(
e
=>
{
log
.
error
(
'
Bot
'
,
'
Contact.find not found
'
)
})
}
})
}
}
})
.
init
()
.
catch
(
e
=>
console
.
error
(
e
))
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录