Skip to content

Commit

Permalink
fix(qq): send image attached to message
Browse files Browse the repository at this point in the history
  • Loading branch information
XxLittleCxX authored and shigma committed Nov 22, 2023
1 parent e7bf3dd commit 1d830da
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
11 changes: 2 additions & 9 deletions adapters/qq/src/internal/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<QQ.SendFileResponse>(`/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<QQ.SendFileResponse>(`/v2/groups/${group_openid}/files`, data)
}

// @TODO enum
Expand Down
33 changes: 24 additions & 9 deletions adapters/qq/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,10 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
private content: string = ''
private useMarkdown = false
private rows: QQ.Button[][] = []
private attachedFile: QQ.SendFileResponse;

Check failure on line 154 in adapters/qq/src/message.ts

View workflow job for this annotation

GitHub Actions / lint

Extra semicolon
// 先图后文
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']
Expand All @@ -162,14 +164,19 @@ export class QQMessageEncoder<C extends Context = Context> 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

Check failure on line 170 in adapters/qq/src/message.ts

View workflow job for this annotation

GitHub Actions / lint

Missing trailing comma
}
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) || ' ',
Expand Down Expand Up @@ -204,6 +211,7 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
// this.results.push(session.event.message)
// session.app.emit(session, 'send', session)
this.content = ''
this.attachedFile = null
this.rows = []
}

Expand All @@ -225,20 +233,22 @@ export class QQMessageEncoder<C extends Context = Context> 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
this.errors.push(e)
this.bot.logger.warn('[response] %s %o', e.response?.status, e.response?.data)
}
entry?.dispose?.()
return res
}

decodeButton(attrs: Dict, label: string) {
Expand Down Expand Up @@ -288,9 +298,14 @@ export class QQMessageEncoder<C extends Context = Context> 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([])
Expand Down
10 changes: 9 additions & 1 deletion adapters/qq/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,7 @@ export enum MessageType {
EMBED = 4,
// @TODO merge?
AT = 5,
MEDIA = 7,
}

export interface SendMessageParams {
Expand All @@ -1185,6 +1186,7 @@ export interface SendMessageParams {
msg_seq?: number
// @TODO merge?
timestamp: number
media?: Partial<SendFileResponse>
}

export enum FileType {
Expand All @@ -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: {
Expand Down

0 comments on commit 1d830da

Please sign in to comment.