feat: 发送视频和文件

This commit is contained in:
linyuchen 2024-02-27 01:28:42 +08:00
parent 1f657f3e84
commit f5ac499861
6 changed files with 121 additions and 23 deletions

View File

@ -2,13 +2,14 @@ import {
AtType, AtType,
ElementType, ElementType,
SendFaceElement, SendFaceElement,
SendFileElement,
SendPicElement, SendPicElement,
SendPttElement, SendPttElement,
SendReplyElement, SendReplyElement,
SendTextElement SendTextElement
} from "./types"; } from "./types";
import {NTQQApi} from "./ntcall"; import {NTQQApi} from "./ntcall";
import {encodeSilk} from "../common/utils"; import {encodeSilk, log} from "../common/utils";
import fs from "fs"; import fs from "fs";
@ -80,12 +81,34 @@ export class SendMsgElementConstructor {
}; };
} }
static async file(filePath: string, isVideo:boolean = false): Promise<SendFileElement> {
const {md5, fileName, path, fileSize} = await NTQQApi.uploadFile(filePath);
let element: SendFileElement = {
elementType: ElementType.FILE,
elementId: "",
fileElement: {
fileName,
"filePath": path,
"fileSize": (fileSize).toString(),
}
}
if (isVideo){
element.fileElement.picHeight = 0;
element.fileElement.picWidth = 0;
}
return element;
}
static video(filePath: string): Promise<SendFileElement> {
return SendMsgElementConstructor.file(filePath, true);
}
static async ptt(pttPath: string): Promise<SendPttElement> { static async ptt(pttPath: string): Promise<SendPttElement> {
const {converted, path: silkPath, duration} = await encodeSilk(pttPath); const {converted, path: silkPath, duration} = await encodeSilk(pttPath);
// log("生成语音", silkPath, duration); // log("生成语音", silkPath, duration);
const {md5, fileName, path, fileSize} = await NTQQApi.uploadFile(silkPath, ElementType.PTT); const {md5, fileName, path, fileSize} = await NTQQApi.uploadFile(silkPath, ElementType.PTT);
if (converted){ if (converted) {
fs.unlink(silkPath, ()=>{}); fs.unlink(silkPath, () => {
});
} }
return { return {
elementType: ElementType.PTT, elementType: ElementType.PTT,

View File

@ -62,6 +62,7 @@ export interface GroupMember {
export enum ElementType { export enum ElementType {
TEXT = 1, TEXT = 1,
PIC = 2, PIC = 2,
FILE = 3,
PTT = 4, PTT = 4,
FACE = 6, FACE = 6,
REPLY = 7, REPLY = 7,
@ -136,7 +137,28 @@ export interface SendFaceElement {
faceElement: FaceElement faceElement: FaceElement
} }
export type SendMessageElement = SendTextElement | SendPttElement | SendPicElement | SendReplyElement | SendFaceElement export interface SendFileElement {
"elementType": ElementType.FILE,
"elementId": "",
"fileElement": {
"fileMd5"?: "",
"fileName": string,
"filePath": string,
"fileSize": string,
"picHeight"?: number,
"picWidth"?: number,
"picThumbPath"?: {},
"file10MMd5"?: "",
"fileSha"?: "",
"fileSha3"?: "",
"fileUuid"?: "",
"fileSubId"?: "",
"thumbFileSize"?: number
}
}
export type SendMessageElement = SendTextElement | SendPttElement |
SendPicElement | SendReplyElement | SendFaceElement | SendFileElement
export enum AtType { export enum AtType {
notAt = 0, notAt = 0,
@ -206,6 +228,31 @@ export interface FaceElement {
faceType: 1 faceType: 1
} }
export interface VideoElement {
"filePath": string,
"fileName": string,
"videoMd5": string,
"thumbMd5": string
"fileTime": 87, // second
"thumbSize": 314235, // byte
"fileFormat": 2, // 2表示mp4
"fileSize": string, // byte
"thumbWidth": number,
"thumbHeight": number,
"busiType": 0, // 未知
"subBusiType": 0, // 未知
"thumbPath": {},
"transferStatus": 0, // 未知
"progress": 0, // 下载进度?
"invalidState": 0, // 未知
"fileUuid": string, // 可以用于下载链接?
"fileSubId": "",
"fileBizId": null,
"originVideoMd5": "",
"import_rich_media_context": null,
"sourceVideoCodecFormat": 0
}
export interface RawMessage { export interface RawMessage {
msgId: string; msgId: string;
msgShortId?: number; // 自己维护的消息id msgShortId?: number; // 自己维护的消息id
@ -239,6 +286,7 @@ export interface RawMessage {
arkElement: ArkElement; arkElement: ArkElement;
grayTipElement: GrayTipElement; grayTipElement: GrayTipElement;
faceElement: FaceElement; faceElement: FaceElement;
videoElement: VideoElement
}[]; }[];
} }
@ -276,12 +324,12 @@ export interface GroupNotify {
warningTips: string warningTips: string
} }
export enum GroupRequestOperateTypes{ export enum GroupRequestOperateTypes {
approve = 1, approve = 1,
reject = 2 reject = 2
} }
export interface FriendRequest{ export interface FriendRequest {
friendUid: string, friendUid: string,
reqTime: string, // 时间戳,秒 reqTime: string, // 时间戳,秒
extWords: string, // 申请人填写的验证消息 extWords: string, // 申请人填写的验证消息
@ -290,7 +338,8 @@ export interface FriendRequest{
sourceId: number, sourceId: number,
groupCode: string groupCode: string
} }
export interface FriendRequestNotify{
export interface FriendRequestNotify {
data: { data: {
unreadNums: number, unreadNums: number,
buddyReqs: FriendRequest[] buddyReqs: FriendRequest[]

View File

@ -10,6 +10,7 @@ import * as fs from "fs";
import {log} from "../../common/utils"; import {log} from "../../common/utils";
import {v4 as uuidv4} from "uuid" import {v4 as uuidv4} from "uuid"
import {decodeCQCode} from "../cqcode"; import {decodeCQCode} from "../cqcode";
import {Send} from "express";
function checkSendMessage(sendMsgList: OB11MessageData[]) { function checkSendMessage(sendMsgList: OB11MessageData[]) {
function checkUri(uri: string): boolean { function checkUri(uri: string): boolean {
@ -145,13 +146,13 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
peerUid: selfInfo.uid peerUid: selfInfo.uid
} }
let nodeIds: string[] = [] let nodeIds: string[] = []
for (const messageNode of messageNodes){ for (const messageNode of messageNodes) {
// 一个node表示一个人的消息 // 一个node表示一个人的消息
let nodeId = messageNode.data.id; let nodeId = messageNode.data.id;
// 有nodeId表示一个子转发消息卡片 // 有nodeId表示一个子转发消息卡片
if (nodeId) { if (nodeId) {
let nodeMsg = getHistoryMsgByShortId(nodeId); let nodeMsg = getHistoryMsgByShortId(nodeId);
if (nodeMsg){ if (nodeMsg) {
nodeIds.push(nodeMsg.msgId); nodeIds.push(nodeMsg.msgId);
} }
} else { } else {
@ -230,6 +231,8 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
} }
break; break;
case OB11MessageDataType.image: case OB11MessageDataType.image:
case OB11MessageDataType.file:
case OB11MessageDataType.video:
case OB11MessageDataType.voice: { case OB11MessageDataType.voice: {
const file = sendMsg.data?.file const file = sendMsg.data?.file
if (file) { if (file) {
@ -238,11 +241,13 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
if (!isLocal) { // 只删除http和base64转过来的文件 if (!isLocal) { // 只删除http和base64转过来的文件
deleteAfterSentFiles.push(path) deleteAfterSentFiles.push(path)
} }
if (sendMsg.type === OB11MessageDataType.image) { const constructorMap = {
sendElements.push(await SendMsgElementConstructor.pic(path)) [OB11MessageDataType.image]: SendMsgElementConstructor.pic,
} else { [OB11MessageDataType.voice]: SendMsgElementConstructor.ptt,
sendElements.push(await SendMsgElementConstructor.ptt(path)) [OB11MessageDataType.video]: SendMsgElementConstructor.video,
[OB11MessageDataType.file]: SendMsgElementConstructor.file,
} }
sendElements.push(await constructorMap[sendMsg.type](path));
} }
} }
} }

View File

@ -88,6 +88,14 @@ export class OB11Constructor {
continue; continue;
} }
message_data["data"]["text"] = text message_data["data"]["text"] = text
} else if (element.replyElement) {
message_data["type"] = "reply"
const replyMsg = getHistoryMsgBySeq(element.replyElement.replayMsgSeq)
if (replyMsg) {
message_data["data"]["id"] = replyMsg.msgShortId.toString()
} else {
continue
}
} else if (element.picElement) { } else if (element.picElement) {
message_data["type"] = "image" message_data["type"] = "image"
message_data["data"]["file_id"] = element.picElement.fileUuid message_data["data"]["file_id"] = element.picElement.fileUuid
@ -99,13 +107,15 @@ export class OB11Constructor {
element.elementId, element.picElement.thumbPath.get(0), element.picElement.sourcePath) element.elementId, element.picElement.thumbPath.get(0), element.picElement.sourcePath)
} catch (e) { } catch (e) {
} }
} else if (element.replyElement) { } else if (element.videoElement) {
message_data["type"] = "reply" message_data["type"] = OB11MessageDataType.video;
const replyMsg = getHistoryMsgBySeq(element.replyElement.replayMsgSeq) message_data["data"]["file"] = element.pttElement.filePath
if (replyMsg) { message_data["data"]["file_id"] = element.pttElement.fileUuid
message_data["data"]["id"] = replyMsg.msgShortId.toString() // 怎么拿到url呢
} else { try {
continue // await NTQQApi.downloadMedia(msg.msgId, msg.chatType, msg.peerUid,
// element.elementId, element.picElement.thumbPath.get(0), element.picElement.sourcePath)
} catch (e) {
} }
} else if (element.pttElement) { } else if (element.pttElement) {
message_data["type"] = OB11MessageDataType.voice; message_data["type"] = OB11MessageDataType.voice;
@ -125,6 +135,7 @@ export class OB11Constructor {
message_data["type"] = OB11MessageDataType.face; message_data["type"] = OB11MessageDataType.face;
message_data["data"]["id"] = element.faceElement.faceIndex.toString(); message_data["data"]["id"] = element.faceElement.faceIndex.toString();
} }
if (message_data.data.file) { if (message_data.data.file) {
let filePath: string = message_data.data.file; let filePath: string = message_data.data.file;
if (!enableLocalFile2Url) { if (!enableLocalFile2Url) {

View File

@ -3,7 +3,7 @@ import {EventType} from "../OB11BaseEvent";
export class OB11FriendRequestEvent extends OB11BaseNoticeEvent { export class OB11FriendRequestEvent extends OB11BaseNoticeEvent {
post_type = EventType.REQUEST // post_type = EventType.REQUEST
user_id: number; user_id: number;
request_type: "friend" = "friend"; request_type: "friend" = "friend";
comment: string; comment: string;

View File

@ -1,4 +1,4 @@
import {AtType, RawMessage} from "../ntqqapi/types"; import {RawMessage} from "../ntqqapi/types";
import {EventType} from "./event/OB11BaseEvent"; import {EventType} from "./event/OB11BaseEvent";
export interface OB11User { export interface OB11User {
@ -85,7 +85,9 @@ export interface OB11Return<DataType> {
export enum OB11MessageDataType { export enum OB11MessageDataType {
text = "text", text = "text",
image = "image", image = "image",
video = "video",
voice = "record", voice = "record",
file = "file",
at = "at", at = "at",
reply = "reply", reply = "reply",
json = "json", json = "json",
@ -115,6 +117,14 @@ export interface OB11MessageRecord extends OB11MessageFileBase {
type: OB11MessageDataType.voice type: OB11MessageDataType.voice
} }
export interface OB11MessageFile extends OB11MessageFileBase {
type: OB11MessageDataType.file
}
export interface OB11MessageVideo extends OB11MessageFileBase {
type: OB11MessageDataType.video
}
export interface OB11MessageAt { export interface OB11MessageAt {
type: OB11MessageDataType.at type: OB11MessageDataType.at
data: { data: {
@ -152,7 +162,7 @@ export type OB11MessageData =
OB11MessageText | OB11MessageText |
OB11MessageFace | OB11MessageFace |
OB11MessageAt | OB11MessageReply | OB11MessageAt | OB11MessageReply |
OB11MessageImage | OB11MessageRecord | OB11MessageImage | OB11MessageRecord | OB11MessageFile | OB11MessageVideo |
OB11MessageNode OB11MessageNode
export interface OB11PostSendMsg { export interface OB11PostSendMsg {