wechaty-puppet-matrix 0.0.23 → 0.0.24
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/cjs/src/matrix/events/event-friendship.d.ts.map +1 -1
- package/dist/cjs/src/matrix/events/event-message.d.ts.map +1 -1
- package/dist/cjs/src/matrix/events/event-room-invite.d.ts.map +1 -1
- package/dist/cjs/src/matrix/events/event-room-join.d.ts.map +1 -1
- package/dist/cjs/src/matrix/events/event-room-leave.d.ts.map +1 -1
- package/dist/cjs/src/matrix/events/event-room-topic.d.ts.map +1 -1
- package/dist/cjs/src/matrix/service/request.d.ts +3 -0
- package/dist/cjs/src/matrix/service/request.d.ts.map +1 -1
- package/dist/cjs/src/matrix/service/request.js +91 -6
- package/dist/cjs/src/matrix/utils/string.d.ts.map +1 -1
- package/dist/cjs/src/puppet-matrix.d.ts +2 -0
- package/dist/cjs/src/puppet-matrix.d.ts.map +1 -1
- package/dist/cjs/src/puppet-matrix.js +99 -5
- package/dist/cjs/src/utils/normalize-filebox.d.ts.map +1 -1
- package/dist/cjs/src/utils/sns-xml-generator.d.ts.map +1 -1
- package/dist/esm/src/matrix/events/event-friendship.d.ts.map +1 -1
- package/dist/esm/src/matrix/events/event-message.d.ts.map +1 -1
- package/dist/esm/src/matrix/events/event-room-invite.d.ts.map +1 -1
- package/dist/esm/src/matrix/events/event-room-join.d.ts.map +1 -1
- package/dist/esm/src/matrix/events/event-room-leave.d.ts.map +1 -1
- package/dist/esm/src/matrix/events/event-room-topic.d.ts.map +1 -1
- package/dist/esm/src/matrix/service/request.d.ts +3 -0
- package/dist/esm/src/matrix/service/request.d.ts.map +1 -1
- package/dist/esm/src/matrix/service/request.js +91 -6
- package/dist/esm/src/matrix/utils/string.d.ts.map +1 -1
- package/dist/esm/src/puppet-matrix.d.ts +2 -0
- package/dist/esm/src/puppet-matrix.d.ts.map +1 -1
- package/dist/esm/src/puppet-matrix.js +99 -5
- package/dist/esm/src/utils/normalize-filebox.d.ts.map +1 -1
- package/dist/esm/src/utils/sns-xml-generator.d.ts.map +1 -1
- package/package.json +2 -1
- package/src/matrix/service/request.ts +112 -6
- package/src/puppet-matrix.ts +161 -68
|
@@ -10,6 +10,7 @@ import imageSize from 'image-size';
|
|
|
10
10
|
import fs from 'fs-extra';
|
|
11
11
|
import os from 'os';
|
|
12
12
|
import path from 'path';
|
|
13
|
+
import mqtt from 'mqtt';
|
|
13
14
|
const PRE = '[PuppetMatrix]';
|
|
14
15
|
export var NotifyTypeEnum;
|
|
15
16
|
(function (NotifyTypeEnum) {
|
|
@@ -360,7 +361,66 @@ class Client extends EventEmitter {
|
|
|
360
361
|
log.error('socket had already been opened!');
|
|
361
362
|
return;
|
|
362
363
|
}
|
|
363
|
-
await this.
|
|
364
|
+
await this.createMqttConnection();
|
|
365
|
+
}
|
|
366
|
+
async createMqttConnection() {
|
|
367
|
+
if (!this.tokenInfo) {
|
|
368
|
+
log.error('Token info not available');
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
function randomRange(min, max) {
|
|
372
|
+
return Math.floor(Math.random() * (max - min)) + min;
|
|
373
|
+
}
|
|
374
|
+
const client = mqtt.connect(this.tokenInfo.mqServer, {
|
|
375
|
+
username: this.tokenInfo.mqUser,
|
|
376
|
+
password: this.tokenInfo.mqPsd,
|
|
377
|
+
keepalive: 60,
|
|
378
|
+
reconnectPeriod: 1000,
|
|
379
|
+
connectTimeout: 30 * 1000,
|
|
380
|
+
clientId: this.tokenInfo.guid + randomRange(1, 10000),
|
|
381
|
+
});
|
|
382
|
+
client.on('connect', () => {
|
|
383
|
+
this.connectionStatus.status = 'connected';
|
|
384
|
+
this.reconnectAttempts = 0;
|
|
385
|
+
log.info('MQTT connection opened');
|
|
386
|
+
this.socket = client;
|
|
387
|
+
client.subscribe(`aibotk/msg/${this.tokenInfo.guid}`, (err) => {
|
|
388
|
+
if (err) {
|
|
389
|
+
log.error('Failed to subscribe to topic', err);
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
log.info('Subscribed to topic');
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
client.on('error', (error) => {
|
|
397
|
+
log.warn('MQTT connection error', error);
|
|
398
|
+
});
|
|
399
|
+
client.on('close', () => {
|
|
400
|
+
this.connectionStatus.status = 'disconnected';
|
|
401
|
+
log.warn('MQTT connection closed');
|
|
402
|
+
this.socket = null;
|
|
403
|
+
void this.reconnect();
|
|
404
|
+
});
|
|
405
|
+
client.on('message', (topic, message) => {
|
|
406
|
+
this.resetHeartbeatTimeout();
|
|
407
|
+
log.verbose(PRE, 'Received message on topic %s: %s', topic, message.toString());
|
|
408
|
+
if (message.toString() === 'ping' || message.toString() === 'pong') {
|
|
409
|
+
log.verbose('Received heartbeat');
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
try {
|
|
413
|
+
const payload = JSON.parse(message.toString());
|
|
414
|
+
log.info('Received payload', JSON.stringify(payload));
|
|
415
|
+
void this.eventParse(payload);
|
|
416
|
+
}
|
|
417
|
+
catch (error) {
|
|
418
|
+
log.warn(PRE, 'Error parsing message: %s', error);
|
|
419
|
+
this.emit('error', error.message);
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
this.socket = client;
|
|
423
|
+
this.startHeartbeat();
|
|
364
424
|
}
|
|
365
425
|
async createWebSocket() {
|
|
366
426
|
if (!this.tokenInfo) {
|
|
@@ -424,16 +484,15 @@ class Client extends EventEmitter {
|
|
|
424
484
|
}
|
|
425
485
|
startHeartbeat() {
|
|
426
486
|
this.stopHeartbeat();
|
|
427
|
-
this.socket?.send('ping');
|
|
428
487
|
this.heartbeatTimer = setInterval(() => {
|
|
429
|
-
if (this.socket?.
|
|
488
|
+
if (this.socket?.connected) {
|
|
430
489
|
try {
|
|
431
|
-
this.socket.
|
|
490
|
+
this.socket.publish(`aibotk/msg/${this.tokenInfo.guid}`, 'pong');
|
|
432
491
|
this.setHeartbeatTimeout();
|
|
433
492
|
}
|
|
434
493
|
catch (error) {
|
|
435
494
|
log.error('Failed to send heartbeat:', error);
|
|
436
|
-
void this.
|
|
495
|
+
void this.handleMqttClose();
|
|
437
496
|
}
|
|
438
497
|
}
|
|
439
498
|
}, this.HEARTBEAT_INTERVAL);
|
|
@@ -454,7 +513,7 @@ class Client extends EventEmitter {
|
|
|
454
513
|
}
|
|
455
514
|
this.heartbeatTimeoutTimer = setTimeout(() => {
|
|
456
515
|
log.warn('Heartbeat timeout, reconnecting...');
|
|
457
|
-
void this.
|
|
516
|
+
void this.handleMqttClose();
|
|
458
517
|
}, this.HEARTBEAT_TIMEOUT);
|
|
459
518
|
}
|
|
460
519
|
resetHeartbeatTimeout() {
|
|
@@ -463,6 +522,32 @@ class Client extends EventEmitter {
|
|
|
463
522
|
this.heartbeatTimeoutTimer = undefined;
|
|
464
523
|
}
|
|
465
524
|
}
|
|
525
|
+
async handleMqttClose() {
|
|
526
|
+
this.connectionStatus.status = 'disconnected';
|
|
527
|
+
log.warn('MQTT connection closed');
|
|
528
|
+
if (this.socket) {
|
|
529
|
+
this.socket.end();
|
|
530
|
+
this.socket = null;
|
|
531
|
+
}
|
|
532
|
+
await this.reconnectMqtt();
|
|
533
|
+
}
|
|
534
|
+
async reconnectMqtt() {
|
|
535
|
+
if (this.reconnectAttempts >= this.MAX_RECONNECT_ATTEMPTS) {
|
|
536
|
+
log.error('Max reconnect attempts reached. Stopping reconnection.');
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
const delay = Math.min(this.INITIAL_RECONNECT_DELAY * Math.pow(2, this.reconnectAttempts), this.MAX_RECONNECT_DELAY);
|
|
540
|
+
this.reconnectAttempts++;
|
|
541
|
+
log.info(`Reconnecting in ${delay}ms (Attempt ${this.reconnectAttempts})`);
|
|
542
|
+
setTimeout(() => {
|
|
543
|
+
try {
|
|
544
|
+
void this.createMqttConnection();
|
|
545
|
+
}
|
|
546
|
+
catch (error) {
|
|
547
|
+
log.warn('Reconnection failed', error);
|
|
548
|
+
}
|
|
549
|
+
}, delay);
|
|
550
|
+
}
|
|
466
551
|
async handleWebSocketClose(code, reason) {
|
|
467
552
|
this.connectionStatus.status = 'disconnected';
|
|
468
553
|
log.warn(`WebSocket closed: Code ${code}, Reason ${reason}`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../../../../src/matrix/utils/string.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,eAAe,
|
|
1
|
+
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../../../../src/matrix/utils/string.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,eAAe,GAAI,GAAG,MAAM,WAExC,CAAA"}
|
|
@@ -21,6 +21,7 @@ declare class PuppetMatrix extends PUPPET.Puppet {
|
|
|
21
21
|
private _verifyInterval?;
|
|
22
22
|
private _qrcodeStatuasInterval?;
|
|
23
23
|
private _getQrcodeTimes;
|
|
24
|
+
private _roomJoinTemp;
|
|
24
25
|
static readonly VERSION: string;
|
|
25
26
|
constructor(options?: PuppetEngineOptions);
|
|
26
27
|
get client(): Client | undefined;
|
|
@@ -109,6 +110,7 @@ declare class PuppetMatrix extends PUPPET.Puppet {
|
|
|
109
110
|
tap(postId: string, type: PUPPET.types.Tap, tap?: boolean): Promise<boolean | void>;
|
|
110
111
|
syncRoom(): Promise<void>;
|
|
111
112
|
private _getRoomMemberList;
|
|
113
|
+
private _updateRoomMemberMap;
|
|
112
114
|
private _updateContactCache;
|
|
113
115
|
private _updateRoomMember;
|
|
114
116
|
_updateRoom(roomId: string): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"puppet-matrix.d.ts","sourceRoot":"","sources":["../../../src/puppet-matrix.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,
|
|
1
|
+
{"version":3,"file":"puppet-matrix.d.ts","sourceRoot":"","sources":["../../../src/puppet-matrix.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAA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GAAG,IAAI,CAAC;IAY1F,QAAQ;YAWA,kBAAkB;YAkClB,oBAAoB;YAwDpB,mBAAmB;YAgCnB,iBAAiB;IAYlB,WAAW,CAAE,MAAM,EAAE,MAAM;YAgB1B,cAAc;YAcd,cAAc;YA2Ed,eAAe;YAcf,iBAAiB;IAa/B,OAAO,CAAC,gBAAgB;YAeV,uBAAuB;YASvB,uBAAuB;YASvB,uBAAuB;YAgBvB,sBAAsB;IAuBvB,WAAW;CAQzB;AAED,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,CAAA;AAEhC,eAAe,YAAY,CAAA"}
|
|
@@ -38,11 +38,13 @@ class PuppetMatrix extends PUPPET.Puppet {
|
|
|
38
38
|
_verifyInterval;
|
|
39
39
|
_qrcodeStatuasInterval;
|
|
40
40
|
_getQrcodeTimes;
|
|
41
|
+
_roomJoinTemp = {};
|
|
41
42
|
static VERSION = VERSION;
|
|
42
43
|
constructor(options = {}) {
|
|
43
44
|
super(options);
|
|
44
45
|
this.options = options;
|
|
45
46
|
this.options.engine = Client;
|
|
47
|
+
this._roomJoinTemp = {};
|
|
46
48
|
this._getQrcodeTimes = 0;
|
|
47
49
|
this.options.maxGetQrcoderTimes = 20;
|
|
48
50
|
if (!this.options.token) {
|
|
@@ -114,6 +116,18 @@ class PuppetMatrix extends PUPPET.Puppet {
|
|
|
114
116
|
this.emit('room-leave', payload);
|
|
115
117
|
}));
|
|
116
118
|
this._client.on('room-join', this.wrapAsync(async ({ roomId, inviterId, joinIds, joinUsers }) => {
|
|
119
|
+
for (const joinId of joinIds) {
|
|
120
|
+
if (this._roomJoinTemp[`${roomId}-${joinId}`]) {
|
|
121
|
+
joinIds = joinIds.filter(id => id !== joinId);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
this._roomJoinTemp[`${roomId}-${joinId}`] = joinId;
|
|
125
|
+
const temTimer = setTimeout(() => {
|
|
126
|
+
delete this._roomJoinTemp[`${roomId}-${joinId}`];
|
|
127
|
+
clearTimeout(temTimer);
|
|
128
|
+
}, 50000);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
117
131
|
const payload = {
|
|
118
132
|
inviteeIdList: joinIds,
|
|
119
133
|
inviterId,
|
|
@@ -128,7 +142,9 @@ class PuppetMatrix extends PUPPET.Puppet {
|
|
|
128
142
|
await this._cacheMgr?.setRoom(roomId, roomInfo);
|
|
129
143
|
}
|
|
130
144
|
}
|
|
131
|
-
|
|
145
|
+
if (joinIds.length) {
|
|
146
|
+
this.emit('room-join', payload);
|
|
147
|
+
}
|
|
132
148
|
}));
|
|
133
149
|
this._client.on('logout', this.wrapAsync(async (message) => {
|
|
134
150
|
log.info(PRE, 'Logout event: %s', message);
|
|
@@ -894,8 +910,15 @@ class PuppetMatrix extends PUPPET.Puppet {
|
|
|
894
910
|
if (roomMemberMap[contactId]) {
|
|
895
911
|
return roomMemberMap[contactId];
|
|
896
912
|
}
|
|
897
|
-
const
|
|
898
|
-
|
|
913
|
+
const memberInfo = await this._updateRoomMemberMap(roomId, contactId);
|
|
914
|
+
const ret = await this._cacheMgr.getRoomMember(roomId);
|
|
915
|
+
if (ret) {
|
|
916
|
+
const newMap = await this._getRoomMemberList(roomId, true);
|
|
917
|
+
return newMap[contactId] || memberInfo;
|
|
918
|
+
}
|
|
919
|
+
else {
|
|
920
|
+
return memberInfo;
|
|
921
|
+
}
|
|
899
922
|
}
|
|
900
923
|
async roomMemberRawPayloadParser(rawPayload) {
|
|
901
924
|
return engineRoomMemberToWechaty(rawPayload);
|
|
@@ -1032,6 +1055,60 @@ class PuppetMatrix extends PUPPET.Puppet {
|
|
|
1032
1055
|
}
|
|
1033
1056
|
return ret;
|
|
1034
1057
|
}
|
|
1058
|
+
async _updateRoomMemberMap(roomId, contactId) {
|
|
1059
|
+
if (!this._cacheMgr) {
|
|
1060
|
+
return {
|
|
1061
|
+
avatar: '',
|
|
1062
|
+
city: '',
|
|
1063
|
+
country: '',
|
|
1064
|
+
name: '',
|
|
1065
|
+
alias: '',
|
|
1066
|
+
province: '',
|
|
1067
|
+
wxid: contactId,
|
|
1068
|
+
sex: 0,
|
|
1069
|
+
wxNum: '',
|
|
1070
|
+
};
|
|
1071
|
+
}
|
|
1072
|
+
const ret = await this._cacheMgr.getRoomMember(roomId);
|
|
1073
|
+
if (ret) {
|
|
1074
|
+
const existContact = await this._cacheMgr.getContact(contactId);
|
|
1075
|
+
if (existContact) {
|
|
1076
|
+
ret[contactId] = existContact;
|
|
1077
|
+
await this._updateRoomMember(roomId, ret);
|
|
1078
|
+
return existContact;
|
|
1079
|
+
}
|
|
1080
|
+
else {
|
|
1081
|
+
const contact = await this._client?.searchContact([contactId]);
|
|
1082
|
+
if (!Array.isArray(contact)) {
|
|
1083
|
+
ret[contactId] = contact;
|
|
1084
|
+
await this._updateRoomMember(roomId, ret);
|
|
1085
|
+
return contact;
|
|
1086
|
+
}
|
|
1087
|
+
return {
|
|
1088
|
+
avatar: '',
|
|
1089
|
+
city: '',
|
|
1090
|
+
country: '',
|
|
1091
|
+
name: '',
|
|
1092
|
+
alias: '',
|
|
1093
|
+
province: '',
|
|
1094
|
+
wxid: contactId,
|
|
1095
|
+
sex: 0,
|
|
1096
|
+
wxNum: '',
|
|
1097
|
+
};
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
return {
|
|
1101
|
+
avatar: '',
|
|
1102
|
+
city: '',
|
|
1103
|
+
country: '',
|
|
1104
|
+
name: '',
|
|
1105
|
+
alias: '',
|
|
1106
|
+
province: '',
|
|
1107
|
+
wxid: contactId,
|
|
1108
|
+
sex: 0,
|
|
1109
|
+
wxNum: '',
|
|
1110
|
+
};
|
|
1111
|
+
}
|
|
1035
1112
|
async _updateContactCache(contact) {
|
|
1036
1113
|
if (!contact.wxid) {
|
|
1037
1114
|
log.warn(PRE, `wxid is required for contact: ${JSON.stringify(contact)}`);
|
|
@@ -1042,7 +1119,7 @@ class PuppetMatrix extends PUPPET.Puppet {
|
|
|
1042
1119
|
const roomId = contact.wxid;
|
|
1043
1120
|
let finalPayload = contact;
|
|
1044
1121
|
if (oldRoomPayload) {
|
|
1045
|
-
finalPayload =
|
|
1122
|
+
finalPayload = { ...oldRoomPayload, ...contact };
|
|
1046
1123
|
}
|
|
1047
1124
|
await this._cacheMgr.setRoom(roomId, finalPayload);
|
|
1048
1125
|
if (!finalPayload.chatroommemberList || finalPayload.chatroommemberList.length === 0) {
|
|
@@ -1053,7 +1130,7 @@ class PuppetMatrix extends PUPPET.Puppet {
|
|
|
1053
1130
|
else {
|
|
1054
1131
|
const oldContactPaylod = await this._cacheMgr.getContact(contact.wxid);
|
|
1055
1132
|
if (oldContactPaylod) {
|
|
1056
|
-
await this._cacheMgr.setContact(contact.wxid,
|
|
1133
|
+
await this._cacheMgr.setContact(contact.wxid, { ...oldContactPaylod, ...contact });
|
|
1057
1134
|
}
|
|
1058
1135
|
else {
|
|
1059
1136
|
await this._cacheMgr.setContact(contact.wxid, contact);
|
|
@@ -1133,12 +1210,29 @@ class PuppetMatrix extends PUPPET.Puppet {
|
|
|
1133
1210
|
break;
|
|
1134
1211
|
}
|
|
1135
1212
|
case EventType.RoomJoin: {
|
|
1213
|
+
const roomJoin = event.payload;
|
|
1214
|
+
await this._updateRoomMember(roomJoin.roomId);
|
|
1215
|
+
for (const joinId of roomJoin.inviteeIdList) {
|
|
1216
|
+
if (this._roomJoinTemp[`${roomJoin.roomId}-${joinId}`]) {
|
|
1217
|
+
roomJoin.inviteeIdList = roomJoin.inviteeIdList.filter(id => id !== joinId);
|
|
1218
|
+
}
|
|
1219
|
+
else {
|
|
1220
|
+
this._roomJoinTemp[`${roomJoin.roomId}-${joinId}`] = joinId;
|
|
1221
|
+
const temTimer = setTimeout(() => {
|
|
1222
|
+
delete this._roomJoinTemp[`${roomJoin.roomId}-${joinId}`];
|
|
1223
|
+
clearTimeout(temTimer);
|
|
1224
|
+
}, 50000);
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
this.emit('room-join', roomJoin);
|
|
1136
1228
|
break;
|
|
1137
1229
|
}
|
|
1138
1230
|
case EventType.RoomLeave: {
|
|
1139
1231
|
break;
|
|
1140
1232
|
}
|
|
1141
1233
|
case EventType.RoomTopic: {
|
|
1234
|
+
const roomTopic = event.payload;
|
|
1235
|
+
this.emit('room-topic', roomTopic);
|
|
1142
1236
|
break;
|
|
1143
1237
|
}
|
|
1144
1238
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalize-filebox.d.ts","sourceRoot":"","sources":["../../../../src/utils/normalize-filebox.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EACR,MAA0B,UAAU,CAAA;AACrC,OAAO,EAEL,gBAAgB,EACjB,MAA0B,UAAU,CAAA;AA4BrC,QAAA,MAAM,cAAc,
|
|
1
|
+
{"version":3,"file":"normalize-filebox.d.ts","sourceRoot":"","sources":["../../../../src/utils/normalize-filebox.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EACR,MAA0B,UAAU,CAAA;AACrC,OAAO,EAEL,gBAAgB,EACjB,MAA0B,UAAU,CAAA;AA4BrC,QAAA,MAAM,cAAc,GAAI,SAAS,gBAAgB,YA8BhD,CAAA;AAED,QAAA,MAAM,oBAAoB,GAAI,aAAa,OAAO,OAAO,MAAY,SAAS,gBAAgB,wCAkB7F,CAAA;AAED,OAAO,EACL,cAAc,EACd,oBAAoB,GACrB,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sns-xml-generator.d.ts","sourceRoot":"","sources":["../../../../src/utils/sns-xml-generator.ts"],"names":[],"mappings":"AAAA,KAAK,KAAK,GAAG;IACX,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;IAChB,aAAa,EAAE,MAAM,CAAA;IACrB,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,MAAM,CAAA;IAChB,UAAU,EAAE,MAAM,CAAA;IAClB,WAAW,EAAE,MAAM,CAAA;CACpB,CAAA;AAED,eAAO,MAAM,aAAa,
|
|
1
|
+
{"version":3,"file":"sns-xml-generator.d.ts","sourceRoot":"","sources":["../../../../src/utils/sns-xml-generator.ts"],"names":[],"mappings":"AAAA,KAAK,KAAK,GAAG;IACX,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;IAChB,aAAa,EAAE,MAAM,CAAA;IACrB,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,MAAM,CAAA;IAChB,UAAU,EAAE,MAAM,CAAA;IAClB,WAAW,EAAE,MAAM,CAAA;CACpB,CAAA;AAED,eAAO,MAAM,aAAa,GAAI,MAAM,MAAM,EAAE,SAAS,MAAM,KAAG,MAkD7D,CAAA;AAED,eAAO,MAAM,cAAc,GAAI,MAAM,MAAM,EAAE,SAAS,MAAM,EAAE,OAAO,KAAK,KAAG,MAgD5E,CAAA;AAED,eAAO,MAAM,cAAc,GAAI,MAAM,MAAM,EAAE,aAAa,MAAM,EAAE,WAAW,KAAK,EAAE,KAAG,MAkEtF,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wechaty-puppet-matrix",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.24",
|
|
4
4
|
"description": "Puppet matrix for Wechaty",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"typings": "./dist/esm/src/mod.d.ts",
|
|
@@ -77,6 +77,7 @@
|
|
|
77
77
|
"fs-extra": "^10.1.0",
|
|
78
78
|
"image-size": "^1.1.1",
|
|
79
79
|
"lru-cache": "^6.0.0",
|
|
80
|
+
"mqtt": "^5.10.4",
|
|
80
81
|
"node-cleanup": "^2.1.2",
|
|
81
82
|
"wechaty-web-panel": "^1.6.76",
|
|
82
83
|
"ws": "^8.18.0",
|
|
@@ -11,6 +11,7 @@ import imageSize from 'image-size'
|
|
|
11
11
|
import fs from 'fs-extra'
|
|
12
12
|
import os from 'os'
|
|
13
13
|
import path from 'path'
|
|
14
|
+
import mqtt from 'mqtt'
|
|
14
15
|
const PRE = '[PuppetMatrix]'
|
|
15
16
|
|
|
16
17
|
/**
|
|
@@ -708,7 +709,78 @@ class Client extends EventEmitter {
|
|
|
708
709
|
return
|
|
709
710
|
}
|
|
710
711
|
|
|
711
|
-
await this.
|
|
712
|
+
await this.createMqttConnection()
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
async createMqttConnection (): Promise<void> {
|
|
716
|
+
if (!this.tokenInfo) {
|
|
717
|
+
log.error('Token info not available')
|
|
718
|
+
return
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
function randomRange (min: number, max: number) {
|
|
722
|
+
// min最小值,max最大值
|
|
723
|
+
return Math.floor(Math.random() * (max - min)) + min
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
const client = mqtt.connect(this.tokenInfo.mqServer, {
|
|
727
|
+
username: this.tokenInfo.mqUser,
|
|
728
|
+
password: this.tokenInfo.mqPsd,
|
|
729
|
+
keepalive: 60,
|
|
730
|
+
reconnectPeriod: 1000,
|
|
731
|
+
connectTimeout: 30 * 1000,
|
|
732
|
+
clientId: this.tokenInfo.guid + randomRange(1, 10000),
|
|
733
|
+
})
|
|
734
|
+
|
|
735
|
+
client.on('connect', () => {
|
|
736
|
+
this.connectionStatus.status = 'connected'
|
|
737
|
+
this.reconnectAttempts = 0
|
|
738
|
+
log.info('MQTT connection opened')
|
|
739
|
+
this.socket = client
|
|
740
|
+
|
|
741
|
+
// 订阅主题
|
|
742
|
+
client.subscribe(`aibotk/msg/${this.tokenInfo.guid}`, (err) => {
|
|
743
|
+
if (err) {
|
|
744
|
+
log.error('Failed to subscribe to topic', err)
|
|
745
|
+
} else {
|
|
746
|
+
log.info('Subscribed to topic')
|
|
747
|
+
}
|
|
748
|
+
})
|
|
749
|
+
})
|
|
750
|
+
|
|
751
|
+
client.on('error', (error: any) => {
|
|
752
|
+
log.warn('MQTT connection error', error)
|
|
753
|
+
})
|
|
754
|
+
|
|
755
|
+
client.on('close', () => {
|
|
756
|
+
this.connectionStatus.status = 'disconnected'
|
|
757
|
+
log.warn('MQTT connection closed')
|
|
758
|
+
this.socket = null
|
|
759
|
+
void this.reconnect()
|
|
760
|
+
})
|
|
761
|
+
|
|
762
|
+
client.on('message', (topic, message) => {
|
|
763
|
+
this.resetHeartbeatTimeout() // 收到任何消息都重置心跳超时
|
|
764
|
+
|
|
765
|
+
log.verbose(PRE, 'Received message on topic %s: %s', topic, message.toString())
|
|
766
|
+
if (message.toString() === 'ping' || message.toString() === 'pong') {
|
|
767
|
+
log.verbose('Received heartbeat')
|
|
768
|
+
return
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
try {
|
|
772
|
+
const payload = JSON.parse(message.toString())
|
|
773
|
+
log.info('Received payload', JSON.stringify(payload))
|
|
774
|
+
void this.eventParse(payload)
|
|
775
|
+
} catch (error) {
|
|
776
|
+
log.warn(PRE, 'Error parsing message: %s', error)
|
|
777
|
+
// @ts-ignore
|
|
778
|
+
this.emit('error', (error as Error).message)
|
|
779
|
+
}
|
|
780
|
+
})
|
|
781
|
+
|
|
782
|
+
this.socket = client
|
|
783
|
+
this.startHeartbeat()
|
|
712
784
|
}
|
|
713
785
|
|
|
714
786
|
async createWebSocket (): Promise<void> {
|
|
@@ -792,15 +864,14 @@ class Client extends EventEmitter {
|
|
|
792
864
|
|
|
793
865
|
private startHeartbeat (): void {
|
|
794
866
|
this.stopHeartbeat() // 确保清理现有定时器
|
|
795
|
-
this.socket?.send('ping')
|
|
796
867
|
this.heartbeatTimer = setInterval(() => {
|
|
797
|
-
if (this.socket?.
|
|
868
|
+
if (this.socket?.connected) {
|
|
798
869
|
try {
|
|
799
|
-
this.socket.
|
|
870
|
+
this.socket.publish(`aibotk/msg/${this.tokenInfo.guid}`, 'pong')
|
|
800
871
|
this.setHeartbeatTimeout()
|
|
801
872
|
} catch (error) {
|
|
802
873
|
log.error('Failed to send heartbeat:', error)
|
|
803
|
-
void this.
|
|
874
|
+
void this.handleMqttClose()
|
|
804
875
|
}
|
|
805
876
|
}
|
|
806
877
|
}, this.HEARTBEAT_INTERVAL)
|
|
@@ -826,7 +897,7 @@ class Client extends EventEmitter {
|
|
|
826
897
|
}
|
|
827
898
|
this.heartbeatTimeoutTimer = setTimeout(() => {
|
|
828
899
|
log.warn('Heartbeat timeout, reconnecting...')
|
|
829
|
-
void this.
|
|
900
|
+
void this.handleMqttClose()
|
|
830
901
|
}, this.HEARTBEAT_TIMEOUT)
|
|
831
902
|
}
|
|
832
903
|
|
|
@@ -838,6 +909,41 @@ class Client extends EventEmitter {
|
|
|
838
909
|
}
|
|
839
910
|
}
|
|
840
911
|
|
|
912
|
+
private async handleMqttClose () {
|
|
913
|
+
this.connectionStatus.status = 'disconnected'
|
|
914
|
+
log.warn('MQTT connection closed')
|
|
915
|
+
if (this.socket) {
|
|
916
|
+
this.socket.end()
|
|
917
|
+
this.socket = null
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
// 触发重连
|
|
921
|
+
await this.reconnectMqtt()
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
private async reconnectMqtt (): Promise<void> {
|
|
925
|
+
if (this.reconnectAttempts >= this.MAX_RECONNECT_ATTEMPTS) {
|
|
926
|
+
log.error('Max reconnect attempts reached. Stopping reconnection.')
|
|
927
|
+
return
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
// 指数退避重连策略
|
|
931
|
+
const delay = Math.min(
|
|
932
|
+
this.INITIAL_RECONNECT_DELAY * Math.pow(2, this.reconnectAttempts),
|
|
933
|
+
this.MAX_RECONNECT_DELAY,
|
|
934
|
+
)
|
|
935
|
+
|
|
936
|
+
this.reconnectAttempts++
|
|
937
|
+
log.info(`Reconnecting in ${delay}ms (Attempt ${this.reconnectAttempts})`)
|
|
938
|
+
setTimeout(() => {
|
|
939
|
+
try {
|
|
940
|
+
void this.createMqttConnection()
|
|
941
|
+
} catch (error) {
|
|
942
|
+
log.warn('Reconnection failed', error)
|
|
943
|
+
}
|
|
944
|
+
}, delay)
|
|
945
|
+
}
|
|
946
|
+
|
|
841
947
|
private async handleWebSocketClose (code?: number, reason?: Buffer | String) {
|
|
842
948
|
this.connectionStatus.status = 'disconnected'
|
|
843
949
|
log.warn(`WebSocket closed: Code ${code}, Reason ${reason}`)
|