@karinjs/adapter-qqbot 1.1.1 → 1.2.1

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
@@ -17,44 +17,31 @@ export type Params = Array<{
17
17
  }>;
18
18
  export declare class QQBotApi {
19
19
  #private;
20
- /**
21
- * 开发者的appId
22
- */
20
+ /** 开发者的appId */
23
21
  appId: string;
24
- /**
25
- * Bot的昵称
26
- */
22
+ /** Bot头像 */
23
+ avatar: string;
24
+ /** Bot的昵称 */
27
25
  nick: string;
28
- /**
29
- * wss地址
30
- */
26
+ /** wss地址 */
31
27
  wssUrl: string;
32
- /**
33
- * wss连接
34
- */
28
+ /** wss连接 */
35
29
  wss: WebSocket;
30
+ /** wss连接唯一值 */
36
31
  seq: number;
37
- /**
38
- * 心跳内容
39
- */
32
+ /** 心跳内容 */
40
33
  heartbeat: {
41
34
  op: 1;
42
35
  d: null | number;
43
36
  session_id: string;
44
37
  };
45
- /**
46
- * 心跳生命周期 单位:毫秒
47
- */
38
+ /** 心跳生命周期 单位:毫秒 */
48
39
  heartbeat_interval: number;
49
- /**
50
- * 请求头 用于鉴权
51
- */
40
+ /** 请求头 用于鉴权 */
52
41
  headers: {
53
42
  [key: string]: string;
54
43
  };
55
- /**
56
- * 基本请求接口
57
- */
44
+ /** 基本请求接口 */
58
45
  host: string;
59
46
  constructor(config: AccountCfgType);
60
47
  logger(level: LoggerLevel, ...args: any[]): void;
@@ -88,6 +75,21 @@ export declare class QQBotApi {
88
75
  * 获取wss地址
89
76
  */
90
77
  getWssUrl(): Promise<void>;
78
+ /**
79
+ * 获取当前Bot信息
80
+ */
81
+ getBotInfo(): Promise<{
82
+ /** Bot id */
83
+ id: string;
84
+ /** Bot昵称 */
85
+ username: string;
86
+ /** Bot头像 */
87
+ avatar: string;
88
+ /** Bot分享链接 */
89
+ share_url: string;
90
+ /** 欢迎信息 */
91
+ welcome_msg: string;
92
+ }>;
91
93
  /**
92
94
  * 创建wss连接
93
95
  */
package/lib/core/api.js CHANGED
@@ -3,51 +3,35 @@ import WebSocket from 'ws';
3
3
  import { Intents } from './intents.js';
4
4
  import { common, logger } from 'node-karin';
5
5
  export class QQBotApi {
6
- /**
7
- * 开发者的appId
8
- */
6
+ /** 开发者的appId */
9
7
  appId;
10
- /**
11
- * Bot的昵称
12
- */
8
+ /** Bot头像 */
9
+ avatar;
10
+ /** Bot的昵称 */
13
11
  nick;
14
- /**
15
- * 调用凭证
16
- */
12
+ /** 调用凭证 */
17
13
  #access_token;
18
- /**
19
- * wss地址
20
- */
14
+ /** wss地址 */
21
15
  wssUrl;
22
- /**
23
- * 开发者的Secret 仅在内部使用 防止泄露
24
- */
16
+ /** 开发者的Secret 仅在内部使用 防止泄露 */
25
17
  #Secret;
26
- /**
27
- * wss连接
28
- */
18
+ /** wss连接 */
29
19
  wss;
20
+ /** wss连接唯一值 */
30
21
  seq = 0;
31
- /**
32
- * 心跳内容
33
- */
22
+ /** 心跳内容 */
34
23
  heartbeat;
35
- /**
36
- * 心跳生命周期 单位:毫秒
37
- */
24
+ /** 心跳生命周期 单位:毫秒 */
38
25
  heartbeat_interval;
39
- /**
40
- * 请求头 用于鉴权
41
- */
26
+ /** 请求头 用于鉴权 */
42
27
  headers = {};
43
- /**
44
- * 基本请求接口
45
- */
28
+ /** 基本请求接口 */
46
29
  host;
47
30
  #config;
48
31
  constructor(config) {
49
32
  this.#config = config;
50
33
  this.appId = String(this.#config.appId);
34
+ this.avatar = '';
51
35
  this.nick = '';
52
36
  this.#Secret = this.#config.secret;
53
37
  this.host = this.#config.sandBox ? this.#config.sandBoxApi : this.#config.qqBotApi;
@@ -142,6 +126,14 @@ export class QQBotApi {
142
126
  const data = await got.get(url, { headers: this.headers }).json();
143
127
  this.wssUrl = data.url;
144
128
  }
129
+ /**
130
+ * 获取当前Bot信息
131
+ */
132
+ async getBotInfo() {
133
+ const url = `${this.host}/users/@me`;
134
+ const data = await got.get(url, { headers: this.headers }).json();
135
+ return data;
136
+ }
145
137
  /**
146
138
  * 创建wss连接
147
139
  */
@@ -215,6 +207,8 @@ export class QQBotApi {
215
207
  this.heartbeat.session_id = data.d.session_id;
216
208
  /** 心跳 */
217
209
  setInterval(() => this.wss.send(JSON.stringify(this.heartbeat)), this.heartbeat_interval);
210
+ const info = await this.getBotInfo();
211
+ this.avatar = info.avatar;
218
212
  this.nick = data.d.user.username;
219
213
  this.wss.emit('start');
220
214
  break;
@@ -36,7 +36,7 @@ export declare class AdapterQQBot implements KarinAdapter {
36
36
  SendMessage(_contact: Contact, elements: Array<KarinElement>): Promise<{
37
37
  message_id: string;
38
38
  }>;
39
- getAvatarUrl(): string;
39
+ getAvatarUrl(user_id: string, size?: number): string;
40
40
  getGroupAvatar(): string;
41
41
  GetCurrentAccount(): Promise<{
42
42
  account_uid: string;
package/lib/core/index.js CHANGED
@@ -114,6 +114,7 @@ export class AdapterQQBot {
114
114
  e.replyCallback = async (elements) => await this._sendNsg(elements, "users" /* PathType.Friends */, e.contact.peer, e.message_id);
115
115
  listener.emit('message', e);
116
116
  });
117
+ /** 等待建立连接 注册Bot */
117
118
  this.super.wss.on('start', () => {
118
119
  this.account.name = this.super.nick;
119
120
  this.logger('info', `建立连接成功: ${this.account.name}`);
@@ -122,7 +123,6 @@ export class AdapterQQBot {
122
123
  if (index)
123
124
  this.adapter.index = index;
124
125
  });
125
- /** 等待建立连接 注册Bot */
126
126
  }
127
127
  async _sendNsg(elements, type, openid, message_id) {
128
128
  return await this.KarinConvertAdapter(elements, type, openid, message_id);
@@ -179,7 +179,7 @@ export class AdapterQQBot {
179
179
  * @param data karin格式消息
180
180
  * */
181
181
  async KarinConvertAdapter(data, type, openid, message_id) {
182
- let seq = 0;
182
+ let seq = common.random(1, 999999);
183
183
  /** 待发送列表 */
184
184
  const send_list = [];
185
185
  /** 采用并发上传 */
@@ -269,8 +269,10 @@ export class AdapterQQBot {
269
269
  this.logger('info', `${logger.green('Send private input: ')}${text.join('')}`);
270
270
  return { message_id: 'input' };
271
271
  }
272
- getAvatarUrl() {
273
- return 'https://p.qlogo.cn/gh/967068507/967068507/0';
272
+ getAvatarUrl(user_id, size = 0) {
273
+ if (user_id === this.account.uid)
274
+ return this.super.avatar;
275
+ return `https://q.qlogo.cn/qqapp/${this.account.uid}/${user_id}/${size}`;
274
276
  }
275
277
  getGroupAvatar() {
276
278
  return 'https://p.qlogo.cn/gh/967068507/967068507/0';
@@ -11,7 +11,7 @@ export async function markdownTemplate(Opt) {
11
11
  const image = [];
12
12
  /** 待发送列表 */
13
13
  const send_list = [];
14
- let seq = 0;
14
+ let seq = common.random(1, 999999);
15
15
  let keyboard;
16
16
  const params = [];
17
17
  const markdown = [];
@@ -38,7 +38,7 @@ export async function markdownTemplate(Opt) {
38
38
  const mediaFn = async (file, type, name, FileType) => {
39
39
  const { url } = await handler.call('qqbot.files', { file, type, name });
40
40
  const { file_info } = await bot.super.uploadMedia(openid, type, url, FileType);
41
- const opt = bot.super.buildMedia(file_info, message_id, seq);
41
+ const opt = bot.super.buildMedia(file_info, message_id, ++seq);
42
42
  const res = await bot.super.sendMessage(openid, type, opt);
43
43
  result.raw_data.push(res);
44
44
  };
@@ -147,7 +147,7 @@ export async function markdownRaw(Opt) {
147
147
  const { bot, data, type, openid, message_id } = Opt;
148
148
  /** 待发送列表 */
149
149
  const send_list = [];
150
- let seq = 0;
150
+ let seq = common.random(1, 999999);
151
151
  const list = [];
152
152
  const result = {
153
153
  message_id: '',
@@ -166,7 +166,7 @@ export async function markdownRaw(Opt) {
166
166
  const mediaFn = async (file, type, name, FileType) => {
167
167
  const { url } = await handler.call('qqbot.files', { file, type, name });
168
168
  const { file_info } = await bot.super.uploadMedia(openid, type, url, FileType);
169
- const opt = bot.super.buildMedia(file_info, message_id, seq);
169
+ const opt = bot.super.buildMedia(file_info, message_id, ++seq);
170
170
  const res = await bot.super.sendMessage(openid, type, opt);
171
171
  result.raw_data.push(res);
172
172
  };
@@ -14,6 +14,7 @@ declare class Common {
14
14
  /** ffmpeg */
15
15
  ffmpeg: string;
16
16
  constructor();
17
+ random(min: number, max: number): number;
17
18
  /**
18
19
  * 语音转silk
19
20
  * @param voice - 语音文件
@@ -14,6 +14,9 @@ class Common {
14
14
  this.ffmpeg = '';
15
15
  this.initFfmpeg();
16
16
  }
17
+ random(min, max) {
18
+ return lodash.random(min, max);
19
+ }
17
20
  /**
18
21
  * 语音转silk
19
22
  * @param voice - 语音文件
@@ -164,14 +167,14 @@ class Common {
164
167
  */
165
168
  async getQQBotText(text) {
166
169
  const urls = this.getUrls(text);
167
- const data = await this.getQrCode(urls);
170
+ const data = urls.length ? (await this.getQrCode(urls)) : undefined;
168
171
  /** 使用for循环来替换字符串 保证一致性 */
169
172
  for (const url of urls) {
170
173
  text = text.replace(url, '[请扫码查看]');
171
174
  }
172
175
  return {
173
176
  text,
174
- data: urls.length ? data : undefined,
177
+ data,
175
178
  };
176
179
  }
177
180
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@karinjs/adapter-qqbot",
3
- "version": "1.1.1",
3
+ "version": "1.2.1",
4
4
  "author": "shijin",
5
5
  "type": "module",
6
6
  "description": "karin plugin template",