@karinjs/adapter-qqbot 1.2.8 → 1.2.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.
package/lib/core/api.d.ts CHANGED
@@ -52,6 +52,12 @@ export declare class QQBotApi extends EventEmitter {
52
52
  * @param options 请求参数
53
53
  */
54
54
  post(url: string, options: any): Promise<import("got").Response<string> | undefined>;
55
+ /**
56
+ * delete请求
57
+ * @param url 请求地址
58
+ * @param options 请求参数
59
+ */
60
+ delete(url: string): Promise<import("got").Response<string> | undefined>;
55
61
  /**
56
62
  * 获取调用凭证
57
63
  * @param clientSecret 开发者的Secret
@@ -103,6 +109,13 @@ export declare class QQBotApi extends EventEmitter {
103
109
  * @param options 请求参数
104
110
  */
105
111
  sendMessage(openid: string, type: PathType, options: SendMessageOptions): Promise<SendMessageResponse>;
112
+ /**
113
+ * 撤回消息
114
+ * @param openid 用户、群openid
115
+ * @param type 请求路径类型
116
+ * @param message_id 消息id
117
+ */
118
+ recallMessage(openid: string, type: PathType, message_id: string): Promise<boolean>;
106
119
  /**
107
120
  * 构建发送文本消息请求参数
108
121
  */
@@ -133,10 +146,11 @@ export declare class QQBotApi extends EventEmitter {
133
146
  /**
134
147
  * 构建发送富媒体请求参数
135
148
  * 富媒体消息只能单独发送 并且必须进行上传
149
+ * @param content 附带的文字信息,仅在file_type为1时有效,其余情况请传空字符串
136
150
  * @param file_info 富媒体接口返回的file_info
137
151
  * @param id 消息id或者事件id
138
152
  */
139
- buildMedia(file_info: string, id?: string, seq?: number): SendMessageOptions;
153
+ buildMedia(content: string, file_info: string, id?: string, seq?: number): SendMessageOptions;
140
154
  /**
141
155
  * 构建发送Markdown模板请求参数
142
156
  * @param custom_template_id 模板id
package/lib/core/api.js CHANGED
@@ -59,6 +59,19 @@ export class QQBotApi extends EventEmitter {
59
59
  logger.error(`[got] 请求错误: ${JSON.stringify(error.response.body)}`);
60
60
  }
61
61
  }
62
+ /**
63
+ * delete请求
64
+ * @param url 请求地址
65
+ * @param options 请求参数
66
+ */
67
+ async delete(url) {
68
+ try {
69
+ return await got.delete(url, { headers: this.headers });
70
+ }
71
+ catch (error) {
72
+ logger.error(`[got] 请求错误: ${JSON.stringify(error.response.body)}`);
73
+ }
74
+ }
62
75
  /**
63
76
  * 获取调用凭证
64
77
  * @param clientSecret 开发者的Secret
@@ -280,6 +293,17 @@ export class QQBotApi extends EventEmitter {
280
293
  const result = await this.post(url, options);
281
294
  return JSON.parse(result.body);
282
295
  }
296
+ /**
297
+ * 撤回消息
298
+ * @param openid 用户、群openid
299
+ * @param type 请求路径类型
300
+ * @param message_id 消息id
301
+ */
302
+ async recallMessage(openid, type, message_id) {
303
+ const url = this.buildUrl(openid, type, "messages" /* V2ApiType.Message */);
304
+ const result = await this.delete(`${url}/${message_id}`);
305
+ return result.status === 200;
306
+ }
283
307
  /**
284
308
  * 构建发送文本消息请求参数
285
309
  */
@@ -332,12 +356,13 @@ export class QQBotApi extends EventEmitter {
332
356
  /**
333
357
  * 构建发送富媒体请求参数
334
358
  * 富媒体消息只能单独发送 并且必须进行上传
359
+ * @param content 附带的文字信息,仅在file_type为1时有效,其余情况请传空字符串
335
360
  * @param file_info 富媒体接口返回的file_info
336
361
  * @param id 消息id或者事件id
337
362
  */
338
- buildMedia(file_info, id, seq) {
363
+ buildMedia(content, file_info, id, seq) {
339
364
  const options = {
340
- content: '',
365
+ content,
341
366
  msg_type: 7 /* MessageType.Media */,
342
367
  media: {
343
368
  file_info,
@@ -33,9 +33,7 @@ export declare class AdapterQQBot implements KarinAdapter {
33
33
  * @param data karin格式消息
34
34
  * */
35
35
  KarinConvertAdapter(data: Array<KarinElement>, type: PathType, openid: string, message_id?: string): Promise<ReplyReturn>;
36
- SendMessage(_contact: Contact, elements: Array<KarinElement>): Promise<{
37
- message_id: string;
38
- }>;
36
+ SendMessage(contact: Contact, elements: Array<KarinElement>): Promise<ReplyReturn>;
39
37
  getAvatarUrl(user_id: string, size?: number): string;
40
38
  getGroupAvatarUrl(): string;
41
39
  GetCurrentAccount(): Promise<{
@@ -43,6 +41,7 @@ export declare class AdapterQQBot implements KarinAdapter {
43
41
  account_uin: string;
44
42
  account_name: string;
45
43
  }>;
44
+ RecallMessage(contact: Contact, message_id: string): Promise<boolean>;
46
45
  GetEssenceMessageList(): Promise<any>;
47
46
  DownloadForwardMessage(): Promise<any>;
48
47
  SetEssenceMessage(): Promise<any>;
@@ -56,7 +55,6 @@ export declare class AdapterQQBot implements KarinAdapter {
56
55
  UploadForwardMessage(): Promise<any>;
57
56
  sendForwardMessage(): Promise<any>;
58
57
  SendMessageByResId(): Promise<any>;
59
- RecallMessage(): Promise<any>;
60
58
  GetMessage(): Promise<any>;
61
59
  GetHistoryMessage(): Promise<any>;
62
60
  VoteUser(): Promise<any>;
package/lib/core/index.js CHANGED
@@ -19,7 +19,7 @@ export class AdapterQQBot {
19
19
  const { appId, secret, ...restConfig } = this.#config;
20
20
  this.markdown = restConfig;
21
21
  this.account = { uid: appId, uin: appId, name: '' };
22
- this.adapter = { id: 'QQ', name: 'QQBot', type: 'internal', sub_type: 'internal', start_time: Date.now(), connect: '', index: 0 };
22
+ this.adapter = { id: 'QQBot', name: 'QQBot', type: 'internal', sub_type: 'internal', start_time: Date.now(), connect: '', index: 0 };
23
23
  this.version = { name: 'QQBot', app_name: 'QQBot', version: Config.package.version };
24
24
  this.super = new QQBotApi(this.#config);
25
25
  switch (this.markdown.sendMode) {
@@ -79,7 +79,7 @@ export class AdapterQQBot {
79
79
  contact: {
80
80
  scene: "group" /* Scene.Group */,
81
81
  peer: group_id,
82
- sub_peer: '',
82
+ sub_peer: null,
83
83
  },
84
84
  group_id,
85
85
  raw_message: '',
@@ -112,7 +112,7 @@ export class AdapterQQBot {
112
112
  contact: {
113
113
  scene: "friend" /* Scene.Private */,
114
114
  peer: user_id,
115
- sub_peer: '',
115
+ sub_peer: null,
116
116
  },
117
117
  group_id: '',
118
118
  raw_message: '',
@@ -187,6 +187,8 @@ export class AdapterQQBot {
187
187
  * */
188
188
  async KarinConvertAdapter(data, type, openid, message_id) {
189
189
  let seq = common.random(1, 999999);
190
+ /** 待排版的原始消息队列 */
191
+ let msg = [];
190
192
  /** 待发送列表 */
191
193
  const send_list = [];
192
194
  /** 采用并发上传 */
@@ -195,10 +197,10 @@ export class AdapterQQBot {
195
197
  switch (i.type) {
196
198
  case 'text': {
197
199
  const qr = await Common.getQQBotText(i.text);
198
- results.push({ index, options: this.super.buildText(qr.text, message_id, ++seq) });
200
+ results.push({ index, type: i.type, content: qr.text });
199
201
  if (qr.data) {
200
202
  const { file_info } = await this.super.uploadMedia(openid, type, qr.data.base64, 1 /* FileType.Image */);
201
- results.push({ index, options: this.super.buildMedia(file_info, message_id, ++seq) });
203
+ results.push({ index, type: 'image', content: file_info });
202
204
  }
203
205
  break;
204
206
  }
@@ -211,7 +213,7 @@ export class AdapterQQBot {
211
213
  /** 上传 */
212
214
  const { file_info } = await this.super.uploadMedia(openid, type, file, 3 /* FileType.Record */);
213
215
  /** 构建发送参数 */
214
- results.push({ index, options: this.super.buildMedia(file_info, message_id, ++seq) });
216
+ results.push({ index, type: i.type, content: file_info });
215
217
  break;
216
218
  }
217
219
  case 'image':
@@ -225,7 +227,7 @@ export class AdapterQQBot {
225
227
  /** 上传 */
226
228
  const { file_info } = await this.super.uploadMedia(openid, type, i.file, map[i.type]);
227
229
  /** 构建发送参数 */
228
- results.push({ index, options: this.super.buildMedia(file_info, message_id, ++seq) });
230
+ results.push({ index, type: i.type, content: file_info });
229
231
  break;
230
232
  }
231
233
  // 不支持的消息类型
@@ -235,8 +237,30 @@ export class AdapterQQBot {
235
237
  return results;
236
238
  })).then((allResults) => {
237
239
  allResults.flat().sort((a, b) => a.index - b.index).forEach(result => {
238
- send_list.push(result.options);
240
+ switch (result.type) {
241
+ case 'text': {
242
+ msg.push(result.content);
243
+ break;
244
+ }
245
+ case 'image': {
246
+ const content = msg.length ? msg.join('') : '';
247
+ msg = [];
248
+ send_list.push(this.super.buildMedia(content, result.content, message_id, seq++));
249
+ break;
250
+ }
251
+ default: {
252
+ if (msg.length) {
253
+ send_list.push(this.super.buildText(msg.join(''), message_id, seq++));
254
+ msg = [];
255
+ }
256
+ send_list.push(this.super.buildMedia('', result.content, message_id, seq++));
257
+ break;
258
+ }
259
+ }
239
260
  });
261
+ if (msg.length) {
262
+ send_list.push(this.super.buildText(msg.join(''), message_id, seq++));
263
+ }
240
264
  });
241
265
  const result = {
242
266
  message_id: '',
@@ -253,28 +277,8 @@ export class AdapterQQBot {
253
277
  result.message_time = result.raw_data[0].time;
254
278
  return result;
255
279
  }
256
- async SendMessage(_contact, elements) {
257
- const text = [];
258
- for (const v of elements) {
259
- switch (v.type) {
260
- case 'at':
261
- text.push(`@${v.uid}`);
262
- break;
263
- case 'face':
264
- text.push(`[表情:${v.id}]`);
265
- break;
266
- case 'text':
267
- text.push(v.text);
268
- break;
269
- case 'image':
270
- // text.push(await this.#MsgToFile(v.type, v.file))
271
- break;
272
- default:
273
- text.push(`[未知消息类型:${JSON.stringify(v)}]`);
274
- }
275
- }
276
- this.logger('info', `${logger.green('Send private input: ')}${text.join('')}`);
277
- return { message_id: 'input' };
280
+ async SendMessage(contact, elements) {
281
+ return await this.KarinConvertAdapter(elements, contact.scene === 'group' ? "groups" /* PathType.Groups */ : "users" /* PathType.Friends */, contact.peer);
278
282
  }
279
283
  getAvatarUrl(user_id, size = 0) {
280
284
  if (user_id === this.account.uid)
@@ -285,7 +289,10 @@ export class AdapterQQBot {
285
289
  return 'https://p.qlogo.cn/gh/967068507/967068507/0';
286
290
  }
287
291
  async GetCurrentAccount() {
288
- return { account_uid: 'input', account_uin: 'input', account_name: 'input' };
292
+ return { account_uid: this.account.uid, account_uin: this.account.uin, account_name: this.account.name };
293
+ }
294
+ async RecallMessage(contact, message_id) {
295
+ return await this.super.recallMessage(contact.peer, contact.scene === 'group' ? "groups" /* PathType.Groups */ : "users" /* PathType.Friends */, message_id);
289
296
  }
290
297
  async GetEssenceMessageList() { throw new Error('Method not implemented.'); }
291
298
  async DownloadForwardMessage() { throw new Error('Method not implemented.'); }
@@ -300,7 +307,6 @@ export class AdapterQQBot {
300
307
  async UploadForwardMessage() { throw new Error('Method not implemented.'); }
301
308
  async sendForwardMessage() { throw new Error('Method not implemented.'); }
302
309
  async SendMessageByResId() { throw new Error('Method not implemented.'); }
303
- async RecallMessage() { throw new Error('Method not implemented.'); }
304
310
  async GetMessage() { throw new Error('Method not implemented.'); }
305
311
  async GetHistoryMessage() { throw new Error('Method not implemented.'); }
306
312
  async VoteUser() { throw new Error('Method not implemented.'); }
@@ -39,7 +39,7 @@ export async function markdownTemplate(Opt) {
39
39
  const mediaFn = async (file, type, name, FileType) => {
40
40
  const { url } = await handler.call('qqbot.files', { file, type, name });
41
41
  const { file_info } = await bot.super.uploadMedia(openid, type, url, FileType);
42
- const opt = bot.super.buildMedia(file_info, message_id, ++seq);
42
+ const opt = bot.super.buildMedia('', file_info, message_id, ++seq);
43
43
  const res = await bot.super.sendMessage(openid, type, opt);
44
44
  result.raw_data.push(res);
45
45
  };
@@ -173,7 +173,7 @@ export async function markdownRaw(Opt) {
173
173
  const mediaFn = async (file, type, name, FileType) => {
174
174
  const { url } = await handler.call('qqbot.files', { file, type, name });
175
175
  const { file_info } = await bot.super.uploadMedia(openid, type, url, FileType);
176
- const opt = bot.super.buildMedia(file_info, message_id, ++seq);
176
+ const opt = bot.super.buildMedia('', file_info, message_id, ++seq);
177
177
  const res = await bot.super.sendMessage(openid, type, opt);
178
178
  result.raw_data.push(res);
179
179
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@karinjs/adapter-qqbot",
3
- "version": "1.2.8",
3
+ "version": "1.2.9",
4
4
  "author": "shijin",
5
5
  "type": "module",
6
6
  "description": "karin plugin template",