fix: 通过回测解决 Rich Header 抽风无法加载时出现没有发送者信息的消息

This commit is contained in:
Clansty 2024-02-09 15:57:58 +08:00
parent f627a94e69
commit d2a60b3cd0
No known key found for this signature in database
GPG Key ID: 3A6BE8BAF2EDE134
6 changed files with 36 additions and 10 deletions

View File

@ -31,6 +31,11 @@ export default class TelegramChat {
return await this.client.sendMessage(this.entity, params);
}
public async getMessage(params: Parameters<typeof this.client.getMessages>[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({

View File

@ -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;
}

View File

@ -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));

View File

@ -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,
) {

View File

@ -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} (<code>${qGroup.group_id}</code>)已与 ` +
`Telegram 群 ${(tgChat.entity as Api.Channel).title} (<code>${tgChatId}</code>)关联`);
if (!(tgChat.entity instanceof Api.Channel)) {

View File

@ -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 });
}