@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,1287 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
21
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
22
|
+
};
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.PuppetWeChat = void 0;
|
|
25
|
+
// tslint:disable:member-ordering
|
|
26
|
+
// tslint:disable:arrow-parens
|
|
27
|
+
// tslint:disable:object-literal-key-quotes
|
|
28
|
+
const path_1 = __importDefault(require("path"));
|
|
29
|
+
const url_1 = __importDefault(require("url"));
|
|
30
|
+
const bl_1 = __importDefault(require("bl"));
|
|
31
|
+
const md5_1 = __importDefault(require("md5"));
|
|
32
|
+
const mime_1 = __importDefault(require("mime"));
|
|
33
|
+
const request_1 = __importDefault(require("request"));
|
|
34
|
+
const rx_queue_1 = require("rx-queue");
|
|
35
|
+
const watchdog_1 = require("watchdog");
|
|
36
|
+
const wechaty_puppet_1 = require("wechaty-puppet");
|
|
37
|
+
const config_1 = require("./config");
|
|
38
|
+
const mod_1 = require("./pure-function-helpers/mod");
|
|
39
|
+
const bridge_1 = require("./bridge");
|
|
40
|
+
const event_1 = require("./event");
|
|
41
|
+
const web_schemas_1 = require("./web-schemas");
|
|
42
|
+
class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
43
|
+
constructor(options = {}) {
|
|
44
|
+
super(options);
|
|
45
|
+
this.options = options;
|
|
46
|
+
this.fileId = 0;
|
|
47
|
+
this.bridge = new bridge_1.Bridge({
|
|
48
|
+
endpoint: options.endpoint || process.env.WECHATY_PUPPET_PUPPETEER_ENDPOINT,
|
|
49
|
+
extspam: options.token || process.env.WECHATY_PUPPET_WECHAT_TOKEN,
|
|
50
|
+
head: typeof options.head === 'boolean' ? options.head : (0, config_1.envHead)(),
|
|
51
|
+
launchOptions: options.launchOptions,
|
|
52
|
+
memory: this.memory,
|
|
53
|
+
stealthless: typeof options.stealthless === 'boolean' ? options.stealthless : (0, config_1.envStealthless)(),
|
|
54
|
+
});
|
|
55
|
+
const SCAN_TIMEOUT = 2 * 60 * 1000; // 2 minutes
|
|
56
|
+
this.scanWatchdog = new watchdog_1.Watchdog(SCAN_TIMEOUT, 'Scan');
|
|
57
|
+
this.initWatchdogForScan();
|
|
58
|
+
}
|
|
59
|
+
async start() {
|
|
60
|
+
config_1.log.verbose('PuppetWeChat', `start() with ${this.memory.name}`);
|
|
61
|
+
if (this.state.on()) {
|
|
62
|
+
config_1.log.warn('PuppetWeChat', 'start() is called on a ON puppet. await ready(on) and return.');
|
|
63
|
+
await this.state.ready('on');
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
this.state.on('pending');
|
|
67
|
+
try {
|
|
68
|
+
/**
|
|
69
|
+
* Overwrite the memory in bridge
|
|
70
|
+
* because it could be changed between constructor() and start()
|
|
71
|
+
*/
|
|
72
|
+
this.bridge.options.memory = this.memory;
|
|
73
|
+
// this.initWatchdog()
|
|
74
|
+
// this.initWatchdogForScan()
|
|
75
|
+
this.bridge = await this.initBridge();
|
|
76
|
+
config_1.log.verbose('PuppetWeChat', 'initBridge() done');
|
|
77
|
+
/**
|
|
78
|
+
* state must set to `live`
|
|
79
|
+
* before feed Watchdog
|
|
80
|
+
*/
|
|
81
|
+
this.state.on(true);
|
|
82
|
+
/**
|
|
83
|
+
* Feed the dog and start watch
|
|
84
|
+
*/
|
|
85
|
+
const food = {
|
|
86
|
+
data: 'inited',
|
|
87
|
+
timeout: 2 * 60 * 1000, // 2 mins for first login
|
|
88
|
+
};
|
|
89
|
+
this.emit('heartbeat', food);
|
|
90
|
+
/**
|
|
91
|
+
* Save cookie for every 5 minutes
|
|
92
|
+
*/
|
|
93
|
+
const throttleQueue = new rx_queue_1.ThrottleQueue(5 * 60 * 1000);
|
|
94
|
+
this.on('heartbeat', data => throttleQueue.next(data));
|
|
95
|
+
throttleQueue.subscribe(async (data) => {
|
|
96
|
+
config_1.log.verbose('PuppetWeChat', 'start() throttleQueue.subscribe() new item: %s', data);
|
|
97
|
+
await this.saveCookie();
|
|
98
|
+
});
|
|
99
|
+
config_1.log.verbose('PuppetWeChat', 'start() done');
|
|
100
|
+
// this.emit('start')
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
catch (e) {
|
|
104
|
+
config_1.log.error('PuppetWeChat', 'start() exception: %s', e);
|
|
105
|
+
// this.state.off(true)
|
|
106
|
+
this.emit('error', e);
|
|
107
|
+
await this.stop();
|
|
108
|
+
throw e;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async stop() {
|
|
112
|
+
config_1.log.verbose('PuppetWeChat', 'stop()');
|
|
113
|
+
if (this.state.off()) {
|
|
114
|
+
config_1.log.warn('PuppetWeChat', 'stop() is called on a OFF puppet. await ready(off) and return.');
|
|
115
|
+
await this.state.ready('off');
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
this.state.off('pending');
|
|
119
|
+
config_1.log.verbose('PuppetWeChat', 'stop() make watchdog sleep before do stop');
|
|
120
|
+
/**
|
|
121
|
+
* Clean listeners for `watchdog`
|
|
122
|
+
*/
|
|
123
|
+
// this.watchdog.sleep()
|
|
124
|
+
this.scanWatchdog.sleep();
|
|
125
|
+
// this.watchdog.removeAllListeners()
|
|
126
|
+
this.scanWatchdog.removeAllListeners();
|
|
127
|
+
this.removeAllListeners('watchdog');
|
|
128
|
+
try {
|
|
129
|
+
await this.bridge.stop();
|
|
130
|
+
// register the removeListeners micro task at then end of the task queue
|
|
131
|
+
setImmediate(() => this.bridge.removeAllListeners());
|
|
132
|
+
}
|
|
133
|
+
catch (e) {
|
|
134
|
+
config_1.log.error('PuppetWeChat', 'this.bridge.quit() exception: %s', e.message);
|
|
135
|
+
throw e;
|
|
136
|
+
}
|
|
137
|
+
finally {
|
|
138
|
+
this.state.off(true);
|
|
139
|
+
// this.emit('stop')
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Deal with SCAN events
|
|
144
|
+
*
|
|
145
|
+
* if web browser stay at login qrcode page long time,
|
|
146
|
+
* sometimes the qrcode will not refresh, leave there expired.
|
|
147
|
+
* so we need to refresh the page after a while
|
|
148
|
+
*/
|
|
149
|
+
initWatchdogForScan() {
|
|
150
|
+
config_1.log.verbose('PuppetWeChat', 'initWatchdogForScan()');
|
|
151
|
+
const puppet = this;
|
|
152
|
+
const dog = this.scanWatchdog;
|
|
153
|
+
// clean the dog because this could be re-inited
|
|
154
|
+
// dog.removeAllListeners()
|
|
155
|
+
puppet.on('scan', info => dog.feed({
|
|
156
|
+
data: info,
|
|
157
|
+
type: 'scan',
|
|
158
|
+
}));
|
|
159
|
+
puppet.on('login', ( /* user */) => {
|
|
160
|
+
// dog.feed({
|
|
161
|
+
// data: user,
|
|
162
|
+
// type: 'login',
|
|
163
|
+
// })
|
|
164
|
+
// do not monitor `scan` event anymore
|
|
165
|
+
// after user login
|
|
166
|
+
dog.sleep();
|
|
167
|
+
});
|
|
168
|
+
// active monitor again for `scan` event
|
|
169
|
+
puppet.on('logout', user => dog.feed({
|
|
170
|
+
data: user,
|
|
171
|
+
type: 'logout',
|
|
172
|
+
}));
|
|
173
|
+
dog.on('reset', async (food, timePast) => {
|
|
174
|
+
config_1.log.warn('PuppetWeChat', 'initScanWatchdog() on(reset) lastFood: %s, timePast: %s', food.data, timePast);
|
|
175
|
+
try {
|
|
176
|
+
await this.bridge.reload();
|
|
177
|
+
}
|
|
178
|
+
catch (e) {
|
|
179
|
+
config_1.log.error('PuppetWeChat', 'initScanWatchdog() on(reset) exception: %s', e);
|
|
180
|
+
try {
|
|
181
|
+
config_1.log.error('PuppetWeChat', 'initScanWatchdog() on(reset) try to recover by bridge.{quit,init}()', e);
|
|
182
|
+
await this.bridge.stop();
|
|
183
|
+
await this.bridge.start();
|
|
184
|
+
config_1.log.error('PuppetWeChat', 'initScanWatchdog() on(reset) recover successful');
|
|
185
|
+
}
|
|
186
|
+
catch (e) {
|
|
187
|
+
config_1.log.error('PuppetWeChat', 'initScanWatchdog() on(reset) recover FAIL: %s', e);
|
|
188
|
+
this.emit('error', e);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
async initBridge() {
|
|
194
|
+
config_1.log.verbose('PuppetWeChat', 'initBridge()');
|
|
195
|
+
if (this.state.off()) {
|
|
196
|
+
const e = new Error('initBridge() found targetState != live, no init anymore');
|
|
197
|
+
config_1.log.warn('PuppetWeChat', e.message);
|
|
198
|
+
throw e;
|
|
199
|
+
}
|
|
200
|
+
this.bridge.on('dong', (data) => this.emit('dong', { data }));
|
|
201
|
+
// this.bridge.on('ding' , Event.onDing.bind(this))
|
|
202
|
+
this.bridge.on('heartbeat', (data) => this.emit('heartbeat', { data: data + 'bridge ding' }));
|
|
203
|
+
this.bridge.on('error', (e) => this.emit('error', { data: (e && e.message) || String(e) }));
|
|
204
|
+
this.bridge.on('log', event_1.Event.onLog.bind(this));
|
|
205
|
+
this.bridge.on('login', event_1.Event.onLogin.bind(this));
|
|
206
|
+
this.bridge.on('logout', event_1.Event.onLogout.bind(this));
|
|
207
|
+
this.bridge.on('message', event_1.Event.onMessage.bind(this));
|
|
208
|
+
this.bridge.on('scan', event_1.Event.onScan.bind(this));
|
|
209
|
+
this.bridge.on('unload', event_1.Event.onUnload.bind(this));
|
|
210
|
+
try {
|
|
211
|
+
await this.bridge.start();
|
|
212
|
+
}
|
|
213
|
+
catch (e) {
|
|
214
|
+
config_1.log.error('PuppetWeChat', 'initBridge() exception: %s', e.message);
|
|
215
|
+
await this.bridge.stop().catch(e => {
|
|
216
|
+
config_1.log.error('PuppetWeChat', 'initBridge() this.bridge.stop() rejection: %s', e);
|
|
217
|
+
});
|
|
218
|
+
this.emit('error', e);
|
|
219
|
+
throw e;
|
|
220
|
+
}
|
|
221
|
+
return this.bridge;
|
|
222
|
+
}
|
|
223
|
+
async getBaseRequest() {
|
|
224
|
+
try {
|
|
225
|
+
const json = await this.bridge.getBaseRequest();
|
|
226
|
+
const obj = JSON.parse(json);
|
|
227
|
+
return obj.BaseRequest;
|
|
228
|
+
}
|
|
229
|
+
catch (e) {
|
|
230
|
+
config_1.log.error('PuppetWeChat', 'send() exception: %s', e.message);
|
|
231
|
+
throw e;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
unref() {
|
|
235
|
+
config_1.log.verbose('PuppetWeChat', 'unref ()');
|
|
236
|
+
super.unref();
|
|
237
|
+
if (this.scanWatchdog) {
|
|
238
|
+
this.scanWatchdog.unref();
|
|
239
|
+
}
|
|
240
|
+
// TODO: unref() the puppeteer
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
*
|
|
244
|
+
* Message
|
|
245
|
+
*
|
|
246
|
+
*/
|
|
247
|
+
async messageRawPayload(id) {
|
|
248
|
+
const rawPayload = await this.bridge.getMessage(id);
|
|
249
|
+
return rawPayload;
|
|
250
|
+
}
|
|
251
|
+
async messageRawPayloadParser(rawPayload) {
|
|
252
|
+
config_1.log.verbose('PuppetWeChat', 'messageRawPayloadParser(%s) @ %s', rawPayload, this);
|
|
253
|
+
const payload = (0, mod_1.messageRawPayloadParser)(rawPayload);
|
|
254
|
+
return payload;
|
|
255
|
+
}
|
|
256
|
+
async messageRecall(messageId) {
|
|
257
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(messageId);
|
|
258
|
+
}
|
|
259
|
+
async messageFile(messageId) {
|
|
260
|
+
const rawPayload = await this.messageRawPayload(messageId);
|
|
261
|
+
const fileBox = await this.messageRawPayloadToFile(rawPayload);
|
|
262
|
+
return fileBox;
|
|
263
|
+
}
|
|
264
|
+
async messageUrl(messageId) {
|
|
265
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(messageId);
|
|
266
|
+
}
|
|
267
|
+
async messageMiniProgram(messageId) {
|
|
268
|
+
config_1.log.verbose('PuppetWeChat', 'messageMiniProgram(%s)', messageId);
|
|
269
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(messageId);
|
|
270
|
+
}
|
|
271
|
+
async messageRawPayloadToFile(rawPayload) {
|
|
272
|
+
let url = await this.messageRawPayloadToUrl(rawPayload);
|
|
273
|
+
if (!url) {
|
|
274
|
+
throw new Error('no url for type ' + wechaty_puppet_1.MessageType[rawPayload.MsgType]);
|
|
275
|
+
}
|
|
276
|
+
// use http instead of https, because https will only success on the very first request!
|
|
277
|
+
url = url.replace(/^https/i, 'http');
|
|
278
|
+
const parsedUrl = new url_1.default.URL(url);
|
|
279
|
+
const msgFileName = (0, mod_1.messageFilename)(rawPayload);
|
|
280
|
+
if (!msgFileName) {
|
|
281
|
+
throw new Error('no filename');
|
|
282
|
+
}
|
|
283
|
+
const cookies = await this.cookies();
|
|
284
|
+
const headers = {
|
|
285
|
+
Accept: '*/*',
|
|
286
|
+
// 'Accept-Encoding': 'gzip, deflate, sdch',
|
|
287
|
+
// 'Accept-Encoding': 'gzip, deflate, sdch, br', // MsgType.IMAGE | VIDEO
|
|
288
|
+
'Accept-Encoding': 'identity;q=1, *;q=0',
|
|
289
|
+
'Accept-Language': 'zh-CN,zh;q=0.8',
|
|
290
|
+
// 'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.6,en-US;q=0.4,en;q=0.2',
|
|
291
|
+
Cookie: cookies.map(c => `${c.name}=${c.value}`).join('; '),
|
|
292
|
+
// Accept: 'image/webp,image/*,*/*;q=0.8',
|
|
293
|
+
// Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', // MsgType.IMAGE | VIDEO
|
|
294
|
+
Host: parsedUrl.hostname,
|
|
295
|
+
Range: 'bytes=0-',
|
|
296
|
+
// Referer: protocol + '//wx.qq.com/',
|
|
297
|
+
Referer: url,
|
|
298
|
+
// 'Upgrade-Insecure-Requests': 1, // MsgType.VIDEO | IMAGE
|
|
299
|
+
/**
|
|
300
|
+
* pgv_pvi=6639183872; pgv_si=s8359147520; webwx_data_ticket=gSeBbuhX+0kFdkXbgeQwr6Ck
|
|
301
|
+
*/
|
|
302
|
+
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) '
|
|
303
|
+
+ 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
|
|
304
|
+
};
|
|
305
|
+
const fileBox = wechaty_puppet_1.FileBox.fromUrl(url, msgFileName, headers);
|
|
306
|
+
return fileBox;
|
|
307
|
+
}
|
|
308
|
+
async messageSendUrl(conversationId, urlLinkPayload) {
|
|
309
|
+
(0, wechaty_puppet_1.throwUnsupportedError)(conversationId, urlLinkPayload);
|
|
310
|
+
}
|
|
311
|
+
async messageSendMiniProgram(conversationId, miniProgramPayload) {
|
|
312
|
+
config_1.log.verbose('PuppetWeChat', 'messageSendMiniProgram("%s", %s)', conversationId, JSON.stringify(miniProgramPayload));
|
|
313
|
+
(0, wechaty_puppet_1.throwUnsupportedError)(conversationId, miniProgramPayload);
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* TODO: Test this function if it could work...
|
|
317
|
+
*/
|
|
318
|
+
// public async forward(baseData: MsgRawObj, patchData: MsgRawObj): Promise<boolean> {
|
|
319
|
+
async messageForward(conversationId, messageId) {
|
|
320
|
+
config_1.log.silly('PuppetWeChat', 'forward(receiver=%s, messageId=%s)', conversationId, messageId);
|
|
321
|
+
let rawPayload = await this.messageRawPayload(messageId);
|
|
322
|
+
// rawPayload = Object.assign({}, rawPayload)
|
|
323
|
+
const newMsg = {};
|
|
324
|
+
const largeFileSize = 25 * 1024 * 1024;
|
|
325
|
+
// let ret = false
|
|
326
|
+
// if you know roomId or userId, you can use `Room.load(roomId)` or `Contact.load(userId)`
|
|
327
|
+
// let sendToList: Contact[] = [].concat(sendTo as any || [])
|
|
328
|
+
// sendToList = sendToList.filter(s => {
|
|
329
|
+
// if ((s instanceof Room || s instanceof Contact) && s.id) {
|
|
330
|
+
// return true
|
|
331
|
+
// }
|
|
332
|
+
// return false
|
|
333
|
+
// }) as Contact[]
|
|
334
|
+
// if (sendToList.length < 1) {
|
|
335
|
+
// throw new Error('param must be Room or Contact and array')
|
|
336
|
+
// }
|
|
337
|
+
if (rawPayload.FileSize >= largeFileSize && !rawPayload.Signature) {
|
|
338
|
+
// if has RawObj.Signature, can forward the 25Mb+ file
|
|
339
|
+
config_1.log.warn('MediaMessage', 'forward() Due to webWx restrictions, '
|
|
340
|
+
+ 'more than 25MB of files can not be downloaded and can not be forwarded.');
|
|
341
|
+
throw new Error('forward() Due to webWx restrictions, '
|
|
342
|
+
+ 'more than 25MB of files can not be downloaded and can not be forwarded.');
|
|
343
|
+
}
|
|
344
|
+
newMsg.FromUserName = this.id || '';
|
|
345
|
+
newMsg.isTranspond = true;
|
|
346
|
+
newMsg.MsgIdBeforeTranspond = rawPayload.MsgIdBeforeTranspond || rawPayload.MsgId;
|
|
347
|
+
newMsg.MMSourceMsgId = rawPayload.MsgId;
|
|
348
|
+
// In room msg, the content prefix sender:, need to be removed,
|
|
349
|
+
// otherwise the forwarded sender will display the source message sender,
|
|
350
|
+
// causing self () to determine the error
|
|
351
|
+
newMsg.Content = (0, mod_1.unescapeHtml)(rawPayload.Content.replace(/^@\w+:<br\/>/, '')).replace(/^[\w-]+:<br\/>/, '');
|
|
352
|
+
newMsg.MMIsChatRoom = (0, mod_1.isRoomId)(conversationId);
|
|
353
|
+
// The following parameters need to be overridden after calling createMessage()
|
|
354
|
+
rawPayload = Object.assign(rawPayload, newMsg);
|
|
355
|
+
// for (let i = 0; i < sendToList.length; i++) {
|
|
356
|
+
// newMsg.ToUserName = sendToList[i].id
|
|
357
|
+
// // all call success return true
|
|
358
|
+
// ret = (i === 0 ? true : ret) && await config.puppetInstance().forward(m, newMsg)
|
|
359
|
+
// }
|
|
360
|
+
newMsg.ToUserName = conversationId;
|
|
361
|
+
// ret = await config.puppetInstance().forward(m, newMsg)
|
|
362
|
+
// return ret
|
|
363
|
+
const baseData = rawPayload;
|
|
364
|
+
const patchData = newMsg;
|
|
365
|
+
try {
|
|
366
|
+
const ret = await this.bridge.forward(baseData, patchData);
|
|
367
|
+
if (!ret) {
|
|
368
|
+
throw new Error('forward failed');
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
catch (e) {
|
|
372
|
+
config_1.log.error('PuppetWeChat', 'forward() exception: %s', e.message);
|
|
373
|
+
throw e;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
async messageSendText(conversationId, text) {
|
|
377
|
+
config_1.log.verbose('PuppetWeChat', 'messageSendText(%s, %s)', conversationId, text);
|
|
378
|
+
try {
|
|
379
|
+
await this.bridge.send(conversationId, text);
|
|
380
|
+
}
|
|
381
|
+
catch (e) {
|
|
382
|
+
config_1.log.error('PuppetWeChat', 'messageSendText() exception: %s', e.message);
|
|
383
|
+
throw e;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
async login(userId) {
|
|
387
|
+
return super.login(userId);
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* logout from browser, then server will emit `logout` event
|
|
391
|
+
*/
|
|
392
|
+
async logout() {
|
|
393
|
+
config_1.log.verbose('PuppetWeChat', 'logout()');
|
|
394
|
+
const user = this.selfId();
|
|
395
|
+
if (!user) {
|
|
396
|
+
config_1.log.warn('PuppetWeChat', 'logout() without self()');
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
try {
|
|
400
|
+
await this.bridge.logout();
|
|
401
|
+
}
|
|
402
|
+
catch (e) {
|
|
403
|
+
config_1.log.error('PuppetWeChat', 'logout() exception: %s', e.message);
|
|
404
|
+
throw e;
|
|
405
|
+
}
|
|
406
|
+
finally {
|
|
407
|
+
this.id = undefined;
|
|
408
|
+
this.emit('logout', { contactId: user, data: 'logout()' });
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
*
|
|
413
|
+
* ContactSelf
|
|
414
|
+
*
|
|
415
|
+
*
|
|
416
|
+
*/
|
|
417
|
+
async contactSelfQRCode() {
|
|
418
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)();
|
|
419
|
+
}
|
|
420
|
+
async contactSelfName(name) {
|
|
421
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(name);
|
|
422
|
+
}
|
|
423
|
+
async contactSelfSignature(signature) {
|
|
424
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(signature);
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
*
|
|
428
|
+
* Contact
|
|
429
|
+
*
|
|
430
|
+
*/
|
|
431
|
+
async contactRawPayload(id) {
|
|
432
|
+
config_1.log.silly('PuppetWeChat', 'contactRawPayload(%s) @ %s', id, this);
|
|
433
|
+
try {
|
|
434
|
+
const rawPayload = await this.bridge.getContact(id);
|
|
435
|
+
return rawPayload;
|
|
436
|
+
}
|
|
437
|
+
catch (e) {
|
|
438
|
+
config_1.log.error('PuppetWeChat', 'contactRawPayload(%s) exception: %s', id, e.message);
|
|
439
|
+
throw e;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
async contactRawPayloadParser(rawPayload) {
|
|
443
|
+
config_1.log.silly('PuppetWeChat', 'contactParseRawPayload(Object.keys(payload).length=%d)', Object.keys(rawPayload).length);
|
|
444
|
+
if (!Object.keys(rawPayload).length) {
|
|
445
|
+
config_1.log.error('PuppetWeChat', 'contactParseRawPayload(Object.keys(payload).length=%d)', Object.keys(rawPayload).length);
|
|
446
|
+
config_1.log.error('PuppetWeChat', 'contactParseRawPayload() got empty rawPayload!');
|
|
447
|
+
throw new Error('empty raw payload');
|
|
448
|
+
// return {
|
|
449
|
+
// gender: Gender.Unknown,
|
|
450
|
+
// type: Contact.Type.Unknown,
|
|
451
|
+
// }
|
|
452
|
+
}
|
|
453
|
+
// this.id = rawPayload.UserName
|
|
454
|
+
// MMActualSender??? MMPeerUserName??? `getUserContact(message.MMActualSender,message.MMPeerUserName).HeadImgUrl`
|
|
455
|
+
// uin: rawPayload.Uin, // stable id: 4763975 || getCookie("wxuin")
|
|
456
|
+
return {
|
|
457
|
+
address: rawPayload.Alias,
|
|
458
|
+
alias: rawPayload.RemarkName,
|
|
459
|
+
avatar: rawPayload.HeadImgUrl,
|
|
460
|
+
city: rawPayload.City,
|
|
461
|
+
friend: rawPayload.stranger === undefined
|
|
462
|
+
? undefined
|
|
463
|
+
: !rawPayload.stranger,
|
|
464
|
+
gender: rawPayload.Sex,
|
|
465
|
+
id: rawPayload.UserName,
|
|
466
|
+
name: (0, mod_1.plainText)(rawPayload.NickName || ''),
|
|
467
|
+
province: rawPayload.Province,
|
|
468
|
+
signature: rawPayload.Signature,
|
|
469
|
+
star: !!rawPayload.StarFriend,
|
|
470
|
+
/**
|
|
471
|
+
* @see 1. https://github.com/Chatie/webwx-app-tracker/blob/
|
|
472
|
+
* 7c59d35c6ea0cff38426a4c5c912a086c4c512b2/formatted/webwxApp.js#L3243
|
|
473
|
+
* @see 2. https://github.com/Urinx/WeixinBot/blob/master/README.md
|
|
474
|
+
* @ignore
|
|
475
|
+
*/
|
|
476
|
+
type: (!!rawPayload.UserName && !rawPayload.UserName.startsWith('@@') && !!(rawPayload.VerifyFlag & 8))
|
|
477
|
+
? wechaty_puppet_1.ContactType.Official
|
|
478
|
+
: wechaty_puppet_1.ContactType.Personal,
|
|
479
|
+
weixin: rawPayload.Alias, // Wechat ID
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
ding(data) {
|
|
483
|
+
config_1.log.verbose('PuppetWeChat', 'ding(%s)', data || '');
|
|
484
|
+
this.bridge.ding(data);
|
|
485
|
+
}
|
|
486
|
+
async contactAvatar(contactId, file) {
|
|
487
|
+
config_1.log.verbose('PuppetWeChat', 'contactAvatar(%s)', contactId);
|
|
488
|
+
if (file) {
|
|
489
|
+
throw new Error('not support');
|
|
490
|
+
}
|
|
491
|
+
const payload = await this.contactPayload(contactId);
|
|
492
|
+
if (!payload.avatar) {
|
|
493
|
+
throw new Error('Can not get avatar: no payload.avatar!');
|
|
494
|
+
}
|
|
495
|
+
try {
|
|
496
|
+
const hostname = await this.hostname();
|
|
497
|
+
const avatarUrl = `http://${hostname}${payload.avatar}&type=big`; // add '&type=big' to get big image
|
|
498
|
+
const cookieList = await this.cookies();
|
|
499
|
+
config_1.log.silly('PuppeteerContact', 'avatar() url: %s', avatarUrl);
|
|
500
|
+
/**
|
|
501
|
+
* FileBox headers (will be used in NodeJS.http.get param options)
|
|
502
|
+
*/
|
|
503
|
+
const headers = {
|
|
504
|
+
cookie: cookieList.map(c => `${c.name}=${c.value}`).join('; '),
|
|
505
|
+
};
|
|
506
|
+
const fileName = (payload.name || 'unknown') + '-avatar.jpg';
|
|
507
|
+
return wechaty_puppet_1.FileBox.fromUrl(avatarUrl, fileName, headers);
|
|
508
|
+
}
|
|
509
|
+
catch (err) {
|
|
510
|
+
config_1.log.warn('PuppeteerContact', 'avatar() exception: %s', err.stack);
|
|
511
|
+
throw err;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
async contactQrcode(contactId) {
|
|
515
|
+
if (contactId !== this.selfId()) {
|
|
516
|
+
throw new Error('can not set avatar for others');
|
|
517
|
+
}
|
|
518
|
+
throw new Error('not supported');
|
|
519
|
+
// return await this.bridge.WXqr
|
|
520
|
+
}
|
|
521
|
+
async contactAlias(contactId, alias) {
|
|
522
|
+
if (typeof alias === 'undefined') {
|
|
523
|
+
throw new Error('to be implement');
|
|
524
|
+
}
|
|
525
|
+
try {
|
|
526
|
+
const ret = await this.bridge.contactAlias(contactId, alias);
|
|
527
|
+
if (!ret) {
|
|
528
|
+
config_1.log.warn('PuppetWeChat', 'contactRemark(%s, %s) bridge.contactAlias() return false', contactId, alias);
|
|
529
|
+
throw new Error('bridge.contactAlias fail');
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
catch (e) {
|
|
533
|
+
config_1.log.warn('PuppetWeChat', 'contactRemark(%s, %s) rejected: %s', contactId, alias, e.message);
|
|
534
|
+
throw e;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
async contactList() {
|
|
538
|
+
const idList = await this.bridge.contactList();
|
|
539
|
+
return idList;
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
*
|
|
543
|
+
* Room
|
|
544
|
+
*
|
|
545
|
+
*/
|
|
546
|
+
async roomRawPayload(id) {
|
|
547
|
+
config_1.log.verbose('PuppetWeChat', 'roomRawPayload(%s)', id);
|
|
548
|
+
try {
|
|
549
|
+
let rawPayload;
|
|
550
|
+
// = await this.bridge.getContact(room.id) as PuppeteerRoomRawPayload
|
|
551
|
+
// let currNum = rawPayload.MemberList && rawPayload.MemberList.length || 0
|
|
552
|
+
// let prevNum = room.memberList().length
|
|
553
|
+
// rawPayload && rawPayload.MemberList && this.rawObj.MemberList.length || 0
|
|
554
|
+
let prevLength = 0;
|
|
555
|
+
/**
|
|
556
|
+
* @todo use Misc.retry() to replace the following loop
|
|
557
|
+
*/
|
|
558
|
+
let ttl = 7;
|
|
559
|
+
while (ttl-- /* && currNum !== prevNum */) {
|
|
560
|
+
rawPayload = await this.bridge.getContact(id);
|
|
561
|
+
if (rawPayload) {
|
|
562
|
+
const currLength = (rawPayload.MemberList && rawPayload.MemberList.length) || 0;
|
|
563
|
+
config_1.log.silly('PuppetWeChat', 'roomPayload() this.bridge.getContact(%s) '
|
|
564
|
+
+ 'MemberList.length:(prev:%d, curr:%d) at ttl:%d', id, prevLength, currLength, ttl);
|
|
565
|
+
if (prevLength === currLength) {
|
|
566
|
+
config_1.log.silly('PuppetWeChat', 'roomPayload() puppet.getContact(%s) done at ttl:%d with length:%d', this.id, ttl, currLength);
|
|
567
|
+
return rawPayload;
|
|
568
|
+
}
|
|
569
|
+
if (currLength >= prevLength) {
|
|
570
|
+
prevLength = currLength;
|
|
571
|
+
}
|
|
572
|
+
else {
|
|
573
|
+
config_1.log.warn('PuppetWeChat', 'roomRawPayload() currLength(%d) <= prevLength(%d) ???', currLength, prevLength);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
config_1.log.silly('PuppetWeChat', `roomPayload() puppet.getContact(${id}) retry at ttl:%d`, ttl);
|
|
577
|
+
await new Promise(resolve => setTimeout(resolve, 1000)); // wait for 1 second
|
|
578
|
+
}
|
|
579
|
+
throw new Error('no payload');
|
|
580
|
+
}
|
|
581
|
+
catch (e) {
|
|
582
|
+
config_1.log.error('PuppetWeChat', 'roomRawPayload(%s) exception: %s', id, e.message);
|
|
583
|
+
throw e;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
async roomRawPayloadParser(rawPayload) {
|
|
587
|
+
config_1.log.verbose('PuppetWeChat', 'roomRawPayloadParser(%s)', rawPayload);
|
|
588
|
+
// const payload = await this.roomPayload(rawPayload.UserName)
|
|
589
|
+
// console.log(rawPayload)
|
|
590
|
+
// const memberList = (rawPayload.MemberList || [])
|
|
591
|
+
// .map(m => this.Contact.load(m.UserName))
|
|
592
|
+
// await Promise.all(memberList.map(c => c.ready()))
|
|
593
|
+
const id = rawPayload.UserName;
|
|
594
|
+
// const rawMemberList = rawPayload.MemberList || []
|
|
595
|
+
// const memberIdList = rawMemberList.map(rawMember => rawMember.UserName)
|
|
596
|
+
// const nameMap = await this.roomParseMap('name' , rawPayload.MemberList)
|
|
597
|
+
// const roomAliasMap = await this.roomParseMap('roomAlias' , rawPayload.MemberList)
|
|
598
|
+
// const contactAliasMap = await this.roomParseMap('contactAlias', rawPayload.MemberList)
|
|
599
|
+
// const aliasDict = {} as { [id: string]: string | undefined }
|
|
600
|
+
// if (Array.isArray(rawPayload.MemberList)) {
|
|
601
|
+
// rawPayload.MemberList.forEach(rawMember => {
|
|
602
|
+
// aliasDict[rawMember.UserName] = rawMember.DisplayName
|
|
603
|
+
// })
|
|
604
|
+
// // const memberListPayload = await Promise.all(
|
|
605
|
+
// // rawPayload.MemberList
|
|
606
|
+
// // .map(rawMember => rawMember.UserName)
|
|
607
|
+
// // .map(contactId => this.contactPayload(contactId)),
|
|
608
|
+
// // )
|
|
609
|
+
// // console.log(memberListPayload)
|
|
610
|
+
// // memberListPayload.forEach(payload => aliasDict[payload.id] = payload.alias)
|
|
611
|
+
// // console.log(aliasDict)
|
|
612
|
+
// }
|
|
613
|
+
const memberIdList = rawPayload.MemberList
|
|
614
|
+
? rawPayload.MemberList.map(m => m.UserName)
|
|
615
|
+
: [];
|
|
616
|
+
const roomPayload = {
|
|
617
|
+
adminIdList: [],
|
|
618
|
+
id,
|
|
619
|
+
memberIdList,
|
|
620
|
+
topic: (0, mod_1.plainText)(rawPayload.NickName || ''),
|
|
621
|
+
// aliasDict,
|
|
622
|
+
// nameMap,
|
|
623
|
+
// roomAliasMap,
|
|
624
|
+
// contactAliasMap,
|
|
625
|
+
};
|
|
626
|
+
// console.log(roomPayload)
|
|
627
|
+
return roomPayload;
|
|
628
|
+
}
|
|
629
|
+
async roomList() {
|
|
630
|
+
config_1.log.verbose('PuppetPupppeteer', 'roomList()');
|
|
631
|
+
const idList = await this.bridge.roomList();
|
|
632
|
+
return idList;
|
|
633
|
+
}
|
|
634
|
+
async roomDel(roomId, contactId) {
|
|
635
|
+
try {
|
|
636
|
+
await this.bridge.roomDelMember(roomId, contactId);
|
|
637
|
+
}
|
|
638
|
+
catch (e) {
|
|
639
|
+
config_1.log.warn('PuppetWeChat', 'roomDelMember(%s, %d) rejected: %s', roomId, contactId, e.message);
|
|
640
|
+
throw e;
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
async roomAvatar(roomId) {
|
|
644
|
+
config_1.log.verbose('PuppetWeChat', 'roomAvatar(%s)', roomId);
|
|
645
|
+
const payload = await this.roomPayload(roomId);
|
|
646
|
+
if (payload.avatar) {
|
|
647
|
+
return wechaty_puppet_1.FileBox.fromUrl(payload.avatar);
|
|
648
|
+
}
|
|
649
|
+
config_1.log.warn('PuppetWeChat', 'roomAvatar() avatar not found, use the chatie default.');
|
|
650
|
+
return (0, config_1.qrCodeForChatie)();
|
|
651
|
+
}
|
|
652
|
+
async roomAdd(roomId, contactId) {
|
|
653
|
+
try {
|
|
654
|
+
await this.bridge.roomAddMember(roomId, contactId);
|
|
655
|
+
}
|
|
656
|
+
catch (e) {
|
|
657
|
+
config_1.log.warn('PuppetWeChat', 'roomAddMember(%s) rejected: %s', contactId, e.message);
|
|
658
|
+
throw e;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
async roomTopic(roomId, topic) {
|
|
662
|
+
if (!topic) {
|
|
663
|
+
const payload = await this.roomPayload(roomId);
|
|
664
|
+
return payload.topic;
|
|
665
|
+
}
|
|
666
|
+
try {
|
|
667
|
+
await this.bridge.roomModTopic(roomId, topic);
|
|
668
|
+
}
|
|
669
|
+
catch (e) {
|
|
670
|
+
config_1.log.warn('PuppetWeChat', 'roomTopic(%s) rejected: %s', topic, e.message);
|
|
671
|
+
throw e;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
async roomCreate(contactIdList, topic) {
|
|
675
|
+
try {
|
|
676
|
+
const roomId = await this.bridge.roomCreate(contactIdList, topic);
|
|
677
|
+
if (!roomId) {
|
|
678
|
+
throw new Error('PuppetWeChat.roomCreate() roomId "' + roomId + '" not found');
|
|
679
|
+
}
|
|
680
|
+
return roomId;
|
|
681
|
+
}
|
|
682
|
+
catch (e) {
|
|
683
|
+
config_1.log.warn('PuppetWeChat', 'roomCreate(%s, %s) rejected: %s', contactIdList.join(','), topic, e.message);
|
|
684
|
+
throw e;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
async roomAnnounce(roomId, text) {
|
|
688
|
+
config_1.log.warn('PuppetWeChat', 'roomAnnounce(%s, %s) not supported', roomId, text || '');
|
|
689
|
+
if (text) {
|
|
690
|
+
return;
|
|
691
|
+
}
|
|
692
|
+
return '';
|
|
693
|
+
}
|
|
694
|
+
async roomQuit(roomId) {
|
|
695
|
+
config_1.log.warn('PuppetWeChat', 'roomQuit(%s) not supported by Web API', roomId);
|
|
696
|
+
}
|
|
697
|
+
async roomQRCode(roomId) {
|
|
698
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(roomId);
|
|
699
|
+
}
|
|
700
|
+
async roomMemberList(roomId) {
|
|
701
|
+
config_1.log.verbose('PuppetWeChat', 'roommemberList(%s)', roomId);
|
|
702
|
+
const rawPayload = await this.roomRawPayload(roomId);
|
|
703
|
+
const memberIdList = (rawPayload.MemberList || [])
|
|
704
|
+
.map(member => member.UserName);
|
|
705
|
+
return memberIdList;
|
|
706
|
+
}
|
|
707
|
+
async roomMemberRawPayload(roomId, contactId) {
|
|
708
|
+
config_1.log.verbose('PuppetWeChat', 'roomMemberRawPayload(%s, %s)', roomId, contactId);
|
|
709
|
+
const rawPayload = await this.roomRawPayload(roomId);
|
|
710
|
+
const memberPayloadList = rawPayload.MemberList || [];
|
|
711
|
+
const memberPayloadResult = memberPayloadList.filter(payload => payload.UserName === contactId);
|
|
712
|
+
if (memberPayloadResult.length > 0) {
|
|
713
|
+
return memberPayloadResult[0];
|
|
714
|
+
}
|
|
715
|
+
else {
|
|
716
|
+
throw new Error('not found');
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
async roomMemberRawPayloadParser(rawPayload) {
|
|
720
|
+
config_1.log.verbose('PuppetWeChat', 'roomMemberRawPayloadParser(%s)', rawPayload);
|
|
721
|
+
const payload = {
|
|
722
|
+
avatar: rawPayload.HeadImgUrl,
|
|
723
|
+
id: rawPayload.UserName,
|
|
724
|
+
name: rawPayload.NickName,
|
|
725
|
+
roomAlias: rawPayload.DisplayName,
|
|
726
|
+
};
|
|
727
|
+
return payload;
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
*
|
|
731
|
+
* Room Invitation
|
|
732
|
+
*
|
|
733
|
+
*/
|
|
734
|
+
async roomInvitationAccept(roomInvitationId) {
|
|
735
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(roomInvitationId);
|
|
736
|
+
}
|
|
737
|
+
async roomInvitationRawPayload(roomInvitationId) {
|
|
738
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(roomInvitationId);
|
|
739
|
+
}
|
|
740
|
+
async roomInvitationRawPayloadParser(rawPayload) {
|
|
741
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(rawPayload);
|
|
742
|
+
}
|
|
743
|
+
/**
|
|
744
|
+
*
|
|
745
|
+
* Friendship
|
|
746
|
+
*
|
|
747
|
+
*/
|
|
748
|
+
async friendshipRawPayload(id) {
|
|
749
|
+
config_1.log.warn('PuppetWeChat', 'friendshipRawPayload(%s)', id);
|
|
750
|
+
const rawPayload = await this.bridge.getMessage(id);
|
|
751
|
+
if (!rawPayload) {
|
|
752
|
+
throw new Error('no rawPayload');
|
|
753
|
+
}
|
|
754
|
+
return rawPayload;
|
|
755
|
+
}
|
|
756
|
+
async friendshipRawPayloadParser(rawPayload) {
|
|
757
|
+
config_1.log.warn('PuppetWeChat', 'friendshipRawPayloadParser(%s)', rawPayload);
|
|
758
|
+
const timestamp = Math.floor(Date.now() / 1000); // in seconds
|
|
759
|
+
switch (rawPayload.MsgType) {
|
|
760
|
+
case web_schemas_1.WebMessageType.VERIFYMSG: {
|
|
761
|
+
if (!rawPayload.RecommendInfo) {
|
|
762
|
+
throw new Error('no RecommendInfo');
|
|
763
|
+
}
|
|
764
|
+
const recommendInfo = rawPayload.RecommendInfo;
|
|
765
|
+
if (!recommendInfo) {
|
|
766
|
+
throw new Error('no recommendInfo');
|
|
767
|
+
}
|
|
768
|
+
const payloadReceive = {
|
|
769
|
+
contactId: recommendInfo.UserName,
|
|
770
|
+
hello: recommendInfo.Content,
|
|
771
|
+
id: rawPayload.MsgId,
|
|
772
|
+
ticket: recommendInfo.Ticket,
|
|
773
|
+
timestamp,
|
|
774
|
+
type: wechaty_puppet_1.FriendshipType.Receive,
|
|
775
|
+
};
|
|
776
|
+
return payloadReceive;
|
|
777
|
+
}
|
|
778
|
+
case web_schemas_1.WebMessageType.SYS: {
|
|
779
|
+
const payloadConfirm = {
|
|
780
|
+
contactId: rawPayload.FromUserName,
|
|
781
|
+
id: rawPayload.MsgId,
|
|
782
|
+
timestamp,
|
|
783
|
+
type: wechaty_puppet_1.FriendshipType.Confirm,
|
|
784
|
+
};
|
|
785
|
+
return payloadConfirm;
|
|
786
|
+
}
|
|
787
|
+
default:
|
|
788
|
+
throw new Error('not supported friend request message raw payload');
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
async friendshipSearchPhone(phone) {
|
|
792
|
+
throw (0, wechaty_puppet_1.throwUnsupportedError)(phone);
|
|
793
|
+
}
|
|
794
|
+
async friendshipSearchWeixin(weixin) {
|
|
795
|
+
throw (0, wechaty_puppet_1.throwUnsupportedError)(weixin);
|
|
796
|
+
}
|
|
797
|
+
async friendshipAdd(contactId, hello) {
|
|
798
|
+
try {
|
|
799
|
+
await this.bridge.verifyUserRequest(contactId, hello);
|
|
800
|
+
}
|
|
801
|
+
catch (e) {
|
|
802
|
+
config_1.log.warn('PuppetWeChat', 'friendshipAdd() bridge.verifyUserRequest(%s, %s) rejected: %s', contactId, hello, e.message);
|
|
803
|
+
throw e;
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
async friendshipAccept(friendshipId) {
|
|
807
|
+
const payload = await this.friendshipPayload(friendshipId);
|
|
808
|
+
try {
|
|
809
|
+
await this.bridge.verifyUserOk(payload.contactId, payload.ticket);
|
|
810
|
+
}
|
|
811
|
+
catch (e) {
|
|
812
|
+
config_1.log.warn('PuppetWeChat', 'bridge.verifyUserOk(%s, %s) rejected: %s', payload.contactId, payload.ticket, e.message);
|
|
813
|
+
throw e;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* @private
|
|
818
|
+
* For issue #668
|
|
819
|
+
*/
|
|
820
|
+
async waitStable() {
|
|
821
|
+
config_1.log.verbose('PuppetWeChat', 'waitStable()');
|
|
822
|
+
let maxNum = 0;
|
|
823
|
+
let curNum = 0;
|
|
824
|
+
let unchangedNum = 0;
|
|
825
|
+
const SLEEP_SECOND = 1;
|
|
826
|
+
const STABLE_CHECK_NUM = 3;
|
|
827
|
+
while (unchangedNum < STABLE_CHECK_NUM) {
|
|
828
|
+
// wait 1 second
|
|
829
|
+
await new Promise(resolve => setTimeout(resolve, SLEEP_SECOND * 1000));
|
|
830
|
+
const contactList = await this.contactList();
|
|
831
|
+
curNum = contactList.length;
|
|
832
|
+
if (curNum > 0 && curNum === maxNum) {
|
|
833
|
+
unchangedNum++;
|
|
834
|
+
}
|
|
835
|
+
else /* curNum < maxNum */ {
|
|
836
|
+
unchangedNum = 0;
|
|
837
|
+
}
|
|
838
|
+
if (curNum > maxNum) {
|
|
839
|
+
maxNum = curNum;
|
|
840
|
+
}
|
|
841
|
+
config_1.log.silly('PuppetWeChat', 'readyStable() while() curNum=%s, maxNum=%s, unchangedNum=%s', curNum, maxNum, unchangedNum);
|
|
842
|
+
}
|
|
843
|
+
config_1.log.verbose('PuppetWeChat', 'readyStable() emit(ready)');
|
|
844
|
+
this.emit('ready', { data: 'stable' });
|
|
845
|
+
}
|
|
846
|
+
/**
|
|
847
|
+
* https://www.chatie.io:8080/api
|
|
848
|
+
* location.hostname = www.chatie.io
|
|
849
|
+
* location.host = www.chatie.io:8080
|
|
850
|
+
* See: https://stackoverflow.com/a/11379802/1123955
|
|
851
|
+
*/
|
|
852
|
+
async hostname() {
|
|
853
|
+
try {
|
|
854
|
+
const name = await this.bridge.hostname();
|
|
855
|
+
if (!name) {
|
|
856
|
+
throw new Error('no hostname found');
|
|
857
|
+
}
|
|
858
|
+
return name;
|
|
859
|
+
}
|
|
860
|
+
catch (e) {
|
|
861
|
+
config_1.log.error('PuppetWeChat', 'hostname() exception:%s', e);
|
|
862
|
+
this.emit('error', e);
|
|
863
|
+
throw e;
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
async cookies() {
|
|
867
|
+
return this.bridge.cookies();
|
|
868
|
+
}
|
|
869
|
+
async saveCookie() {
|
|
870
|
+
const cookieList = await this.bridge.cookies();
|
|
871
|
+
await this.memory.set(config_1.MEMORY_SLOT, cookieList);
|
|
872
|
+
await this.memory.save();
|
|
873
|
+
}
|
|
874
|
+
extToType(ext) {
|
|
875
|
+
switch (ext) {
|
|
876
|
+
case '.bmp':
|
|
877
|
+
case '.jpeg':
|
|
878
|
+
case '.jpg':
|
|
879
|
+
case '.png':
|
|
880
|
+
return web_schemas_1.WebMessageType.IMAGE;
|
|
881
|
+
case '.gif':
|
|
882
|
+
return web_schemas_1.WebMessageType.EMOTICON;
|
|
883
|
+
case '.mp4':
|
|
884
|
+
return web_schemas_1.WebMessageType.VIDEO;
|
|
885
|
+
default:
|
|
886
|
+
return web_schemas_1.WebMessageType.APP;
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
// public async readyMedia(): Promise<this> {
|
|
890
|
+
async messageRawPayloadToUrl(rawPayload) {
|
|
891
|
+
config_1.log.silly('PuppetWeChat', 'readyMedia()');
|
|
892
|
+
// let type = MessageType.Unknown
|
|
893
|
+
let url;
|
|
894
|
+
try {
|
|
895
|
+
switch (rawPayload.MsgType) {
|
|
896
|
+
case web_schemas_1.WebMessageType.EMOTICON:
|
|
897
|
+
// type = MessageType.Emoticon
|
|
898
|
+
url = await this.bridge.getMsgEmoticon(rawPayload.MsgId);
|
|
899
|
+
break;
|
|
900
|
+
case web_schemas_1.WebMessageType.IMAGE:
|
|
901
|
+
// type = MessageType.Image
|
|
902
|
+
url = await this.bridge.getMsgImg(rawPayload.MsgId);
|
|
903
|
+
break;
|
|
904
|
+
case web_schemas_1.WebMessageType.VIDEO:
|
|
905
|
+
case web_schemas_1.WebMessageType.MICROVIDEO:
|
|
906
|
+
// type = MessageType.Video
|
|
907
|
+
url = await this.bridge.getMsgVideo(rawPayload.MsgId);
|
|
908
|
+
break;
|
|
909
|
+
case web_schemas_1.WebMessageType.VOICE:
|
|
910
|
+
// type = MessageType.Audio
|
|
911
|
+
url = await this.bridge.getMsgVoice(rawPayload.MsgId);
|
|
912
|
+
break;
|
|
913
|
+
case web_schemas_1.WebMessageType.APP:
|
|
914
|
+
switch (rawPayload.AppMsgType) {
|
|
915
|
+
case web_schemas_1.WebAppMsgType.ATTACH:
|
|
916
|
+
if (!rawPayload.MMAppMsgDownloadUrl) {
|
|
917
|
+
throw new Error('no MMAppMsgDownloadUrl');
|
|
918
|
+
}
|
|
919
|
+
// had set in Message
|
|
920
|
+
// type = MessageType.Attachment
|
|
921
|
+
url = rawPayload.MMAppMsgDownloadUrl;
|
|
922
|
+
break;
|
|
923
|
+
case web_schemas_1.WebAppMsgType.URL:
|
|
924
|
+
case web_schemas_1.WebAppMsgType.READER_TYPE:
|
|
925
|
+
if (!rawPayload.Url) {
|
|
926
|
+
throw new Error('no Url');
|
|
927
|
+
}
|
|
928
|
+
// had set in Message
|
|
929
|
+
// type = MessageType.Attachment
|
|
930
|
+
url = rawPayload.Url;
|
|
931
|
+
break;
|
|
932
|
+
default: {
|
|
933
|
+
const e = new Error('ready() unsupported typeApp(): ' + rawPayload.AppMsgType);
|
|
934
|
+
config_1.log.warn('PuppeteerMessage', e.message);
|
|
935
|
+
throw e;
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
break;
|
|
939
|
+
case web_schemas_1.WebMessageType.TEXT:
|
|
940
|
+
if (rawPayload.SubMsgType === web_schemas_1.WebMessageType.LOCATION) {
|
|
941
|
+
// type = MessageType.Image
|
|
942
|
+
url = await this.bridge.getMsgPublicLinkImg(rawPayload.MsgId);
|
|
943
|
+
}
|
|
944
|
+
break;
|
|
945
|
+
default:
|
|
946
|
+
/**
|
|
947
|
+
* not a support media message, do nothing.
|
|
948
|
+
*/
|
|
949
|
+
return null;
|
|
950
|
+
// return this
|
|
951
|
+
}
|
|
952
|
+
if (!url) {
|
|
953
|
+
// if (!this.payload.url) {
|
|
954
|
+
// /**
|
|
955
|
+
// * not a support media message, do nothing.
|
|
956
|
+
// */
|
|
957
|
+
// return this
|
|
958
|
+
// }
|
|
959
|
+
// url = this.payload.url
|
|
960
|
+
// return {
|
|
961
|
+
// type: MessageType.Unknown,
|
|
962
|
+
// }
|
|
963
|
+
return null;
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
catch (e) {
|
|
967
|
+
config_1.log.warn('PuppetWeChat', 'ready() exception: %s', e.message);
|
|
968
|
+
throw e;
|
|
969
|
+
}
|
|
970
|
+
return url;
|
|
971
|
+
}
|
|
972
|
+
async uploadMedia(file, toUserName) {
|
|
973
|
+
const filename = file.name;
|
|
974
|
+
const ext = path_1.default.extname(filename); // message.ext()
|
|
975
|
+
// const contentType = Misc.mime(ext)
|
|
976
|
+
const contentType = mime_1.default.getType(ext);
|
|
977
|
+
// const contentType = message.mimeType()
|
|
978
|
+
if (!contentType) {
|
|
979
|
+
throw new Error('no MIME Type found on mediaMessage: ' + file.name);
|
|
980
|
+
}
|
|
981
|
+
let mediatype;
|
|
982
|
+
switch (ext) {
|
|
983
|
+
case '.bmp':
|
|
984
|
+
case '.jpeg':
|
|
985
|
+
case '.jpg':
|
|
986
|
+
case '.png':
|
|
987
|
+
case '.gif':
|
|
988
|
+
mediatype = "pic" /* WebMediaType.Image */;
|
|
989
|
+
break;
|
|
990
|
+
case '.mp4':
|
|
991
|
+
mediatype = "video" /* WebMediaType.Video */;
|
|
992
|
+
break;
|
|
993
|
+
default:
|
|
994
|
+
mediatype = "doc" /* WebMediaType.Attachment */;
|
|
995
|
+
}
|
|
996
|
+
const buffer = await new Promise((resolve, reject) => {
|
|
997
|
+
file.pipe(new bl_1.default((err, data) => {
|
|
998
|
+
if (err)
|
|
999
|
+
reject(err);
|
|
1000
|
+
else
|
|
1001
|
+
resolve(data);
|
|
1002
|
+
}));
|
|
1003
|
+
});
|
|
1004
|
+
// Sending video files is not allowed to exceed 20MB
|
|
1005
|
+
// https://github.com/Chatie/webwx-app-tracker/blob/
|
|
1006
|
+
// 7c59d35c6ea0cff38426a4c5c912a086c4c512b2/formatted/webwxApp.js#L1115
|
|
1007
|
+
const MAX_FILE_SIZE = 100 * 1024 * 1024;
|
|
1008
|
+
const LARGE_FILE_SIZE = 25 * 1024 * 1024;
|
|
1009
|
+
const MAX_VIDEO_SIZE = 20 * 1024 * 1024;
|
|
1010
|
+
if (mediatype === "video" /* WebMediaType.Video */ && buffer.length > MAX_VIDEO_SIZE) {
|
|
1011
|
+
throw new Error(`Sending video files is not allowed to exceed ${MAX_VIDEO_SIZE / 1024 / 1024}MB`);
|
|
1012
|
+
}
|
|
1013
|
+
if (buffer.length > MAX_FILE_SIZE) {
|
|
1014
|
+
throw new Error(`Sending files is not allowed to exceed ${MAX_FILE_SIZE / 1024 / 1024}MB`);
|
|
1015
|
+
}
|
|
1016
|
+
const fileMd5 = (0, md5_1.default)(buffer);
|
|
1017
|
+
const baseRequest = await this.getBaseRequest();
|
|
1018
|
+
const passTicket = await this.bridge.getPassticket();
|
|
1019
|
+
const uploadMediaUrl = await this.bridge.getUploadMediaUrl();
|
|
1020
|
+
const checkUploadUrl = await this.bridge.getCheckUploadUrl();
|
|
1021
|
+
const cookie = await this.bridge.cookies();
|
|
1022
|
+
const first = cookie.find(c => c.name === 'webwx_data_ticket');
|
|
1023
|
+
const webwxDataTicket = first && first.value;
|
|
1024
|
+
const size = buffer.length;
|
|
1025
|
+
const fromUserName = this.selfId();
|
|
1026
|
+
const id = 'WU_FILE_' + this.fileId;
|
|
1027
|
+
this.fileId++;
|
|
1028
|
+
const hostname = await this.bridge.hostname();
|
|
1029
|
+
const headers = {
|
|
1030
|
+
Cookie: cookie.map(c => c.name + '=' + c.value).join('; '),
|
|
1031
|
+
Referer: `https://${hostname}`,
|
|
1032
|
+
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 '
|
|
1033
|
+
+ '(KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
|
|
1034
|
+
};
|
|
1035
|
+
config_1.log.silly('PuppetWeChat', 'uploadMedia() headers:%s', JSON.stringify(headers));
|
|
1036
|
+
const uploadMediaRequest = {
|
|
1037
|
+
AESKey: '',
|
|
1038
|
+
BaseRequest: baseRequest,
|
|
1039
|
+
ClientMediaId: +new Date(),
|
|
1040
|
+
DataLen: size,
|
|
1041
|
+
FileMd5: fileMd5,
|
|
1042
|
+
FromUserName: fromUserName,
|
|
1043
|
+
MediaType: "doc" /* WebMediaType.Attachment */,
|
|
1044
|
+
Signature: '',
|
|
1045
|
+
StartPos: 0,
|
|
1046
|
+
ToUserName: toUserName,
|
|
1047
|
+
TotalLen: size,
|
|
1048
|
+
UploadType: 2,
|
|
1049
|
+
};
|
|
1050
|
+
const checkData = {
|
|
1051
|
+
BaseRequest: baseRequest,
|
|
1052
|
+
FileMd5: fileMd5,
|
|
1053
|
+
FileName: filename,
|
|
1054
|
+
FileSize: size,
|
|
1055
|
+
FileType: 7,
|
|
1056
|
+
FromUserName: fromUserName,
|
|
1057
|
+
ToUserName: toUserName,
|
|
1058
|
+
};
|
|
1059
|
+
const mediaData = {
|
|
1060
|
+
FileMd5: fileMd5,
|
|
1061
|
+
FileName: filename,
|
|
1062
|
+
FileSize: size,
|
|
1063
|
+
MMFileExt: ext,
|
|
1064
|
+
MediaId: '',
|
|
1065
|
+
ToUserName: toUserName,
|
|
1066
|
+
};
|
|
1067
|
+
// If file size > 25M, must first call checkUpload to get Signature and AESKey, otherwise it will fail to upload
|
|
1068
|
+
// https://github.com/Chatie/webwx-app-tracker/blob/
|
|
1069
|
+
// 7c59d35c6ea0cff38426a4c5c912a086c4c512b2/formatted/webwxApp.js#L1132 #1182
|
|
1070
|
+
if (size > LARGE_FILE_SIZE) {
|
|
1071
|
+
let ret;
|
|
1072
|
+
try {
|
|
1073
|
+
ret = await new Promise((resolve, reject) => {
|
|
1074
|
+
const r = {
|
|
1075
|
+
headers,
|
|
1076
|
+
json: checkData,
|
|
1077
|
+
url: `https://${hostname}${checkUploadUrl}`,
|
|
1078
|
+
};
|
|
1079
|
+
request_1.default.post(r, (err, _ /* res */, body) => {
|
|
1080
|
+
try {
|
|
1081
|
+
if (err) {
|
|
1082
|
+
reject(err);
|
|
1083
|
+
}
|
|
1084
|
+
else {
|
|
1085
|
+
let obj = body;
|
|
1086
|
+
if (typeof body !== 'object') {
|
|
1087
|
+
config_1.log.silly('PuppetWeChat', 'updateMedia() typeof body = %s', typeof body);
|
|
1088
|
+
try {
|
|
1089
|
+
obj = JSON.parse(body);
|
|
1090
|
+
}
|
|
1091
|
+
catch (e) {
|
|
1092
|
+
config_1.log.error('PuppetWeChat', 'updateMedia() body = %s', body);
|
|
1093
|
+
config_1.log.error('PuppetWeChat', 'updateMedia() exception: %s', e);
|
|
1094
|
+
this.emit('error', e);
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
if (typeof obj !== 'object' || obj.BaseResponse.Ret !== 0) {
|
|
1098
|
+
const errMsg = obj.BaseResponse || 'api return err';
|
|
1099
|
+
config_1.log.silly('PuppetWeChat', 'uploadMedia() checkUpload err:%s \nreq:%s\nret:%s', JSON.stringify(errMsg), JSON.stringify(r), body);
|
|
1100
|
+
reject(new Error('chackUpload err:' + JSON.stringify(errMsg)));
|
|
1101
|
+
}
|
|
1102
|
+
resolve({
|
|
1103
|
+
AESKey: obj.AESKey,
|
|
1104
|
+
Signature: obj.Signature,
|
|
1105
|
+
});
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
catch (e) {
|
|
1109
|
+
reject(e);
|
|
1110
|
+
}
|
|
1111
|
+
});
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
catch (e) {
|
|
1115
|
+
config_1.log.error('PuppetWeChat', 'uploadMedia() checkUpload exception: %s', e.message);
|
|
1116
|
+
throw e;
|
|
1117
|
+
}
|
|
1118
|
+
if (!ret.Signature) {
|
|
1119
|
+
config_1.log.error('PuppetWeChat', 'uploadMedia(): chackUpload failed to get Signature');
|
|
1120
|
+
throw new Error('chackUpload failed to get Signature');
|
|
1121
|
+
}
|
|
1122
|
+
uploadMediaRequest.Signature = ret.Signature;
|
|
1123
|
+
uploadMediaRequest.AESKey = ret.AESKey;
|
|
1124
|
+
mediaData.Signature = ret.Signature;
|
|
1125
|
+
}
|
|
1126
|
+
else {
|
|
1127
|
+
delete uploadMediaRequest.Signature;
|
|
1128
|
+
delete uploadMediaRequest.AESKey;
|
|
1129
|
+
}
|
|
1130
|
+
config_1.log.verbose('PuppetWeChat', 'uploadMedia() webwx_data_ticket: %s', webwxDataTicket);
|
|
1131
|
+
config_1.log.verbose('PuppetWeChat', 'uploadMedia() pass_ticket: %s', passTicket);
|
|
1132
|
+
/**
|
|
1133
|
+
* If FILE.SIZE > 1M, file buffer need to split for upload.
|
|
1134
|
+
* Split strategy:
|
|
1135
|
+
* BASE_LENGTH: 512 * 1024
|
|
1136
|
+
* chunks: split number
|
|
1137
|
+
* chunk: the index of chunks
|
|
1138
|
+
*/
|
|
1139
|
+
const BASE_LENGTH = 512 * 1024;
|
|
1140
|
+
const chunks = Math.ceil(buffer.length / BASE_LENGTH);
|
|
1141
|
+
const bufferData = [];
|
|
1142
|
+
for (let i = 0; i < chunks; i++) {
|
|
1143
|
+
const tempBuffer = buffer.slice(i * BASE_LENGTH, (i + 1) * BASE_LENGTH);
|
|
1144
|
+
bufferData.push(tempBuffer);
|
|
1145
|
+
}
|
|
1146
|
+
async function getMediaId(buffer, index) {
|
|
1147
|
+
const formData = {
|
|
1148
|
+
chunk: index,
|
|
1149
|
+
chunks,
|
|
1150
|
+
filename: {
|
|
1151
|
+
options: {
|
|
1152
|
+
contentType,
|
|
1153
|
+
filename,
|
|
1154
|
+
size,
|
|
1155
|
+
},
|
|
1156
|
+
value: buffer,
|
|
1157
|
+
},
|
|
1158
|
+
id,
|
|
1159
|
+
lastModifiedDate: Date().toString(),
|
|
1160
|
+
mediatype,
|
|
1161
|
+
name: filename,
|
|
1162
|
+
pass_ticket: passTicket || '',
|
|
1163
|
+
size,
|
|
1164
|
+
type: contentType,
|
|
1165
|
+
uploadmediarequest: JSON.stringify(uploadMediaRequest),
|
|
1166
|
+
webwx_data_ticket: webwxDataTicket,
|
|
1167
|
+
};
|
|
1168
|
+
try {
|
|
1169
|
+
return await new Promise((resolve, reject) => {
|
|
1170
|
+
try {
|
|
1171
|
+
request_1.default.post({
|
|
1172
|
+
formData,
|
|
1173
|
+
headers,
|
|
1174
|
+
url: uploadMediaUrl + '?f=json',
|
|
1175
|
+
}, (err, _, body) => {
|
|
1176
|
+
if (err) {
|
|
1177
|
+
reject(err);
|
|
1178
|
+
}
|
|
1179
|
+
else {
|
|
1180
|
+
let obj = body;
|
|
1181
|
+
if (typeof body !== 'object') {
|
|
1182
|
+
obj = JSON.parse(body);
|
|
1183
|
+
}
|
|
1184
|
+
resolve(obj.MediaId || '');
|
|
1185
|
+
}
|
|
1186
|
+
});
|
|
1187
|
+
}
|
|
1188
|
+
catch (e) {
|
|
1189
|
+
reject(e);
|
|
1190
|
+
}
|
|
1191
|
+
});
|
|
1192
|
+
}
|
|
1193
|
+
catch (e) {
|
|
1194
|
+
config_1.log.error('PuppetWeChat', 'uploadMedia() uploadMedia exception: %s', e.message);
|
|
1195
|
+
throw new Error('uploadMedia err: ' + e.message);
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
let mediaId = '';
|
|
1199
|
+
for (let i = 0; i < bufferData.length; i++) {
|
|
1200
|
+
mediaId = await getMediaId(bufferData[i], i);
|
|
1201
|
+
}
|
|
1202
|
+
if (!mediaId) {
|
|
1203
|
+
config_1.log.error('PuppetWeChat', 'uploadMedia(): upload fail');
|
|
1204
|
+
throw new Error('PuppetWeChat.uploadMedia(): upload fail');
|
|
1205
|
+
}
|
|
1206
|
+
return Object.assign(mediaData, { MediaId: mediaId });
|
|
1207
|
+
}
|
|
1208
|
+
async messageSendFile(conversationId, file) {
|
|
1209
|
+
config_1.log.verbose('PuppetWeChat', 'messageSendFile(%s, file=%s)', conversationId, file.toString());
|
|
1210
|
+
let mediaData;
|
|
1211
|
+
let rawPayload = {};
|
|
1212
|
+
if (!rawPayload || !rawPayload.MediaId) {
|
|
1213
|
+
try {
|
|
1214
|
+
mediaData = await this.uploadMedia(file, conversationId);
|
|
1215
|
+
rawPayload = Object.assign(rawPayload, mediaData);
|
|
1216
|
+
config_1.log.silly('PuppetWeChat', 'Upload completed, new rawObj:%s', JSON.stringify(rawPayload));
|
|
1217
|
+
}
|
|
1218
|
+
catch (e) {
|
|
1219
|
+
config_1.log.error('PuppetWeChat', 'sendMedia() exception: %s', e.message);
|
|
1220
|
+
throw e;
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
else {
|
|
1224
|
+
// To support forward file
|
|
1225
|
+
config_1.log.silly('PuppetWeChat', 'skip upload file, rawObj:%s', JSON.stringify(rawPayload));
|
|
1226
|
+
mediaData = {
|
|
1227
|
+
FileName: rawPayload.FileName,
|
|
1228
|
+
FileSize: rawPayload.FileSize,
|
|
1229
|
+
MMFileExt: rawPayload.MMFileExt,
|
|
1230
|
+
MediaId: rawPayload.MediaId,
|
|
1231
|
+
MsgType: rawPayload.MsgType,
|
|
1232
|
+
ToUserName: conversationId,
|
|
1233
|
+
};
|
|
1234
|
+
if (rawPayload.Signature) {
|
|
1235
|
+
mediaData.Signature = rawPayload.Signature;
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
// console.log('mediaData.MsgType', mediaData.MsgType)
|
|
1239
|
+
// console.log('rawObj.MsgType', message.rawObj && message.rawObj.MsgType)
|
|
1240
|
+
mediaData.MsgType = this.extToType(path_1.default.extname(file.name));
|
|
1241
|
+
config_1.log.silly('PuppetWeChat', 'sendMedia() destination: %s, mediaId: %s, MsgType; %s)', conversationId, mediaData.MediaId, mediaData.MsgType);
|
|
1242
|
+
let ret = false;
|
|
1243
|
+
try {
|
|
1244
|
+
ret = await this.bridge.sendMedia(mediaData);
|
|
1245
|
+
}
|
|
1246
|
+
catch (e) {
|
|
1247
|
+
config_1.log.error('PuppetWeChat', 'sendMedia() exception: %s', e.message);
|
|
1248
|
+
throw e;
|
|
1249
|
+
}
|
|
1250
|
+
if (!ret) {
|
|
1251
|
+
throw new Error('sendMedia fail');
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
async messageSendContact(conversationId, contactId) {
|
|
1255
|
+
config_1.log.verbose('PuppetWeChat', 'messageSend("%s", %s)', conversationId, contactId);
|
|
1256
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)();
|
|
1257
|
+
}
|
|
1258
|
+
async messageImage(messageId, imageType) {
|
|
1259
|
+
config_1.log.verbose('PuppetWeChat', 'messageImage(%s, %s)', messageId, imageType);
|
|
1260
|
+
return this.messageFile(messageId);
|
|
1261
|
+
}
|
|
1262
|
+
async messageContact(messageId) {
|
|
1263
|
+
config_1.log.verbose('PuppetWeChat', 'messageContact(%s)', messageId);
|
|
1264
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(messageId);
|
|
1265
|
+
}
|
|
1266
|
+
/**
|
|
1267
|
+
*
|
|
1268
|
+
* Tag
|
|
1269
|
+
*
|
|
1270
|
+
*/
|
|
1271
|
+
async tagContactAdd(tagId, contactId) {
|
|
1272
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(tagId, contactId);
|
|
1273
|
+
}
|
|
1274
|
+
async tagContactRemove(tagId, contactId) {
|
|
1275
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(tagId, contactId);
|
|
1276
|
+
}
|
|
1277
|
+
async tagContactDelete(tagId) {
|
|
1278
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(tagId);
|
|
1279
|
+
}
|
|
1280
|
+
async tagContactList(contactId) {
|
|
1281
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(contactId);
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
exports.PuppetWeChat = PuppetWeChat;
|
|
1285
|
+
PuppetWeChat.VERSION = config_1.VERSION;
|
|
1286
|
+
exports.default = PuppetWeChat;
|
|
1287
|
+
//# sourceMappingURL=puppet-wechat.js.map
|