feat: 获取文件下载地址

This commit is contained in:
Clansty 2022-03-04 17:47:28 +08:00
parent 49c624343f
commit 52c3d68b4c
No known key found for this signature in database
GPG Key ID: 05F8479BA63A8E92
4 changed files with 44 additions and 2 deletions

View File

@ -0,0 +1,40 @@
import Telegram from '../client/Telegram';
import OicqClient from '../client/OicqClient';
import { Api } from 'telegram';
import db from '../providers/db';
import { Button } from 'telegram/tl/custom/button';
import { getLogger } from 'log4js';
const GET_FILE_REGEX = /^\/start file-(\d+)$/;
export default class FileController {
private readonly log = getLogger('FileController');
constructor(private readonly tgBot: Telegram,
private readonly oicq: OicqClient) {
tgBot.addNewMessageEventHandler(this.onTelegramMessage);
}
private onTelegramMessage = async (message: Api.Message) => {
if (!message.isPrivate || !message.message) return false;
if (!GET_FILE_REGEX.test(message.message)) return false;
const id = Number(GET_FILE_REGEX.exec(message.message)[1]);
try {
const fileInfo = await db.file.findFirst({
where: { id },
});
const downloadUrl = await this.oicq.getChat(Number(fileInfo.roomId)).getFileUrl(fileInfo.fileId);
await message.reply({
message: fileInfo.info,
buttons: Button.url('⏬下载', downloadUrl),
});
}
catch (e) {
this.log.error('获取文件下载地址失败', e);
await message.reply({
message: `获取文件下载地址失败:${e.message}\n${e}`,
});
}
return true;
};
}

View File

@ -34,7 +34,7 @@ export default class ForwardController {
await db.message.create({
data: {
qqRoomId: pair.qqRoomId,
qqSenderId: event.sender.user_id,
qqSenderId: event.user_id,
time: event.time,
brief: event.raw_message,
seq: event.seq,

View File

@ -5,6 +5,7 @@ import SetupController from './controllers/SetupController';
import OicqClient from './client/OicqClient';
import ConfigController from './controllers/ConfigController';
import ForwardController from './controllers/ForwardController';
import FileController from './controllers/FileController';
(async () => {
configure({
@ -49,4 +50,5 @@ import ForwardController from './controllers/ForwardController';
}
new ConfigController(tgBot, tgUser, oicq);
new ForwardController(tgBot, tgUser, oicq);
new FileController(tgBot, oicq);
})();

View File

@ -100,7 +100,7 @@ export default class ForwardService {
const dbEntry = await db.file.create({
data: { fileId: elem.fid, roomId: pair.qqRoomId, info: message },
});
button = Button.url('⏬ 获取下载地址',
button = Button.url('⏬获取下载地址',
`https://t.me/${this.tgBot.me.username}?start=file-${dbEntry.id}`);
}
break;