@pacoyang/wechaty-puppet-wechat 0.28.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +14 -0
- package/.eslintrc.js +7 -0
- package/.github/workflows/npm.yml +91 -0
- package/.markdownlintrc +13 -0
- package/.travis.yml +62 -0
- package/.vscode/settings.json +73 -0
- package/CODEOWNERS +9 -0
- package/LICENSE +201 -0
- package/NOTICE +8 -0
- package/README.md +145 -0
- package/dist/examples/demo.d.ts +20 -0
- package/dist/examples/demo.d.ts.map +1 -0
- package/dist/examples/demo.js +113 -0
- package/dist/examples/demo.js.map +1 -0
- package/dist/scripts/retry-unit-tests.d.ts +10 -0
- package/dist/scripts/retry-unit-tests.d.ts.map +1 -0
- package/dist/scripts/retry-unit-tests.js +47 -0
- package/dist/scripts/retry-unit-tests.js.map +1 -0
- package/dist/src/bridge.d.ts +100 -0
- package/dist/src/bridge.d.ts.map +1 -0
- package/dist/src/bridge.js +927 -0
- package/dist/src/bridge.js.map +1 -0
- package/dist/src/bridge.spec.d.ts +21 -0
- package/dist/src/bridge.spec.d.ts.map +1 -0
- package/dist/src/bridge.spec.js +191 -0
- package/dist/src/bridge.spec.js.map +1 -0
- package/dist/src/config.d.ts +10 -0
- package/dist/src/config.d.ts.map +1 -0
- package/dist/src/config.js +57 -0
- package/dist/src/config.js.map +1 -0
- package/dist/src/event.d.ts +23 -0
- package/dist/src/event.d.ts.map +1 -0
- package/dist/src/event.js +166 -0
- package/dist/src/event.js.map +1 -0
- package/dist/src/event.spec.d.ts +3 -0
- package/dist/src/event.spec.d.ts.map +1 -0
- package/dist/src/event.spec.js +41 -0
- package/dist/src/event.spec.js.map +1 -0
- package/dist/src/firer.d.ts +50 -0
- package/dist/src/firer.d.ts.map +1 -0
- package/dist/src/firer.js +409 -0
- package/dist/src/firer.js.map +1 -0
- package/dist/src/firer.spec.d.ts +21 -0
- package/dist/src/firer.spec.d.ts.map +1 -0
- package/dist/src/firer.spec.js +190 -0
- package/dist/src/firer.spec.js.map +1 -0
- package/dist/src/mod.d.ts +23 -0
- package/dist/src/mod.d.ts.map +1 -0
- package/dist/src/mod.js +28 -0
- package/dist/src/mod.js.map +1 -0
- package/dist/src/puppet-wechat.d.ts +164 -0
- package/dist/src/puppet-wechat.d.ts.map +1 -0
- package/dist/src/puppet-wechat.js +1287 -0
- package/dist/src/puppet-wechat.js.map +1 -0
- package/dist/src/puppet-wechat.spec.d.ts +21 -0
- package/dist/src/puppet-wechat.spec.d.ts.map +1 -0
- package/dist/src/puppet-wechat.spec.js +123 -0
- package/dist/src/puppet-wechat.spec.js.map +1 -0
- package/dist/src/pure-function-helpers/is-type.d.ts +3 -0
- package/dist/src/pure-function-helpers/is-type.d.ts.map +1 -0
- package/dist/src/pure-function-helpers/is-type.js +12 -0
- package/dist/src/pure-function-helpers/is-type.js.map +1 -0
- package/dist/src/pure-function-helpers/message-extname.d.ts +3 -0
- package/dist/src/pure-function-helpers/message-extname.d.ts.map +1 -0
- package/dist/src/pure-function-helpers/message-extname.js +44 -0
- package/dist/src/pure-function-helpers/message-extname.js.map +1 -0
- package/dist/src/pure-function-helpers/message-filename.d.ts +3 -0
- package/dist/src/pure-function-helpers/message-filename.d.ts.map +1 -0
- package/dist/src/pure-function-helpers/message-filename.js +19 -0
- package/dist/src/pure-function-helpers/message-filename.js.map +1 -0
- package/dist/src/pure-function-helpers/message-raw-payload-parser.d.ts +4 -0
- package/dist/src/pure-function-helpers/message-raw-payload-parser.d.ts.map +1 -0
- package/dist/src/pure-function-helpers/message-raw-payload-parser.js +67 -0
- package/dist/src/pure-function-helpers/message-raw-payload-parser.js.map +1 -0
- package/dist/src/pure-function-helpers/mod.d.ts +7 -0
- package/dist/src/pure-function-helpers/mod.d.ts.map +1 -0
- package/dist/src/pure-function-helpers/mod.js +23 -0
- package/dist/src/pure-function-helpers/mod.js.map +1 -0
- package/dist/src/pure-function-helpers/normalize-scan-status.d.ts +3 -0
- package/dist/src/pure-function-helpers/normalize-scan-status.d.ts.map +1 -0
- package/dist/src/pure-function-helpers/normalize-scan-status.js +21 -0
- package/dist/src/pure-function-helpers/normalize-scan-status.js.map +1 -0
- package/dist/src/pure-function-helpers/normalize-scan-status.spec.d.ts +21 -0
- package/dist/src/pure-function-helpers/normalize-scan-status.spec.d.ts.map +1 -0
- package/dist/src/pure-function-helpers/normalize-scan-status.spec.js +37 -0
- package/dist/src/pure-function-helpers/normalize-scan-status.spec.js.map +1 -0
- package/dist/src/pure-function-helpers/web-message-type.d.ts +4 -0
- package/dist/src/pure-function-helpers/web-message-type.d.ts.map +1 -0
- package/dist/src/pure-function-helpers/web-message-type.js +58 -0
- package/dist/src/pure-function-helpers/web-message-type.js.map +1 -0
- package/dist/src/pure-function-helpers/xml.d.ts +14 -0
- package/dist/src/pure-function-helpers/xml.d.ts.map +1 -0
- package/dist/src/pure-function-helpers/xml.js +64 -0
- package/dist/src/pure-function-helpers/xml.js.map +1 -0
- package/dist/src/version.d.ts +6 -0
- package/dist/src/version.d.ts.map +1 -0
- package/dist/src/version.js +9 -0
- package/dist/src/version.js.map +1 -0
- package/dist/src/version.spec.d.ts +3 -0
- package/dist/src/version.spec.d.ts.map +1 -0
- package/dist/src/version.spec.js +13 -0
- package/dist/src/version.spec.js.map +1 -0
- package/dist/src/web-schemas.d.ts +249 -0
- package/dist/src/web-schemas.d.ts.map +1 -0
- package/dist/src/web-schemas.js +118 -0
- package/dist/src/web-schemas.js.map +1 -0
- package/dist/src/wechaty-bro.js +868 -0
- package/dist/tests/puppeteer-contact.spec.d.ts +21 -0
- package/dist/tests/puppeteer-contact.spec.d.ts.map +1 -0
- package/dist/tests/puppeteer-contact.spec.js +67 -0
- package/dist/tests/puppeteer-contact.spec.js.map +1 -0
- package/dist/tests/puppeteer-friendship.spec.d.ts +21 -0
- package/dist/tests/puppeteer-friendship.spec.d.ts.map +1 -0
- package/dist/tests/puppeteer-friendship.spec.js +115 -0
- package/dist/tests/puppeteer-friendship.spec.js.map +1 -0
- package/dist/tests/puppeteer-message.spec.d.ts +21 -0
- package/dist/tests/puppeteer-message.spec.d.ts.map +1 -0
- package/dist/tests/puppeteer-message.spec.js +184 -0
- package/dist/tests/puppeteer-message.spec.js.map +1 -0
- package/dist/tests/puppeteer-room.spec.d.ts +21 -0
- package/dist/tests/puppeteer-room.spec.d.ts.map +1 -0
- package/dist/tests/puppeteer-room.spec.js +129 -0
- package/dist/tests/puppeteer-room.spec.js.map +1 -0
- package/dist/tests/puppeteer.spec.d.ts +21 -0
- package/dist/tests/puppeteer.spec.d.ts.map +1 -0
- package/dist/tests/puppeteer.spec.js +213 -0
- package/dist/tests/puppeteer.spec.js.map +1 -0
- package/docs/images/puppeteer-logo.png +0 -0
- package/docs/images/wechaty-puppet-wechat.png +0 -0
- package/examples/demo.ts +130 -0
- package/package.json +94 -0
- package/scripts/generate-version.sh +18 -0
- package/scripts/npm-pack-testing.sh +49 -0
- package/scripts/package-publish-config-tag.sh +13 -0
- package/scripts/retry-unit-tests.ts +57 -0
- package/src/bridge.spec.ts +224 -0
- package/src/bridge.ts +1122 -0
- package/src/config.ts +71 -0
- package/src/event.spec.ts +41 -0
- package/src/event.ts +247 -0
- package/src/firer.spec.ts +208 -0
- package/src/firer.ts +530 -0
- package/src/mod.ts +29 -0
- package/src/puppet-wechat.spec.ts +149 -0
- package/src/puppet-wechat.ts +1659 -0
- package/src/pure-function-helpers/is-type.ts +7 -0
- package/src/pure-function-helpers/message-extname.ts +57 -0
- package/src/pure-function-helpers/message-filename.ts +25 -0
- package/src/pure-function-helpers/message-raw-payload-parser.ts +85 -0
- package/src/pure-function-helpers/mod.ts +6 -0
- package/src/pure-function-helpers/normalize-scan-status.spec.ts +44 -0
- package/src/pure-function-helpers/normalize-scan-status.ts +23 -0
- package/src/pure-function-helpers/web-message-type.ts +71 -0
- package/src/pure-function-helpers/xml.ts +79 -0
- package/src/typings.d.ts +19 -0
- package/src/version.spec.ts +10 -0
- package/src/version.ts +6 -0
- package/src/web-schemas.ts +298 -0
- package/src/wechaty-bro.js +868 -0
- package/tests/fixtures/inject-file.js +7 -0
- package/tests/fixtures/smoke-testing.ts +42 -0
- package/tests/puppeteer-contact.spec.ts +78 -0
- package/tests/puppeteer-friendship.spec.ts +157 -0
- package/tests/puppeteer-message.spec.ts +249 -0
- package/tests/puppeteer-room.spec.ts +166 -0
- package/tests/puppeteer.spec.ts +262 -0
- package/tsconfig.json +26 -0
package/src/firer.ts
ADDED
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wechaty - https://github.com/chatie/wechaty
|
|
3
|
+
*
|
|
4
|
+
* @copyright 2016-2018 Huan LI <zixia@zixia.net>
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
// tslint:disable:no-var-requires
|
|
21
|
+
// tslint:disable:arrow-parens
|
|
22
|
+
|
|
23
|
+
// const retryPromise = require('retry-promise').default
|
|
24
|
+
// import cuid from 'cuid'
|
|
25
|
+
|
|
26
|
+
import {
|
|
27
|
+
log,
|
|
28
|
+
} from './config'
|
|
29
|
+
|
|
30
|
+
import { PuppetWeChat } from './puppet-wechat'
|
|
31
|
+
import {
|
|
32
|
+
// WebRecomendInfo,
|
|
33
|
+
WebMessageRawPayload,
|
|
34
|
+
} from './web-schemas'
|
|
35
|
+
|
|
36
|
+
// import {
|
|
37
|
+
// // FriendRequestPayload,
|
|
38
|
+
// FriendRequestType,
|
|
39
|
+
// FriendRequestPayloadReceive,
|
|
40
|
+
// FriendRequestPayloadConfirm,
|
|
41
|
+
// } from 'wechaty-puppet'
|
|
42
|
+
|
|
43
|
+
const REGEX_CONFIG = {
|
|
44
|
+
friendConfirm: [
|
|
45
|
+
/^You have added (.+) as your WeChat contact. Start chatting!$/,
|
|
46
|
+
/^你已添加了(.+),现在可以开始聊天了。$/,
|
|
47
|
+
/^(.+) just added you to his\/her contacts list. Send a message to him\/her now!$/,
|
|
48
|
+
/^(.+)刚刚把你添加到通讯录,现在可以开始聊天了。$/,
|
|
49
|
+
],
|
|
50
|
+
|
|
51
|
+
roomJoinInvite: [
|
|
52
|
+
// There are 3 blank(charCode is 32) here. eg: You invited 管理员 to the group chat.
|
|
53
|
+
/^(.+?) invited (.+) to the group chat.\s+$/,
|
|
54
|
+
|
|
55
|
+
// There no no blank or punctuation here. eg: 管理员 invited 小桔建群助手 to the group chat
|
|
56
|
+
/^(.+?) invited (.+) to the group chat$/,
|
|
57
|
+
|
|
58
|
+
// There are 2 blank(charCode is 32) here. eg: 你邀请"管理员"加入了群聊
|
|
59
|
+
/^(.+?)邀请"(.+)"加入了群聊\s+$/,
|
|
60
|
+
|
|
61
|
+
// There no no blank or punctuation here. eg: "管理员"邀请"宁锐锋"加入了群聊
|
|
62
|
+
/^"(.+?)"邀请"(.+)"加入了群聊$/,
|
|
63
|
+
],
|
|
64
|
+
|
|
65
|
+
roomJoinQrcode: [
|
|
66
|
+
// Wechat change this, should desperate. See more in pr#651
|
|
67
|
+
// /^" (.+)" joined the group chat via the QR Code shared by "?(.+?)".$/,
|
|
68
|
+
|
|
69
|
+
// There are 2 blank(charCode is 32) here. Qrcode is shared by bot.
|
|
70
|
+
// eg: "管理员" joined group chat via the QR code you shared.
|
|
71
|
+
/^"(.+)" joined group chat via the QR code "?(.+?)"? shared.\s+$/,
|
|
72
|
+
|
|
73
|
+
// There are no blank(charCode is 32) here. Qrcode isn't shared by bot.
|
|
74
|
+
// eg: "宁锐锋" joined the group chat via the QR Code shared by "管理员".
|
|
75
|
+
/^"(.+)" joined the group chat via the QR Code shared by "?(.+?)".$/,
|
|
76
|
+
|
|
77
|
+
// There are 2 blank(charCode is 32) here. Qrcode is shared by bot. eg: "管理员"通过扫描你分享的二维码加入群聊
|
|
78
|
+
/^"(.+)"通过扫描(.+?)分享的二维码加入群聊\s+$/,
|
|
79
|
+
|
|
80
|
+
// There are 1 blank(charCode is 32) here. Qrode isn't shared by bot. eg: " 苏轼"通过扫描"管理员"分享的二维码加入群聊
|
|
81
|
+
/^"\s+(.+)"通过扫描"(.+?)"分享的二维码加入群聊$/,
|
|
82
|
+
],
|
|
83
|
+
|
|
84
|
+
// no list
|
|
85
|
+
roomLeaveIKickOther: [
|
|
86
|
+
/^(You) removed "(.+)" from the group chat$/,
|
|
87
|
+
/^(你)将"(.+)"移出了群聊$/,
|
|
88
|
+
],
|
|
89
|
+
|
|
90
|
+
roomLeaveOtherKickMe: [
|
|
91
|
+
/^(You) were removed from the group chat by "(.+)"$/,
|
|
92
|
+
/^(你)被"(.+)"移出群聊$/,
|
|
93
|
+
],
|
|
94
|
+
|
|
95
|
+
roomTopic: [
|
|
96
|
+
/^"?(.+?)"? changed the group name to "(.+)"$/,
|
|
97
|
+
/^"?(.+?)"?修改群名为“(.+)”$/,
|
|
98
|
+
],
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export class Firer {
|
|
102
|
+
|
|
103
|
+
constructor (
|
|
104
|
+
public puppet: PuppetWeChat,
|
|
105
|
+
) {
|
|
106
|
+
//
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// public async checkFriendRequest(
|
|
110
|
+
// rawPayload : WebMessageRawPayload,
|
|
111
|
+
// ): Promise<void> {
|
|
112
|
+
// if (!rawPayload.RecommendInfo) {
|
|
113
|
+
// throw new Error('no RecommendInfo')
|
|
114
|
+
// }
|
|
115
|
+
// const recommendInfo: WebRecomendInfo = rawPayload.RecommendInfo
|
|
116
|
+
// log.verbose('PuppetWeChatFirer', 'fireFriendRequest(%s)', recommendInfo)
|
|
117
|
+
|
|
118
|
+
// if (!recommendInfo) {
|
|
119
|
+
// throw new Error('no recommendInfo')
|
|
120
|
+
// }
|
|
121
|
+
|
|
122
|
+
// const contactId = recommendInfo.UserName
|
|
123
|
+
// const hello = recommendInfo.Content
|
|
124
|
+
// const ticket = recommendInfo.Ticket
|
|
125
|
+
// const type = FriendRequestType.Receive
|
|
126
|
+
// const id = cuid()
|
|
127
|
+
|
|
128
|
+
// const payloadReceive: FriendRequestPayloadReceive = {
|
|
129
|
+
// id,
|
|
130
|
+
// contactId,
|
|
131
|
+
// hello,
|
|
132
|
+
// ticket,
|
|
133
|
+
// type,
|
|
134
|
+
// }
|
|
135
|
+
|
|
136
|
+
// this.puppet.cacheFriendRequestPayload.set(id, payloadReceive)
|
|
137
|
+
|
|
138
|
+
// this.puppet.emit('friend', id)
|
|
139
|
+
// }
|
|
140
|
+
|
|
141
|
+
public async checkFriendConfirm (
|
|
142
|
+
rawPayload : WebMessageRawPayload,
|
|
143
|
+
) {
|
|
144
|
+
const content = rawPayload.Content
|
|
145
|
+
log.silly('PuppetWeChatFirer', 'fireFriendConfirm(%s)', content)
|
|
146
|
+
|
|
147
|
+
if (!this.parseFriendConfirm(content)) {
|
|
148
|
+
return
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// const contactId = rawPayload.FromUserName
|
|
152
|
+
// const type = FriendRequestType.Confirm
|
|
153
|
+
|
|
154
|
+
// const id = cuid()
|
|
155
|
+
|
|
156
|
+
// const payloadConfirm: FriendRequestPayloadConfirm = {
|
|
157
|
+
// id,
|
|
158
|
+
// contactId,
|
|
159
|
+
// type,
|
|
160
|
+
// }
|
|
161
|
+
|
|
162
|
+
// this.puppet.cacheFriendRequestPayload.set(id, payloadConfirm)
|
|
163
|
+
|
|
164
|
+
this.puppet.emit('friendship', { friendshipId: rawPayload.MsgId })
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
public async checkRoomJoin (
|
|
168
|
+
rawPayload : WebMessageRawPayload,
|
|
169
|
+
): Promise<boolean> {
|
|
170
|
+
|
|
171
|
+
const text = rawPayload.Content
|
|
172
|
+
const roomId = rawPayload.FromUserName
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Get the display names of invitee & inviter
|
|
176
|
+
*/
|
|
177
|
+
let inviteeNameList : string[]
|
|
178
|
+
let inviterName : string
|
|
179
|
+
|
|
180
|
+
try {
|
|
181
|
+
[inviteeNameList, inviterName] = this.parseRoomJoin(text)
|
|
182
|
+
} catch (e) {
|
|
183
|
+
log.silly('PuppetWeChatFirer', 'checkRoomJoin() "%s" is not a join message', text)
|
|
184
|
+
return false // not a room join message
|
|
185
|
+
}
|
|
186
|
+
log.silly('PuppetWeChatFirer', 'checkRoomJoin() inviteeList: %s, inviter: %s',
|
|
187
|
+
inviteeNameList.join(','),
|
|
188
|
+
inviterName,
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Convert the display name to Contact ID
|
|
193
|
+
*/
|
|
194
|
+
let inviterContactId: undefined | string
|
|
195
|
+
const inviteeContactIdList: string[] = []
|
|
196
|
+
|
|
197
|
+
if (/^You|你$/i.test(inviterName)) { // === 'You' || inviter === '你' || inviter === 'you'
|
|
198
|
+
inviterContactId = this.puppet.selfId()
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const sleep = 1000
|
|
202
|
+
const timeout = 60 * 1000
|
|
203
|
+
let ttl = timeout / sleep
|
|
204
|
+
|
|
205
|
+
let ready = true
|
|
206
|
+
|
|
207
|
+
while (ttl-- > 0) {
|
|
208
|
+
log.silly('PuppetWeChatFirer', 'fireRoomJoin() retry() ttl %d', ttl)
|
|
209
|
+
|
|
210
|
+
if (!ready) {
|
|
211
|
+
await new Promise(resolve => setTimeout(resolve, timeout))
|
|
212
|
+
ready = true
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* loop inviteeNameList
|
|
217
|
+
* set inviteeContactIdList
|
|
218
|
+
*/
|
|
219
|
+
for (let i = 0; i < inviteeNameList.length; i++) {
|
|
220
|
+
const inviteeName = inviteeNameList[i]
|
|
221
|
+
|
|
222
|
+
const inviteeContactId = inviteeContactIdList[i]
|
|
223
|
+
if (inviteeContactId) {
|
|
224
|
+
/**
|
|
225
|
+
* had already got ContactId for Room Member
|
|
226
|
+
* try to resolve the ContactPayload
|
|
227
|
+
*/
|
|
228
|
+
try {
|
|
229
|
+
await this.puppet.contactPayload(inviteeContactId)
|
|
230
|
+
} catch (e) {
|
|
231
|
+
log.warn('PuppetWeChatFirer', 'fireRoomJoin() contactPayload(%s) exception: %s',
|
|
232
|
+
inviteeContactId,
|
|
233
|
+
e.message,
|
|
234
|
+
)
|
|
235
|
+
ready = false
|
|
236
|
+
}
|
|
237
|
+
} else {
|
|
238
|
+
/**
|
|
239
|
+
* only had Name of RoomMember
|
|
240
|
+
* try to resolve the ContactId & ContactPayload
|
|
241
|
+
*/
|
|
242
|
+
const memberIdList = await this.puppet.roomMemberSearch(roomId, inviteeName)
|
|
243
|
+
|
|
244
|
+
if (memberIdList.length <= 0) {
|
|
245
|
+
ready = false
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const contactId = memberIdList[0]
|
|
249
|
+
// XXX: Take out the first one if we have matched many contact.
|
|
250
|
+
inviteeContactIdList[i] = contactId
|
|
251
|
+
|
|
252
|
+
try {
|
|
253
|
+
await this.puppet.contactPayload(contactId)
|
|
254
|
+
} catch (e) {
|
|
255
|
+
ready = false
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (!inviterContactId) {
|
|
263
|
+
const contactIdList = await this.puppet.roomMemberSearch(roomId, inviterName)
|
|
264
|
+
|
|
265
|
+
if (contactIdList.length > 0) {
|
|
266
|
+
inviterContactId = contactIdList[0]
|
|
267
|
+
} else {
|
|
268
|
+
ready = false
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (ready) {
|
|
273
|
+
log.silly('PuppetWeChatFirer', 'fireRoomJoin() resolve() inviteeContactIdList: %s, inviterContactId: %s',
|
|
274
|
+
inviteeContactIdList.join(','),
|
|
275
|
+
inviterContactId,
|
|
276
|
+
)
|
|
277
|
+
/**
|
|
278
|
+
* Resolve All Payload again to make sure the data is ready.
|
|
279
|
+
*/
|
|
280
|
+
await Promise.all(
|
|
281
|
+
inviteeContactIdList.map(
|
|
282
|
+
id => this.puppet.contactPayload(id),
|
|
283
|
+
),
|
|
284
|
+
)
|
|
285
|
+
|
|
286
|
+
if (!inviterContactId) {
|
|
287
|
+
throw new Error('no inviterContactId')
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
await this.puppet.contactPayload(inviterContactId)
|
|
291
|
+
await this.puppet.roomPayload(roomId)
|
|
292
|
+
|
|
293
|
+
const timestamp = Math.floor(Date.now() / 1000) // in seconds
|
|
294
|
+
this.puppet.emit('room-join', {
|
|
295
|
+
inviteeIdList : inviteeContactIdList,
|
|
296
|
+
inviterId : inviterContactId,
|
|
297
|
+
roomId,
|
|
298
|
+
timestamp,
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
return true
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
log.warn('PuppetWeChatFier', 'fireRoomJoin() resolve payload fail.')
|
|
306
|
+
return false
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* You removed "Bruce LEE" from the group chat
|
|
311
|
+
*/
|
|
312
|
+
public async checkRoomLeave (
|
|
313
|
+
rawPayload : WebMessageRawPayload,
|
|
314
|
+
): Promise<boolean> {
|
|
315
|
+
log.verbose('PuppetWeChatFirer', 'fireRoomLeave(%s)', rawPayload.Content)
|
|
316
|
+
|
|
317
|
+
const roomId = rawPayload.FromUserName
|
|
318
|
+
|
|
319
|
+
let leaverName : string
|
|
320
|
+
let removerName : string
|
|
321
|
+
|
|
322
|
+
try {
|
|
323
|
+
[leaverName, removerName] = this.parseRoomLeave(rawPayload.Content)
|
|
324
|
+
} catch (e) {
|
|
325
|
+
log.silly('PuppetWeChatFirer', 'fireRoomLeave() %s', e.message)
|
|
326
|
+
return false
|
|
327
|
+
}
|
|
328
|
+
log.silly('PuppetWeChatFirer', 'fireRoomLeave() got leaverName: %s', leaverName)
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* FIXME: leaver maybe is a list
|
|
332
|
+
* @lijiarui: I have checked, leaver will never be a list.
|
|
333
|
+
* If the bot remove 2 leavers at the same time,
|
|
334
|
+
* it will be 2 sys message, instead of 1 sys message contains 2 leavers.
|
|
335
|
+
*/
|
|
336
|
+
let leaverContactId : undefined | string
|
|
337
|
+
let removerContactId : undefined | string
|
|
338
|
+
|
|
339
|
+
if (/^(You|你)$/i.test(leaverName)) {
|
|
340
|
+
leaverContactId = this.puppet.selfId()
|
|
341
|
+
} else if (/^(You|你)$/i.test(removerName)) {
|
|
342
|
+
removerContactId = this.puppet.selfId()
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (!leaverContactId) {
|
|
346
|
+
const idList = await this.puppet.roomMemberSearch(roomId, leaverName)
|
|
347
|
+
leaverContactId = idList[0]
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (!removerContactId) {
|
|
351
|
+
const idList = await this.puppet.roomMemberSearch(roomId, removerName)
|
|
352
|
+
removerContactId = idList[0]
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (!leaverContactId || !removerContactId) {
|
|
356
|
+
throw new Error('no id')
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* FIXME: leaver maybe is a list
|
|
360
|
+
* @lijiarui 2017: I have checked, leaver will never be a list. If the bot remove 2 leavers at the same time,
|
|
361
|
+
* it will be 2 sys message, instead of 1 sys message contains 2 leavers.
|
|
362
|
+
* @huan 2018 May: we need to generilize the pattern for future usage.
|
|
363
|
+
*/
|
|
364
|
+
const timestamp = Math.floor(Date.now() / 1000) // in seconds
|
|
365
|
+
|
|
366
|
+
this.puppet.emit('room-leave', {
|
|
367
|
+
removeeIdList : [leaverContactId],
|
|
368
|
+
removerId : removerContactId,
|
|
369
|
+
roomId,
|
|
370
|
+
timestamp,
|
|
371
|
+
})
|
|
372
|
+
|
|
373
|
+
setTimeout(async () => {
|
|
374
|
+
await this.puppet.roomPayloadDirty(roomId)
|
|
375
|
+
await this.puppet.roomPayload(roomId)
|
|
376
|
+
}, 10 * 1000) // reload the room data, especially for memberList
|
|
377
|
+
|
|
378
|
+
return true
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
public async checkRoomTopic (
|
|
382
|
+
rawPayload : WebMessageRawPayload,
|
|
383
|
+
): Promise<boolean> {
|
|
384
|
+
let topic : string
|
|
385
|
+
let changer : string
|
|
386
|
+
|
|
387
|
+
try {
|
|
388
|
+
[topic, changer] = this.parseRoomTopic(rawPayload.Content)
|
|
389
|
+
} catch (e) { // not found
|
|
390
|
+
return false
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const roomId = rawPayload.FromUserName
|
|
394
|
+
|
|
395
|
+
const roomPayload = await this.puppet.roomPayload(roomId)
|
|
396
|
+
const oldTopic = roomPayload.topic
|
|
397
|
+
|
|
398
|
+
let changerContactId: undefined | string
|
|
399
|
+
if (/^(You|你)$/.test(changer)) {
|
|
400
|
+
changerContactId = this.puppet.selfId()
|
|
401
|
+
} else {
|
|
402
|
+
changerContactId = (await this.puppet.roomMemberSearch(roomId, changer))[0]
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
if (!changerContactId) {
|
|
406
|
+
log.error('PuppetWeChatFirer', 'fireRoomTopic() changer contact not found for %s', changer)
|
|
407
|
+
return false
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
try {
|
|
411
|
+
const timestamp = Math.floor(Date.now() / 1000) // in seconds
|
|
412
|
+
|
|
413
|
+
this.puppet.emit('room-topic', {
|
|
414
|
+
changerId : changerContactId,
|
|
415
|
+
newTopic : topic,
|
|
416
|
+
oldTopic,
|
|
417
|
+
roomId,
|
|
418
|
+
timestamp,
|
|
419
|
+
})
|
|
420
|
+
return true
|
|
421
|
+
} catch (e) {
|
|
422
|
+
log.error('PuppetWeChatFirer', 'fireRoomTopic() co exception: %s', e.stack)
|
|
423
|
+
return false
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* try to find FriendRequest Confirmation Message
|
|
429
|
+
*/
|
|
430
|
+
private parseFriendConfirm (
|
|
431
|
+
content: string,
|
|
432
|
+
): boolean {
|
|
433
|
+
const reList = REGEX_CONFIG.friendConfirm
|
|
434
|
+
let found = false
|
|
435
|
+
|
|
436
|
+
reList.some(re => !!(found = re.test(content)))
|
|
437
|
+
if (found) {
|
|
438
|
+
return true
|
|
439
|
+
} else {
|
|
440
|
+
return false
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* try to find 'join' event for Room
|
|
446
|
+
*
|
|
447
|
+
* 1.
|
|
448
|
+
* You invited 管理员 to the group chat.
|
|
449
|
+
* You invited 李卓桓.PreAngel、Bruce LEE to the group chat.
|
|
450
|
+
* 2.
|
|
451
|
+
* 管理员 invited 小桔建群助手 to the group chat
|
|
452
|
+
* 管理员 invited 庆次、小桔妹 to the group chat
|
|
453
|
+
*/
|
|
454
|
+
private parseRoomJoin (
|
|
455
|
+
content: string,
|
|
456
|
+
): [string[], string] {
|
|
457
|
+
log.verbose('PuppetWeChatFirer', 'parseRoomJoin(%s)', content)
|
|
458
|
+
|
|
459
|
+
const reListInvite = REGEX_CONFIG.roomJoinInvite
|
|
460
|
+
const reListQrcode = REGEX_CONFIG.roomJoinQrcode
|
|
461
|
+
|
|
462
|
+
let foundInvite: null | string[] = []
|
|
463
|
+
reListInvite.some(re => !!(foundInvite = content.match(re)))
|
|
464
|
+
let foundQrcode: null | string[] = []
|
|
465
|
+
reListQrcode.some(re => !!(foundQrcode = content.match(re)))
|
|
466
|
+
if ((!foundInvite || !foundInvite.length) && (!foundQrcode || !foundQrcode.length)) {
|
|
467
|
+
throw new Error('parseRoomJoin() not found matched re of ' + content)
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* 管理员 invited 庆次、小桔妹 to the group chat
|
|
471
|
+
* "管理员"通过扫描你分享的二维码加入群聊
|
|
472
|
+
*/
|
|
473
|
+
const [inviter, inviteeStr] = foundInvite ? [foundInvite[1], foundInvite[2]] : [foundQrcode[2], foundQrcode[1]]
|
|
474
|
+
|
|
475
|
+
// FIXME: should also compatible english split
|
|
476
|
+
const inviteeList = inviteeStr.split(/、/)
|
|
477
|
+
|
|
478
|
+
return [inviteeList, inviter] // put invitee at first place
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
private parseRoomLeave (
|
|
482
|
+
content: string,
|
|
483
|
+
): [string, string] {
|
|
484
|
+
let matchIKickOther: null | string[] = []
|
|
485
|
+
REGEX_CONFIG.roomLeaveIKickOther.some(
|
|
486
|
+
regex => !!(
|
|
487
|
+
matchIKickOther = content.match(regex)
|
|
488
|
+
),
|
|
489
|
+
)
|
|
490
|
+
|
|
491
|
+
let matchOtherKickMe: null | string[] = []
|
|
492
|
+
REGEX_CONFIG.roomLeaveOtherKickMe.some(
|
|
493
|
+
re => !!(
|
|
494
|
+
matchOtherKickMe = content.match(re)
|
|
495
|
+
),
|
|
496
|
+
)
|
|
497
|
+
|
|
498
|
+
let leaverName : undefined | string
|
|
499
|
+
let removerName : undefined | string
|
|
500
|
+
|
|
501
|
+
if (matchIKickOther && matchIKickOther.length) {
|
|
502
|
+
leaverName = matchIKickOther[2]
|
|
503
|
+
removerName = matchIKickOther[1]
|
|
504
|
+
} else if (matchOtherKickMe && matchOtherKickMe.length) {
|
|
505
|
+
leaverName = matchOtherKickMe[1]
|
|
506
|
+
removerName = matchOtherKickMe[2]
|
|
507
|
+
} else {
|
|
508
|
+
throw new Error('no match')
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
return [leaverName, removerName]
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
private parseRoomTopic (
|
|
515
|
+
content: string,
|
|
516
|
+
): [string, string] {
|
|
517
|
+
const reList = REGEX_CONFIG.roomTopic
|
|
518
|
+
|
|
519
|
+
let found: null | string[] = []
|
|
520
|
+
reList.some(re => !!(found = content.match(re)))
|
|
521
|
+
if (!found || !found.length) {
|
|
522
|
+
throw new Error('checkRoomTopic() not found')
|
|
523
|
+
}
|
|
524
|
+
const [, changer, topic] = found
|
|
525
|
+
return [topic, changer]
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
export default Firer
|
package/src/mod.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wechaty - https://github.com/chatie/wechaty
|
|
3
|
+
*
|
|
4
|
+
* @copyright 2016-2018 Huan LI <zixia@zixia.net>
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
19
|
+
import { PuppetWeChat } from './puppet-wechat'
|
|
20
|
+
|
|
21
|
+
export {
|
|
22
|
+
VERSION,
|
|
23
|
+
log,
|
|
24
|
+
} from './config'
|
|
25
|
+
|
|
26
|
+
export {
|
|
27
|
+
PuppetWeChat,
|
|
28
|
+
}
|
|
29
|
+
export default PuppetWeChat
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
#!/usr/bin/env ts-node
|
|
2
|
+
/**
|
|
3
|
+
* Wechaty - https://github.com/chatie/wechaty
|
|
4
|
+
*
|
|
5
|
+
* @copyright 2016-2018 Huan LI <zixia@zixia.net>
|
|
6
|
+
*
|
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
* you may not use this file except in compliance with the License.
|
|
9
|
+
* You may obtain a copy of the License at
|
|
10
|
+
*
|
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
*
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
// tslint:disable:no-shadowed-variable
|
|
21
|
+
// tslint:disable:no-var-requires
|
|
22
|
+
// tslint:disable:only-arrow-functions
|
|
23
|
+
// tslint:disable:arrow-parens
|
|
24
|
+
|
|
25
|
+
import { test, sinon } from 'tstest'// const sinonTest = require('sinon-test')(sinon, {
|
|
26
|
+
// useFakeTimers: { // https://github.com/sinonjs/lolex
|
|
27
|
+
// advanceTimeDelta : 10,
|
|
28
|
+
// shouldAdvanceTime : true,
|
|
29
|
+
// },
|
|
30
|
+
// })
|
|
31
|
+
|
|
32
|
+
// import { log } from './config'
|
|
33
|
+
// log.level('silly')
|
|
34
|
+
|
|
35
|
+
import { Bridge } from './bridge'
|
|
36
|
+
import { Event } from './event'
|
|
37
|
+
import { PuppetWeChat } from './puppet-wechat'
|
|
38
|
+
|
|
39
|
+
class PuppetTest extends PuppetWeChat {
|
|
40
|
+
|
|
41
|
+
public contactRawPayload (id: string) {
|
|
42
|
+
return super.contactRawPayload(id)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public roomRawPayload (id: string) {
|
|
46
|
+
return super.roomRawPayload(id)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public messageRawPayload (id: string) {
|
|
50
|
+
return super.messageRawPayload(id)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// test('Puppet smoke testing', async t => {
|
|
56
|
+
// const puppet = new PuppetTest()
|
|
57
|
+
// const wechaty = new WechatyTest({ puppet })
|
|
58
|
+
// wechaty.initPuppetAccessory(puppet)
|
|
59
|
+
|
|
60
|
+
// t.ok(puppet.state.off(), 'should be OFF state after instanciate')
|
|
61
|
+
// puppet.state.on('pending')
|
|
62
|
+
// t.ok(puppet.state.on(), 'should be ON state after set')
|
|
63
|
+
// t.ok(puppet.state.pending(), 'should be pending state after set')
|
|
64
|
+
// })
|
|
65
|
+
|
|
66
|
+
test('login/logout events', async t => {
|
|
67
|
+
const sandbox = sinon.createSandbox()
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
const puppet = new PuppetTest()
|
|
71
|
+
|
|
72
|
+
sandbox.stub(Event, 'onScan') // block the scan event to prevent reset logined user
|
|
73
|
+
|
|
74
|
+
sandbox.stub(Bridge.prototype, 'getUserName').resolves('mockedUserName')
|
|
75
|
+
sandbox.stub(Bridge.prototype, 'contactList')
|
|
76
|
+
.onFirstCall().resolves([])
|
|
77
|
+
.onSecondCall().resolves(['1'])
|
|
78
|
+
.resolves(['1', '2'])
|
|
79
|
+
|
|
80
|
+
sandbox.stub(puppet, 'contactRawPayload').resolves({
|
|
81
|
+
NickName: 'mockedNickName',
|
|
82
|
+
UserName: 'mockedUserName',
|
|
83
|
+
} as any)
|
|
84
|
+
// sandbox.stub(puppet, 'waitStable').resolves()
|
|
85
|
+
|
|
86
|
+
const readySpy = sandbox.spy()
|
|
87
|
+
puppet.on('ready', readySpy)
|
|
88
|
+
|
|
89
|
+
await puppet.start()
|
|
90
|
+
t.pass('should be inited')
|
|
91
|
+
t.is(puppet.logonoff(), false, 'should be not logined')
|
|
92
|
+
|
|
93
|
+
const future = new Promise(resolve => puppet.once('login', resolve))
|
|
94
|
+
.catch(e => t.fail(e))
|
|
95
|
+
puppet.bridge.emit('login', 'TestPuppetWeChat')
|
|
96
|
+
await future
|
|
97
|
+
|
|
98
|
+
t.is(puppet.logonoff(), true, 'should be logined')
|
|
99
|
+
|
|
100
|
+
t.ok((puppet.bridge.getUserName as any).called, 'bridge.getUserName should be called')
|
|
101
|
+
|
|
102
|
+
// FIXME: improve the performance of the test by mocking the time
|
|
103
|
+
// TODO(huan) July 2018: use sinon.clock / sinon.useFakeTimers() at here
|
|
104
|
+
await new Promise(resolve => setTimeout(resolve, 7000))
|
|
105
|
+
|
|
106
|
+
// Puppet will not ready the contact, so the contactRawPayload might not be called at here. Huan, 2018.6
|
|
107
|
+
// t.ok((puppet.contactRawPayload as any).called, 'puppet.contactRawPayload should be called')
|
|
108
|
+
|
|
109
|
+
t.ok((Bridge.prototype.contactList as any).called, 'contactList stub should be called')
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 6 times is:
|
|
113
|
+
*
|
|
114
|
+
* 0, 1, 2 is for first 3 calls for contactList()
|
|
115
|
+
*
|
|
116
|
+
* 3, 4, 5 is PuppetWeChat.waitStable() for `unchangedNum` to reach 3 times.
|
|
117
|
+
*/
|
|
118
|
+
t.is((Bridge.prototype.contactList as any).callCount, 6, 'should call stubContacList 6 times')
|
|
119
|
+
|
|
120
|
+
t.ok(readySpy.called, 'should emit ready event, after login')
|
|
121
|
+
|
|
122
|
+
const logoutPromise = new Promise((resolve) => puppet.once('logout', () => resolve('logoutFired')))
|
|
123
|
+
puppet.bridge.emit('logout')
|
|
124
|
+
t.is(await logoutPromise, 'logoutFired', 'should fire logout event')
|
|
125
|
+
t.is(puppet.logonoff(), false, 'should be logouted')
|
|
126
|
+
|
|
127
|
+
await puppet.stop()
|
|
128
|
+
} catch (e) {
|
|
129
|
+
t.fail(e)
|
|
130
|
+
} finally {
|
|
131
|
+
sandbox.restore()
|
|
132
|
+
}
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* FIXME: increase test times from 1 to 3 Huan(202006)
|
|
137
|
+
*/
|
|
138
|
+
test('restart() 1 times', async t => {
|
|
139
|
+
const puppet = new PuppetWeChat()
|
|
140
|
+
|
|
141
|
+
let n = 1
|
|
142
|
+
|
|
143
|
+
while (n--) {
|
|
144
|
+
await puppet.start()
|
|
145
|
+
await puppet.stop()
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
t.pass('restarted many times')
|
|
149
|
+
})
|