@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/bridge.ts
ADDED
|
@@ -0,0 +1,1122 @@
|
|
|
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
|
+
// tslint:disable:arrow-parens
|
|
20
|
+
|
|
21
|
+
import { EventEmitter } from 'events'
|
|
22
|
+
import fs from 'fs'
|
|
23
|
+
import path from 'path'
|
|
24
|
+
import {
|
|
25
|
+
Browser,
|
|
26
|
+
ChromeArgOptions,
|
|
27
|
+
Dialog,
|
|
28
|
+
launch,
|
|
29
|
+
LaunchOptions,
|
|
30
|
+
Page,
|
|
31
|
+
Protocol,
|
|
32
|
+
} from 'puppeteer'
|
|
33
|
+
import puppeteerExtra from 'puppeteer-extra'
|
|
34
|
+
import stealthPlugin from 'puppeteer-extra-plugin-stealth'
|
|
35
|
+
import { StateSwitch } from 'state-switch'
|
|
36
|
+
import { parseString } from 'xml2js'
|
|
37
|
+
|
|
38
|
+
import {
|
|
39
|
+
MemoryCard,
|
|
40
|
+
} from 'memory-card'
|
|
41
|
+
|
|
42
|
+
/* tslint:disable:no-var-requires */
|
|
43
|
+
// const retryPromise = require('retry-promise').default
|
|
44
|
+
|
|
45
|
+
import {
|
|
46
|
+
log,
|
|
47
|
+
MEMORY_SLOT,
|
|
48
|
+
retry,
|
|
49
|
+
} from './config'
|
|
50
|
+
|
|
51
|
+
import {
|
|
52
|
+
WebContactRawPayload,
|
|
53
|
+
WebMessageMediaPayload,
|
|
54
|
+
WebMessageRawPayload,
|
|
55
|
+
WebRoomRawPayload,
|
|
56
|
+
} from './web-schemas'
|
|
57
|
+
|
|
58
|
+
import {
|
|
59
|
+
unescapeHtml,
|
|
60
|
+
} from './pure-function-helpers/mod'
|
|
61
|
+
|
|
62
|
+
export interface InjectResult {
|
|
63
|
+
code: number,
|
|
64
|
+
message: string,
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface BridgeOptions {
|
|
68
|
+
endpoint? : string,
|
|
69
|
+
head? : boolean,
|
|
70
|
+
launchOptions? : LaunchOptions,
|
|
71
|
+
memory : MemoryCard,
|
|
72
|
+
stealthless? : boolean,
|
|
73
|
+
extspam? : string
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export class Bridge extends EventEmitter {
|
|
77
|
+
|
|
78
|
+
private browser : undefined | Browser
|
|
79
|
+
private page : undefined | Page
|
|
80
|
+
private state : StateSwitch
|
|
81
|
+
|
|
82
|
+
constructor (
|
|
83
|
+
public options: BridgeOptions,
|
|
84
|
+
) {
|
|
85
|
+
super()
|
|
86
|
+
log.verbose('PuppetWeChatBridge', 'constructor()')
|
|
87
|
+
|
|
88
|
+
this.state = new StateSwitch('PuppetWeChatBridge', log)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
public async start (): Promise<void> {
|
|
92
|
+
log.verbose('PuppetWeChatBridge', 'start()')
|
|
93
|
+
|
|
94
|
+
this.state.on('pending')
|
|
95
|
+
try {
|
|
96
|
+
this.browser = await this.initBrowser()
|
|
97
|
+
log.verbose('PuppetWeChatBridge', 'start() initBrowser() done')
|
|
98
|
+
|
|
99
|
+
this.on('load', this.onLoad.bind(this))
|
|
100
|
+
|
|
101
|
+
const ready = new Promise(resolve => this.once('ready', resolve))
|
|
102
|
+
this.page = await this.initPage(this.browser)
|
|
103
|
+
await ready
|
|
104
|
+
|
|
105
|
+
this.state.on(true)
|
|
106
|
+
log.verbose('PuppetWeChatBridge', 'start() initPage() done')
|
|
107
|
+
} catch (e) {
|
|
108
|
+
log.error('PuppetWeChatBridge', 'start() exception: %s', e)
|
|
109
|
+
this.state.off(true)
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
if (this.page) {
|
|
113
|
+
await this.page.close()
|
|
114
|
+
}
|
|
115
|
+
if (this.browser) {
|
|
116
|
+
await this.browser.close()
|
|
117
|
+
}
|
|
118
|
+
} catch (e2) {
|
|
119
|
+
log.error('PuppetWeChatBridge', 'start() exception %s, close page/browser exception %s', e, e2)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
this.emit('error', e)
|
|
123
|
+
throw e
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public async initBrowser (): Promise<Browser> {
|
|
128
|
+
log.verbose('PuppetWeChatBridge', 'initBrowser()')
|
|
129
|
+
const launchOptions = { ...this.options.launchOptions } as LaunchOptions & ChromeArgOptions
|
|
130
|
+
const headless = !(this.options.head)
|
|
131
|
+
const launchOptionsArgs = launchOptions.args || []
|
|
132
|
+
if (this.options.endpoint) {
|
|
133
|
+
launchOptions.executablePath = this.options.endpoint
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const options = {
|
|
137
|
+
...launchOptions,
|
|
138
|
+
args: [
|
|
139
|
+
'--audio-output-channels=0',
|
|
140
|
+
'--disable-default-apps',
|
|
141
|
+
'--disable-translate',
|
|
142
|
+
'--disable-gpu',
|
|
143
|
+
'--disable-setuid-sandbox',
|
|
144
|
+
'--disable-sync',
|
|
145
|
+
'--hide-scrollbars',
|
|
146
|
+
'--mute-audio',
|
|
147
|
+
'--no-sandbox',
|
|
148
|
+
...launchOptionsArgs,
|
|
149
|
+
],
|
|
150
|
+
headless,
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
log.verbose('PuppetWeChatBridge', 'initBrowser() with options=%s', JSON.stringify(options))
|
|
154
|
+
|
|
155
|
+
let browser
|
|
156
|
+
|
|
157
|
+
if (!this.options.stealthless) {
|
|
158
|
+
/**
|
|
159
|
+
* Puppeteer 4.0
|
|
160
|
+
* https://github.com/berstend/puppeteer-extra/issues/211#issuecomment-636283110
|
|
161
|
+
*/
|
|
162
|
+
const plugin = stealthPlugin()
|
|
163
|
+
plugin.onBrowser = () => {}
|
|
164
|
+
puppeteerExtra.use(plugin)
|
|
165
|
+
browser = await puppeteerExtra.launch(options)
|
|
166
|
+
} else {
|
|
167
|
+
browser = await launch(options)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const version = await browser.version()
|
|
171
|
+
log.verbose('PuppetWeChatBridge', 'initBrowser() version: %s', version)
|
|
172
|
+
|
|
173
|
+
return browser
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
public async onDialog (dialog: Dialog) {
|
|
177
|
+
log.warn('PuppetWeChatBridge', 'onDialog() page.on(dialog) type:%s message:%s',
|
|
178
|
+
dialog.type, dialog.message())
|
|
179
|
+
try {
|
|
180
|
+
// XXX: Which ONE is better?
|
|
181
|
+
await dialog.accept()
|
|
182
|
+
// await dialog.dismiss()
|
|
183
|
+
} catch (e) {
|
|
184
|
+
log.error('PuppetWeChatBridge', 'onDialog() dialog.dismiss() reject: %s', e)
|
|
185
|
+
}
|
|
186
|
+
this.emit('error', new Error(`${dialog.type}(${dialog.message()})`))
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
public async onLoad (page: Page): Promise<void> {
|
|
190
|
+
log.verbose('PuppetWeChatBridge', 'onLoad() page.url=%s', page.url())
|
|
191
|
+
|
|
192
|
+
if (this.state.off()) {
|
|
193
|
+
log.verbose('PuppetWeChatBridge', 'onLoad() OFF state detected. NOP')
|
|
194
|
+
return // reject(new Error('onLoad() OFF state detected'))
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
try {
|
|
198
|
+
const emitExist = await page.evaluate(() => {
|
|
199
|
+
return typeof window.wechatyPuppetBridgeEmit === 'function'
|
|
200
|
+
})
|
|
201
|
+
if (!emitExist) {
|
|
202
|
+
/**
|
|
203
|
+
* expose window.wechatyPuppetBridgeEmit at here.
|
|
204
|
+
* enable wechaty-bro.js to emit message to bridge
|
|
205
|
+
*/
|
|
206
|
+
await page.exposeFunction('wechatyPuppetBridgeEmit', this.emit.bind(this))
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
await this.readyAngular(page)
|
|
210
|
+
await this.inject(page)
|
|
211
|
+
await this.clickSwitchAccount(page)
|
|
212
|
+
|
|
213
|
+
this.emit('ready')
|
|
214
|
+
|
|
215
|
+
} catch (e) {
|
|
216
|
+
log.error('PuppetWeChatBridge', 'onLoad() exception: %s', e)
|
|
217
|
+
await page.close()
|
|
218
|
+
this.emit('error', e)
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
public async initPage (browser: Browser): Promise<Page> {
|
|
223
|
+
log.verbose('PuppetWeChatBridge', 'initPage()')
|
|
224
|
+
|
|
225
|
+
// set this in time because the following callbacks
|
|
226
|
+
// might be called before initPage() return.
|
|
227
|
+
const page = this.page = await browser.newPage()
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Can we support UOS with puppeteer? #127
|
|
231
|
+
* https://github.com/wechaty/wechaty-puppet-wechat/issues/127
|
|
232
|
+
*/
|
|
233
|
+
await this.uosPatch(page)
|
|
234
|
+
|
|
235
|
+
page.on('error', e => this.emit('error', e))
|
|
236
|
+
page.on('dialog', this.onDialog.bind(this))
|
|
237
|
+
|
|
238
|
+
const cookieList = (
|
|
239
|
+
await this.options.memory.get(MEMORY_SLOT)
|
|
240
|
+
) as Protocol.Network.Cookie[]
|
|
241
|
+
|
|
242
|
+
const url = this.entryUrl(cookieList)
|
|
243
|
+
log.verbose('PuppetWeChatBridge', 'initPage() before page.goto(url)')
|
|
244
|
+
|
|
245
|
+
// Does this related to(?) the CI Error: exception: Navigation Timeout Exceeded: 30000ms exceeded
|
|
246
|
+
await page.goto(url)
|
|
247
|
+
log.verbose('PuppetWeChatBridge', 'initPage() after page.goto(url)')
|
|
248
|
+
|
|
249
|
+
// await this.uosPatch(page)
|
|
250
|
+
void this.uosPatch
|
|
251
|
+
|
|
252
|
+
if (cookieList && cookieList.length) {
|
|
253
|
+
await page.setCookie(...cookieList)
|
|
254
|
+
log.silly('PuppetWeChatBridge', 'initPage() page.setCookie() %s cookies set back', cookieList.length)
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
page.on('load', () => this.emit('load', page))
|
|
258
|
+
await page.reload() // reload page to make effect of the new cookie.
|
|
259
|
+
|
|
260
|
+
return page
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
private async uosPatch (page: Page) {
|
|
264
|
+
/**
|
|
265
|
+
* Can we support UOS with puppeteer? #127
|
|
266
|
+
* https://github.com/wechaty/wechaty-puppet-wechat/issues/127
|
|
267
|
+
*
|
|
268
|
+
* Credit: @luvletter2333 https://github.com/luvletter2333
|
|
269
|
+
*/
|
|
270
|
+
const UOS_PATCH_CLIENT_VERSION = '2.0.0'
|
|
271
|
+
const UOS_PATCH_EXTSPAM = this.options.extspam ?? 'Gp8ICJkIEpkICggwMDAwMDAwMRAGGoAI1GiJSIpeO1RZTq9QBKsRbPJdi84ropi16EYI10WB6g74sGmRwSNXjPQnYUKYotKkvLGpshucCaeWZMOylnc6o2AgDX9grhQQx7fm2DJRTyuNhUlwmEoWhjoG3F0ySAWUsEbH3bJMsEBwoB//0qmFJob74ffdaslqL+IrSy7LJ76/G5TkvNC+J0VQkpH1u3iJJs0uUYyLDzdBIQ6Ogd8LDQ3VKnJLm4g/uDLe+G7zzzkOPzCjXL+70naaQ9medzqmh+/SmaQ6uFWLDQLcRln++wBwoEibNpG4uOJvqXy+ql50DjlNchSuqLmeadFoo9/mDT0q3G7o/80P15ostktjb7h9bfNc+nZVSnUEJXbCjTeqS5UYuxn+HTS5nZsPVxJA2O5GdKCYK4x8lTTKShRstqPfbQpplfllx2fwXcSljuYi3YipPyS3GCAqf5A7aYYwJ7AvGqUiR2SsVQ9Nbp8MGHET1GxhifC692APj6SJxZD3i1drSYZPMMsS9rKAJTGz2FEupohtpf2tgXm6c16nDk/cw+C7K7me5j5PLHv55DFCS84b06AytZPdkFZLj7FHOkcFGJXitHkX5cgww7vuf6F3p0yM/W73SoXTx6GX4G6Hg2rYx3O/9VU2Uq8lvURB4qIbD9XQpzmyiFMaytMnqxcZJcoXCtfkTJ6pI7a92JpRUvdSitg967VUDUAQnCXCM/m0snRkR9LtoXAO1FUGpwlp1EfIdCZFPKNnXMeqev0j9W9ZrkEs9ZWcUEexSj5z+dKYQBhIICviYUQHVqBTZSNy22PlUIeDeIs11j7q4t8rD8LPvzAKWVqXE+5lS1JPZkjg4y5hfX1Dod3t96clFfwsvDP6xBSe1NBcoKbkyGxYK0UvPGtKQEE0Se2zAymYDv41klYE9s+rxp8e94/H8XhrL9oGm8KWb2RmYnAE7ry9gd6e8ZuBRIsISlJAE/e8y8xFmP031S6Lnaet6YXPsFpuFsdQs535IjcFd75hh6DNMBYhSfjv456cvhsb99+fRw/KVZLC3yzNSCbLSyo9d9BI45Plma6V8akURQA/qsaAzU0VyTIqZJkPDTzhuCl92vD2AD/QOhx6iwRSVPAxcRFZcWjgc2wCKh+uCYkTVbNQpB9B90YlNmI3fWTuUOUjwOzQRxJZj11NsimjOJ50qQwTTFj6qQvQ1a/I+MkTx5UO+yNHl718JWcR3AXGmv/aa9rD1eNP8ioTGlOZwPgmr2sor2iBpKTOrB83QgZXP+xRYkb4zVC+LoAXEoIa1+zArywlgREer7DLePukkU6wHTkuSaF+ge5Of1bXuU4i938WJHj0t3D8uQxkJvoFi/EYN/7u2P1zGRLV4dHVUsZMGCCtnO6BBigFMAA='
|
|
272
|
+
|
|
273
|
+
const uosHeaders = {
|
|
274
|
+
'client-version' : UOS_PATCH_CLIENT_VERSION,
|
|
275
|
+
extspam : UOS_PATCH_EXTSPAM,
|
|
276
|
+
}
|
|
277
|
+
// add RequestInterception
|
|
278
|
+
await page.setRequestInterception(true)
|
|
279
|
+
page.on('request', async req => {
|
|
280
|
+
const url = new URL(req.url())
|
|
281
|
+
if (url.pathname === '/' && url.search.indexOf('target=t') === -1) {
|
|
282
|
+
if (url.search === '' || url.search === '?') {
|
|
283
|
+
url.search = '?'
|
|
284
|
+
} else {
|
|
285
|
+
url.search += '&'
|
|
286
|
+
}
|
|
287
|
+
url.search += 'target=t'
|
|
288
|
+
await req.continue({ url })
|
|
289
|
+
return
|
|
290
|
+
}
|
|
291
|
+
if (url.pathname === '/cgi-bin/mmwebwx-bin/webwxnewloginpage') {
|
|
292
|
+
const override = {
|
|
293
|
+
headers: {
|
|
294
|
+
...req.headers(),
|
|
295
|
+
...uosHeaders,
|
|
296
|
+
},
|
|
297
|
+
}
|
|
298
|
+
await req.continue(override)
|
|
299
|
+
return
|
|
300
|
+
}
|
|
301
|
+
await req.continue()
|
|
302
|
+
})
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
public async readyAngular (page: Page): Promise<void> {
|
|
306
|
+
log.verbose('PuppetWeChatBridge', 'readyAngular()')
|
|
307
|
+
|
|
308
|
+
try {
|
|
309
|
+
await page.waitForFunction("typeof window.angular !== 'undefined'")
|
|
310
|
+
} catch (e) {
|
|
311
|
+
log.verbose('PuppetWeChatBridge', 'readyAngular() exception: %s', e)
|
|
312
|
+
|
|
313
|
+
const blockedMessage = await this.testBlockedMessage()
|
|
314
|
+
if (blockedMessage) { // Wechat Account Blocked
|
|
315
|
+
// TODO: advertise for puppet-padchat
|
|
316
|
+
log.info('PuppetWeChatBridge', `
|
|
317
|
+
|
|
318
|
+
Please see: Account Login Issue <https://github.com/wechaty/wechaty/issues/872>
|
|
319
|
+
|
|
320
|
+
`)
|
|
321
|
+
throw new Error(blockedMessage)
|
|
322
|
+
} else {
|
|
323
|
+
throw e
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
public async inject (page: Page): Promise<void> {
|
|
329
|
+
log.verbose('PuppetWeChatBridge', 'inject()')
|
|
330
|
+
|
|
331
|
+
const WECHATY_BRO_JS_FILE = path.join(
|
|
332
|
+
__dirname,
|
|
333
|
+
'wechaty-bro.js',
|
|
334
|
+
)
|
|
335
|
+
|
|
336
|
+
try {
|
|
337
|
+
const sourceCode = fs.readFileSync(WECHATY_BRO_JS_FILE)
|
|
338
|
+
.toString()
|
|
339
|
+
|
|
340
|
+
let retObj = await page.evaluate(sourceCode) as InjectResult
|
|
341
|
+
|
|
342
|
+
if (retObj && /^(2|3)/.test(retObj.code.toString())) {
|
|
343
|
+
// HTTP Code 2XX & 3XX
|
|
344
|
+
log.silly('PuppetWeChatBridge', 'inject() eval(Wechaty) return code[%d] message[%s]',
|
|
345
|
+
retObj.code, retObj.message)
|
|
346
|
+
} else { // HTTP Code 4XX & 5XX
|
|
347
|
+
throw new Error('execute injectio error: ' + retObj.code + ', ' + retObj.message)
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
retObj = await this.proxyWechaty('init')
|
|
351
|
+
if (retObj && /^(2|3)/.test(retObj.code.toString())) {
|
|
352
|
+
// HTTP Code 2XX & 3XX
|
|
353
|
+
log.silly('PuppetWeChatBridge', 'inject() Wechaty.init() return code[%d] message[%s]',
|
|
354
|
+
retObj.code, retObj.message)
|
|
355
|
+
} else { // HTTP Code 4XX & 5XX
|
|
356
|
+
throw new Error('execute proxyWechaty(init) error: ' + retObj.code + ', ' + retObj.message)
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const SUCCESS_CIPHER = 'ding() OK!'
|
|
360
|
+
const future = new Promise(resolve => this.once('dong', resolve))
|
|
361
|
+
this.ding(SUCCESS_CIPHER)
|
|
362
|
+
const r = await future
|
|
363
|
+
if (r !== SUCCESS_CIPHER) {
|
|
364
|
+
throw new Error('fail to get right return from call ding()')
|
|
365
|
+
}
|
|
366
|
+
log.silly('PuppetWeChatBridge', 'inject() ding success')
|
|
367
|
+
|
|
368
|
+
} catch (e) {
|
|
369
|
+
log.verbose('PuppetWeChatBridge', 'inject() exception: %s. stack: %s', e.message, e.stack)
|
|
370
|
+
throw e
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
public async logout (): Promise<any> {
|
|
375
|
+
log.verbose('PuppetWeChatBridge', 'logout()')
|
|
376
|
+
try {
|
|
377
|
+
return await this.proxyWechaty('logout')
|
|
378
|
+
} catch (e) {
|
|
379
|
+
log.error('PuppetWeChatBridge', 'logout() exception: %s', e.message)
|
|
380
|
+
throw e
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
public async stop (): Promise<void> {
|
|
385
|
+
log.verbose('PuppetWeChatBridge', 'stop()')
|
|
386
|
+
|
|
387
|
+
if (!this.page) {
|
|
388
|
+
throw new Error('no page')
|
|
389
|
+
}
|
|
390
|
+
if (!this.browser) {
|
|
391
|
+
throw new Error('no browser')
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
this.state.off('pending')
|
|
395
|
+
|
|
396
|
+
try {
|
|
397
|
+
await this.page.close()
|
|
398
|
+
log.silly('PuppetWeChatBridge', 'stop() page.close()-ed')
|
|
399
|
+
} catch (e) {
|
|
400
|
+
log.warn('PuppetWeChatBridge', 'stop() page.close() exception: %s', e)
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
try {
|
|
404
|
+
await this.browser.close()
|
|
405
|
+
log.silly('PuppetWeChatBridge', 'stop() browser.close()-ed')
|
|
406
|
+
} catch (e) {
|
|
407
|
+
log.warn('PuppetWeChatBridge', 'stop() browser.close() exception: %s', e)
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
this.state.off(true)
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
public async getUserName (): Promise<string> {
|
|
414
|
+
log.verbose('PuppetWeChatBridge', 'getUserName()')
|
|
415
|
+
|
|
416
|
+
try {
|
|
417
|
+
const userName = await this.proxyWechaty('getUserName')
|
|
418
|
+
return userName
|
|
419
|
+
} catch (e) {
|
|
420
|
+
log.error('PuppetWeChatBridge', 'getUserName() exception: %s', e.message)
|
|
421
|
+
throw e
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
public async contactAlias (contactId: string, alias: null | string): Promise<boolean> {
|
|
426
|
+
try {
|
|
427
|
+
return await this.proxyWechaty('contactRemark', contactId, alias)
|
|
428
|
+
} catch (e) {
|
|
429
|
+
log.verbose('PuppetWeChatBridge', 'contactRemark() exception: %s', e.message)
|
|
430
|
+
// Issue #509 return false instead of throw when contact is not a friend.
|
|
431
|
+
// throw e
|
|
432
|
+
log.warn('PuppetWeChatBridge', 'contactRemark() does not work on contact is not a friend')
|
|
433
|
+
return false
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
public async contactList (): Promise<string[]> {
|
|
438
|
+
try {
|
|
439
|
+
return await this.proxyWechaty('contactList')
|
|
440
|
+
} catch (e) {
|
|
441
|
+
log.error('PuppetWeChatBridge', 'contactList() exception: %s', e.message)
|
|
442
|
+
throw e
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
public async roomList (): Promise<string[]> {
|
|
447
|
+
try {
|
|
448
|
+
return await this.proxyWechaty('roomList')
|
|
449
|
+
} catch (e) {
|
|
450
|
+
log.error('PuppetWeChatBridge', 'roomList() exception: %s', e.message)
|
|
451
|
+
throw e
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
public async roomDelMember (
|
|
456
|
+
roomId: string,
|
|
457
|
+
contactId: string,
|
|
458
|
+
): Promise<number> {
|
|
459
|
+
if (!roomId || !contactId) {
|
|
460
|
+
throw new Error('no roomId or contactId')
|
|
461
|
+
}
|
|
462
|
+
try {
|
|
463
|
+
return await this.proxyWechaty('roomDelMember', roomId, contactId)
|
|
464
|
+
} catch (e) {
|
|
465
|
+
log.error('PuppetWeChatBridge', 'roomDelMember(%s, %s) exception: %s', roomId, contactId, e.message)
|
|
466
|
+
throw e
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
public async roomAddMember (
|
|
471
|
+
roomId: string,
|
|
472
|
+
contactId: string,
|
|
473
|
+
): Promise<number> {
|
|
474
|
+
log.verbose('PuppetWeChatBridge', 'roomAddMember(%s, %s)', roomId, contactId)
|
|
475
|
+
|
|
476
|
+
if (!roomId || !contactId) {
|
|
477
|
+
throw new Error('no roomId or contactId')
|
|
478
|
+
}
|
|
479
|
+
try {
|
|
480
|
+
return await this.proxyWechaty('roomAddMember', roomId, contactId)
|
|
481
|
+
} catch (e) {
|
|
482
|
+
log.error('PuppetWeChatBridge', 'roomAddMember(%s, %s) exception: %s', roomId, contactId, e.message)
|
|
483
|
+
throw e
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
public async roomModTopic (
|
|
488
|
+
roomId: string,
|
|
489
|
+
topic: string,
|
|
490
|
+
): Promise<string> {
|
|
491
|
+
if (!roomId) {
|
|
492
|
+
throw new Error('no roomId')
|
|
493
|
+
}
|
|
494
|
+
try {
|
|
495
|
+
await this.proxyWechaty('roomModTopic', roomId, topic)
|
|
496
|
+
return topic
|
|
497
|
+
} catch (e) {
|
|
498
|
+
log.error('PuppetWeChatBridge', 'roomModTopic(%s, %s) exception: %s', roomId, topic, e.message)
|
|
499
|
+
throw e
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
public async roomCreate (contactIdList: string[], topic?: string): Promise<string> {
|
|
504
|
+
if (!contactIdList || !Array.isArray(contactIdList)) {
|
|
505
|
+
throw new Error('no valid contactIdList')
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
try {
|
|
509
|
+
const roomId = await this.proxyWechaty('roomCreate', contactIdList, topic)
|
|
510
|
+
if (typeof roomId === 'object') {
|
|
511
|
+
// It is a Error Object send back by callback in browser(WechatyBro)
|
|
512
|
+
throw roomId
|
|
513
|
+
}
|
|
514
|
+
return roomId
|
|
515
|
+
} catch (e) {
|
|
516
|
+
log.error('PuppetWeChatBridge', 'roomCreate(%s) exception: %s', contactIdList, e.message)
|
|
517
|
+
throw e
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
public async verifyUserRequest (
|
|
522
|
+
contactId: string,
|
|
523
|
+
hello: string,
|
|
524
|
+
): Promise<boolean> {
|
|
525
|
+
log.verbose('PuppetWeChatBridge', 'verifyUserRequest(%s, %s)', contactId, hello)
|
|
526
|
+
|
|
527
|
+
if (!contactId) {
|
|
528
|
+
throw new Error('no valid contactId')
|
|
529
|
+
}
|
|
530
|
+
try {
|
|
531
|
+
return await this.proxyWechaty('verifyUserRequest', contactId, hello)
|
|
532
|
+
} catch (e) {
|
|
533
|
+
log.error('PuppetWeChatBridge', 'verifyUserRequest(%s, %s) exception: %s', contactId, hello, e.message)
|
|
534
|
+
throw e
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
public async verifyUserOk (
|
|
539
|
+
contactId: string,
|
|
540
|
+
ticket: string,
|
|
541
|
+
): Promise<boolean> {
|
|
542
|
+
log.verbose('PuppetWeChatBridge', 'verifyUserOk(%s, %s)', contactId, ticket)
|
|
543
|
+
|
|
544
|
+
if (!contactId || !ticket) {
|
|
545
|
+
throw new Error('no valid contactId or ticket')
|
|
546
|
+
}
|
|
547
|
+
try {
|
|
548
|
+
return await this.proxyWechaty('verifyUserOk', contactId, ticket)
|
|
549
|
+
} catch (e) {
|
|
550
|
+
log.error('PuppetWeChatBridge', 'verifyUserOk(%s, %s) exception: %s', contactId, ticket, e.message)
|
|
551
|
+
throw e
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
public async send (
|
|
556
|
+
toUserName: string,
|
|
557
|
+
text: string,
|
|
558
|
+
): Promise<void> {
|
|
559
|
+
log.verbose('PuppetWeChatBridge', 'send(%s, %s)', toUserName, text)
|
|
560
|
+
|
|
561
|
+
if (!toUserName) {
|
|
562
|
+
throw new Error('UserName not found')
|
|
563
|
+
}
|
|
564
|
+
if (!text) {
|
|
565
|
+
throw new Error('cannot say nothing')
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
try {
|
|
569
|
+
const ret = await this.proxyWechaty('send', toUserName, text)
|
|
570
|
+
if (!ret) {
|
|
571
|
+
throw new Error('send fail')
|
|
572
|
+
}
|
|
573
|
+
} catch (e) {
|
|
574
|
+
log.error('PuppetWeChatBridge', 'send() exception: %s', e.message)
|
|
575
|
+
throw e
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
public async getMsgImg (id: string): Promise<string> {
|
|
580
|
+
log.verbose('PuppetWeChatBridge', 'getMsgImg(%s)', id)
|
|
581
|
+
|
|
582
|
+
try {
|
|
583
|
+
return await this.proxyWechaty('getMsgImg', id)
|
|
584
|
+
} catch (e) {
|
|
585
|
+
log.silly('PuppetWeChatBridge', 'proxyWechaty(getMsgImg, %d) exception: %s', id, e.message)
|
|
586
|
+
throw e
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
public async getMsgEmoticon (id: string): Promise<string> {
|
|
591
|
+
log.verbose('PuppetWeChatBridge', 'getMsgEmoticon(%s)', id)
|
|
592
|
+
|
|
593
|
+
try {
|
|
594
|
+
return await this.proxyWechaty('getMsgEmoticon', id)
|
|
595
|
+
} catch (e) {
|
|
596
|
+
log.silly('PuppetWeChatBridge', 'proxyWechaty(getMsgEmoticon, %d) exception: %s', id, e.message)
|
|
597
|
+
throw e
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
public async getMsgVideo (id: string): Promise<string> {
|
|
602
|
+
log.verbose('PuppetWeChatBridge', 'getMsgVideo(%s)', id)
|
|
603
|
+
|
|
604
|
+
try {
|
|
605
|
+
return await this.proxyWechaty('getMsgVideo', id)
|
|
606
|
+
} catch (e) {
|
|
607
|
+
log.silly('PuppetWeChatBridge', 'proxyWechaty(getMsgVideo, %d) exception: %s', id, e.message)
|
|
608
|
+
throw e
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
public async getMsgVoice (id: string): Promise<string> {
|
|
613
|
+
log.verbose('PuppetWeChatBridge', 'getMsgVoice(%s)', id)
|
|
614
|
+
|
|
615
|
+
try {
|
|
616
|
+
return await this.proxyWechaty('getMsgVoice', id)
|
|
617
|
+
} catch (e) {
|
|
618
|
+
log.silly('PuppetWeChatBridge', 'proxyWechaty(getMsgVoice, %d) exception: %s', id, e.message)
|
|
619
|
+
throw e
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
public async getMsgPublicLinkImg (id: string): Promise<string> {
|
|
624
|
+
log.verbose('PuppetWeChatBridge', 'getMsgPublicLinkImg(%s)', id)
|
|
625
|
+
|
|
626
|
+
try {
|
|
627
|
+
return await this.proxyWechaty('getMsgPublicLinkImg', id)
|
|
628
|
+
} catch (e) {
|
|
629
|
+
log.silly('PuppetWeChatBridge', 'proxyWechaty(getMsgPublicLinkImg, %d) exception: %s', id, e.message)
|
|
630
|
+
throw e
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
public async getMessage (id: string): Promise<WebMessageRawPayload> {
|
|
635
|
+
try {
|
|
636
|
+
return await retry(async (retryException, attempt) => {
|
|
637
|
+
log.silly('PuppetWeChatBridge', 'getMessage(%s) retry attempt %d',
|
|
638
|
+
id,
|
|
639
|
+
attempt,
|
|
640
|
+
)
|
|
641
|
+
try {
|
|
642
|
+
const rawPayload = await this.proxyWechaty('getMessage', id)
|
|
643
|
+
|
|
644
|
+
if (rawPayload && Object.keys(rawPayload).length > 0) {
|
|
645
|
+
return rawPayload
|
|
646
|
+
}
|
|
647
|
+
throw new Error('got empty return value at attempt: ' + attempt)
|
|
648
|
+
} catch (e) {
|
|
649
|
+
log.verbose('PuppetWeChatBridge', 'getMessage() proxyWechaty(getMessage, %s) exception: %s', id, e.message)
|
|
650
|
+
retryException(e)
|
|
651
|
+
}
|
|
652
|
+
})
|
|
653
|
+
|
|
654
|
+
} catch (e) {
|
|
655
|
+
log.error('PuppetWeChatBridge', 'promiseRetry() getContact() finally FAIL: %s', e.message)
|
|
656
|
+
throw e
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
public async getContact (id: string): Promise<WebContactRawPayload | WebRoomRawPayload> {
|
|
661
|
+
try {
|
|
662
|
+
return await retry(async (retryException, attempt) => {
|
|
663
|
+
log.silly('PuppetWeChatBridge', 'getContact(%s) retry attempt %d',
|
|
664
|
+
id,
|
|
665
|
+
attempt,
|
|
666
|
+
)
|
|
667
|
+
try {
|
|
668
|
+
const rawPayload = await this.proxyWechaty('getContact', id)
|
|
669
|
+
|
|
670
|
+
if (rawPayload && Object.keys(rawPayload).length > 0) {
|
|
671
|
+
return rawPayload
|
|
672
|
+
}
|
|
673
|
+
throw new Error('got empty return value at attempt: ' + attempt)
|
|
674
|
+
} catch (e) {
|
|
675
|
+
log.verbose('PuppetWeChatBridge', 'getContact() proxyWechaty(getContact, %s) exception: %s', id, e.message)
|
|
676
|
+
retryException(e)
|
|
677
|
+
}
|
|
678
|
+
})
|
|
679
|
+
|
|
680
|
+
} catch (e) {
|
|
681
|
+
log.error('PuppetWeChatBridge', 'promiseRetry() getContact() finally FAIL: %s', e.message)
|
|
682
|
+
throw e
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
public async getBaseRequest (): Promise<string> {
|
|
687
|
+
log.verbose('PuppetWeChatBridge', 'getBaseRequest()')
|
|
688
|
+
|
|
689
|
+
try {
|
|
690
|
+
return await this.proxyWechaty('getBaseRequest')
|
|
691
|
+
} catch (e) {
|
|
692
|
+
log.silly('PuppetWeChatBridge', 'proxyWechaty(getBaseRequest) exception: %s', e.message)
|
|
693
|
+
throw e
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
public async getPassticket (): Promise<string> {
|
|
698
|
+
log.verbose('PuppetWeChatBridge', 'getPassticket()')
|
|
699
|
+
|
|
700
|
+
try {
|
|
701
|
+
return await this.proxyWechaty('getPassticket')
|
|
702
|
+
} catch (e) {
|
|
703
|
+
log.silly('PuppetWeChatBridge', 'proxyWechaty(getPassticket) exception: %s', e.message)
|
|
704
|
+
throw e
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
public async getCheckUploadUrl (): Promise<string> {
|
|
709
|
+
log.verbose('PuppetWeChatBridge', 'getCheckUploadUrl()')
|
|
710
|
+
|
|
711
|
+
try {
|
|
712
|
+
return await this.proxyWechaty('getCheckUploadUrl')
|
|
713
|
+
} catch (e) {
|
|
714
|
+
log.silly('PuppetWeChatBridge', 'proxyWechaty(getCheckUploadUrl) exception: %s', e.message)
|
|
715
|
+
throw e
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
public async getUploadMediaUrl (): Promise<string> {
|
|
720
|
+
log.verbose('PuppetWeChatBridge', 'getUploadMediaUrl()')
|
|
721
|
+
|
|
722
|
+
try {
|
|
723
|
+
return await this.proxyWechaty('getUploadMediaUrl')
|
|
724
|
+
} catch (e) {
|
|
725
|
+
log.silly('PuppetWeChatBridge', 'proxyWechaty(getUploadMediaUrl) exception: %s', e.message)
|
|
726
|
+
throw e
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
public async sendMedia (mediaData: WebMessageMediaPayload): Promise<boolean> {
|
|
731
|
+
log.verbose('PuppetWeChatBridge', 'sendMedia(mediaData)')
|
|
732
|
+
|
|
733
|
+
if (!mediaData.ToUserName) {
|
|
734
|
+
throw new Error('UserName not found')
|
|
735
|
+
}
|
|
736
|
+
if (!mediaData.MediaId) {
|
|
737
|
+
throw new Error('cannot say nothing')
|
|
738
|
+
}
|
|
739
|
+
try {
|
|
740
|
+
return await this.proxyWechaty('sendMedia', mediaData)
|
|
741
|
+
} catch (e) {
|
|
742
|
+
log.error('PuppetWeChatBridge', 'sendMedia() exception: %s', e.message)
|
|
743
|
+
throw e
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
public async forward (baseData: WebMessageRawPayload, patchData: WebMessageRawPayload): Promise<boolean> {
|
|
748
|
+
log.verbose('PuppetWeChatBridge', 'forward()')
|
|
749
|
+
|
|
750
|
+
if (!baseData.ToUserName) {
|
|
751
|
+
throw new Error('UserName not found')
|
|
752
|
+
}
|
|
753
|
+
if (!patchData.MMActualContent && !patchData.MMSendContent && !patchData.Content) {
|
|
754
|
+
throw new Error('cannot say nothing')
|
|
755
|
+
}
|
|
756
|
+
try {
|
|
757
|
+
return await this.proxyWechaty('forward', baseData, patchData)
|
|
758
|
+
} catch (e) {
|
|
759
|
+
log.error('PuppetWeChatBridge', 'forward() exception: %s', e.message)
|
|
760
|
+
throw e
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/**
|
|
765
|
+
* Proxy Call to Wechaty in Bridge
|
|
766
|
+
*/
|
|
767
|
+
public async proxyWechaty (
|
|
768
|
+
wechatyFunc : string,
|
|
769
|
+
...args : any[]
|
|
770
|
+
): Promise<any> {
|
|
771
|
+
log.silly('PuppetWeChatBridge', 'proxyWechaty(%s%s)',
|
|
772
|
+
wechatyFunc,
|
|
773
|
+
args.length === 0
|
|
774
|
+
? ''
|
|
775
|
+
: ', ' + args.join(', '),
|
|
776
|
+
)
|
|
777
|
+
|
|
778
|
+
if (!this.page) {
|
|
779
|
+
throw new Error('no page')
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
try {
|
|
783
|
+
const noWechaty = await this.page.evaluate(() => {
|
|
784
|
+
return typeof WechatyBro === 'undefined'
|
|
785
|
+
})
|
|
786
|
+
if (noWechaty) {
|
|
787
|
+
const e = new Error('there is no WechatyBro in browser(yet)')
|
|
788
|
+
throw e
|
|
789
|
+
}
|
|
790
|
+
} catch (e) {
|
|
791
|
+
log.warn('PuppetWeChatBridge', 'proxyWechaty() noWechaty exception: %s', e)
|
|
792
|
+
throw e
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
const argsEncoded = Buffer.from(
|
|
796
|
+
encodeURIComponent(
|
|
797
|
+
JSON.stringify(args),
|
|
798
|
+
),
|
|
799
|
+
).toString('base64')
|
|
800
|
+
// see: http://blog.sqrtthree.com/2015/08/29/utf8-to-b64/
|
|
801
|
+
const argsDecoded = `JSON.parse(decodeURIComponent(window.atob('${argsEncoded}')))`
|
|
802
|
+
|
|
803
|
+
const wechatyScript = `
|
|
804
|
+
WechatyBro
|
|
805
|
+
.${wechatyFunc}
|
|
806
|
+
.apply(
|
|
807
|
+
undefined,
|
|
808
|
+
${argsDecoded},
|
|
809
|
+
)
|
|
810
|
+
`.replace(/[\n\s]+/, ' ')
|
|
811
|
+
// log.silly('PuppetWeChatBridge', 'proxyWechaty(%s, ...args) %s', wechatyFunc, wechatyScript)
|
|
812
|
+
// console.log('proxyWechaty wechatyFunc args[0]: ')
|
|
813
|
+
// console.log(args[0])
|
|
814
|
+
|
|
815
|
+
try {
|
|
816
|
+
const ret = await this.page.evaluate(wechatyScript)
|
|
817
|
+
return ret
|
|
818
|
+
} catch (e) {
|
|
819
|
+
log.verbose('PuppetWeChatBridge', 'proxyWechaty(%s, %s) ', wechatyFunc, args.join(', '))
|
|
820
|
+
log.warn('PuppetWeChatBridge', 'proxyWechaty() exception: %s', e.message)
|
|
821
|
+
throw e
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
public ding (data: any): void {
|
|
826
|
+
log.verbose('PuppetWeChatBridge', 'ding(%s)', data || '')
|
|
827
|
+
|
|
828
|
+
this.proxyWechaty('ding', data)
|
|
829
|
+
.then(dongData => {
|
|
830
|
+
return this.emit('dong', dongData)
|
|
831
|
+
})
|
|
832
|
+
.catch(e => {
|
|
833
|
+
log.error('PuppetWeChatBridge', 'ding(%s) exception: %s', data, e.message)
|
|
834
|
+
this.emit('error', e)
|
|
835
|
+
})
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
public preHtmlToXml (text: string): string {
|
|
839
|
+
log.verbose('PuppetWeChatBridge', 'preHtmlToXml()')
|
|
840
|
+
|
|
841
|
+
const preRegex = /^<pre[^>]*>([^<]+)<\/pre>$/i
|
|
842
|
+
const matches = text.match(preRegex)
|
|
843
|
+
if (!matches) {
|
|
844
|
+
return text
|
|
845
|
+
}
|
|
846
|
+
return unescapeHtml(matches[1])
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
public async innerHTML (): Promise<string> {
|
|
850
|
+
const html = await this.evaluate(() => {
|
|
851
|
+
return window.document.body.innerHTML
|
|
852
|
+
})
|
|
853
|
+
return html
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
/**
|
|
857
|
+
* Throw if there's a blocked message
|
|
858
|
+
*/
|
|
859
|
+
public async testBlockedMessage (text?: string): Promise<string | false> {
|
|
860
|
+
if (!text) {
|
|
861
|
+
text = await this.innerHTML()
|
|
862
|
+
}
|
|
863
|
+
if (!text) {
|
|
864
|
+
throw new Error('testBlockedMessage() no text found!')
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
const textSnip = text.substr(0, 50).replace(/\n/, '')
|
|
868
|
+
log.verbose('PuppetWeChatBridge', 'testBlockedMessage(%s)',
|
|
869
|
+
textSnip)
|
|
870
|
+
|
|
871
|
+
interface BlockedMessage {
|
|
872
|
+
error?: {
|
|
873
|
+
ret : number,
|
|
874
|
+
message : string,
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
let obj: BlockedMessage
|
|
878
|
+
|
|
879
|
+
try {
|
|
880
|
+
// see unit test for detail
|
|
881
|
+
const tryXmlText = this.preHtmlToXml(text)
|
|
882
|
+
// obj = JSON.parse(toJson(tryXmlText))
|
|
883
|
+
obj = await new Promise((resolve, reject) => {
|
|
884
|
+
parseString(tryXmlText, { explicitArray: false }, (err, result) => {
|
|
885
|
+
if (err) {
|
|
886
|
+
return reject(err)
|
|
887
|
+
}
|
|
888
|
+
return resolve(result)
|
|
889
|
+
})
|
|
890
|
+
})
|
|
891
|
+
} catch (e) {
|
|
892
|
+
log.warn('PuppetWeChatBridge', 'testBlockedMessage() toJson() exception: %s', e)
|
|
893
|
+
return false
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
if (!obj) {
|
|
897
|
+
// FIXME: when will this happen?
|
|
898
|
+
log.warn('PuppetWeChatBridge', 'testBlockedMessage() toJson(%s) return empty obj', textSnip)
|
|
899
|
+
return false
|
|
900
|
+
}
|
|
901
|
+
if (!obj.error) {
|
|
902
|
+
return false
|
|
903
|
+
}
|
|
904
|
+
const ret = +obj.error.ret
|
|
905
|
+
const message = obj.error.message
|
|
906
|
+
|
|
907
|
+
log.warn('PuppetWeChatBridge', 'testBlockedMessage() error.ret=%s', ret)
|
|
908
|
+
|
|
909
|
+
if (ret === 1203) {
|
|
910
|
+
// <error>
|
|
911
|
+
// <ret>1203</ret>
|
|
912
|
+
// <message>当前登录环境异常。为了你的帐号安全,暂时不能登录web微信。你可以通过手机客户端或者windows微信登录。</message>
|
|
913
|
+
// </error>
|
|
914
|
+
return message
|
|
915
|
+
}
|
|
916
|
+
return message // other error message
|
|
917
|
+
|
|
918
|
+
// return new Promise<string | false>(resolve => {
|
|
919
|
+
// parseString(tryXmlText, { explicitArray: false }, (err, obj: BlockedMessage) => {
|
|
920
|
+
// if (err) { // HTML can not be parsed to JSON
|
|
921
|
+
// return resolve(false)
|
|
922
|
+
// }
|
|
923
|
+
// if (!obj) {
|
|
924
|
+
// // FIXME: when will this happen?
|
|
925
|
+
// log.warn('PuppetWeChatBridge', 'testBlockedMessage() parseString(%s) return empty obj', textSnip)
|
|
926
|
+
// return resolve(false)
|
|
927
|
+
// }
|
|
928
|
+
// if (!obj.error) {
|
|
929
|
+
// return resolve(false)
|
|
930
|
+
// }
|
|
931
|
+
// const ret = +obj.error.ret
|
|
932
|
+
// const message = obj.error.message
|
|
933
|
+
|
|
934
|
+
// log.warn('PuppetWeChatBridge', 'testBlockedMessage() error.ret=%s', ret)
|
|
935
|
+
|
|
936
|
+
// if (ret === 1203) {
|
|
937
|
+
// // <error>
|
|
938
|
+
// // <ret>1203</ret>
|
|
939
|
+
// // <message>当前登录环境异常。为了你的帐号安全,暂时不能登录web微信。你可以通过手机客户端或者windows微信登录。</message>
|
|
940
|
+
// // </error>
|
|
941
|
+
// return resolve(message)
|
|
942
|
+
// }
|
|
943
|
+
// return resolve(message) // other error message
|
|
944
|
+
// })
|
|
945
|
+
// })
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
public async clickSwitchAccount (page: Page): Promise<boolean> {
|
|
949
|
+
log.verbose('PuppetWeChatBridge', 'clickSwitchAccount()')
|
|
950
|
+
|
|
951
|
+
// https://github.com/GoogleChrome/puppeteer/issues/537#issuecomment-334918553
|
|
952
|
+
// async function listXpath(thePage: Page, xpath: string): Promise<ElementHandle[]> {
|
|
953
|
+
// log.verbose('PuppetWeChatBridge', 'clickSwitchAccount() listXpath()')
|
|
954
|
+
|
|
955
|
+
// try {
|
|
956
|
+
// const nodeHandleList = await (thePage as any).evaluateHandle(xpathInner => {
|
|
957
|
+
// const nodeList: Node[] = []
|
|
958
|
+
// const query = document.evaluate(xpathInner, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
|
|
959
|
+
// for (let i = 0, length = query.snapshotLength; i < length; ++i) {
|
|
960
|
+
// nodeList.push(query.snapshotItem(i))
|
|
961
|
+
// }
|
|
962
|
+
// return nodeList
|
|
963
|
+
// }, xpath)
|
|
964
|
+
// const properties = await nodeHandleList.getProperties()
|
|
965
|
+
|
|
966
|
+
// const elementHandleList: ElementHandle[] = []
|
|
967
|
+
// const releasePromises: Promise<void>[] = []
|
|
968
|
+
|
|
969
|
+
// for (const property of properties.values()) {
|
|
970
|
+
// const element = property.asElement()
|
|
971
|
+
// if (element)
|
|
972
|
+
// elementHandleList.push(element)
|
|
973
|
+
// else
|
|
974
|
+
// releasePromises.push(property.dispose())
|
|
975
|
+
// }
|
|
976
|
+
// await Promise.all(releasePromises)
|
|
977
|
+
// return elementHandleList
|
|
978
|
+
// } catch (e) {
|
|
979
|
+
// log.verbose('PuppetWeChatBridge', 'clickSwitchAccount() listXpath() exception: %s', e)
|
|
980
|
+
// return []
|
|
981
|
+
// }
|
|
982
|
+
// }
|
|
983
|
+
|
|
984
|
+
// TODO: use page.$x() (with puppeteer v1.1 or above) to replace DIY version of listXpath() instead.
|
|
985
|
+
// See: https://github.com/GoogleChrome/puppeteer/blob/v1.1.0/docs/api.md#pagexexpression
|
|
986
|
+
|
|
987
|
+
const XPATH_SELECTOR
|
|
988
|
+
= "//div[contains(@class,'association') and contains(@class,'show')]/a[@ng-click='qrcodeLogin()']"
|
|
989
|
+
|
|
990
|
+
try {
|
|
991
|
+
// const [button] = await listXpath(page, XPATH_SELECTOR)
|
|
992
|
+
const [button] = await page.$x(XPATH_SELECTOR)
|
|
993
|
+
if (button) {
|
|
994
|
+
await button.click()
|
|
995
|
+
log.silly('PuppetWeChatBridge', 'clickSwitchAccount() clicked!')
|
|
996
|
+
return true
|
|
997
|
+
|
|
998
|
+
} else {
|
|
999
|
+
log.silly('PuppetWeChatBridge', 'clickSwitchAccount() button not found')
|
|
1000
|
+
return false
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
} catch (e) {
|
|
1004
|
+
log.silly('PuppetWeChatBridge', 'clickSwitchAccount() exception: %s', e)
|
|
1005
|
+
throw e
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
public async hostname (): Promise<string | null> {
|
|
1010
|
+
log.verbose('PuppetWeChatBridge', 'hostname()')
|
|
1011
|
+
|
|
1012
|
+
if (!this.page) {
|
|
1013
|
+
throw new Error('no page')
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
try {
|
|
1017
|
+
const hostname = await this.page.evaluate(() => window.location.hostname)
|
|
1018
|
+
log.silly('PuppetWeChatBridge', 'hostname() got %s', hostname)
|
|
1019
|
+
return hostname
|
|
1020
|
+
} catch (e) {
|
|
1021
|
+
log.error('PuppetWeChatBridge', 'hostname() exception: %s', e)
|
|
1022
|
+
this.emit('error', e)
|
|
1023
|
+
return null
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
public async cookies (cookieList: Cookie[]): Promise<void>
|
|
1028
|
+
public async cookies (): Promise<Cookie[]>
|
|
1029
|
+
|
|
1030
|
+
public async cookies (cookieList?: Protocol.Network.Cookie[]): Promise<void | Protocol.Network.Cookie[]> {
|
|
1031
|
+
if (!this.page) {
|
|
1032
|
+
throw new Error('no page')
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
if (cookieList) {
|
|
1036
|
+
try {
|
|
1037
|
+
await this.page.setCookie(...cookieList)
|
|
1038
|
+
} catch (e) {
|
|
1039
|
+
log.error('PuppetWeChatBridge', 'cookies(%s) reject: %s', cookieList, e)
|
|
1040
|
+
this.emit('error', e)
|
|
1041
|
+
}
|
|
1042
|
+
// RETURN
|
|
1043
|
+
} else {
|
|
1044
|
+
cookieList = await this.page.cookies()
|
|
1045
|
+
return cookieList
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
/**
|
|
1050
|
+
* name
|
|
1051
|
+
*/
|
|
1052
|
+
public entryUrl (cookieList?: Protocol.Network.Cookie[]): string {
|
|
1053
|
+
log.verbose('PuppetWeChatBridge', 'cookieDomain(%s)', cookieList)
|
|
1054
|
+
|
|
1055
|
+
/**
|
|
1056
|
+
* `?target=t` is from https://github.com/wechaty/wechaty-puppet-wechat/pull/129
|
|
1057
|
+
*/
|
|
1058
|
+
const DEFAULT_URL = 'https://wx.qq.com?target=t'
|
|
1059
|
+
|
|
1060
|
+
if (!cookieList || cookieList.length === 0) {
|
|
1061
|
+
log.silly('PuppetWeChatBridge', 'cookieDomain() no cookie, return default %s', DEFAULT_URL)
|
|
1062
|
+
return DEFAULT_URL
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
const wxCookieList = cookieList.filter(c => /^webwx_auth_ticket|webwxuvid$/.test(c.name))
|
|
1066
|
+
if (!wxCookieList.length) {
|
|
1067
|
+
log.silly('PuppetWeChatBridge', 'cookieDomain() no valid cookie, return default hostname')
|
|
1068
|
+
return DEFAULT_URL
|
|
1069
|
+
}
|
|
1070
|
+
let domain = wxCookieList[0].domain
|
|
1071
|
+
if (!domain) {
|
|
1072
|
+
log.silly('PuppetWeChatBridge', 'cookieDomain() no valid domain in cookies, return default hostname')
|
|
1073
|
+
return DEFAULT_URL
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
domain = domain.slice(1)
|
|
1077
|
+
if (domain === 'wechat.com') {
|
|
1078
|
+
domain = 'web.wechat.com'
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
let url
|
|
1082
|
+
if (/^http/.test(domain)) {
|
|
1083
|
+
url = domain
|
|
1084
|
+
} else {
|
|
1085
|
+
// Protocol error (Page.navigate): Cannot navigate to invalid URL undefined
|
|
1086
|
+
url = `https://${domain}`
|
|
1087
|
+
}
|
|
1088
|
+
log.silly('PuppetWeChatBridge', 'cookieDomain() got %s', url)
|
|
1089
|
+
|
|
1090
|
+
return url + '?target=t'
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
public async reload (): Promise<void> {
|
|
1094
|
+
log.verbose('PuppetWeChatBridge', 'reload()')
|
|
1095
|
+
|
|
1096
|
+
if (!this.page) {
|
|
1097
|
+
throw new Error('no page')
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
await this.page.reload()
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
public async evaluate (fn: () => any, ...args: any[]): Promise<any> {
|
|
1104
|
+
log.silly('PuppetWeChatBridge', 'evaluate()')
|
|
1105
|
+
|
|
1106
|
+
if (!this.page) {
|
|
1107
|
+
throw new Error('no page')
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
try {
|
|
1111
|
+
return await this.page.evaluate(fn, ...args)
|
|
1112
|
+
} catch (e) {
|
|
1113
|
+
log.error('PuppetWeChatBridge', 'evaluate() exception: %s', e)
|
|
1114
|
+
this.emit('error', e)
|
|
1115
|
+
return null
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
export type Cookie = Protocol.Network.Cookie
|
|
1122
|
+
export default Bridge
|