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: 优化直接发送模式的图文混排,修复构建 #30

Closed
wants to merge 3 commits into from
Closed
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
66 changes: 35 additions & 31 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@
KarinAdapter,
KarinElement,
MessageSubType,
NodeElement,
button,
} from 'node-karin'

/**
* - QQBot适配器
*/
export class AdapterQQBot implements KarinAdapter {

Check failure on line 41 in src/core/index.ts

View workflow job for this annotation

GitHub Actions / build

Class 'AdapterQQBot' incorrectly implements interface 'KarinAdapter'.
super: QQBotApi
socket!: WebSocket
/** 账号配置类型定义 */
Expand Down Expand Up @@ -118,14 +119,14 @@
contact: {
scene: Scene.Group as Scene.Group,
peer: group_id,
sub_peer: '',
sub_peer: null,
},
group_id,
raw_message: '',
}

const e = new KarinMessage(message)
e.bot = this

Check failure on line 129 in src/core/index.ts

View workflow job for this annotation

GitHub Actions / build

Type 'this' is not assignable to type 'KarinAdapter'.
e.replyCallback = async elements => await this._sendNsg(e, elements, PathType.Groups, e.contact.peer, e.message_id)

karin.emit('adapter.message', e)
Expand Down Expand Up @@ -155,14 +156,14 @@
contact: {
scene: Scene.Private as Scene.Private,
peer: user_id,
sub_peer: '',
sub_peer: null,
},
group_id: '',
raw_message: '',
}

const e = new KarinMessage(message)
e.bot = this

Check failure on line 166 in src/core/index.ts

View workflow job for this annotation

GitHub Actions / build

Type 'this' is not assignable to type 'KarinAdapter'.
e.replyCallback = async elements => await this._sendNsg(e, elements, PathType.Friends, e.contact.peer, e.message_id)

karin.emit('adapter.message', e)
Expand All @@ -173,7 +174,7 @@
this.account.name = this.super.nick
this.logger('info', `建立连接成功: ${this.account.name}`)
/** 注册bot */
const index = karin.addBot({ type: this.adapter.type, bot: this })

Check failure on line 177 in src/core/index.ts

View workflow job for this annotation

GitHub Actions / build

Type 'this' is not assignable to type 'KarinAdapter'.
if (index) this.adapter.index = index
})
}
Expand Down Expand Up @@ -240,6 +241,8 @@
* */
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 +252,10 @@
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 +269,7 @@
/** 上传 */
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 +284,7 @@
/** 上传 */
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 +294,30 @@
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 +338,8 @@
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 +352,7 @@
}

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 All @@ -361,7 +366,6 @@
async UploadPrivateFile (): Promise<any> { throw new Error('Method not implemented.') }
async UploadGroupFile (): Promise<any> { throw new Error('Method not implemented.') }
async UploadForwardMessage (): Promise<any> { throw new Error('Method not implemented.') }
async sendForwardMessage (): Promise<any> { throw new Error('Method not implemented.') }
async SendMessageByResId (): Promise<any> { throw new Error('Method not implemented.') }
async RecallMessage (): Promise<any> { throw new Error('Method not implemented.') }
async GetMessage (): 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
Loading