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

feat(qq): support image send without temp server #250

Merged
merged 4 commits into from Mar 24, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 9 additions & 13 deletions adapters/qq/src/message.ts
Expand Up @@ -2,7 +2,6 @@ import * as QQ from './types'
import { Context, Dict, h, MessageEncoder, Quester } from '@satorijs/satori'
import { QQBot } from './bot'
import { QQGuildBot } from './bot/guild'
import { Entry } from '@satorijs/server-temp'

export const escapeMarkdown = (val: string) =>
val
Expand Down Expand Up @@ -301,26 +300,24 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
}

async sendFile(type: string, attrs: Dict) {
let url = attrs.src || attrs.url, entry: Entry | undefined
if (await this.bot.ctx.http.isLocal(url)) {
const temp = this.bot.ctx.get('server.temp')
if (!temp) {
return this.bot.logger.warn('missing temporary file service, cannot send assets with private url')
}
entry = await temp.create(url)
url = entry.url
}
await this.flush()
const url = attrs.src || attrs.url
let file_type = 0
if (type === 'img' || type === 'image') file_type = 1
else if (type === 'video') file_type = 2
else if (type === 'audio') file_type = 3
else return
const data: QQ.Message.File.Request = {
file_type,
url,
srv_send_msg: false,
}
const capture = /^data:([\w/-]+);base64,(.*)$/.exec(url)
if (capture?.[2]) {
data.file_data = capture[2]
} else if (await this.bot.ctx.http.isLocal(url)) {
data.file_data = Buffer.from((await this.bot.ctx.http.file(url)).data).toString('base64')
} else {
data.url = url
}
let res: QQ.Message.File.Response
try {
if (this.session.isDirect) {
Expand All @@ -337,7 +334,6 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
await this.sendFile(type, attrs)
}
}
entry?.dispose?.()
this.retry = false
return res
}
Expand Down
2 changes: 1 addition & 1 deletion adapters/qq/src/types.ts
Expand Up @@ -455,7 +455,7 @@ export namespace Message {
}
export interface Request {
file_type: Type
url: string
url?: string
srv_send_msg: boolean
file_data?: unknown
}
Expand Down