mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
feat: sendMessage
This commit is contained in:
parent
b900d25c38
commit
401dfa5399
41
src/laana-v0.1.2/action/message.ts
Normal file
41
src/laana-v0.1.2/action/message.ts
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
import { NapCatLaanaAdapter } from '@/laana-v0.1.2';
|
||||||
|
import { NapCatCore } from '@/core';
|
||||||
|
import { LaanaActionHandler } from '@/laana-v0.1.2/action/index';
|
||||||
|
import fs from 'fs';
|
||||||
|
|
||||||
|
export class LaanaMessageActionHandler {
|
||||||
|
constructor(
|
||||||
|
public core: NapCatCore,
|
||||||
|
public laana: NapCatLaanaAdapter,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
impl: LaanaActionHandler = {
|
||||||
|
sendMessage: async (params) => {
|
||||||
|
const { elements, fileCacheIds } = await this.laana.utils.msg.laanaMessageToRaw(params.message!, params);
|
||||||
|
let cacheSize = 0;
|
||||||
|
try {
|
||||||
|
for (const cacheId of fileCacheIds) {
|
||||||
|
cacheSize += fs.statSync(this.laana.utils.file.toLocalPath(cacheId)).size;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
this.core.context.logger.logWarn('文件缓存大小计算失败', e);
|
||||||
|
}
|
||||||
|
const estimatedSendMsgTimeout =
|
||||||
|
cacheSize / 1024 / 256 * 1000 + // file upload time
|
||||||
|
1000 * fileCacheIds.length + // request timeout
|
||||||
|
10000; // fallback timeout
|
||||||
|
const sentMsg = await this.core.apis.MsgApi.sendMsg(
|
||||||
|
await this.laana.utils.msg.laanaPeerToRaw(params.targetPeer!),
|
||||||
|
elements,
|
||||||
|
true, // TODO: add 'wait complete' (bool) field
|
||||||
|
estimatedSendMsgTimeout,
|
||||||
|
);
|
||||||
|
if (!sentMsg) {
|
||||||
|
throw '消息发送失败';
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
msgId: sentMsg.msgId
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user