Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.Veneno.
wechaty
提交
a5759bea
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,发现更多精彩内容 >>
提交
a5759bea
编写于
10月 23, 2016
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
#34 add example for contact-bot
上级
3435e8cc
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
101 addition
and
2 deletion
+101
-2
example/contact-bot.ts
example/contact-bot.ts
+86
-0
src/contact.ts
src/contact.ts
+15
-2
未找到文件。
example/contact-bot.ts
0 → 100644
浏览文件 @
a5759bea
/**
*
* Wechaty - Wechat for Bot
*
* Connecting ChatBots
* https://github.com/wechaty/wechaty
*
*/
import
{
Config
,
Contact
,
Wechaty
,
log
}
from
'
../
'
const
welcome
=
`
=============== Powered by Wechaty ===============
-------- https://github.com/wechaty/wechaty --------
Hello,
I'm a Wechaty Botie with the following super powers:
1. List all your contacts with weixn id & name
__________________________________________________
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
=
Wechaty
.
instance
({
profile
:
Config
.
DEFAULT_PROFILE
})
bot
.
on
(
'
login
'
,
function
(
this
,
user
)
{
log
.
info
(
'
Bot
'
,
`
${
user
.
name
()}
logined`
)
this
.
say
(
'
wechaty contact-bot just logined
'
)
main
()
})
.
on
(
'
logout
'
,
user
=>
log
.
info
(
'
Bot
'
,
`
${
user
.
name
()}
logouted`
))
.
on
(
'
error
'
,
e
=>
log
.
info
(
'
Bot
'
,
'
error: %s
'
,
e
))
.
on
(
'
scan
'
,
(
url
,
code
)
=>
{
if
(
!
/201|200/
.
test
(
String
(
code
)))
{
let
loginUrl
=
url
.
replace
(
/
\/
qrcode
\/
/
,
'
/l/
'
)
require
(
'
qrcode-terminal
'
).
generate
(
loginUrl
)
}
console
.
log
(
`
${
url
}
\n[
${
code
}
] Scan QR Code in above url to login: `
)
})
bot
.
init
()
.
catch
(
e
=>
{
log
.
error
(
'
Bot
'
,
'
init() fail: %s
'
,
e
)
bot
.
quit
()
process
.
exit
(
-
1
)
})
async
function
main
()
{
const
contactList
=
await
Contact
.
findAll
()
log
.
info
(
'
Bot
'
,
'
#######################
'
)
log
.
info
(
'
Bot
'
,
'
Contact number: %d
\n
'
,
contactList
.
length
)
const
MAX
=
17
for
(
let
i
=
0
;
i
<
contactList
.
length
;
i
++
)
{
const
contact
=
contactList
[
i
]
await
contact
.
ready
()
if
(
!
contact
.
weixin
())
{
await
contact
.
refresh
()
}
log
.
info
(
'
Bot
'
,
'
Contact: %s: %s
'
,
contact
.
weixin
(),
contact
.
name
())
if
(
i
>
MAX
)
{
log
.
info
(
'
Bot
'
,
'
Contact too many, I only show you the first %d ...
'
,
MAX
)
break
}
}
log
.
info
(
'
Bot
'
,
'
I will re-dump contact names after 7 second...
'
)
setTimeout
(
main
,
7000
)
}
src/contact.ts
浏览文件 @
a5759bea
...
...
@@ -53,6 +53,7 @@ export class Contact implements Sayable {
private
static
pool
=
new
Map
<
string
,
Contact
>
()
private
obj
:
ContactObj
|
null
private
dirtyObj
:
ContactObj
|
null
private
rawObj
:
ContactRawObj
constructor
(
public
readonly
id
:
string
)
{
...
...
@@ -89,6 +90,7 @@ export class Contact implements Sayable {
}
}
public
weixin
()
{
return
this
.
obj
&&
this
.
obj
.
weixin
||
''
}
public
name
()
{
return
UtilLib
.
plainText
(
this
.
obj
&&
this
.
obj
.
name
||
''
)
}
public
remark
()
{
return
this
.
obj
&&
this
.
obj
.
remark
}
public
stranger
()
{
return
this
.
obj
&&
this
.
obj
.
stranger
}
...
...
@@ -100,6 +102,14 @@ export class Contact implements Sayable {
return
!!
(
this
.
obj
&&
this
.
obj
.
id
)
}
public
async
refresh
():
Promise
<
this
>
{
if
(
this
.
isReady
())
{
this
.
dirtyObj
=
this
.
obj
}
this
.
obj
=
null
return
this
.
ready
()
}
public
async
ready
(
contactGetter
?:
(
id
:
string
)
=>
Promise
<
ContactRawObj
>
):
Promise
<
this
>
{
log
.
silly
(
'
Contact
'
,
'
ready(
'
+
(
contactGetter
?
typeof
contactGetter
:
''
)
+
'
)
'
)
if
(
!
this
.
id
)
{
...
...
@@ -157,7 +167,10 @@ export class Contact implements Sayable {
return
selfId
===
userId
}
public
static
findAll
(
query
:
ContactQueryFilter
):
Promise
<
Contact
[]
>
{
public
static
findAll
(
query
?:
ContactQueryFilter
):
Promise
<
Contact
[]
>
{
if
(
!
query
)
{
query
=
{
name
:
/.*/
}
}
log
.
verbose
(
'
Cotnact
'
,
'
findAll({ name: %s })
'
,
query
.
name
)
const
name
=
query
.
name
...
...
@@ -192,7 +205,7 @@ export class Contact implements Sayable {
log
.
verbose
(
'
Contact
'
,
'
find(%s)
'
,
query
.
name
)
const
contactList
=
await
Contact
.
findAll
(
query
)
if
(
!
contactList
||
contactList
.
length
<
1
)
{
if
(
!
contactList
||
!
contactList
.
length
)
{
throw
new
Error
(
'
find not found any contact
'
)
}
return
contactList
[
0
]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录