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