Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/qqfix'
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 2, 2023
2 parents 9b4463b + 8821f60 commit 6aee80d
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 115 deletions.
2 changes: 1 addition & 1 deletion adapters/discord/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class WsClient<C extends Context = Context> extends Adapter.WsClient<C, D
this.bot.dispatch(this.bot.session({
type: 'internal',
_type: 'discord/' + parsed.t.toLowerCase().replace(/_/g, '-'),
_data: parsed,
_data: parsed.d,
}))
if (parsed.t === 'READY') {
this._sessionId = parsed.d.session_id
Expand Down
19 changes: 10 additions & 9 deletions adapters/qq/src/internal/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as QQ from '../types'
export class GroupInternal {
constructor(private http: () => Quester) { }

async sendPrivateMessage(openid: string, data: QQ.SendMessageParams) {
async sendPrivateMessage(openid: string, data: QQ.Message.Request) {
return this.http().post<{
sendResult: {
msg_id: string
Expand All @@ -16,22 +16,23 @@ export class GroupInternal {
}>(`/v2/users/${openid}/messages`, data)
}

async sendMessage(group_openid: string, data: QQ.SendMessageParams) {
async sendMessage(group_openid: string, data: QQ.Message.Request) {
return this.http().post<{
group_code: string
msg: string
// id: string
// timestamp: number
} & {
code: number
message: string
data: any
}>(`/v2/groups/${group_openid}/messages`, data)
}

// /v2/channels/{channel_id}/messages new api?
async sendFilePrivate(openid: string, data: QQ.SendFileParams) {
return this.http().post<QQ.SendFileResponse>(`/v2/users/${openid}/files`, data)
async sendFilePrivate(openid: string, data: QQ.Message.File.Request) {
return this.http().post<QQ.Message.File.Response>(`/v2/users/${openid}/files`, data)
}

async sendFileGuild(group_openid: string, data: QQ.SendFileParams) {
return this.http().post<QQ.SendFileResponse>(`/v2/groups/${group_openid}/files`, data)
async sendFileGuild(group_openid: string, data: QQ.Message.File.Request) {
return this.http().post<QQ.Message.File.Response>(`/v2/groups/${group_openid}/files`, data)
}

// @TODO enum
Expand Down
10 changes: 9 additions & 1 deletion adapters/qq/src/internal/guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ export class GuildInternal {
return message
}

async sendMessage(channelId: string, data: QQ.Message.ChannelRequest) {
return this.http().post<QQ.Message>(`/channels/${channelId}/messages`, data)
}

async sendDM(guildId: string, data: QQ.Message.ChannelRequest) {
return this.http().post<QQ.Message>(`/dms/${guildId}/messages`, data)
}

async deleteMessage(channel_id: string, message_id: string, hidetip = false) {
return this.http().delete(`/channels/${channel_id}/messages/${message_id}?hidetip=${hidetip.toString()}`)
}
Expand Down Expand Up @@ -258,7 +266,7 @@ export class GuildInternal {
}>(`/channels/${channel_id}/threads/${thread_id}`)
}

async createPost(channel_id: string, data: QQ.CreatePostRequest) {
async createPost(channel_id: string, data: QQ.Forum.CreatePostRequest) {
return this.http().put<{
task_id: string
create_time: string
Expand Down
86 changes: 75 additions & 11 deletions adapters/qq/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class QQGuildMessageEncoder<C extends Context = Context> extends MessageE
msg_id = null
}

let r: QQ.Message
let r: Partial<QQ.Message.Response>
this.bot.logger.debug('use form data %s', useFormData)
try {
if (useFormData) {
Expand All @@ -58,7 +58,7 @@ export class QQGuildMessageEncoder<C extends Context = Context> extends MessageE
image: this.fileUrl,
},
...(this.reference ? {
messageReference: {
message_reference: {
message_id: this.reference,
},
} : {}),
Expand Down Expand Up @@ -86,8 +86,24 @@ export class QQGuildMessageEncoder<C extends Context = Context> extends MessageE
}

// https://bot.q.qq.com/wiki/develop/api/gateway/direct_message.html#%E6%B3%A8%E6%84%8F
// this.results.push(session.event.message)
// session.app.emit(session, 'send', session)
/**
* active msg, http 202: {"code":304023,"message":"push message is waiting for audit now","data":{"message_audit":{"audit_id":"xxx"}}}
* passive msg, http 200: Partial<QQ.Message>
*/
if (r.id) {
session.messageId = r.id
session.app.emit(session, 'send', session)
this.results.push(session.event.message)
} else if (r.code === 304023 && this.bot.config.parent.intents & QQ.Intents.MESSAGE_AUDIT) {
try {
const auditData: QQ.MessageAudited = await this.audit(r.data.message_audit.audit_id)
session.messageId = auditData.message_id
session.app.emit(session, 'send', session)
this.results.push(session.event.message)
} catch (e) {
this.bot.logger.error(e)
}
}
this.content = ''
this.file = null
this.filename = null
Expand All @@ -96,6 +112,25 @@ export class QQGuildMessageEncoder<C extends Context = Context> extends MessageE
this.retry = false
}

async audit(audit_id: string): Promise<QQ.MessageAudited> {
return new Promise((resolve, reject) => {
const dispose = this.bot.ctx.on('qq/message-audit-pass', (data) => {
if (data.audit_id === audit_id) {
dispose()
dispose2()
resolve(data)
}
})
const dispose2 = this.bot.ctx.on('qq/message-audit-reject', (data) => {
if (data.audit_id === audit_id) {
dispose()
dispose2()
reject(data)
}
})
})
}

async resolveFile(attrs: Dict, download = false) {
if (attrs) this.resource = attrs
if (!download && !await this.bot.ctx.http.isPrivate(this.resource.url)) {
Expand Down Expand Up @@ -150,7 +185,7 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
private content: string = ''
private useMarkdown = false
private rows: QQ.Button[][] = []
private attachedFile: QQ.SendFileResponse
private attachedFile: QQ.Message.File.Response

// 先图后文
async flush() {
Expand All @@ -162,21 +197,21 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
msg_id = this.options.session.messageId
msg_seq = ++this.options.session['seq']
}
const data: QQ.SendMessageParams = {
const data: QQ.Message.Request = {
content: this.content,
msg_type: QQ.MessageType.TEXT,
msg_type: QQ.Message.Type.TEXT,
timestamp: Math.floor(Date.now() / 1000),
msg_id,
msg_seq,
}
if (this.attachedFile) {
if (!data.content.length) data.content = ' '
data.media = this.attachedFile
data.msg_type = QQ.MessageType.MEDIA
data.msg_type = QQ.Message.Type.MEDIA
}

if (this.useMarkdown) {
data.msg_type = QQ.MessageType.MARKDOWN
data.msg_type = QQ.Message.Type.MARKDOWN
delete data.content
data.markdown = {
content: escapeMarkdown(this.content) || ' ',
Expand All @@ -201,6 +236,16 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
if (resp.msg !== 'success') {
this.bot.logger.warn(resp)
}
if (resp.code === 304023 && this.bot.config.intents & QQ.Intents.MESSAGE_AUDIT) {
try {
const auditData: QQ.MessageAudited = await this.audit(resp.data.message_audit.audit_id)
session.messageId = auditData.message_id
session.app.emit(session, 'send', session)
this.results.push(session.event.message)
} catch (e) {
this.bot.logger.error(e)
}
}
}
} catch (e) {
if (!Quester.isAxiosError(e)) throw e
Expand All @@ -215,6 +260,25 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
this.rows = []
}

async audit(audit_id: string): Promise<QQ.MessageAudited> {
return new Promise((resolve, reject) => {
const dispose = this.bot.ctx.on('qq/message-audit-pass', (data) => {
if (data.audit_id === audit_id) {
dispose()
dispose2()
resolve(data)
}
})
const dispose2 = this.bot.ctx.on('qq/message-audit-reject', (data) => {
if (data.audit_id === audit_id) {
dispose()
dispose2()
reject(data)
}
})
})
}

async sendFile(type: string, attrs: Dict) {
let url = attrs.url, entry: Entry | undefined
if (await this.bot.ctx.http.isPrivate(url)) {
Expand All @@ -230,12 +294,12 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
if (type === 'image') file_type = 1
else if (type === 'video') file_type = 2
else return
const data: QQ.SendFileParams = {
const data: QQ.Message.File.Request = {
file_type,
url,
srv_send_msg: false,
}
let res: QQ.SendFileResponse
let res: QQ.Message.File.Response
try {
if (this.session.isDirect) {
res = await this.bot.internal.sendFilePrivate(this.options.session.event.message.user.id, data)
Expand Down
Loading

0 comments on commit 6aee80d

Please sign in to comment.