From 1d830da79fc9fe73a4888d975e6155ba19740ea1 Mon Sep 17 00:00:00 2001 From: LittleC <26459759+XxLittleCxX@users.noreply.github.com> Date: Wed, 22 Nov 2023 17:03:58 +0800 Subject: [PATCH] fix(qq): send image attached to message --- adapters/qq/src/internal/group.ts | 11 ++--------- adapters/qq/src/message.ts | 33 ++++++++++++++++++++++--------- adapters/qq/src/types.ts | 10 +++++++++- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/adapters/qq/src/internal/group.ts b/adapters/qq/src/internal/group.ts index 19d54ccc..bbaf36bf 100644 --- a/adapters/qq/src/internal/group.ts +++ b/adapters/qq/src/internal/group.ts @@ -27,18 +27,11 @@ export class GroupInternal { // /v2/channels/{channel_id}/messages new api? async sendFilePrivate(openid: string, data: QQ.SendFileParams) { - return this.http().post<{ - // id: string - // timestamp: number - file_uuid: string - }>(`/v2/users/${openid}/files`, data) + return this.http().post(`/v2/users/${openid}/files`, data) } async sendFileGuild(group_openid: string, data: QQ.SendFileParams) { - return this.http().post<{ - id: string - timestamp: number - }>(`/v2/groups/${group_openid}/files`, data) + return this.http().post(`/v2/groups/${group_openid}/files`, data) } // @TODO enum diff --git a/adapters/qq/src/message.ts b/adapters/qq/src/message.ts index 67e59d89..a499b883 100644 --- a/adapters/qq/src/message.ts +++ b/adapters/qq/src/message.ts @@ -151,8 +151,10 @@ export class QQMessageEncoder extends MessageEncode private content: string = '' private useMarkdown = false private rows: QQ.Button[][] = [] + private attachedFile: QQ.SendFileResponse; + // ε…ˆε›ΎεŽζ–‡ async flush() { - if (!this.content.trim() && !this.rows.flat().length) return + if (!this.content.trim() && !this.rows.flat().length && !this.attachedFile) return this.trimButtons() this.options.session['seq'] ||= 0 let msg_id = this.options?.session?.messageId, msg_seq: number = ++this.options.session['seq'] @@ -162,14 +164,19 @@ export class QQMessageEncoder extends MessageEncode } const data: QQ.SendMessageParams = { content: this.content, - msg_type: 0, + msg_type: QQ.MessageType.TEXT, timestamp: Math.floor(Date.now() / 1000), msg_id, - msg_seq, + msg_seq + } + if (this.attachedFile) { + if (!data.content.length) data.content = ' ' + data.media = this.attachedFile + data.msg_type = QQ.MessageType.MEDIA } if (this.useMarkdown) { - data.msg_type = 2 + data.msg_type = QQ.MessageType.MARKDOWN delete data.content data.markdown = { content: escapeMarkdown(this.content) || ' ', @@ -204,6 +211,7 @@ export class QQMessageEncoder extends MessageEncode // this.results.push(session.event.message) // session.app.emit(session, 'send', session) this.content = '' + this.attachedFile = null this.rows = [] } @@ -225,13 +233,14 @@ export class QQMessageEncoder extends MessageEncode const data: QQ.SendFileParams = { file_type, url, - srv_send_msg: true, + srv_send_msg: false, } + let res: QQ.SendFileResponse try { if (this.session.isDirect) { - await this.bot.internal.sendFilePrivate(this.options.session.event.message.user.id, data) + res = await this.bot.internal.sendFilePrivate(this.options.session.event.message.user.id, data) } else { - await this.bot.internal.sendFileGuild(this.session.guildId, data) + res = await this.bot.internal.sendFileGuild(this.session.guildId, data) } } catch (e) { if (!Quester.isAxiosError(e)) throw e @@ -239,6 +248,7 @@ export class QQMessageEncoder extends MessageEncode this.bot.logger.warn('[response] %s %o', e.response?.status, e.response?.data) } entry?.dispose?.() + return res } decodeButton(attrs: Dict, label: string) { @@ -288,9 +298,14 @@ export class QQMessageEncoder extends MessageEncode if (type === 'text') { this.content += escape(attrs.content) } else if (type === 'image' && attrs.url) { - await this.sendFile(type, attrs) + await this.flush() + const data = await this.sendFile(type, attrs) + if (data) this.attachedFile = data } else if (type === 'video' && attrs.url) { - await this.sendFile(type, attrs) + await this.flush() + const data = await this.sendFile(type, attrs) + if (data) this.attachedFile = data + await this.flush() // text can't send with video } else if (type === 'button-group') { this.useMarkdown = true this.rows.push([]) diff --git a/adapters/qq/src/types.ts b/adapters/qq/src/types.ts index 517ca9f6..75c82dee 100644 --- a/adapters/qq/src/types.ts +++ b/adapters/qq/src/types.ts @@ -1164,6 +1164,7 @@ export enum MessageType { EMBED = 4, // @TODO merge? AT = 5, + MEDIA = 7, } export interface SendMessageParams { @@ -1185,6 +1186,7 @@ export interface SendMessageParams { msg_seq?: number // @TODO merge? timestamp: number + media?: Partial } export enum FileType { @@ -1197,10 +1199,16 @@ export enum FileType { export interface SendFileParams { file_type: FileType url: string - srv_send_msg: true + srv_send_msg: boolean file_data?: unknown } +export interface SendFileResponse { + file_uuid: string + file_info: string + ttl: number +} + export interface UserMessage { id: string author: {