fix: Music sign return null then throw exception

This commit is contained in:
linyuchen 2024-05-07 17:39:44 +08:00
parent e9d43a9449
commit 58e6e3cbda

View File

@ -48,22 +48,28 @@ function checkSendMessage(sendMsgList: OB11MessageData[]) {
let data = msg['data'] let data = msg['data']
if (type === 'text' && !data['text']) { if (type === 'text' && !data['text']) {
return 400 return 400
} else if (['image', 'voice', 'record'].includes(type)) { }
else if (['image', 'voice', 'record'].includes(type)) {
if (!data['file']) { if (!data['file']) {
return 400 return 400
} else { }
else {
if (checkUri(data['file'])) { if (checkUri(data['file'])) {
return 200 return 200
} else { }
else {
return 400 return 400
} }
} }
} else if (type === 'at' && !data['qq']) { }
return 400 else if (type === 'at' && !data['qq']) {
} else if (type === 'reply' && !data['id']) {
return 400 return 400
} }
} else { else if (type === 'reply' && !data['id']) {
return 400
}
}
else {
return 400 return 400
} }
} }
@ -85,10 +91,12 @@ export function convertMessage2List(message: OB11MessageMixType, autoEscape = fa
}, },
}, },
] ]
} else { }
else {
message = decodeCQCode(message.toString()) message = decodeCQCode(message.toString())
} }
} else if (!Array.isArray(message)) { }
else if (!Array.isArray(message)) {
message = [message] message = [message]
} }
return message return message
@ -106,172 +114,172 @@ export async function createSendElements(
continue continue
} }
switch (sendMsg.type) { switch (sendMsg.type) {
case OB11MessageDataType.text: case OB11MessageDataType.text: {
{ const text = sendMsg.data?.text
const text = sendMsg.data?.text if (text) {
if (text) { sendElements.push(SendMsgElementConstructor.text(sendMsg.data!.text))
sendElements.push(SendMsgElementConstructor.text(sendMsg.data!.text))
}
} }
}
break break
case OB11MessageDataType.at: case OB11MessageDataType.at: {
{ if (!target) {
if (!target) { continue
continue }
} let atQQ = sendMsg.data?.qq
let atQQ = sendMsg.data?.qq if (atQQ) {
if (atQQ) { atQQ = atQQ.toString()
atQQ = atQQ.toString() if (atQQ === 'all') {
if (atQQ === 'all') { // todo查询剩余的at全体次数
// todo查询剩余的at全体次数 const groupCode = (target as Group)?.groupCode
const groupCode = (target as Group)?.groupCode let remainAtAllCount = 1
let remainAtAllCount = 1 let isAdmin: boolean = true
let isAdmin: boolean = true if (groupCode) {
if (groupCode) { try {
try { remainAtAllCount = (await NTQQGroupApi.getGroupAtAllRemainCount(groupCode)).atInfo
remainAtAllCount = (await NTQQGroupApi.getGroupAtAllRemainCount(groupCode)).atInfo .RemainAtAllCountForUin
.RemainAtAllCountForUin log(`${groupCode}剩余at全体次数`, remainAtAllCount)
log(`${groupCode}剩余at全体次数`, remainAtAllCount) const self = await getGroupMember((target as Group)?.groupCode, selfInfo.uin)
const self = await getGroupMember((target as Group)?.groupCode, selfInfo.uin) isAdmin = self.role === GroupMemberRole.admin || self.role === GroupMemberRole.owner
isAdmin = self.role === GroupMemberRole.admin || self.role === GroupMemberRole.owner } catch (e) {
} catch (e) {}
}
if (isAdmin && remainAtAllCount > 0) {
sendElements.push(SendMsgElementConstructor.at(atQQ, atQQ, AtType.atAll, '全体成员'))
}
} else {
// const atMember = group?.members.find(m => m.uin == atQQ)
const atMember = await getGroupMember((target as Group)?.groupCode, atQQ)
if (atMember) {
sendElements.push(
SendMsgElementConstructor.at(atQQ, atMember.uid, AtType.atUser, atMember.cardName || atMember.nick),
)
} }
} }
if (isAdmin && remainAtAllCount > 0) {
sendElements.push(SendMsgElementConstructor.at(atQQ, atQQ, AtType.atAll, '全体成员'))
}
} }
} else {
break // const atMember = group?.members.find(m => m.uin == atQQ)
case OB11MessageDataType.reply: const atMember = await getGroupMember((target as Group)?.groupCode, atQQ)
{ if (atMember) {
let replyMsgId = sendMsg.data.id
if (replyMsgId) {
const replyMsg = await dbUtil.getMsgByShortId(parseInt(replyMsgId))
if (replyMsg) {
sendElements.push( sendElements.push(
SendMsgElementConstructor.reply( SendMsgElementConstructor.at(atQQ, atMember.uid, AtType.atUser, atMember.cardName || atMember.nick),
replyMsg.msgSeq,
replyMsg.msgId,
replyMsg.senderUin,
replyMsg.senderUin,
),
) )
} }
} }
} }
}
break break
case OB11MessageDataType.face: case OB11MessageDataType.reply: {
{ let replyMsgId = sendMsg.data.id
const faceId = sendMsg.data?.id if (replyMsgId) {
if (faceId) { const replyMsg = await dbUtil.getMsgByShortId(parseInt(replyMsgId))
sendElements.push(SendMsgElementConstructor.face(parseInt(faceId))) if (replyMsg) {
sendElements.push(
SendMsgElementConstructor.reply(
replyMsg.msgSeq,
replyMsg.msgId,
replyMsg.senderUin,
replyMsg.senderUin,
),
)
} }
} }
}
break
case OB11MessageDataType.face: {
const faceId = sendMsg.data?.id
if (faceId) {
sendElements.push(SendMsgElementConstructor.face(parseInt(faceId)))
}
}
break break
case OB11MessageDataType.mface: { case OB11MessageDataType.mface: {
sendElements.push( sendElements.push(
SendMsgElementConstructor.mface(sendMsg.data.emoji_package_id, sendMsg.data.emoji_id, sendMsg.data.key, sendMsg.data.summary), SendMsgElementConstructor.mface(sendMsg.data.emoji_package_id, sendMsg.data.emoji_id, sendMsg.data.key, sendMsg.data.summary),
) )
}break; }
break
case OB11MessageDataType.image: case OB11MessageDataType.image:
case OB11MessageDataType.file: case OB11MessageDataType.file:
case OB11MessageDataType.video: case OB11MessageDataType.video:
case OB11MessageDataType.voice: case OB11MessageDataType.voice: {
{ const data = (sendMsg as OB11MessageFile).data
const data = (sendMsg as OB11MessageFile).data let file = data.file
let file = data.file const payloadFileName = data?.name
const payloadFileName = data?.name if (file) {
if (file) { const cache = await dbUtil.getFileCache(file)
const cache = await dbUtil.getFileCache(file) if (cache) {
if (cache) { if (fs.existsSync(cache.filePath)) {
if (fs.existsSync(cache.filePath)) { file = 'file://' + cache.filePath
file = 'file://' + cache.filePath
} else if (cache.downloadFunc) {
await cache.downloadFunc()
file = cache.filePath
} else if (cache.url) {
file = cache.url
}
log('找到文件缓存', file)
} }
const { path, isLocal, fileName, errMsg } = await uri2local(file) else if (cache.downloadFunc) {
if (errMsg) { await cache.downloadFunc()
throw errMsg file = cache.filePath
} }
if (path) { else if (cache.url) {
if (!isLocal) { file = cache.url
// 只删除http和base64转过来的文件 }
deleteAfterSentFiles.push(path) log('找到文件缓存', file)
} }
if (sendMsg.type === OB11MessageDataType.file) { const { path, isLocal, fileName, errMsg } = await uri2local(file)
log('发送文件', path, payloadFileName || fileName) if (errMsg) {
sendElements.push(await SendMsgElementConstructor.file(path, payloadFileName || fileName)) throw errMsg
} else if (sendMsg.type === OB11MessageDataType.video) { }
log('发送视频', path, payloadFileName || fileName) if (path) {
let thumb = sendMsg.data?.thumb if (!isLocal) {
if (thumb) { // 只删除http和base64转过来的文件
let uri2LocalRes = await uri2local(thumb) deleteAfterSentFiles.push(path)
if (uri2LocalRes.success) { }
thumb = uri2LocalRes.path if (sendMsg.type === OB11MessageDataType.file) {
} log('发送文件', path, payloadFileName || fileName)
sendElements.push(await SendMsgElementConstructor.file(path, payloadFileName || fileName))
}
else if (sendMsg.type === OB11MessageDataType.video) {
log('发送视频', path, payloadFileName || fileName)
let thumb = sendMsg.data?.thumb
if (thumb) {
let uri2LocalRes = await uri2local(thumb)
if (uri2LocalRes.success) {
thumb = uri2LocalRes.path
} }
sendElements.push(await SendMsgElementConstructor.video(path, payloadFileName || fileName, thumb))
} else if (sendMsg.type === OB11MessageDataType.voice) {
sendElements.push(await SendMsgElementConstructor.ptt(path))
} else if (sendMsg.type === OB11MessageDataType.image) {
sendElements.push(
await SendMsgElementConstructor.pic(
path,
sendMsg.data.summary || '',
<PicSubType>parseInt(sendMsg.data?.subType?.toString()) || 0,
),
)
} }
sendElements.push(await SendMsgElementConstructor.video(path, payloadFileName || fileName, thumb))
}
else if (sendMsg.type === OB11MessageDataType.voice) {
sendElements.push(await SendMsgElementConstructor.ptt(path))
}
else if (sendMsg.type === OB11MessageDataType.image) {
sendElements.push(
await SendMsgElementConstructor.pic(
path,
sendMsg.data.summary || '',
<PicSubType>parseInt(sendMsg.data?.subType?.toString()) || 0,
),
)
} }
} }
} }
}
break break
case OB11MessageDataType.json: case OB11MessageDataType.json: {
{ sendElements.push(SendMsgElementConstructor.ark(sendMsg.data.data))
sendElements.push(SendMsgElementConstructor.ark(sendMsg.data.data)) }
}
break break
case OB11MessageDataType.poke: case OB11MessageDataType.poke: {
{ let qq = sendMsg.data?.qq || sendMsg.data?.id
let qq = sendMsg.data?.qq || sendMsg.data?.id if (qq) {
if (qq) { if ('groupCode' in target) {
if ('groupCode' in target) { crychic.sendGroupPoke(target.groupCode, qq.toString())
crychic.sendGroupPoke(target.groupCode, qq.toString())
} else {
if (!qq) {
qq = parseInt(target.uin)
}
crychic.sendFriendPoke(qq.toString())
}
sendElements.push(SendMsgElementConstructor.poke('', ''))
} }
else {
if (!qq) {
qq = parseInt(target.uin)
}
crychic.sendFriendPoke(qq.toString())
}
sendElements.push(SendMsgElementConstructor.poke('', ''))
} }
}
break break
case OB11MessageDataType.dice: case OB11MessageDataType.dice: {
{ const resultId = sendMsg.data?.result
const resultId = sendMsg.data?.result sendElements.push(SendMsgElementConstructor.dice(resultId))
sendElements.push(SendMsgElementConstructor.dice(resultId)) }
}
break break
case OB11MessageDataType.RPS: case OB11MessageDataType.RPS: {
{ const resultId = sendMsg.data?.result
const resultId = sendMsg.data?.result sendElements.push(SendMsgElementConstructor.rps(resultId))
sendElements.push(SendMsgElementConstructor.rps(resultId)) }
}
break break
} }
} }
@ -294,7 +302,8 @@ export async function sendMsg(
const returnMsg = await NTQQMsgApi.sendMsg(peer, sendElements, waitComplete, 20000) const returnMsg = await NTQQMsgApi.sendMsg(peer, sendElements, waitComplete, 20000)
log('消息发送结果', returnMsg) log('消息发送结果', returnMsg)
returnMsg.msgShortId = await dbUtil.addMsg(returnMsg) returnMsg.msgShortId = await dbUtil.addMsg(returnMsg)
deleteAfterSentFiles.map((f) => fs.unlink(f, () => {})) deleteAfterSentFiles.map((f) => fs.unlink(f, () => {
}))
return returnMsg return returnMsg
} }
@ -358,7 +367,8 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
if (friend) { if (friend) {
// peer.name = friend.nickName // peer.name = friend.nickName
peer.peerUid = friend.uid peer.peerUid = friend.uid
} else { }
else {
peer.chatType = ChatType.temp peer.chatType = ChatType.temp
const tempUserUid = getUidByUin(payload.user_id.toString()) const tempUserUid = getUidByUin(payload.user_id.toString())
if (!tempUserUid) { if (!tempUserUid) {
@ -371,11 +381,14 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
} }
if (payload?.group_id && payload.message_type === 'group') { if (payload?.group_id && payload.message_type === 'group') {
await genGroupPeer() await genGroupPeer()
} else if (payload?.user_id) { }
else if (payload?.user_id) {
genFriendPeer() genFriendPeer()
} else if (payload.group_id) { }
else if (payload.group_id) {
await genGroupPeer() await genGroupPeer()
} else { }
else {
throw '发送消息参数错误, 请指定group_id或user_id' throw '发送消息参数错误, 请指定group_id或user_id'
} }
const messages = convertMessage2List( const messages = convertMessage2List(
@ -389,7 +402,8 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
} catch (e) { } catch (e) {
throw '发送转发消息失败 ' + e.toString() throw '发送转发消息失败 ' + e.toString()
} }
} else if (this.getSpecialMsgNum(payload, OB11MessageDataType.music)) { }
else if (this.getSpecialMsgNum(payload, OB11MessageDataType.music)) {
const music = messages[0] as OB11MessageMusic const music = messages[0] as OB11MessageMusic
if (music) { if (music) {
const { musicSignUrl } = getConfigUtil().getConfig() const { musicSignUrl } = getConfigUtil().getConfig()
@ -406,20 +420,20 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
;(postData as CustomMusicSignPostData).singer = music.data.content ;(postData as CustomMusicSignPostData).singer = music.data.content
delete (postData as OB11MessageCustomMusic['data']).content delete (postData as OB11MessageCustomMusic['data']).content
} }
if (type === 'custom'){ if (type === 'custom') {
const customMusicData = music.data as CustomMusicSignPostData const customMusicData = music.data as CustomMusicSignPostData
if (!customMusicData.url){ if (!customMusicData.url) {
throw ('自定义音卡缺少参数url'); throw ('自定义音卡缺少参数url')
} }
if (!customMusicData.audio){ if (!customMusicData.audio) {
throw('自定义音卡缺少参数audio'); throw ('自定义音卡缺少参数audio')
} }
if (!customMusicData.title){ if (!customMusicData.title) {
throw('自定义音卡缺少参数title'); throw ('自定义音卡缺少参数title')
} }
} }
if (type === 'qq' || type === '163') { if (type === 'qq' || type === '163') {
const idMusicData = music.data as IdMusicSignPostData; const idMusicData = music.data as IdMusicSignPostData
if (!idMusicData.id) { if (!idMusicData.id) {
throw '音乐卡片缺少id参数' throw '音乐卡片缺少id参数'
} }
@ -427,6 +441,9 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
let jsonContent: string let jsonContent: string
try { try {
jsonContent = await new MusicSign(musicSignUrl).sign(postData) jsonContent = await new MusicSign(musicSignUrl).sign(postData)
if (!jsonContent){
throw '音乐消息生成失败,可能是签名服务器问题, 请检查提交内容'
}
} catch (e) { } catch (e) {
throw `签名音乐消息失败:${e}` throw `签名音乐消息失败:${e}`
} }
@ -444,7 +461,8 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
} }
} }
const returnMsg = await sendMsg(peer, sendElements, deleteAfterSentFiles) const returnMsg = await sendMsg(peer, sendElements, deleteAfterSentFiles)
deleteAfterSentFiles.map((f) => fs.unlink(f, () => {})) deleteAfterSentFiles.map((f) => fs.unlink(f, () => {
}))
return { message_id: returnMsg.msgShortId } return { message_id: returnMsg.msgShortId }
} }
@ -502,7 +520,8 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
let nodeMsg = await dbUtil.getMsgByShortId(parseInt(nodeId)) let nodeMsg = await dbUtil.getMsgByShortId(parseInt(nodeId))
if (!needClone) { if (!needClone) {
nodeMsgIds.push(nodeMsg.msgId) nodeMsgIds.push(nodeMsg.msgId)
} else { }
else {
if (nodeMsg.peerUid !== selfInfo.uid) { if (nodeMsg.peerUid !== selfInfo.uid) {
const cloneMsg = await this.cloneMsg(nodeMsg) const cloneMsg = await this.cloneMsg(nodeMsg)
if (cloneMsg) { if (cloneMsg) {
@ -510,7 +529,8 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
} }
} }
} }
} else { }
else {
// 自定义的消息 // 自定义的消息
// 提取消息段发给自己生成消息id // 提取消息段发给自己生成消息id
try { try {
@ -532,7 +552,8 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
} }
sendElementsSplit[splitIndex] = [ele] sendElementsSplit[splitIndex] = [ele]
splitIndex++ splitIndex++
} else { }
else {
sendElementsSplit[splitIndex].push(ele) sendElementsSplit[splitIndex].push(ele)
} }
log(sendElementsSplit) log(sendElementsSplit)
@ -544,7 +565,8 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
await sleep(500) await sleep(500)
log('转发节点生成成功', nodeMsg.msgId) log('转发节点生成成功', nodeMsg.msgId)
} }
deleteAfterSentFiles.map((f) => fs.unlink(f, () => {})) deleteAfterSentFiles.map((f) => fs.unlink(f, () => {
}))
} catch (e) { } catch (e) {
log('生成转发消息节点失败', e) log('生成转发消息节点失败', e)
} }
@ -561,7 +583,8 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
nodeMsgArray.push(nodeMsg) nodeMsgArray.push(nodeMsg)
if (!srcPeer) { if (!srcPeer) {
srcPeer = { chatType: nodeMsg.chatType, peerUid: nodeMsg.peerUid } srcPeer = { chatType: nodeMsg.chatType, peerUid: nodeMsg.peerUid }
} else if (srcPeer.peerUid !== nodeMsg.peerUid) { }
else if (srcPeer.peerUid !== nodeMsg.peerUid) {
needSendSelf = true needSendSelf = true
srcPeer = selfPeer srcPeer = selfPeer
} }