diff --git a/src/core/api.ts b/src/core/api.ts index 01e3417..5ecc865 100644 --- a/src/core/api.ts +++ b/src/core/api.ts @@ -397,12 +397,13 @@ export class QQBotApi extends EventEmitter { /** * 构建发送富媒体请求参数 * 富媒体消息只能单独发送 并且必须进行上传 + * @param content 附带的文字信息,仅在file_type为1时有效,其余情况请传空字符串 * @param file_info 富媒体接口返回的file_info * @param id 消息id或者事件id */ - buildMedia (file_info: string, id?: string, seq?: number): SendMessageOptions { + buildMedia (content: string, file_info: string, id?: string, seq?: number): SendMessageOptions { const options: SendMessageOptions = { - content: '', + content, msg_type: MessageType.Media, media: { file_info, diff --git a/src/core/index.ts b/src/core/index.ts index ac69a55..f71dc5c 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -31,6 +31,7 @@ import { KarinAdapter, KarinElement, MessageSubType, + NodeElement, button, } from 'node-karin' @@ -118,7 +119,7 @@ export class AdapterQQBot implements KarinAdapter { contact: { scene: Scene.Group as Scene.Group, peer: group_id, - sub_peer: '', + sub_peer: null, }, group_id, raw_message: '', @@ -155,7 +156,7 @@ export class AdapterQQBot implements KarinAdapter { contact: { scene: Scene.Private as Scene.Private, peer: user_id, - sub_peer: '', + sub_peer: null, }, group_id: '', raw_message: '', @@ -240,6 +241,8 @@ export class AdapterQQBot implements KarinAdapter { * */ async KarinConvertAdapter (data: Array, type: PathType, openid: string, message_id?: string): Promise { let seq = common.random(1, 999999) + /** 待排版的原始消息队列 */ + let msg: string[] = [] /** 待发送列表 */ const send_list: SendMessageOptions[] = [] @@ -249,10 +252,10 @@ export class AdapterQQBot implements KarinAdapter { switch (i.type) { case 'text': { const qr = await Common.getQQBotText(i.text) - results.push({ index, options: this.super.buildText(qr.text, message_id, ++seq) }) + results.push({ index, type: i.type, content: qr.text }) if (qr.data) { const { file_info } = await this.super.uploadMedia(openid, type, qr.data.base64, FileType.Image) - results.push({ index, options: this.super.buildMedia(file_info, message_id, ++seq) }) + results.push({ index, type: 'image', content: file_info }) } break } @@ -266,7 +269,7 @@ export class AdapterQQBot implements KarinAdapter { /** 上传 */ const { file_info } = await this.super.uploadMedia(openid, type, file, FileType.Record) /** 构建发送参数 */ - results.push({ index, options: this.super.buildMedia(file_info, message_id, ++seq) }) + results.push({ index, type: i.type, content: file_info }) break } case 'image': @@ -281,7 +284,7 @@ export class AdapterQQBot implements KarinAdapter { /** 上传 */ const { file_info } = await this.super.uploadMedia(openid, type, i.file, map[i.type]) /** 构建发送参数 */ - results.push({ index, options: this.super.buildMedia(file_info, message_id, ++seq) }) + results.push({ index, type: i.type, content: file_info }) break } // 不支持的消息类型 @@ -291,8 +294,30 @@ export class AdapterQQBot implements KarinAdapter { return results })).then((allResults) => { allResults.flat().sort((a, b) => a.index - b.index).forEach(result => { - send_list.push(result.options) + switch (result.type) { + case 'text': { + msg.push(result.content) + break + } + case 'image': { + const content = msg.length ? msg.join('') : '' + msg = [] + send_list.push(this.super.buildMedia(content, result.content, message_id, seq++)) + break + } + default: { + if (msg.length) { + send_list.push(this.super.buildText(msg.join(''), message_id, seq++)) + msg = [] + } + send_list.push(this.super.buildMedia('', result.content, message_id, seq++)) + break + } + } }) + if (msg.length) { + send_list.push(this.super.buildText(msg.join(''), message_id, seq++)) + } }) const result: ReplyReturn = { @@ -313,28 +338,8 @@ export class AdapterQQBot implements KarinAdapter { return result } - async SendMessage (_contact: Contact, elements: Array) { - const text = [] - for (const v of elements) { - switch (v.type) { - case 'at': - text.push(`@${v.uid}`) - break - case 'face': - text.push(`[表情:${v.id}]`) - break - case 'text': - text.push(v.text) - break - case 'image': - // text.push(await this.#MsgToFile(v.type, v.file)) - break - default: - text.push(`[未知消息类型:${JSON.stringify(v)}]`) - } - } - this.logger('info', `${logger.green('Send private input: ')}${text.join('')}`) - return { message_id: 'input' } + async SendMessage (contact: Contact, elements: Array) { + return await this.KarinConvertAdapter(elements, contact.scene === 'group' ? PathType.Groups : PathType.Friends, contact.peer) } getAvatarUrl (user_id: string, size = 0): string { @@ -347,7 +352,7 @@ export class AdapterQQBot implements KarinAdapter { } async GetCurrentAccount () { - return { account_uid: 'input', account_uin: 'input', account_name: 'input' } + return { account_uid: this.account.uid, account_uin: this.account.uin, account_name: this.account.name } } async GetEssenceMessageList (): Promise { throw new Error('Method not implemented.') } @@ -361,7 +366,6 @@ export class AdapterQQBot implements KarinAdapter { async UploadPrivateFile (): Promise { throw new Error('Method not implemented.') } async UploadGroupFile (): Promise { throw new Error('Method not implemented.') } async UploadForwardMessage (): Promise { throw new Error('Method not implemented.') } - async sendForwardMessage (): Promise { throw new Error('Method not implemented.') } async SendMessageByResId (): Promise { throw new Error('Method not implemented.') } async RecallMessage (): Promise { throw new Error('Method not implemented.') } async GetMessage (): Promise { throw new Error('Method not implemented.') } diff --git a/src/core/markdown.ts b/src/core/markdown.ts index e0ef733..5f3edab 100644 --- a/src/core/markdown.ts +++ b/src/core/markdown.ts @@ -68,7 +68,7 @@ export async function markdownTemplate (Opt: { const mediaFn = async (file: string, type: PathType, name: string, FileType: FileType) => { const { url } = await handler.call('qqbot.files', { file, type, name }) const { file_info } = await bot.super.uploadMedia(openid, type, url, FileType) - const opt = bot.super.buildMedia(file_info, message_id, ++seq) + const opt = bot.super.buildMedia('', file_info, message_id, ++seq) const res = await bot.super.sendMessage(openid, type, opt) result.raw_data.push(res) } @@ -218,7 +218,7 @@ export async function markdownRaw (Opt: { const mediaFn = async (file: string, type: PathType, name: string, FileType: FileType) => { const { url } = await handler.call('qqbot.files', { file, type, name }) const { file_info } = await bot.super.uploadMedia(openid, type, url, FileType) - const opt = bot.super.buildMedia(file_info, message_id, ++seq) + const opt = bot.super.buildMedia('', file_info, message_id, ++seq) const res = await bot.super.sendMessage(openid, type, opt) result.raw_data.push(res) }