Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wechaty
wechaty
提交
d00902ad
W
wechaty
项目概览
wechaty
/
wechaty
上一次同步 大约 3 年
通知
304
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,发现更多精彩内容 >>
提交
d00902ad
编写于
4月 28, 2018
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
code clean
上级
74a6efd0
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
125 addition
and
123 deletion
+125
-123
examples/ding-dong-bot.ts
examples/ding-dong-bot.ts
+1
-2
package.json
package.json
+2
-2
src/abstract-puppet/room.ts
src/abstract-puppet/room.ts
+104
-101
src/index.ts
src/index.ts
+9
-4
src/puppet-accessory.ts
src/puppet-accessory.ts
+2
-0
src/puppet-config.ts
src/puppet-config.ts
+1
-8
src/puppet-puppeteer/event.ts
src/puppet-puppeteer/event.ts
+1
-1
src/puppet-puppeteer/puppet-puppeteer.ts
src/puppet-puppeteer/puppet-puppeteer.ts
+4
-4
src/puppet-puppeteer/wechaty-bro.js
src/puppet-puppeteer/wechaty-bro.js
+1
-1
未找到文件。
examples/ding-dong-bot.ts
浏览文件 @
d00902ad
...
...
@@ -28,7 +28,6 @@ const finis = require('finis')
* when you are runing with Docker or NPM instead of Git Source.
*/
import
{
config
,
Wechaty
,
log
,
}
from
'
../src/
'
...
...
@@ -62,7 +61,7 @@ Please wait... I'm trying to login in...
`
console
.
log
(
welcome
)
const
bot
=
Wechaty
.
instance
(
{
profile
:
config
.
default
.
DEFAULT_PROFILE
}
)
const
bot
=
Wechaty
.
instance
()
bot
.
on
(
'
logout
'
,
user
=>
log
.
info
(
'
Bot
'
,
`
${
user
.
name
()}
logouted`
))
...
...
package.json
浏览文件 @
d00902ad
...
...
@@ -8,14 +8,14 @@
"DEFAULT_HEAD"
:
0
,
"DEFAULT_PORT"
:
8080
,
"DEFAULT_PUPPET"
:
"puppeteer"
,
"DEFAULT_PROFILE"
:
"de
mo
"
,
"DEFAULT_PROFILE"
:
"de
fault
"
,
"DEFAULT_PROTOCOL"
:
"io|0.0.1"
,
"DEFAULT_TOKEN"
:
"WECHATY_IO_TOKEN"
,
"DEFAULT_APIHOST"
:
"api.chatie.io"
},
"scripts"
:
{
"clean"
:
"shx rm -fr dist/*"
,
"dist"
:
"npm run clean && tsc && shx cp src/puppet-
web/*.js dist/src/puppet-web
/"
,
"dist"
:
"npm run clean && tsc && shx cp src/puppet-
puppeteer/*.js dist/src/puppet-puppeteer
/"
,
"doc"
:
"npm run dist && echo '# Wechaty v'$(jq -r .version package.json)' Documentation
\n
* https://blog.chatie.io
\n
' > docs/index.md && jsdoc2md dist/src/{wechaty,room,contact,friend-request,message}.js dist/src/puppet-web/{friend-request,schema}.js>> docs/index.md"
,
"coverage"
:
"nyc report --reporter=text-lcov | coveralls"
,
"changelog"
:
"github_changelog_generator -u chatie -p wechaty && sed -i'.bak' /greenkeeper/d CHANGELOG.md && sed -i'.bak' '/An in-range update of/d' CHANGELOG.md && ts-node scripts/sort-contributiveness.ts < CHANGELOG.md > CHANGELOG.new.md 2>/dev/null && cat CHANGELOG.md >> CHANGELOG.new.md && mv CHANGELOG.new.md CHANGELOG.md"
,
...
...
src/abstract-puppet/room.ts
浏览文件 @
d00902ad
...
...
@@ -28,9 +28,12 @@ import PuppetAccessory from '../puppet-accessory'
import
Contact
from
'
./contact
'
import
Message
from
'
./message
'
export
type
RoomEventName
=
'
join
'
|
'
leave
'
|
'
topic
'
export
const
ROOM_EVENT_DICT
=
{
join
:
'
tbw
'
,
leave
:
'
tbw
'
,
topic
:
'
tbw
'
,
}
export
type
RoomEventName
=
keyof
typeof
ROOM_EVENT_DICT
export
interface
RoomMemberQueryFilter
{
name
?:
string
,
...
...
@@ -51,6 +54,104 @@ export interface RoomQueryFilter {
export
abstract
class
Room
extends
PuppetAccessory
implements
Sayable
{
protected
static
readonly
pool
=
new
Map
<
string
,
Room
>
()
/**
* Create a new room.
*
* @static
* @param {Contact[]} contactList
* @param {string} [topic]
* @returns {Promise<Room>}
* @example <caption>Creat a room with 'lijiarui' and 'juxiaomi', the room topic is 'ding - created'</caption>
* const helperContactA = await Contact.find({ name: 'lijiarui' }) // change 'lijiarui' to any contact in your wechat
* const helperContactB = await Contact.find({ name: 'juxiaomi' }) // change 'juxiaomi' to any contact in your wechat
* const contactList = [helperContactA, helperContactB]
* console.log('Bot', 'contactList: %s', contactList.join(','))
* const room = await Room.create(contactList, 'ding')
* console.log('Bot', 'createDingRoom() new ding room created: %s', room)
* await room.topic('ding - created')
* await room.say('ding - created')
*/
public
static
async
create
(
contactList
:
Contact
[],
topic
?:
string
):
Promise
<
Room
>
{
log
.
verbose
(
'
Room
'
,
'
create(%s, %s)
'
,
contactList
.
join
(
'
,
'
),
topic
)
if
(
!
contactList
||
!
Array
.
isArray
(
contactList
))
{
throw
new
Error
(
'
contactList not found
'
)
}
try
{
const
room
=
await
this
.
puppet
.
roomCreate
(
contactList
,
topic
)
return
room
}
catch
(
e
)
{
log
.
error
(
'
Room
'
,
'
create() exception: %s
'
,
e
&&
e
.
stack
||
e
.
message
||
e
)
Raven
.
captureException
(
e
)
throw
e
}
}
/**
* Find room by topic, return all the matched room
*
* @static
* @param {RoomQueryFilter} [query]
* @returns {Promise<Room[]>}
* @example
* const roomList = await Room.findAll() // get the room list of the bot
* const roomList = await Room.findAll({name: 'wechaty'}) // find all of the rooms with name 'wechaty'
*/
public
static
async
findAll
(
query
:
RoomQueryFilter
=
{
topic
:
/.*/
},
):
Promise
<
Room
[]
>
{
log
.
verbose
(
'
Room
'
,
'
findAll({ topic: %s })
'
,
query
.
topic
)
if
(
!
query
.
topic
)
{
throw
new
Error
(
'
topicFilter not found
'
)
}
try
{
const
roomList
=
await
this
.
puppet
.
roomFindAll
(
query
)
await
Promise
.
all
(
roomList
.
map
(
room
=>
room
.
ready
()))
return
roomList
}
catch
(
e
)
{
log
.
verbose
(
'
Room
'
,
'
findAll() rejected: %s
'
,
e
.
message
)
Raven
.
captureException
(
e
)
return
[]
as
Room
[]
// fail safe
}
}
/**
* Try to find a room by filter: {topic: string | RegExp}. If get many, return the first one.
*
* @param {RoomQueryFilter} query
* @returns {Promise<Room | null>} If can find the room, return Room, or return null
*/
public
static
async
find
(
query
:
RoomQueryFilter
):
Promise
<
Room
|
null
>
{
log
.
verbose
(
'
Room
'
,
'
find({ topic: %s })
'
,
query
.
topic
)
const
roomList
=
await
this
.
findAll
(
query
)
if
(
!
roomList
||
roomList
.
length
<
1
)
{
return
null
}
else
if
(
roomList
.
length
>
1
)
{
log
.
warn
(
'
Room
'
,
'
find() got more than one result, return the 1st one.
'
)
}
return
roomList
[
0
]
}
/**
* @private
*/
public
static
load
(
id
:
string
):
Room
{
if
(
!
id
)
{
throw
new
Error
(
'
Room.load() no id
'
)
}
if
(
id
in
this
.
pool
)
{
return
this
.
pool
[
id
]
}
return
this
.
pool
[
id
]
=
new
(
this
as
any
)(
id
)
}
/**
* @private
*/
...
...
@@ -347,90 +448,6 @@ export abstract class Room extends PuppetAccessory implements Sayable {
*/
public
abstract
memberList
():
Contact
[]
/**
* Create a new room.
*
* @static
* @param {Contact[]} contactList
* @param {string} [topic]
* @returns {Promise<Room>}
* @example <caption>Creat a room with 'lijiarui' and 'juxiaomi', the room topic is 'ding - created'</caption>
* const helperContactA = await Contact.find({ name: 'lijiarui' }) // change 'lijiarui' to any contact in your wechat
* const helperContactB = await Contact.find({ name: 'juxiaomi' }) // change 'juxiaomi' to any contact in your wechat
* const contactList = [helperContactA, helperContactB]
* console.log('Bot', 'contactList: %s', contactList.join(','))
* const room = await Room.create(contactList, 'ding')
* console.log('Bot', 'createDingRoom() new ding room created: %s', room)
* await room.topic('ding - created')
* await room.say('ding - created')
*/
public
static
async
create
(
contactList
:
Contact
[],
topic
?:
string
):
Promise
<
Room
>
{
log
.
verbose
(
'
Room
'
,
'
create(%s, %s)
'
,
contactList
.
join
(
'
,
'
),
topic
)
if
(
!
contactList
||
!
Array
.
isArray
(
contactList
))
{
throw
new
Error
(
'
contactList not found
'
)
}
try
{
const
room
=
await
this
.
puppet
.
roomCreate
(
contactList
,
topic
)
return
room
}
catch
(
e
)
{
log
.
error
(
'
Room
'
,
'
create() exception: %s
'
,
e
&&
e
.
stack
||
e
.
message
||
e
)
Raven
.
captureException
(
e
)
throw
e
}
}
/**
* Find room by topic, return all the matched room
*
* @static
* @param {RoomQueryFilter} [query]
* @returns {Promise<Room[]>}
* @example
* const roomList = await Room.findAll() // get the room list of the bot
* const roomList = await Room.findAll({name: 'wechaty'}) // find all of the rooms with name 'wechaty'
*/
public
static
async
findAll
(
query
:
RoomQueryFilter
=
{
topic
:
/.*/
},
):
Promise
<
Room
[]
>
{
log
.
verbose
(
'
Room
'
,
'
findAll({ topic: %s })
'
,
query
.
topic
)
if
(
!
query
.
topic
)
{
throw
new
Error
(
'
topicFilter not found
'
)
}
try
{
const
roomList
=
await
this
.
puppet
.
roomFindAll
(
query
)
await
Promise
.
all
(
roomList
.
map
(
room
=>
room
.
ready
()))
return
roomList
}
catch
(
e
)
{
log
.
verbose
(
'
Room
'
,
'
findAll() rejected: %s
'
,
e
.
message
)
Raven
.
captureException
(
e
)
return
[]
as
Room
[]
// fail safe
}
}
/**
* Try to find a room by filter: {topic: string | RegExp}. If get many, return the first one.
*
* @param {RoomQueryFilter} query
* @returns {Promise<Room | null>} If can find the room, return Room, or return null
*/
public
static
async
find
(
query
:
RoomQueryFilter
):
Promise
<
Room
|
null
>
{
log
.
verbose
(
'
Room
'
,
'
find({ topic: %s })
'
,
query
.
topic
)
const
roomList
=
await
this
.
findAll
(
query
)
if
(
!
roomList
||
roomList
.
length
<
1
)
{
return
null
}
else
if
(
roomList
.
length
>
1
)
{
log
.
warn
(
'
Room
'
,
'
find() got more than one result, return the 1st one.
'
)
}
return
roomList
[
0
]
}
/**
* Force reload data for Room
*
...
...
@@ -446,20 +463,6 @@ export abstract class Room extends PuppetAccessory implements Sayable {
*/
public
abstract
owner
():
Contact
|
null
/**
* @private
*/
public
static
load
(
id
:
string
):
Room
{
if
(
!
id
)
{
throw
new
Error
(
'
Room.load() no id
'
)
}
if
(
id
in
this
.
pool
)
{
return
this
.
pool
[
id
]
}
return
this
.
pool
[
id
]
=
new
(
this
as
any
)(
id
)
}
}
export
default
Room
src/index.ts
浏览文件 @
d00902ad
...
...
@@ -4,6 +4,14 @@ export {
VERSION
,
}
from
'
./config
'
/**
* We need to put `Wechaty` at the beginning of this file for import
* because we have circluar dependencies between `Puppet` & `Wechaty`
*/
import
{
Wechaty
,
}
from
'
./wechaty
'
export
{
Contact
,
FriendRequest
,
...
...
@@ -16,16 +24,13 @@ export {
}
from
'
./deprecated
'
export
{
IoClient
}
from
'
./io-client
'
export
{
Profile
}
from
'
./profile
'
export
{
Misc
}
from
'
./misc
'
export
{
Profile
}
from
'
./profile
'
export
{
PuppetPuppeteer
,
}
from
'
./puppet-puppeteer/
'
import
{
Wechaty
,
}
from
'
./wechaty
'
export
{
Wechaty
,
}
...
...
src/puppet-accessory.ts
浏览文件 @
d00902ad
...
...
@@ -15,6 +15,7 @@ export abstract class PuppetAccessory extends EventEmitter {
log
.
silly
(
'
PuppetAssessory
'
,
'
static set puppet(%s)
'
,
puppet
.
constructor
.
name
)
this
.
_puppet
=
puppet
}
public
static
get
puppet
():
Puppet
{
log
.
silly
(
'
PuppetAssessory
'
,
'
static get puppet()
'
)
...
...
@@ -34,6 +35,7 @@ export abstract class PuppetAccessory extends EventEmitter {
log
.
silly
(
'
PuppetAssessory
'
,
'
set puppet(%s)
'
,
puppet
.
constructor
.
name
)
this
.
_puppet
=
puppet
}
public
get
puppet
():
Puppet
{
log
.
silly
(
'
PuppetAssessory
'
,
'
get puppet() from instance
'
)
...
...
src/puppet-config.ts
浏览文件 @
d00902ad
...
...
@@ -2,14 +2,7 @@ import PuppetPuppeteer from './puppet-puppeteer/'
import
PuppetMock
from
'
./puppet-mock/
'
/**
* would be nice if we have a typing from package.json schema
*
* https://github.com/DefinitelyTyped/DefinitelyTyped/issues/15602
* https://github.com/Microsoft/TypeScript/issues/3136
*/
/**
* Puppet Official Plugins List
* Wechaty Official Puppet Plugins List
*/
export
const
PUPPET_DICT
=
{
mock
:
PuppetMock
,
...
...
src/puppet-puppeteer/event.ts
浏览文件 @
d00902ad
...
...
@@ -109,7 +109,7 @@ async function onLogin(this: PuppetPuppeteer, note: string, ttl = 30): Promise<v
return
}
this
.
scanInfo
=
null
this
.
scanInfo
=
undefined
if
(
this
.
user
)
{
log
.
warn
(
'
PuppetPuppeteerEvent
'
,
'
onLogin(%s) user had already set: "%s"
'
,
note
,
this
.
user
)
...
...
src/puppet-puppeteer/puppet-puppeteer.ts
浏览文件 @
d00902ad
...
...
@@ -56,17 +56,17 @@ import {
MsgRawObj
,
MediaType
,
MsgType
,
}
from
'
./schema
'
}
from
'
./schema
'
import
{
PuppeteerContact
,
PuppeteerContactRawObj
,
}
from
'
./puppeteer-contact
'
}
from
'
./puppeteer-contact
'
import
PuppeteerMessage
from
'
./puppeteer-message
'
import
{
PuppeteerRoom
,
PuppeteerRoomRawObj
,
}
from
'
./puppeteer-room
'
}
from
'
./puppeteer-room
'
import
PuppeteerFriendRequest
from
'
./puppeteer-friend-request
'
export
type
PuppetFoodType
=
'
scan
'
|
'
ding
'
...
...
@@ -74,7 +74,7 @@ export type ScanFoodType = 'scan' | 'login' | 'logout'
export
class
PuppetPuppeteer
extends
Puppet
{
public
bridge
:
Bridge
public
scanInfo
:
ScanData
|
null
public
scanInfo
?:
ScanData
public
scanWatchdog
:
Watchdog
<
ScanFoodType
>
...
...
src/puppet-puppeteer/wechaty-bro.js
浏览文件 @
d00902ad
...
...
@@ -776,7 +776,7 @@
/*
* WechatyBro injectio must return this object.
* Puppet
Web
Bridge need this to decide if injection is successful.
* Puppet
Puppeteer
Bridge need this to decide if injection is successful.
*/
var
retObj
=
{
code
:
200
,
// 2XX ok, 4XX/5XX error. HTTP like
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录