@pacoyang/wechaty-puppet-wechat 0.28.6 → 0.28.8
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/dist/examples/demo.js.map +1 -1
- package/dist/scripts/retry-unit-tests.js +2 -2
- package/dist/scripts/retry-unit-tests.js.map +1 -1
- package/dist/src/bridge.d.ts +82 -4
- package/dist/src/bridge.d.ts.map +1 -1
- package/dist/src/bridge.js +282 -8
- package/dist/src/bridge.js.map +1 -1
- package/dist/src/bridge.spec.js +8 -8
- package/dist/src/bridge.spec.js.map +1 -1
- package/dist/src/config.d.ts +0 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +5 -5
- package/dist/src/config.js.map +1 -1
- package/dist/src/event.d.ts.map +1 -1
- package/dist/src/event.js +10 -2
- package/dist/src/event.js.map +1 -1
- package/dist/src/event.spec.js +1 -1
- package/dist/src/event.spec.js.map +1 -1
- package/dist/src/firer.d.ts.map +1 -1
- package/dist/src/firer.js +12 -3
- package/dist/src/firer.js.map +1 -1
- package/dist/src/firer.spec.js +4 -4
- package/dist/src/firer.spec.js.map +1 -1
- package/dist/src/mod.js +1 -1
- package/dist/src/puppet-wechat.d.ts +108 -3
- package/dist/src/puppet-wechat.d.ts.map +1 -1
- package/dist/src/puppet-wechat.js +524 -54
- package/dist/src/puppet-wechat.js.map +1 -1
- package/dist/src/puppet-wechat.spec.js +2 -2
- package/dist/src/puppet-wechat.spec.js.map +1 -1
- package/dist/src/pure-function-helpers/is-type.js +2 -3
- package/dist/src/pure-function-helpers/is-type.js.map +1 -1
- package/dist/src/pure-function-helpers/message-extname.js +1 -2
- package/dist/src/pure-function-helpers/message-extname.js.map +1 -1
- package/dist/src/pure-function-helpers/message-filename.js +2 -3
- package/dist/src/pure-function-helpers/message-filename.js.map +1 -1
- package/dist/src/pure-function-helpers/message-raw-payload-parser.js +6 -7
- package/dist/src/pure-function-helpers/message-raw-payload-parser.js.map +1 -1
- package/dist/src/pure-function-helpers/mod.js +6 -2
- package/dist/src/pure-function-helpers/mod.js.map +1 -1
- package/dist/src/pure-function-helpers/normalize-scan-status.js +1 -2
- package/dist/src/pure-function-helpers/normalize-scan-status.js.map +1 -1
- package/dist/src/pure-function-helpers/normalize-scan-status.spec.js +2 -2
- package/dist/src/pure-function-helpers/normalize-scan-status.spec.js.map +1 -1
- package/dist/src/pure-function-helpers/web-message-type.js +1 -2
- package/dist/src/pure-function-helpers/web-message-type.js.map +1 -1
- package/dist/src/pure-function-helpers/xml.js +6 -7
- package/dist/src/pure-function-helpers/xml.js.map +1 -1
- package/dist/src/session-monitor.d.ts +90 -0
- package/dist/src/session-monitor.d.ts.map +1 -0
- package/dist/src/session-monitor.js +173 -0
- package/dist/src/session-monitor.js.map +1 -0
- package/dist/src/session-monitor.spec.d.ts +3 -0
- package/dist/src/session-monitor.spec.d.ts.map +1 -0
- package/dist/src/session-monitor.spec.js +65 -0
- package/dist/src/session-monitor.spec.js.map +1 -0
- package/dist/src/version.spec.js +1 -1
- package/dist/src/version.spec.js.map +1 -1
- package/dist/src/web-schemas.js +2 -2
- package/dist/src/web-schemas.js.map +1 -1
- package/dist/src/wechaty-bro.js +58 -3
- package/dist/tests/puppeteer-contact.spec.js +1 -1
- package/dist/tests/puppeteer-contact.spec.js.map +1 -1
- package/dist/tests/puppeteer-friendship.spec.js +4 -4
- package/dist/tests/puppeteer-friendship.spec.js.map +1 -1
- package/dist/tests/puppeteer-message.spec.js +2 -2
- package/dist/tests/puppeteer-message.spec.js.map +1 -1
- package/dist/tests/puppeteer-room.spec.js +1 -1
- package/dist/tests/puppeteer-room.spec.js.map +1 -1
- package/dist/tests/puppeteer.spec.js +12 -12
- package/dist/tests/puppeteer.spec.js.map +1 -1
- package/package.json +1 -1
- package/src/bridge.ts +327 -4
- package/src/event.ts +9 -1
- package/src/firer.ts +12 -3
- package/src/puppet-wechat.ts +540 -13
- package/src/session-monitor.spec.ts +80 -0
- package/src/session-monitor.ts +219 -0
- package/src/wechaty-bro.js +58 -3
|
@@ -37,24 +37,70 @@ const wechaty_puppet_1 = require("wechaty-puppet");
|
|
|
37
37
|
const config_1 = require("./config");
|
|
38
38
|
const mod_1 = require("./pure-function-helpers/mod");
|
|
39
39
|
const bridge_1 = require("./bridge");
|
|
40
|
+
const session_monitor_1 = require("./session-monitor");
|
|
40
41
|
const event_1 = require("./event");
|
|
41
42
|
const web_schemas_1 = require("./web-schemas");
|
|
43
|
+
const HEALTH_CHECK_INTERVAL = 60 * 1000;
|
|
44
|
+
// synccheck is a long poll answered within ~25s, so minutes of silence
|
|
45
|
+
// mean the web app stopped syncing.
|
|
46
|
+
const SYNC_STALL_TIMEOUT = 5 * 60 * 1000;
|
|
47
|
+
const HEALTH_ALERT_INTERVAL = 60 * 60 * 1000;
|
|
48
|
+
// synccheck retcodes (and BaseResponse.Ret values) that mean the server ended
|
|
49
|
+
// the web session: 1100 logged out on the phone, 1101 logged in elsewhere,
|
|
50
|
+
// 1102 session invalid.
|
|
51
|
+
const LOGGED_OUT_RETCODES = ['1100', '1101', '1102'];
|
|
52
|
+
// Each message we send makes the server push a change that the page fetches
|
|
53
|
+
// with webwxsync within a second. A send with no webwxsync for this long
|
|
54
|
+
// means the session stopped receiving although synccheck keeps answering.
|
|
55
|
+
const SEND_SYNC_TIMEOUT = 10 * 60 * 1000;
|
|
56
|
+
// A single failed webwxsync or send is retried by the web app; alert only
|
|
57
|
+
// when they keep failing.
|
|
58
|
+
const API_FAILURES_ALERT = 3;
|
|
59
|
+
// The page can't be asked while it navigates (a reload, the web app's own
|
|
60
|
+
// logout redirect), so one failed check proves nothing. The application
|
|
61
|
+
// restarts the process on this alert, which can cost the login.
|
|
62
|
+
const PAGE_FAILURES_ALERT = 3;
|
|
63
|
+
// One line an hour in the log with what the session reported, so a problem
|
|
64
|
+
// noticed later can be traced back.
|
|
65
|
+
const HEALTH_LOG_INTERVAL = 60 * 60 * 1000;
|
|
66
|
+
// The web app itself ignores a webwxsync or send answering with one of
|
|
67
|
+
// LOGGED_OUT_RETCODES. By default that is only reported, since reloading a
|
|
68
|
+
// session that turns out to be fine risks its login. Set to 1 to log the bot
|
|
69
|
+
// out and reload to the QR code instead, once such answers have been seen
|
|
70
|
+
// to mean what they say.
|
|
71
|
+
const END_SESSION_ON_RET = process.env.WECHATY_PUPPET_WECHAT_END_SESSION_ON_RET === '1';
|
|
72
|
+
// A session the server refuses (see refusedSession) is left for a new login
|
|
73
|
+
// unless this is 0: then it is only reported, as before.
|
|
74
|
+
const END_REFUSED_SESSION = process.env.WECHATY_PUPPET_WECHAT_END_REFUSED_SESSION !== '0';
|
|
75
|
+
// Keep the login page quiet while waiting for a person to trigger login from
|
|
76
|
+
// the console, instead of reloading every 2 minutes in the background. Set to
|
|
77
|
+
// 1 to restore the legacy scan watchdog reload loop.
|
|
78
|
+
const SCAN_WATCHDOG_ENABLED = process.env.WECHATY_PUPPET_WECHAT_SCAN_WATCHDOG === '1';
|
|
42
79
|
class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
43
80
|
constructor(options = {}) {
|
|
44
81
|
super(options);
|
|
45
82
|
this.options = options;
|
|
83
|
+
this.healthAlertTimes = {};
|
|
84
|
+
this.pageFailures = 0;
|
|
85
|
+
this.healthLogTime = 0;
|
|
86
|
+
this.healthChecking = false;
|
|
46
87
|
this.fileId = 0;
|
|
47
88
|
this.bridge = new bridge_1.Bridge({
|
|
48
89
|
endpoint: options.endpoint || process.env.WECHATY_PUPPET_PUPPETEER_ENDPOINT,
|
|
49
90
|
extspam: options.token || process.env.WECHATY_PUPPET_WECHAT_TOKEN,
|
|
50
|
-
head: typeof options.head === 'boolean' ? options.head : config_1.envHead(),
|
|
91
|
+
head: typeof options.head === 'boolean' ? options.head : (0, config_1.envHead)(),
|
|
51
92
|
launchOptions: options.launchOptions,
|
|
52
93
|
memory: this.memory,
|
|
53
|
-
stealthless: typeof options.stealthless === 'boolean' ? options.stealthless : config_1.envStealthless(),
|
|
94
|
+
stealthless: typeof options.stealthless === 'boolean' ? options.stealthless : (0, config_1.envStealthless)(),
|
|
54
95
|
});
|
|
55
96
|
const SCAN_TIMEOUT = 2 * 60 * 1000; // 2 minutes
|
|
56
97
|
this.scanWatchdog = new watchdog_1.Watchdog(SCAN_TIMEOUT, 'Scan');
|
|
57
|
-
|
|
98
|
+
if (SCAN_WATCHDOG_ENABLED) {
|
|
99
|
+
this.initWatchdogForScan();
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
this.scanWatchdog.sleep();
|
|
103
|
+
}
|
|
58
104
|
}
|
|
59
105
|
async start() {
|
|
60
106
|
config_1.log.verbose('PuppetWeChat', `start() with ${this.memory.name}`);
|
|
@@ -84,18 +130,28 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
84
130
|
*/
|
|
85
131
|
const food = {
|
|
86
132
|
data: 'inited',
|
|
87
|
-
timeout: 2 * 60 * 1000,
|
|
133
|
+
timeout: 2 * 60 * 1000, // 2 mins for first login
|
|
88
134
|
};
|
|
89
135
|
this.emit('heartbeat', food);
|
|
90
136
|
/**
|
|
91
137
|
* Save cookie for every 5 minutes
|
|
92
138
|
*/
|
|
93
|
-
|
|
94
|
-
|
|
139
|
+
if (this.heartbeatListener) {
|
|
140
|
+
this.removeListener('heartbeat', this.heartbeatListener);
|
|
141
|
+
}
|
|
142
|
+
const throttleQueue = this.heartbeatQueue = new rx_queue_1.ThrottleQueue(5 * 60 * 1000);
|
|
143
|
+
this.heartbeatListener = data => throttleQueue.next(data);
|
|
144
|
+
this.on('heartbeat', this.heartbeatListener);
|
|
95
145
|
throttleQueue.subscribe(async (data) => {
|
|
96
146
|
config_1.log.verbose('PuppetWeChat', 'start() throttleQueue.subscribe() new item: %s', data);
|
|
97
|
-
await this.saveCookie()
|
|
147
|
+
await this.saveCookie().catch(e => {
|
|
148
|
+
config_1.log.error('PuppetWeChat', 'start() saveCookie() rejection: %s', e && e.message);
|
|
149
|
+
});
|
|
98
150
|
});
|
|
151
|
+
if (SCAN_WATCHDOG_ENABLED) {
|
|
152
|
+
this.initScanWatchdogReset();
|
|
153
|
+
}
|
|
154
|
+
this.startHealthCheck();
|
|
99
155
|
config_1.log.verbose('PuppetWeChat', 'start() done');
|
|
100
156
|
// this.emit('start')
|
|
101
157
|
return;
|
|
@@ -123,8 +179,18 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
123
179
|
// this.watchdog.sleep()
|
|
124
180
|
this.scanWatchdog.sleep();
|
|
125
181
|
// this.watchdog.removeAllListeners()
|
|
126
|
-
|
|
182
|
+
// start() adds the reset listener again
|
|
183
|
+
this.scanWatchdog.removeAllListeners('reset');
|
|
127
184
|
this.removeAllListeners('watchdog');
|
|
185
|
+
this.stopHealthCheck();
|
|
186
|
+
if (this.heartbeatListener) {
|
|
187
|
+
this.removeListener('heartbeat', this.heartbeatListener);
|
|
188
|
+
this.heartbeatListener = undefined;
|
|
189
|
+
}
|
|
190
|
+
if (this.heartbeatQueue) {
|
|
191
|
+
this.heartbeatQueue.unsubscribe();
|
|
192
|
+
this.heartbeatQueue = undefined;
|
|
193
|
+
}
|
|
128
194
|
try {
|
|
129
195
|
await this.bridge.stop();
|
|
130
196
|
// register the removeListeners micro task at then end of the task queue
|
|
@@ -170,6 +236,14 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
170
236
|
data: user,
|
|
171
237
|
type: 'logout',
|
|
172
238
|
}));
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* stop() removes the dog's reset listener, so add it on every start(),
|
|
242
|
+
* or the QR code page would stop refreshing after the first restart.
|
|
243
|
+
*/
|
|
244
|
+
initScanWatchdogReset() {
|
|
245
|
+
const dog = this.scanWatchdog;
|
|
246
|
+
dog.removeAllListeners('reset');
|
|
173
247
|
dog.on('reset', async (food, timePast) => {
|
|
174
248
|
config_1.log.warn('PuppetWeChat', 'initScanWatchdog() on(reset) lastFood: %s, timePast: %s', food.data, timePast);
|
|
175
249
|
try {
|
|
@@ -190,6 +264,386 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
190
264
|
}
|
|
191
265
|
});
|
|
192
266
|
}
|
|
267
|
+
startHealthCheck() {
|
|
268
|
+
this.stopHealthCheck();
|
|
269
|
+
this.healthTimer = setInterval(() => {
|
|
270
|
+
this.checkHealth().catch(e => {
|
|
271
|
+
config_1.log.error('PuppetWeChat', 'checkHealth() rejection: %s', e && e.message);
|
|
272
|
+
});
|
|
273
|
+
}, HEALTH_CHECK_INTERVAL);
|
|
274
|
+
}
|
|
275
|
+
stopHealthCheck() {
|
|
276
|
+
if (this.healthTimer) {
|
|
277
|
+
clearInterval(this.healthTimer);
|
|
278
|
+
this.healthTimer = undefined;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* What the page and its cookies say about the session, for monitoring and
|
|
283
|
+
* for people looking into a problem. Cookie values are never included.
|
|
284
|
+
*/
|
|
285
|
+
async healthReport() {
|
|
286
|
+
const report = {
|
|
287
|
+
cookieNames: [],
|
|
288
|
+
hasDataTicket: false,
|
|
289
|
+
hasPhoneTicket: false,
|
|
290
|
+
lastSyncCheckFailure: this.lastSyncCheckFailure,
|
|
291
|
+
loginPage: null,
|
|
292
|
+
logonoff: this.logonoff(),
|
|
293
|
+
page: null,
|
|
294
|
+
pageAlive: false,
|
|
295
|
+
session: this.bridge.monitor.health(),
|
|
296
|
+
};
|
|
297
|
+
try {
|
|
298
|
+
report.page = await this.bridge.health();
|
|
299
|
+
report.pageAlive = true;
|
|
300
|
+
}
|
|
301
|
+
catch (e) {
|
|
302
|
+
report.error = (e && e.message) || String(e);
|
|
303
|
+
return report;
|
|
304
|
+
}
|
|
305
|
+
try {
|
|
306
|
+
const cookieList = await this.bridge.cookies();
|
|
307
|
+
report.cookieNames = cookieList.map(c => c.name);
|
|
308
|
+
report.hasDataTicket = cookieList.some(c => c.name === 'webwx_data_ticket' && !!c.value);
|
|
309
|
+
report.hasPhoneTicket = cookieList.some(c => c.name === 'wxuin' && !!c.value)
|
|
310
|
+
&& cookieList.some(c => c.name === 'webwx_auth_ticket' && !!c.value);
|
|
311
|
+
}
|
|
312
|
+
catch (e) {
|
|
313
|
+
report.error = (e && e.message) || String(e);
|
|
314
|
+
}
|
|
315
|
+
if (!report.logonoff) {
|
|
316
|
+
try {
|
|
317
|
+
report.loginPage = await this.bridge.loginPageState();
|
|
318
|
+
}
|
|
319
|
+
catch (e) {
|
|
320
|
+
// page may be navigating
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return report;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* The page never reports a logout by itself: a session the server ended
|
|
327
|
+
* used to look logged in until something reloaded the page. Check what the
|
|
328
|
+
* page sees once a minute and act on it.
|
|
329
|
+
*/
|
|
330
|
+
async checkHealth() {
|
|
331
|
+
if (this.state.off() || !this.logonoff()) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
// a page that takes long to answer must not pile up checks
|
|
335
|
+
if (this.healthChecking) {
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
this.healthChecking = true;
|
|
339
|
+
try {
|
|
340
|
+
await this.checkHealthOnce();
|
|
341
|
+
}
|
|
342
|
+
finally {
|
|
343
|
+
this.healthChecking = false;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
async checkHealthOnce() {
|
|
347
|
+
const report = await this.healthReport();
|
|
348
|
+
if (!report.pageAlive) {
|
|
349
|
+
this.pageFailures++;
|
|
350
|
+
const current = this.bridge.currentPage();
|
|
351
|
+
const closed = !current || current.isClosed();
|
|
352
|
+
config_1.log.warn('PuppetWeChat', 'checkHealth() page did not answer (%s in a row, closed: %s): %s', this.pageFailures, closed, report.error);
|
|
353
|
+
if (closed || this.pageFailures >= PAGE_FAILURES_ALERT) {
|
|
354
|
+
this.healthAlert('page', `page gone: ${report.error}`);
|
|
355
|
+
}
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
this.pageFailures = 0;
|
|
359
|
+
this.healthClear('page');
|
|
360
|
+
const session = report.session;
|
|
361
|
+
const now = session.now;
|
|
362
|
+
const sync = session.syncCheck;
|
|
363
|
+
const minutesSince = (time) => Math.round((now - time) / 60000);
|
|
364
|
+
if (now - this.healthLogTime > HEALTH_LOG_INTERVAL) {
|
|
365
|
+
this.healthLogTime = now;
|
|
366
|
+
const ago = (time) => time ? `${Math.round((now - time) / 1000)}s ago` : 'never';
|
|
367
|
+
config_1.log.info('PuppetWeChat', 'health: synccheck %s %s, webwxsync %s %s, send %s %s, received %s, rooms %s, ticket %s', sync ? sync.retcode : '-', ago(sync && sync.time), session.lastSync ? session.lastSync.ret : '-', ago(session.lastSync && session.lastSync.time), session.lastSend ? session.lastSend.ret : '-', ago(session.lastSend && session.lastSend.time), ago(session.lastReceived), report.page ? report.page.roomCount : '-', report.hasDataTicket);
|
|
368
|
+
}
|
|
369
|
+
const ended = [session.lastSync, session.lastSend].find(a => a && LOGGED_OUT_RETCODES.includes(a.ret));
|
|
370
|
+
if (ended) {
|
|
371
|
+
if (END_SESSION_ON_RET) {
|
|
372
|
+
await this.sessionEnded(`${ended.api} Ret ${ended.ret}`);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
this.healthAlert('session', `${ended.api} answered Ret ${ended.ret}: WeChat reports the session as ended while the page still shows it logged in. Reload the page to get a QR code.`);
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
this.healthClear('session');
|
|
379
|
+
}
|
|
380
|
+
const refused = (0, session_monitor_1.refusedSession)(session);
|
|
381
|
+
if (refused) {
|
|
382
|
+
if (END_REFUSED_SESSION) {
|
|
383
|
+
await this.sessionEnded(`the server refuses the session: ${refused}`);
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
this.healthAlert('refused', `the server refuses the session: ${refused}. Messages are neither sent nor received.`);
|
|
387
|
+
}
|
|
388
|
+
else {
|
|
389
|
+
this.healthClear('refused');
|
|
390
|
+
}
|
|
391
|
+
// before the first answer, count from the login
|
|
392
|
+
const lastSyncCheck = sync ? sync.time : session.since;
|
|
393
|
+
if (now - lastSyncCheck > SYNC_STALL_TIMEOUT) {
|
|
394
|
+
this.healthAlert('sync', sync
|
|
395
|
+
? `synccheck stalled: no answer for ${minutesSince(lastSyncCheck)} minutes`
|
|
396
|
+
: `synccheck stalled: none answered in the ${minutesSince(lastSyncCheck)} minutes since login`);
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
this.healthClear('sync');
|
|
400
|
+
}
|
|
401
|
+
for (const [kind, answer] of [['webwxsync', session.lastSync], ['send', session.lastSend]]) {
|
|
402
|
+
if (answer && answer.failures >= API_FAILURES_ALERT) {
|
|
403
|
+
// When automatic ending of refused sessions is on, stay quiet during
|
|
404
|
+
// the 3-minute refusal grace window instead of sending a noisy API
|
|
405
|
+
// alert right before sessionEnded(). If failures stay below the
|
|
406
|
+
// refusal count past REFUSED_AFTER, alert normally.
|
|
407
|
+
const inRefusalGrace = END_REFUSED_SESSION
|
|
408
|
+
&& (0, session_monitor_1.isRefusalCandidate)(answer)
|
|
409
|
+
&& answer.failingSince !== null
|
|
410
|
+
&& (now - answer.failingSince) < session_monitor_1.REFUSED_AFTER;
|
|
411
|
+
if (inRefusalGrace) {
|
|
412
|
+
config_1.log.warn('PuppetWeChat', 'health: %s failing with Ret %s (%s in a row), waiting for refusal threshold', answer.api, answer.ret, answer.failures);
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
this.healthAlert(kind, `${answer.api} keeps failing: ${answer.ret}, ${answer.failures} times in a row`);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
else {
|
|
419
|
+
this.healthClear(kind);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
const sent = session.sentWithoutSync;
|
|
423
|
+
if (sent && now - sent > SEND_SYNC_TIMEOUT) {
|
|
424
|
+
const received = session.lastReceived
|
|
425
|
+
? `, last message received ${minutesSince(session.lastReceived)} minutes ago`
|
|
426
|
+
: '';
|
|
427
|
+
this.healthAlert('receive', `not receiving messages: nothing came from the server in the ${minutesSince(sent)} minutes since a message was sent${received}`);
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
this.healthClear('receive');
|
|
431
|
+
}
|
|
432
|
+
if (!report.hasDataTicket) {
|
|
433
|
+
this.healthAlert('ticket', 'webwx_data_ticket missing: files cannot be sent until the next login');
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
this.healthClear('ticket');
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Log every time, emit an error at most once an hour per kind, so the
|
|
441
|
+
* application can notify someone without flooding them.
|
|
442
|
+
*/
|
|
443
|
+
healthAlert(kind, message) {
|
|
444
|
+
config_1.log.warn('PuppetWeChat', 'health: %s', message);
|
|
445
|
+
const last = this.healthAlertTimes[kind];
|
|
446
|
+
if (last && Date.now() - last < HEALTH_ALERT_INTERVAL) {
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
this.healthAlertTimes[kind] = Date.now();
|
|
450
|
+
this.emit('error', { data: `health: ${message}` });
|
|
451
|
+
}
|
|
452
|
+
healthClear(kind) {
|
|
453
|
+
if (this.healthAlertTimes[kind]) {
|
|
454
|
+
config_1.log.info('PuppetWeChat', 'health: %s recovered', kind);
|
|
455
|
+
delete this.healthAlertTimes[kind];
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* The server ended the session: mark the bot logged out with the reason.
|
|
460
|
+
* The web app goes to its login page by itself after a synccheck retcode.
|
|
461
|
+
* It stays where it is after a failing webwxsync or send: then the
|
|
462
|
+
* session's cookies are dropped, which brings the login page (a plain
|
|
463
|
+
* reload would bring back the same session), where the page waits quietly
|
|
464
|
+
* until a person triggers login confirmation or a QR code from the console.
|
|
465
|
+
*/
|
|
466
|
+
async sessionEnded(reason, reload = true) {
|
|
467
|
+
if (!this.id) {
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
config_1.log.warn('PuppetWeChat', 'sessionEnded() the server ended the web session: %s', reason);
|
|
471
|
+
this.logoutLocally(reason);
|
|
472
|
+
if (!reload) {
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
try {
|
|
476
|
+
await this.bridge.dropSession();
|
|
477
|
+
await this.saveCookie().catch(e => {
|
|
478
|
+
config_1.log.error('PuppetWeChat', 'sessionEnded() saveCookie() rejection: %s', e && e.message);
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
catch (e) {
|
|
482
|
+
config_1.log.error('PuppetWeChat', 'sessionEnded() dropSession() rejection: %s', e && e.message);
|
|
483
|
+
try {
|
|
484
|
+
await this.bridge.reload();
|
|
485
|
+
}
|
|
486
|
+
catch (e) {
|
|
487
|
+
config_1.log.error('PuppetWeChat', 'sessionEnded() reload rejection: %s', e && e.message);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
async waitForLoginPage(predicate = () => true, timeoutMs = 8000) {
|
|
492
|
+
const deadline = Date.now() + timeoutMs;
|
|
493
|
+
let last = null;
|
|
494
|
+
while (Date.now() < deadline) {
|
|
495
|
+
if (this.logonoff()) {
|
|
496
|
+
return null;
|
|
497
|
+
}
|
|
498
|
+
try {
|
|
499
|
+
last = await this.bridge.loginPageState();
|
|
500
|
+
if (last && predicate(last)) {
|
|
501
|
+
return last;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
catch (e) {
|
|
505
|
+
// page may still be navigating
|
|
506
|
+
}
|
|
507
|
+
await new Promise(resolve => setTimeout(resolve, 400));
|
|
508
|
+
}
|
|
509
|
+
return last;
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Drop the current web session (even if the page still thinks it is logged
|
|
513
|
+
* in) and land on the login page without automatically nagging the phone.
|
|
514
|
+
*/
|
|
515
|
+
async forceRelogin(preferQrcode = false) {
|
|
516
|
+
if (this.id) {
|
|
517
|
+
this.logoutLocally('manual relogin');
|
|
518
|
+
}
|
|
519
|
+
await this.bridge.dropSession(preferQrcode);
|
|
520
|
+
await this.saveCookie().catch(e => {
|
|
521
|
+
config_1.log.error('PuppetWeChat', 'forceRelogin() saveCookie() rejection: %s', e && e.message);
|
|
522
|
+
});
|
|
523
|
+
return this.waitForLoginPage();
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Trigger a one-tap phone login confirmation on demand. If the server
|
|
527
|
+
* refuses or no phone auth ticket is present, falls back to a fresh QR code
|
|
528
|
+
* and reports which mode is now active on the login page.
|
|
529
|
+
*/
|
|
530
|
+
async requestPhoneConfirm() {
|
|
531
|
+
if (this.id) {
|
|
532
|
+
await this.forceRelogin(false);
|
|
533
|
+
}
|
|
534
|
+
let state = await this.waitForLoginPage(() => true, 4000);
|
|
535
|
+
if (!state) {
|
|
536
|
+
state = await this.forceRelogin(false);
|
|
537
|
+
}
|
|
538
|
+
if (!state) {
|
|
539
|
+
throw new Error('Login page is not ready yet');
|
|
540
|
+
}
|
|
541
|
+
if (!state.canConfirmOnPhone) {
|
|
542
|
+
const offered = await this.bridge.offerConfirmOnPhone();
|
|
543
|
+
if (!offered) {
|
|
544
|
+
const qr = await this.requestLoginQrcode();
|
|
545
|
+
return {
|
|
546
|
+
canConfirmOnPhone: false,
|
|
547
|
+
mode: 'qrcode',
|
|
548
|
+
qrcodeUrl: qr.qrcodeUrl,
|
|
549
|
+
waitingForPhone: false,
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
await this.bridge.reload();
|
|
553
|
+
state = await this.waitForLoginPage(s => s.canConfirmOnPhone || s.hasQrcode);
|
|
554
|
+
}
|
|
555
|
+
if (!state || !state.canConfirmOnPhone) {
|
|
556
|
+
const qr = await this.requestLoginQrcode();
|
|
557
|
+
return {
|
|
558
|
+
canConfirmOnPhone: false,
|
|
559
|
+
mode: 'qrcode',
|
|
560
|
+
qrcodeUrl: qr.qrcodeUrl,
|
|
561
|
+
waitingForPhone: false,
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
await this.bridge.pushLoginConfirm();
|
|
565
|
+
// Give webwxpushloginurl a moment to either start waiting on the phone or
|
|
566
|
+
// fall back to showing a QR code if the server refused the auth ticket.
|
|
567
|
+
await new Promise(resolve => setTimeout(resolve, 1500));
|
|
568
|
+
if (this.logonoff()) {
|
|
569
|
+
return {
|
|
570
|
+
canConfirmOnPhone: true,
|
|
571
|
+
mode: 'phone',
|
|
572
|
+
qrcodeUrl: null,
|
|
573
|
+
waitingForPhone: false,
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
const after = await this.waitForLoginPage(s => s.waitingForPhone || s.hasQrcode, 4000) || state;
|
|
577
|
+
if (this.logonoff() || after.waitingForPhone) {
|
|
578
|
+
return {
|
|
579
|
+
canConfirmOnPhone: true,
|
|
580
|
+
mode: 'phone',
|
|
581
|
+
qrcodeUrl: null,
|
|
582
|
+
waitingForPhone: !this.logonoff(),
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
return {
|
|
586
|
+
canConfirmOnPhone: !!after.canConfirmOnPhone,
|
|
587
|
+
mode: 'qrcode',
|
|
588
|
+
qrcodeUrl: after.qrcodeUrl,
|
|
589
|
+
waitingForPhone: false,
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* Show a fresh QR code on the login page on demand (dropping the current
|
|
594
|
+
* session first if still logged in).
|
|
595
|
+
*/
|
|
596
|
+
async requestLoginQrcode() {
|
|
597
|
+
if (this.id) {
|
|
598
|
+
await this.forceRelogin(true);
|
|
599
|
+
}
|
|
600
|
+
else {
|
|
601
|
+
await this.bridge.showLoginQrcode();
|
|
602
|
+
}
|
|
603
|
+
const state = await this.waitForLoginPage(s => s.hasQrcode && !!s.qrcodeUrl);
|
|
604
|
+
return { qrcodeUrl: state ? state.qrcodeUrl : null };
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* A failed webwxsync or send answer, as the bridge saw it. Remember it for
|
|
608
|
+
* the logout reason; the minute's check decides what to report.
|
|
609
|
+
*/
|
|
610
|
+
onApiRet(data) {
|
|
611
|
+
config_1.log.warn('PuppetWeChat', 'onApiRet() %s answered %s (%s in a row)', data.api, data.ret, data.failures);
|
|
612
|
+
this.lastApiFailure = data;
|
|
613
|
+
if (LOGGED_OUT_RETCODES.includes(data.ret)) {
|
|
614
|
+
// don't wait for the minute to pass
|
|
615
|
+
this.checkHealth().catch(e => {
|
|
616
|
+
config_1.log.error('PuppetWeChat', 'onApiRet() checkHealth() rejection: %s', e && e.message);
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
onSyncCheck(data) {
|
|
621
|
+
config_1.log.warn('PuppetWeChat', 'onSyncCheck() retcode %s selector %s', data.retcode, data.selector);
|
|
622
|
+
this.lastSyncCheckFailure = { ...data, time: Date.now() };
|
|
623
|
+
if (LOGGED_OUT_RETCODES.includes(data.retcode)) {
|
|
624
|
+
// act now rather than at the next minute's check; the web app is
|
|
625
|
+
// already on its way to the login page
|
|
626
|
+
this.sessionEnded(`synccheck retcode ${data.retcode}`, false).catch(e => {
|
|
627
|
+
config_1.log.error('PuppetWeChat', 'onSyncCheck() sessionEnded() rejection: %s', e && e.message);
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* Mark the bot logged out without telling the page to log out: used when
|
|
633
|
+
* the server has already ended the session.
|
|
634
|
+
*/
|
|
635
|
+
logoutLocally(reason) {
|
|
636
|
+
const user = this.id;
|
|
637
|
+
if (!user) {
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
this.id = undefined;
|
|
641
|
+
this.emit('logout', { contactId: user, data: reason });
|
|
642
|
+
}
|
|
643
|
+
async roomTopicList() {
|
|
644
|
+
const list = await this.bridge.roomTopicList();
|
|
645
|
+
return list.map(r => ({ ...r, topic: (0, mod_1.plainText)(r.topic || '') }));
|
|
646
|
+
}
|
|
193
647
|
async initBridge() {
|
|
194
648
|
config_1.log.verbose('PuppetWeChat', 'initBridge()');
|
|
195
649
|
if (this.state.off()) {
|
|
@@ -206,6 +660,8 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
206
660
|
this.bridge.on('logout', event_1.Event.onLogout.bind(this));
|
|
207
661
|
this.bridge.on('message', event_1.Event.onMessage.bind(this));
|
|
208
662
|
this.bridge.on('scan', event_1.Event.onScan.bind(this));
|
|
663
|
+
this.bridge.on('synccheck', this.onSyncCheck.bind(this));
|
|
664
|
+
this.bridge.on('apiret', this.onApiRet.bind(this));
|
|
209
665
|
this.bridge.on('unload', event_1.Event.onUnload.bind(this));
|
|
210
666
|
try {
|
|
211
667
|
await this.bridge.start();
|
|
@@ -250,11 +706,11 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
250
706
|
}
|
|
251
707
|
async messageRawPayloadParser(rawPayload) {
|
|
252
708
|
config_1.log.verbose('PuppetWeChat', 'messageRawPayloadParser(%s) @ %s', rawPayload, this);
|
|
253
|
-
const payload = mod_1.messageRawPayloadParser(rawPayload);
|
|
709
|
+
const payload = (0, mod_1.messageRawPayloadParser)(rawPayload);
|
|
254
710
|
return payload;
|
|
255
711
|
}
|
|
256
712
|
async messageRecall(messageId) {
|
|
257
|
-
return wechaty_puppet_1.throwUnsupportedError(messageId);
|
|
713
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(messageId);
|
|
258
714
|
}
|
|
259
715
|
async messageFile(messageId) {
|
|
260
716
|
const rawPayload = await this.messageRawPayload(messageId);
|
|
@@ -262,11 +718,11 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
262
718
|
return fileBox;
|
|
263
719
|
}
|
|
264
720
|
async messageUrl(messageId) {
|
|
265
|
-
return wechaty_puppet_1.throwUnsupportedError(messageId);
|
|
721
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(messageId);
|
|
266
722
|
}
|
|
267
723
|
async messageMiniProgram(messageId) {
|
|
268
724
|
config_1.log.verbose('PuppetWeChat', 'messageMiniProgram(%s)', messageId);
|
|
269
|
-
return wechaty_puppet_1.throwUnsupportedError(messageId);
|
|
725
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(messageId);
|
|
270
726
|
}
|
|
271
727
|
async messageRawPayloadToFile(rawPayload) {
|
|
272
728
|
let url = await this.messageRawPayloadToUrl(rawPayload);
|
|
@@ -276,7 +732,7 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
276
732
|
// use http instead of https, because https will only success on the very first request!
|
|
277
733
|
url = url.replace(/^https/i, 'http');
|
|
278
734
|
const parsedUrl = new url_1.default.URL(url);
|
|
279
|
-
const msgFileName = mod_1.messageFilename(rawPayload);
|
|
735
|
+
const msgFileName = (0, mod_1.messageFilename)(rawPayload);
|
|
280
736
|
if (!msgFileName) {
|
|
281
737
|
throw new Error('no filename');
|
|
282
738
|
}
|
|
@@ -286,12 +742,12 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
286
742
|
// 'Accept-Encoding': 'gzip, deflate, sdch',
|
|
287
743
|
// 'Accept-Encoding': 'gzip, deflate, sdch, br', // MsgType.IMAGE | VIDEO
|
|
288
744
|
'Accept-Encoding': 'identity;q=1, *;q=0',
|
|
289
|
-
'Accept-Language': 'zh-CN,zh;q=0.8',
|
|
745
|
+
'Accept-Language': 'zh-CN,zh;q=0.8', // MsgType.IMAGE | VIDEO
|
|
290
746
|
// 'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.6,en-US;q=0.4,en;q=0.2',
|
|
291
747
|
Cookie: cookies.map(c => `${c.name}=${c.value}`).join('; '),
|
|
292
748
|
// Accept: 'image/webp,image/*,*/*;q=0.8',
|
|
293
749
|
// Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', // MsgType.IMAGE | VIDEO
|
|
294
|
-
Host: parsedUrl.hostname,
|
|
750
|
+
Host: parsedUrl.hostname, // 'wx.qq.com', // MsgType.VIDEO | IMAGE
|
|
295
751
|
Range: 'bytes=0-',
|
|
296
752
|
// Referer: protocol + '//wx.qq.com/',
|
|
297
753
|
Referer: url,
|
|
@@ -306,11 +762,11 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
306
762
|
return fileBox;
|
|
307
763
|
}
|
|
308
764
|
async messageSendUrl(conversationId, urlLinkPayload) {
|
|
309
|
-
wechaty_puppet_1.throwUnsupportedError(conversationId, urlLinkPayload);
|
|
765
|
+
(0, wechaty_puppet_1.throwUnsupportedError)(conversationId, urlLinkPayload);
|
|
310
766
|
}
|
|
311
767
|
async messageSendMiniProgram(conversationId, miniProgramPayload) {
|
|
312
768
|
config_1.log.verbose('PuppetWeChat', 'messageSendMiniProgram("%s", %s)', conversationId, JSON.stringify(miniProgramPayload));
|
|
313
|
-
wechaty_puppet_1.throwUnsupportedError(conversationId, miniProgramPayload);
|
|
769
|
+
(0, wechaty_puppet_1.throwUnsupportedError)(conversationId, miniProgramPayload);
|
|
314
770
|
}
|
|
315
771
|
/**
|
|
316
772
|
* TODO: Test this function if it could work...
|
|
@@ -348,8 +804,8 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
348
804
|
// In room msg, the content prefix sender:, need to be removed,
|
|
349
805
|
// otherwise the forwarded sender will display the source message sender,
|
|
350
806
|
// causing self () to determine the error
|
|
351
|
-
newMsg.Content = mod_1.unescapeHtml(rawPayload.Content.replace(/^@\w+:<br\/>/, '')).replace(/^[\w-]+:<br\/>/, '');
|
|
352
|
-
newMsg.MMIsChatRoom = mod_1.isRoomId(conversationId);
|
|
807
|
+
newMsg.Content = (0, mod_1.unescapeHtml)(rawPayload.Content.replace(/^@\w+:<br\/>/, '')).replace(/^[\w-]+:<br\/>/, '');
|
|
808
|
+
newMsg.MMIsChatRoom = (0, mod_1.isRoomId)(conversationId);
|
|
353
809
|
// The following parameters need to be overridden after calling createMessage()
|
|
354
810
|
rawPayload = Object.assign(rawPayload, newMsg);
|
|
355
811
|
// for (let i = 0; i < sendToList.length; i++) {
|
|
@@ -364,8 +820,8 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
364
820
|
const patchData = newMsg;
|
|
365
821
|
try {
|
|
366
822
|
const ret = await this.bridge.forward(baseData, patchData);
|
|
367
|
-
if (
|
|
368
|
-
throw new Error('forward failed');
|
|
823
|
+
if (ret !== true) {
|
|
824
|
+
throw new Error('forward failed: ' + (ret || 'page error') + this.bridge.lastSendFailure());
|
|
369
825
|
}
|
|
370
826
|
}
|
|
371
827
|
catch (e) {
|
|
@@ -384,13 +840,18 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
384
840
|
}
|
|
385
841
|
}
|
|
386
842
|
async login(userId) {
|
|
843
|
+
// failures of the session before this one say nothing about a later logout
|
|
844
|
+
this.lastSyncCheckFailure = undefined;
|
|
845
|
+
this.lastApiFailure = undefined;
|
|
846
|
+
this.pageFailures = 0;
|
|
847
|
+
this.bridge.monitor.reset(userId);
|
|
387
848
|
return super.login(userId);
|
|
388
849
|
}
|
|
389
850
|
/**
|
|
390
851
|
* logout from browser, then server will emit `logout` event
|
|
391
852
|
*/
|
|
392
|
-
async logout() {
|
|
393
|
-
config_1.log.verbose('PuppetWeChat', 'logout()');
|
|
853
|
+
async logout(reason = 'logout()') {
|
|
854
|
+
config_1.log.verbose('PuppetWeChat', 'logout(%s)', reason);
|
|
394
855
|
const user = this.selfId();
|
|
395
856
|
if (!user) {
|
|
396
857
|
config_1.log.warn('PuppetWeChat', 'logout() without self()');
|
|
@@ -405,7 +866,7 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
405
866
|
}
|
|
406
867
|
finally {
|
|
407
868
|
this.id = undefined;
|
|
408
|
-
this.emit('logout', { contactId: user, data:
|
|
869
|
+
this.emit('logout', { contactId: user, data: reason });
|
|
409
870
|
}
|
|
410
871
|
}
|
|
411
872
|
/**
|
|
@@ -415,13 +876,13 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
415
876
|
*
|
|
416
877
|
*/
|
|
417
878
|
async contactSelfQRCode() {
|
|
418
|
-
return wechaty_puppet_1.throwUnsupportedError();
|
|
879
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)();
|
|
419
880
|
}
|
|
420
881
|
async contactSelfName(name) {
|
|
421
|
-
return wechaty_puppet_1.throwUnsupportedError(name);
|
|
882
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(name);
|
|
422
883
|
}
|
|
423
884
|
async contactSelfSignature(signature) {
|
|
424
|
-
return wechaty_puppet_1.throwUnsupportedError(signature);
|
|
885
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(signature);
|
|
425
886
|
}
|
|
426
887
|
/**
|
|
427
888
|
*
|
|
@@ -454,16 +915,16 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
454
915
|
// MMActualSender??? MMPeerUserName??? `getUserContact(message.MMActualSender,message.MMPeerUserName).HeadImgUrl`
|
|
455
916
|
// uin: rawPayload.Uin, // stable id: 4763975 || getCookie("wxuin")
|
|
456
917
|
return {
|
|
457
|
-
address: rawPayload.Alias,
|
|
918
|
+
address: rawPayload.Alias, // XXX: need a stable address for user
|
|
458
919
|
alias: rawPayload.RemarkName,
|
|
459
920
|
avatar: rawPayload.HeadImgUrl,
|
|
460
921
|
city: rawPayload.City,
|
|
461
922
|
friend: rawPayload.stranger === undefined
|
|
462
923
|
? undefined
|
|
463
|
-
: !rawPayload.stranger,
|
|
924
|
+
: !rawPayload.stranger, // assign by injectio.js
|
|
464
925
|
gender: rawPayload.Sex,
|
|
465
926
|
id: rawPayload.UserName,
|
|
466
|
-
name: mod_1.plainText(rawPayload.NickName || ''),
|
|
927
|
+
name: (0, mod_1.plainText)(rawPayload.NickName || ''),
|
|
467
928
|
province: rawPayload.Province,
|
|
468
929
|
signature: rawPayload.Signature,
|
|
469
930
|
star: !!rawPayload.StarFriend,
|
|
@@ -476,7 +937,7 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
476
937
|
type: (!!rawPayload.UserName && !rawPayload.UserName.startsWith('@@') && !!(rawPayload.VerifyFlag & 8))
|
|
477
938
|
? wechaty_puppet_1.ContactType.Official
|
|
478
939
|
: wechaty_puppet_1.ContactType.Personal,
|
|
479
|
-
weixin: rawPayload.Alias,
|
|
940
|
+
weixin: rawPayload.Alias, // Wechat ID
|
|
480
941
|
};
|
|
481
942
|
}
|
|
482
943
|
ding(data) {
|
|
@@ -617,7 +1078,11 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
617
1078
|
adminIdList: [],
|
|
618
1079
|
id,
|
|
619
1080
|
memberIdList,
|
|
620
|
-
topic: mod_1.plainText(rawPayload.NickName || ''),
|
|
1081
|
+
topic: (0, mod_1.plainText)(rawPayload.NickName || ''),
|
|
1082
|
+
// aliasDict,
|
|
1083
|
+
// nameMap,
|
|
1084
|
+
// roomAliasMap,
|
|
1085
|
+
// contactAliasMap,
|
|
621
1086
|
};
|
|
622
1087
|
// console.log(roomPayload)
|
|
623
1088
|
return roomPayload;
|
|
@@ -643,7 +1108,7 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
643
1108
|
return wechaty_puppet_1.FileBox.fromUrl(payload.avatar);
|
|
644
1109
|
}
|
|
645
1110
|
config_1.log.warn('PuppetWeChat', 'roomAvatar() avatar not found, use the chatie default.');
|
|
646
|
-
return config_1.qrCodeForChatie();
|
|
1111
|
+
return (0, config_1.qrCodeForChatie)();
|
|
647
1112
|
}
|
|
648
1113
|
async roomAdd(roomId, contactId) {
|
|
649
1114
|
try {
|
|
@@ -691,7 +1156,7 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
691
1156
|
config_1.log.warn('PuppetWeChat', 'roomQuit(%s) not supported by Web API', roomId);
|
|
692
1157
|
}
|
|
693
1158
|
async roomQRCode(roomId) {
|
|
694
|
-
return wechaty_puppet_1.throwUnsupportedError(roomId);
|
|
1159
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(roomId);
|
|
695
1160
|
}
|
|
696
1161
|
async roomMemberList(roomId) {
|
|
697
1162
|
config_1.log.verbose('PuppetWeChat', 'roommemberList(%s)', roomId);
|
|
@@ -728,13 +1193,13 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
728
1193
|
*
|
|
729
1194
|
*/
|
|
730
1195
|
async roomInvitationAccept(roomInvitationId) {
|
|
731
|
-
return wechaty_puppet_1.throwUnsupportedError(roomInvitationId);
|
|
1196
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(roomInvitationId);
|
|
732
1197
|
}
|
|
733
1198
|
async roomInvitationRawPayload(roomInvitationId) {
|
|
734
|
-
return wechaty_puppet_1.throwUnsupportedError(roomInvitationId);
|
|
1199
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(roomInvitationId);
|
|
735
1200
|
}
|
|
736
1201
|
async roomInvitationRawPayloadParser(rawPayload) {
|
|
737
|
-
return wechaty_puppet_1.throwUnsupportedError(rawPayload);
|
|
1202
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(rawPayload);
|
|
738
1203
|
}
|
|
739
1204
|
/**
|
|
740
1205
|
*
|
|
@@ -785,10 +1250,10 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
785
1250
|
}
|
|
786
1251
|
}
|
|
787
1252
|
async friendshipSearchPhone(phone) {
|
|
788
|
-
throw wechaty_puppet_1.throwUnsupportedError(phone);
|
|
1253
|
+
throw (0, wechaty_puppet_1.throwUnsupportedError)(phone);
|
|
789
1254
|
}
|
|
790
1255
|
async friendshipSearchWeixin(weixin) {
|
|
791
|
-
throw wechaty_puppet_1.throwUnsupportedError(weixin);
|
|
1256
|
+
throw (0, wechaty_puppet_1.throwUnsupportedError)(weixin);
|
|
792
1257
|
}
|
|
793
1258
|
async friendshipAdd(contactId, hello) {
|
|
794
1259
|
try {
|
|
@@ -981,13 +1446,13 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
981
1446
|
case '.jpg':
|
|
982
1447
|
case '.png':
|
|
983
1448
|
case '.gif':
|
|
984
|
-
mediatype = "pic" /* Image */;
|
|
1449
|
+
mediatype = "pic" /* WebMediaType.Image */;
|
|
985
1450
|
break;
|
|
986
1451
|
case '.mp4':
|
|
987
|
-
mediatype = "video" /* Video */;
|
|
1452
|
+
mediatype = "video" /* WebMediaType.Video */;
|
|
988
1453
|
break;
|
|
989
1454
|
default:
|
|
990
|
-
mediatype = "doc" /* Attachment */;
|
|
1455
|
+
mediatype = "doc" /* WebMediaType.Attachment */;
|
|
991
1456
|
}
|
|
992
1457
|
const buffer = await new Promise((resolve, reject) => {
|
|
993
1458
|
file.pipe(new bl_1.default((err, data) => {
|
|
@@ -1003,13 +1468,13 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
1003
1468
|
const MAX_FILE_SIZE = 100 * 1024 * 1024;
|
|
1004
1469
|
const LARGE_FILE_SIZE = 25 * 1024 * 1024;
|
|
1005
1470
|
const MAX_VIDEO_SIZE = 20 * 1024 * 1024;
|
|
1006
|
-
if (mediatype === "video" /* Video */ && buffer.length > MAX_VIDEO_SIZE) {
|
|
1471
|
+
if (mediatype === "video" /* WebMediaType.Video */ && buffer.length > MAX_VIDEO_SIZE) {
|
|
1007
1472
|
throw new Error(`Sending video files is not allowed to exceed ${MAX_VIDEO_SIZE / 1024 / 1024}MB`);
|
|
1008
1473
|
}
|
|
1009
1474
|
if (buffer.length > MAX_FILE_SIZE) {
|
|
1010
1475
|
throw new Error(`Sending files is not allowed to exceed ${MAX_FILE_SIZE / 1024 / 1024}MB`);
|
|
1011
1476
|
}
|
|
1012
|
-
const fileMd5 = md5_1.default(buffer);
|
|
1477
|
+
const fileMd5 = (0, md5_1.default)(buffer);
|
|
1013
1478
|
const baseRequest = await this.getBaseRequest();
|
|
1014
1479
|
const passTicket = await this.bridge.getPassticket();
|
|
1015
1480
|
const uploadMediaUrl = await this.bridge.getUploadMediaUrl();
|
|
@@ -1017,6 +1482,11 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
1017
1482
|
const cookie = await this.bridge.cookies();
|
|
1018
1483
|
const first = cookie.find(c => c.name === 'webwx_data_ticket');
|
|
1019
1484
|
const webwxDataTicket = first && first.value;
|
|
1485
|
+
if (!webwxDataTicket) {
|
|
1486
|
+
// without it the upload form can't even be built
|
|
1487
|
+
this.healthAlert('ticket', 'webwx_data_ticket missing: files cannot be sent until the next login');
|
|
1488
|
+
throw new Error('webwx_data_ticket missing: log in again to send files');
|
|
1489
|
+
}
|
|
1020
1490
|
const size = buffer.length;
|
|
1021
1491
|
const fromUserName = this.selfId();
|
|
1022
1492
|
const id = 'WU_FILE_' + this.fileId;
|
|
@@ -1036,7 +1506,7 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
1036
1506
|
DataLen: size,
|
|
1037
1507
|
FileMd5: fileMd5,
|
|
1038
1508
|
FromUserName: fromUserName,
|
|
1039
|
-
MediaType: "doc" /* Attachment */,
|
|
1509
|
+
MediaType: "doc" /* WebMediaType.Attachment */,
|
|
1040
1510
|
Signature: '',
|
|
1041
1511
|
StartPos: 0,
|
|
1042
1512
|
ToUserName: toUserName,
|
|
@@ -1048,7 +1518,7 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
1048
1518
|
FileMd5: fileMd5,
|
|
1049
1519
|
FileName: filename,
|
|
1050
1520
|
FileSize: size,
|
|
1051
|
-
FileType: 7,
|
|
1521
|
+
FileType: 7, // If do not have this parameter, the api will fail
|
|
1052
1522
|
FromUserName: fromUserName,
|
|
1053
1523
|
ToUserName: toUserName,
|
|
1054
1524
|
};
|
|
@@ -1243,13 +1713,13 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
1243
1713
|
config_1.log.error('PuppetWeChat', 'sendMedia() exception: %s', e.message);
|
|
1244
1714
|
throw e;
|
|
1245
1715
|
}
|
|
1246
|
-
if (
|
|
1247
|
-
throw new Error('sendMedia
|
|
1716
|
+
if (ret !== true) {
|
|
1717
|
+
throw new Error('sendMedia failed: ' + (ret || 'page error') + this.bridge.lastSendFailure());
|
|
1248
1718
|
}
|
|
1249
1719
|
}
|
|
1250
1720
|
async messageSendContact(conversationId, contactId) {
|
|
1251
1721
|
config_1.log.verbose('PuppetWeChat', 'messageSend("%s", %s)', conversationId, contactId);
|
|
1252
|
-
return wechaty_puppet_1.throwUnsupportedError();
|
|
1722
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)();
|
|
1253
1723
|
}
|
|
1254
1724
|
async messageImage(messageId, imageType) {
|
|
1255
1725
|
config_1.log.verbose('PuppetWeChat', 'messageImage(%s, %s)', messageId, imageType);
|
|
@@ -1257,7 +1727,7 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
1257
1727
|
}
|
|
1258
1728
|
async messageContact(messageId) {
|
|
1259
1729
|
config_1.log.verbose('PuppetWeChat', 'messageContact(%s)', messageId);
|
|
1260
|
-
return wechaty_puppet_1.throwUnsupportedError(messageId);
|
|
1730
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(messageId);
|
|
1261
1731
|
}
|
|
1262
1732
|
/**
|
|
1263
1733
|
*
|
|
@@ -1265,16 +1735,16 @@ class PuppetWeChat extends wechaty_puppet_1.Puppet {
|
|
|
1265
1735
|
*
|
|
1266
1736
|
*/
|
|
1267
1737
|
async tagContactAdd(tagId, contactId) {
|
|
1268
|
-
return wechaty_puppet_1.throwUnsupportedError(tagId, contactId);
|
|
1738
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(tagId, contactId);
|
|
1269
1739
|
}
|
|
1270
1740
|
async tagContactRemove(tagId, contactId) {
|
|
1271
|
-
return wechaty_puppet_1.throwUnsupportedError(tagId, contactId);
|
|
1741
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(tagId, contactId);
|
|
1272
1742
|
}
|
|
1273
1743
|
async tagContactDelete(tagId) {
|
|
1274
|
-
return wechaty_puppet_1.throwUnsupportedError(tagId);
|
|
1744
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(tagId);
|
|
1275
1745
|
}
|
|
1276
1746
|
async tagContactList(contactId) {
|
|
1277
|
-
return wechaty_puppet_1.throwUnsupportedError(contactId);
|
|
1747
|
+
return (0, wechaty_puppet_1.throwUnsupportedError)(contactId);
|
|
1278
1748
|
}
|
|
1279
1749
|
}
|
|
1280
1750
|
exports.PuppetWeChat = PuppetWeChat;
|