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,
ElementType,
SendFaceElement,
SendFileElement,
SendPicElement,
SendPttElement,
SendReplyElement,
SendTextElement
} from "./types";
import {NTQQApi} from "./ntcall";
import {encodeSilk} from "../common/utils";
import {encodeSilk, log} from "../common/utils";
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> {
const {converted, path: silkPath, duration} = await encodeSilk(pttPath);
// log("生成语音", silkPath, duration);
const {md5, fileName, path, fileSize} = await NTQQApi.uploadFile(silkPath, ElementType.PTT);
if (converted){
fs.unlink(silkPath, ()=>{});
if (converted) {
fs.unlink(silkPath, () => {
});
}
return {
elementType: ElementType.PTT,

View File

@ -62,6 +62,7 @@ export interface GroupMember {
export enum ElementType {
TEXT = 1,
PIC = 2,
FILE = 3,
PTT = 4,
FACE = 6,
REPLY = 7,
@ -136,7 +137,28 @@ export interface SendFaceElement {
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 {
notAt = 0,
@ -206,6 +228,31 @@ export interface FaceElement {
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 {
msgId: string;
msgShortId?: number; // 自己维护的消息id
@ -239,6 +286,7 @@ export interface RawMessage {
arkElement: ArkElement;
grayTipElement: GrayTipElement;
faceElement: FaceElement;
videoElement: VideoElement
}[];
}
@ -276,12 +324,12 @@ export interface GroupNotify {
warningTips: string
}
export enum GroupRequestOperateTypes{
export enum GroupRequestOperateTypes {
approve = 1,
reject = 2
}
export interface FriendRequest{
export interface FriendRequest {
friendUid: string,
reqTime: string, // 时间戳,秒
extWords: string, // 申请人填写的验证消息
@ -290,7 +338,8 @@ export interface FriendRequest{
sourceId: number,
groupCode: string
}
export interface FriendRequestNotify{
export interface FriendRequestNotify {
data: {
unreadNums: number,
buddyReqs: FriendRequest[]

View File

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

View File

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

View File

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