wx2tg-puppet 2.0.6 → 2.0.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.
@@ -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
@@ -113,20 +116,31 @@ const say = (content, toWxid, ats) => __awaiter(void 0, void 0, void 0, function
113
116
  default:
114
117
  fileType = 'other';
115
118
  }
116
- const base64 = yield content.toBase64();
117
119
  switch (fileType) {
118
120
  case 'image':
119
121
  // console.log('上传图片', base64)
120
- return MsgApi_1.msgApi.UploadImg({
122
+ const base64 = yield content.toBase64();
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
- const res = yield ToolsApi_1.toolsApi.UploadFile({
127
- "base64": base64,
128
- "wxid": config === null || config === void 0 ? void 0 : config.wxid
129
- });
133
+ // 直接从 FileBox 获取文件内容
134
+ const fileBuffer = yield content.toBuffer();
135
+ const fileBlob = new Blob([fileBuffer], { type: 'application/octet-stream' });
136
+ // 创建 FormData
137
+ const formData = new FormData();
138
+ formData.append('wxid', (config === null || config === void 0 ? void 0 : config.wxid) || '');
139
+ formData.append('file', fileBlob, fileName);
140
+ const res = yield ToolsApi_1.toolsApi.UploadFileBinary(formData);
141
+ if (res.data.Code !== 0) {
142
+ throw new Error(`上传文件失败: ${res.data.Message}`);
143
+ }
130
144
  const size = res.data.Data.totalLen;
131
145
  const mediaId = res.data.Data.mediaId;
132
146
  if (!config)
@@ -137,7 +151,10 @@ const say = (content, toWxid, ats) => __awaiter(void 0, void 0, void 0, function
137
151
  "Wxid": config === null || config === void 0 ? void 0 : config.wxid,
138
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>`
139
153
  });
140
- return new Message_1.ResponseMsg(res1.data.Data.List[0]);
154
+ if (res1.data.Code !== 0) {
155
+ throw new Error(`发送文件消息失败: ${res1.data.Message}`);
156
+ }
157
+ return new Message_1.ResponseMsg(res1.data.Data);
141
158
  }
142
159
  }
143
160
  // else if(content instanceof URLLINK){
@@ -165,6 +182,9 @@ const say = (content, toWxid, ats) => __awaiter(void 0, void 0, void 0, function
165
182
  "ToWxid": toWxid,
166
183
  "Wxid": (config === null || config === void 0 ? void 0 : config.wxid) || ''
167
184
  });
185
+ if (res.data.Code !== 0) {
186
+ throw new Error(`发送视频失败: ${res.data.Message}`);
187
+ }
168
188
  return new Message_1.ResponseMsg(res.data.Data.List[0]);
169
189
  }
170
190
  else if (content instanceof VOICE_1.Voice) {
@@ -175,6 +195,9 @@ const say = (content, toWxid, ats) => __awaiter(void 0, void 0, void 0, function
175
195
  "VoiceTime": content.voiceDuration,
176
196
  "Wxid": (config === null || config === void 0 ? void 0 : config.wxid) || ''
177
197
  });
198
+ if (res.data.Code !== 0) {
199
+ throw new Error(`发送语音失败: ${res.data.Message}`);
200
+ }
178
201
  return new Message_1.ResponseMsg(res.data.Data.List[0]);
179
202
  }
180
203
  // }else if(content instanceof MiniApp){
@@ -203,6 +226,9 @@ const say = (content, toWxid, ats) => __awaiter(void 0, void 0, void 0, function
203
226
  "ToWxid": toWxid,
204
227
  "Wxid": (config === null || config === void 0 ? void 0 : config.wxid) || ''
205
228
  });
229
+ if (res.data.Code !== 0) {
230
+ throw new Error(`发送表情失败: ${res.data.Message}`);
231
+ }
206
232
  return new Message_1.ResponseMsg(res.data.Data.emojiItem[0]);
207
233
  }
208
234
  else {
@@ -211,6 +237,7 @@ const say = (content, toWxid, ats) => __awaiter(void 0, void 0, void 0, function
211
237
  }
212
238
  catch (e) {
213
239
  console.error(e);
240
+ throw e;
214
241
  }
215
242
  });
216
243
  exports.say = say;
@@ -226,19 +253,26 @@ const quote = (obj, toWxid) => __awaiter(void 0, void 0, void 0, function* () {
226
253
  "Wxid": config === null || config === void 0 ? void 0 : config.wxid,
227
254
  "Xml": msg
228
255
  });
256
+ if (res.data.Code !== 0) {
257
+ throw new Error(`发送引用消息失败: ${res.data.Message}`);
258
+ }
229
259
  return new Message_1.ResponseMsg(res.data.Data);
230
260
  });
231
261
  exports.quote = quote;
232
262
  // 撤回
233
263
  const revoke = (content) => __awaiter(void 0, void 0, void 0, function* () {
234
264
  const config = yield config_service_1.configService.get();
235
- return MsgApi_1.msgApi.Revoke({
265
+ const res = yield MsgApi_1.msgApi.Revoke({
236
266
  "ClientMsgId": content.msgId,
237
267
  "CreateTime": content.createTime,
238
268
  "NewMsgId": content.newMsgId,
239
269
  "ToUserName": content.toWxid,
240
270
  "Wxid": (config === null || config === void 0 ? void 0 : config.wxid) || ''
241
271
  });
272
+ if (res.data.Code !== 0) {
273
+ throw new Error(`撤回消息失败: ${res.data.Message}`);
274
+ }
275
+ return res;
242
276
  });
243
277
  exports.revoke = revoke;
244
278
  // export const forward = async (content, contact, type) => {
@@ -33,6 +33,8 @@ declare class ToolsApi {
33
33
  UpdateStepNumberApi(body: any): Promise<AxiosResponse<ApiResponse>>;
34
34
  /** 文件上传 */
35
35
  UploadFile(body: any): Promise<AxiosResponse<ApiResponse>>;
36
+ /** 二进制文件上传 */
37
+ UploadFileBinary(formData: FormData): Promise<AxiosResponse<ApiResponse>>;
36
38
  /** 设置/删除代理IP */
37
39
  setproxy(body: any): Promise<AxiosResponse<ApiResponse>>;
38
40
  /** CDN图片下载 */
@@ -67,6 +67,14 @@ class ToolsApi {
67
67
  UploadFile(body) {
68
68
  return axiosInstance_1.axiosInstance.post('/Tools/UploadFile', body);
69
69
  }
70
+ /** 二进制文件上传 */
71
+ UploadFileBinary(formData) {
72
+ return axiosInstance_1.axiosInstance.post('/Tools/UploadFileBinary', formData, {
73
+ headers: {
74
+ 'Content-Type': 'multipart/form-data',
75
+ },
76
+ });
77
+ }
70
78
  /** 设置/删除代理IP */
71
79
  setproxy(body) {
72
80
  return axiosInstance_1.axiosInstance.post('/Tools/setproxy', body);
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.6",
3
+ "version": "2.0.8",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [