diff --git a/src/client/TelegramChat.ts b/src/client/TelegramChat.ts index 90fd6a5..a9e6281 100644 --- a/src/client/TelegramChat.ts +++ b/src/client/TelegramChat.ts @@ -31,6 +31,11 @@ export default class TelegramChat { return await this.client.sendMessage(this.entity, params); } + public async getMessage(params: Parameters[1]) { + const messages = await this.client.getMessages(this.entity, params); + return messages[0]; + } + public async sendSelfDestructingPhoto(params: SendMessageParams, photo: CustomFile, ttlSeconds: number) { // @ts-ignore 定义不好好写的?你家 `FileLike` 明明可以是 `TypeInputMedia` params.file = new Api.InputMediaUploadedPhoto({ diff --git a/src/models/ForwardPairs.ts b/src/models/ForwardPairs.ts index 3d0426a..92b152c 100644 --- a/src/models/ForwardPairs.ts +++ b/src/models/ForwardPairs.ts @@ -18,7 +18,7 @@ export default class ForwardPairs { } // 在 forwardController 创建时初始化 - private async init(oicq: OicqClient, tgBot: Telegram) { + private async init(oicq: OicqClient, tgBot: Telegram, tgUser: Telegram) { const dbValues = await db.forwardPair.findMany({ where: { instanceId: this.instanceId }, }); @@ -26,8 +26,9 @@ export default class ForwardPairs { try { const qq = oicq.getChat(Number(i.qqRoomId)); const tg = await tgBot.getChat(Number(i.tgChatId)); - if (qq && tg) { - this.pairs.push(new Pair(qq, tg, i.id, i.flags)); + const tgUserChat = await tgUser.getChat(Number(i.tgChatId)); + if (qq && tg && tgUserChat) { + this.pairs.push(new Pair(qq, tg, tgUserChat, i.id, i.flags)); } } catch (e) { @@ -36,13 +37,13 @@ export default class ForwardPairs { } } - public static async load(instanceId: number, oicq: OicqClient, tgBot: Telegram) { + public static async load(instanceId: number, oicq: OicqClient, tgBot: Telegram, tgUser: Telegram) { const instance = new this(instanceId); - await instance.init(oicq, tgBot); + await instance.init(oicq, tgBot, tgUser); return instance; } - public async add(qq: Friend | Group, tg: TelegramChat) { + public async add(qq: Friend | Group, tg: TelegramChat, tgUser: TelegramChat) { const dbEntry = await db.forwardPair.create({ data: { qqRoomId: qq instanceof Friend ? qq.user_id : -qq.group_id, @@ -50,7 +51,7 @@ export default class ForwardPairs { instanceId: this.instanceId, }, }); - this.pairs.push(new Pair(qq, tg, dbEntry.id, dbEntry.flags)); + this.pairs.push(new Pair(qq, tg, tgUser, dbEntry.id, dbEntry.flags)); return dbEntry; } diff --git a/src/models/Instance.ts b/src/models/Instance.ts index b83169e..d7a056f 100644 --- a/src/models/Instance.ts +++ b/src/models/Instance.ts @@ -145,7 +145,7 @@ export default class Instance { this.log.info('OICQ 登录完成'); } this.statusReportController = new StatusReportController(this, this.tgBot, this.tgUser, this.oicq); - this.forwardPairs = await ForwardPairs.load(this.id, this.oicq, this.tgBot); + this.forwardPairs = await ForwardPairs.load(this.id, this.oicq, this.tgBot, this.tgUser); this.setupCommands() .then(() => this.log.info('命令设置成功')) .catch(e => this.log.error('命令设置错误', e)); diff --git a/src/models/Pair.ts b/src/models/Pair.ts index 47f1362..c6de07f 100644 --- a/src/models/Pair.ts +++ b/src/models/Pair.ts @@ -16,6 +16,7 @@ export class Pair { constructor( public readonly qq: Friend | Group, private _tg: TelegramChat, + public readonly tgUser: TelegramChat, public dbId: number, private _flags: number, ) { diff --git a/src/services/ConfigService.ts b/src/services/ConfigService.ts index 93deb08..3371fbb 100644 --- a/src/services/ConfigService.ts +++ b/src/services/ConfigService.ts @@ -200,7 +200,7 @@ export default class ConfigService { // 关联写入数据库 const chatForBot = await this.tgBot.getChat(chat.id); status && await status.edit({ text: '正在写数据库…' }); - const dbPair = await this.instance.forwardPairs.add(room, chatForBot); + const dbPair = await this.instance.forwardPairs.add(room, chatForBot, chat); isFinish = true; // 更新头像 @@ -253,7 +253,8 @@ export default class ConfigService { try { const qGroup = this.oicq.getChat(qqRoomId) as Group; const tgChat = await this.tgBot.getChat(tgChatId); - await this.instance.forwardPairs.add(qGroup, tgChat); + const tgUserChat = await this.tgUser.getChat(tgChatId); + await this.instance.forwardPairs.add(qGroup, tgChat, tgUserChat); await tgChat.sendMessage(`QQ群:${qGroup.name} (${qGroup.group_id})已与 ` + `Telegram 群 ${(tgChat.entity as Api.Channel).title} (${tgChatId})关联`); if (!(tgChat.entity instanceof Api.Channel)) { diff --git a/src/services/ForwardService.ts b/src/services/ForwardService.ts index 3e8401f..956bea9 100644 --- a/src/services/ForwardService.ts +++ b/src/services/ForwardService.ts @@ -415,6 +415,7 @@ export default class ForwardService { } catch (e) { if (richHeaderUsed) { + richHeaderUsed = false; this.log.warn('Rich Header 发送错误', messageToSend.file, e); delete messageToSend.file; delete messageToSend.linkPreview; @@ -425,6 +426,23 @@ export default class ForwardService { else throw e; } + if (richHeaderUsed) { + // 测试 Web Preview 内容是否被正确获取 + setTimeout(async () => { + // Telegram Bot 账号无法获取 Web 预览内容,只能用 User 账号获取 + const userMessage = await pair.tgUser.getMessage({ + ids: tgMessage.id, + }); + if (['WebPage', 'WebPageNotModified'].includes((userMessage.media as Api.MessageMediaWebPage)?.webpage?.className)) + return; + // 没有正常获取的话,就加上原先的头部 + this.log.warn('Rich Header 回测错误', messageToSend.file); + await tgMessage.edit({ + text: messageHeader + (message && messageHeader ? '\n' : '') + message, + }); + }, 3000); + } + if (this.instance.workMode === 'personal' && event.message_type === 'group' && event.atall) { await tgMessage.pin({ notify: false }); }