wx2tg-puppet 2.0.7 → 2.0.9

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.
@@ -94,6 +94,9 @@ const say = (content, toWxid, ats) => __awaiter(void 0, void 0, void 0, function
94
94
  "Type": 1,
95
95
  "Wxid": (config === null || config === void 0 ? void 0 : config.wxid) || ''
96
96
  });
97
+ if (res.data.Code !== 0) {
98
+ throw new Error(`发送文本消息失败: ${res.data.Message}`);
99
+ }
97
100
  return new Message_1.ResponseMsg(res.data.Data.List[0]);
98
101
  }
99
102
  else if (content instanceof file_box_1.FileBox) { // filebox
@@ -117,20 +120,27 @@ const say = (content, toWxid, ats) => __awaiter(void 0, void 0, void 0, function
117
120
  case 'image':
118
121
  // console.log('上传图片', base64)
119
122
  const base64 = yield content.toBase64();
120
- return MsgApi_1.msgApi.UploadImg({
123
+ const imgRes = yield MsgApi_1.msgApi.UploadImg({
121
124
  "Base64": base64,
122
125
  "ToWxid": toWxid,
123
126
  "Wxid": (config === null || config === void 0 ? void 0 : config.wxid) || ''
124
127
  });
128
+ if (imgRes.data.Code !== 0) {
129
+ throw new Error(`上传图片失败: ${imgRes.data.Message}`);
130
+ }
131
+ return imgRes;
125
132
  case 'other':
126
133
  // 直接从 FileBox 获取文件内容
127
134
  const fileBuffer = yield content.toBuffer();
128
- const fileBlob = new Blob([fileBuffer]);
135
+ const fileBlob = new Blob([fileBuffer], { type: 'application/octet-stream' });
129
136
  // 创建 FormData
130
137
  const formData = new FormData();
131
138
  formData.append('wxid', (config === null || config === void 0 ? void 0 : config.wxid) || '');
132
139
  formData.append('file', fileBlob, fileName);
133
140
  const res = yield ToolsApi_1.toolsApi.UploadFileBinary(formData);
141
+ if (res.data.Code !== 0) {
142
+ throw new Error(`上传文件失败: ${res.data.Message}`);
143
+ }
134
144
  const size = res.data.Data.totalLen;
135
145
  const mediaId = res.data.Data.mediaId;
136
146
  if (!config)
@@ -141,6 +151,9 @@ const say = (content, toWxid, ats) => __awaiter(void 0, void 0, void 0, function
141
151
  "Wxid": config === null || config === void 0 ? void 0 : config.wxid,
142
152
  "Xml": `<appmsg appid='wxeb7ec651dd0aefa9' sdkver=''><title>${fileName}</title><des></des><action></action><type>6</type><content></content><url></url><lowurl></lowurl><appattach><totallen>${size}</totallen><attachid>${mediaId}</attachid><fileext>${ext}</fileext></appattach><extinfo></extinfo></appmsg>`
143
153
  });
154
+ if (res1.data.Code !== 0) {
155
+ throw new Error(`发送文件消息失败: ${res1.data.Message}`);
156
+ }
144
157
  return new Message_1.ResponseMsg(res1.data.Data);
145
158
  }
146
159
  }
@@ -169,6 +182,9 @@ const say = (content, toWxid, ats) => __awaiter(void 0, void 0, void 0, function
169
182
  "ToWxid": toWxid,
170
183
  "Wxid": (config === null || config === void 0 ? void 0 : config.wxid) || ''
171
184
  });
185
+ if (res.data.Code !== 0) {
186
+ throw new Error(`发送视频失败: ${res.data.Message}`);
187
+ }
172
188
  return new Message_1.ResponseMsg(res.data.Data.List[0]);
173
189
  }
174
190
  else if (content instanceof VOICE_1.Voice) {
@@ -179,6 +195,9 @@ const say = (content, toWxid, ats) => __awaiter(void 0, void 0, void 0, function
179
195
  "VoiceTime": content.voiceDuration,
180
196
  "Wxid": (config === null || config === void 0 ? void 0 : config.wxid) || ''
181
197
  });
198
+ if (res.data.Code !== 0) {
199
+ throw new Error(`发送语音失败: ${res.data.Message}`);
200
+ }
182
201
  return new Message_1.ResponseMsg(res.data.Data.List[0]);
183
202
  }
184
203
  // }else if(content instanceof MiniApp){
@@ -207,6 +226,9 @@ const say = (content, toWxid, ats) => __awaiter(void 0, void 0, void 0, function
207
226
  "ToWxid": toWxid,
208
227
  "Wxid": (config === null || config === void 0 ? void 0 : config.wxid) || ''
209
228
  });
229
+ if (res.data.Code !== 0) {
230
+ throw new Error(`发送表情失败: ${res.data.Message}`);
231
+ }
210
232
  return new Message_1.ResponseMsg(res.data.Data.emojiItem[0]);
211
233
  }
212
234
  else {
@@ -215,6 +237,7 @@ const say = (content, toWxid, ats) => __awaiter(void 0, void 0, void 0, function
215
237
  }
216
238
  catch (e) {
217
239
  console.error(e);
240
+ throw e;
218
241
  }
219
242
  });
220
243
  exports.say = say;
@@ -230,19 +253,26 @@ const quote = (obj, toWxid) => __awaiter(void 0, void 0, void 0, function* () {
230
253
  "Wxid": config === null || config === void 0 ? void 0 : config.wxid,
231
254
  "Xml": msg
232
255
  });
256
+ if (res.data.Code !== 0) {
257
+ throw new Error(`发送引用消息失败: ${res.data.Message}`);
258
+ }
233
259
  return new Message_1.ResponseMsg(res.data.Data);
234
260
  });
235
261
  exports.quote = quote;
236
262
  // 撤回
237
263
  const revoke = (content) => __awaiter(void 0, void 0, void 0, function* () {
238
264
  const config = yield config_service_1.configService.get();
239
- return MsgApi_1.msgApi.Revoke({
265
+ const res = yield MsgApi_1.msgApi.Revoke({
240
266
  "ClientMsgId": content.msgId,
241
267
  "CreateTime": content.createTime,
242
268
  "NewMsgId": content.newMsgId,
243
269
  "ToUserName": content.toWxid,
244
270
  "Wxid": (config === null || config === void 0 ? void 0 : config.wxid) || ''
245
271
  });
272
+ if (res.data.Code !== 0) {
273
+ throw new Error(`撤回消息失败: ${res.data.Message}`);
274
+ }
275
+ return res;
246
276
  });
247
277
  exports.revoke = revoke;
248
278
  // export const forward = async (content, contact, type) => {
@@ -75,11 +75,9 @@ class Contact {
75
75
  */
76
76
  sync() {
77
77
  return __awaiter(this, void 0, void 0, function* () {
78
- config_service_1.configService.get().then(config => {
79
- Contact.fetchAndUpsertContactDetail((config === null || config === void 0 ? void 0 : config.wxid) || '', this.id).then(contact => {
80
- this.contact = contact;
81
- });
82
- });
78
+ const config = yield config_service_1.configService.get();
79
+ const contact = yield Contact.fetchAndUpsertContactDetail((config === null || config === void 0 ? void 0 : config.wxid) || '', this.id);
80
+ this.contact = contact;
83
81
  });
84
82
  }
85
83
  /**
package/dist/index.d.ts CHANGED
@@ -23,6 +23,7 @@ export declare class WxBot extends EventEmitter {
23
23
  private heartbeatInterval;
24
24
  private msgInterval;
25
25
  private recentMsgIds;
26
+ private syncFailureCount;
26
27
  constructor({ baseUrl, databasePath, deviceType }: WxBotOptions);
27
28
  qrcode(style?: number): Promise<{
28
29
  qrCode: any;
package/dist/index.js CHANGED
@@ -51,6 +51,7 @@ class WxBot extends events_1.EventEmitter {
51
51
  this.heartbeatInterval = null;
52
52
  this.msgInterval = null;
53
53
  this.recentMsgIds = []; // Store recent message IDs
54
+ this.syncFailureCount = 0; // Sync failure counter
54
55
  this.baseUrl = baseUrl;
55
56
  this.databasePath = databasePath;
56
57
  this.deviceType = deviceType;
@@ -250,6 +251,7 @@ class WxBot extends events_1.EventEmitter {
250
251
  return __awaiter(this, void 0, void 0, function* () {
251
252
  contact_service_1.contactService.clearRoom();
252
253
  this.clearAllIntervals(); // 确保清理所有定时器
254
+ this.syncFailureCount = 0; // 重置失败计数器
253
255
  // 保存联系人信息
254
256
  const res = yield FriendApi_1.friendApi.GetContractList({
255
257
  "Wxid": userName,
@@ -297,6 +299,27 @@ class WxBot extends events_1.EventEmitter {
297
299
  "Synckey": ""
298
300
  });
299
301
  const data = resp.data;
302
+ if (data.Code !== 0) {
303
+ // Code不为0,增加失败计数
304
+ this.syncFailureCount++;
305
+ if (this.syncFailureCount >= 3) {
306
+ // 连续失败3次,退出登录
307
+ this.syncFailureCount = 0; // 重置计数器
308
+ yield this.logOut();
309
+ yield this.login();
310
+ return;
311
+ }
312
+ // 失败次数不足3次,继续等待下次sync
313
+ return;
314
+ }
315
+ if (data.Message === '用户可能退出') {
316
+ // 用户可能退出,立即退出登录
317
+ yield this.logOut();
318
+ yield this.login();
319
+ return;
320
+ }
321
+ // 成功情况,重置失败计数器
322
+ this.syncFailureCount = 0;
300
323
  if (data.Success) {
301
324
  if (data.Message === '当前未有新消息') {
302
325
  return;
@@ -367,10 +390,6 @@ class WxBot extends events_1.EventEmitter {
367
390
  }
368
391
  }
369
392
  }
370
- else if (data.Message === '用户可能退出') {
371
- yield this.logOut();
372
- yield this.login();
373
- }
374
393
  }), 1000);
375
394
  this.emit('login', userName);
376
395
  });
@@ -378,6 +397,7 @@ class WxBot extends events_1.EventEmitter {
378
397
  logOut() {
379
398
  return __awaiter(this, void 0, void 0, function* () {
380
399
  this.clearAllIntervals(); // 确保清理所有定时器
400
+ this.syncFailureCount = 0; // 重置失败计数器
381
401
  this.emit('logout');
382
402
  });
383
403
  }
package/dist/test.js CHANGED
@@ -14,7 +14,7 @@ const _1 = require(".");
14
14
  const wxBot = new _1.WxBot({
15
15
  baseUrl: 'http://127.0.0.1:8059/api',
16
16
  databasePath: './database.sqlite',
17
- deviceType: 'ipad'
17
+ deviceType: 'ipadX'
18
18
  });
19
19
  yield wxBot.start();
20
20
  wxBot.on('message', (message) => __awaiter(void 0, void 0, void 0, function* () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wx2tg-puppet",
3
- "version": "2.0.7",
3
+ "version": "2.0.9",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [