Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 优化直接发送模式的图文混排 #32

Merged
merged 4 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/core/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
64 changes: 34 additions & 30 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,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: '',
Expand Down Expand Up @@ -155,7 +155,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: '',
Expand Down Expand Up @@ -240,6 +240,8 @@ export class AdapterQQBot implements KarinAdapter {
* */
async KarinConvertAdapter (data: Array<KarinElement>, type: PathType, openid: string, message_id?: string): Promise<ReplyReturn> {
let seq = common.random(1, 999999)
/** 待排版的原始消息队列 */
let msg: string[] = []
/** 待发送列表 */
const send_list: SendMessageOptions[] = []

Expand All @@ -249,10 +251,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
}
Expand All @@ -266,7 +268,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':
Expand All @@ -281,7 +283,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
}
// 不支持的消息类型
Expand All @@ -291,8 +293,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 = {
Expand All @@ -313,28 +337,8 @@ export class AdapterQQBot implements KarinAdapter {
return result
}

async SendMessage (_contact: Contact, elements: Array<KarinElement>) {
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<KarinElement>) {
return await this.KarinConvertAdapter(elements, contact.scene === 'group' ? PathType.Groups : PathType.Friends, contact.peer)
}

getAvatarUrl (user_id: string, size = 0): string {
Expand All @@ -347,7 +351,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<any> { throw new Error('Method not implemented.') }
Expand Down
4 changes: 2 additions & 2 deletions src/core/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down