mirror of https://github.com/Nofated095/Q2TG.git
fix: 通过回测解决 Rich Header 抽风无法加载时出现没有发送者信息的消息
This commit is contained in:
parent
f627a94e69
commit
d2a60b3cd0
|
@ -31,6 +31,11 @@ export default class TelegramChat {
|
||||||
return await this.client.sendMessage(this.entity, params);
|
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) {
|
public async sendSelfDestructingPhoto(params: SendMessageParams, photo: CustomFile, ttlSeconds: number) {
|
||||||
// @ts-ignore 定义不好好写的?你家 `FileLike` 明明可以是 `TypeInputMedia`
|
// @ts-ignore 定义不好好写的?你家 `FileLike` 明明可以是 `TypeInputMedia`
|
||||||
params.file = new Api.InputMediaUploadedPhoto({
|
params.file = new Api.InputMediaUploadedPhoto({
|
||||||
|
|
|
@ -18,7 +18,7 @@ export default class ForwardPairs {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 在 forwardController 创建时初始化
|
// 在 forwardController 创建时初始化
|
||||||
private async init(oicq: OicqClient, tgBot: Telegram) {
|
private async init(oicq: OicqClient, tgBot: Telegram, tgUser: Telegram) {
|
||||||
const dbValues = await db.forwardPair.findMany({
|
const dbValues = await db.forwardPair.findMany({
|
||||||
where: { instanceId: this.instanceId },
|
where: { instanceId: this.instanceId },
|
||||||
});
|
});
|
||||||
|
@ -26,8 +26,9 @@ export default class ForwardPairs {
|
||||||
try {
|
try {
|
||||||
const qq = oicq.getChat(Number(i.qqRoomId));
|
const qq = oicq.getChat(Number(i.qqRoomId));
|
||||||
const tg = await tgBot.getChat(Number(i.tgChatId));
|
const tg = await tgBot.getChat(Number(i.tgChatId));
|
||||||
if (qq && tg) {
|
const tgUserChat = await tgUser.getChat(Number(i.tgChatId));
|
||||||
this.pairs.push(new Pair(qq, tg, i.id, i.flags));
|
if (qq && tg && tgUserChat) {
|
||||||
|
this.pairs.push(new Pair(qq, tg, tgUserChat, i.id, i.flags));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (e) {
|
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);
|
const instance = new this(instanceId);
|
||||||
await instance.init(oicq, tgBot);
|
await instance.init(oicq, tgBot, tgUser);
|
||||||
return instance;
|
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({
|
const dbEntry = await db.forwardPair.create({
|
||||||
data: {
|
data: {
|
||||||
qqRoomId: qq instanceof Friend ? qq.user_id : -qq.group_id,
|
qqRoomId: qq instanceof Friend ? qq.user_id : -qq.group_id,
|
||||||
|
@ -50,7 +51,7 @@ export default class ForwardPairs {
|
||||||
instanceId: this.instanceId,
|
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;
|
return dbEntry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -145,7 +145,7 @@ export default class Instance {
|
||||||
this.log.info('OICQ 登录完成');
|
this.log.info('OICQ 登录完成');
|
||||||
}
|
}
|
||||||
this.statusReportController = new StatusReportController(this, this.tgBot, this.tgUser, this.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()
|
this.setupCommands()
|
||||||
.then(() => this.log.info('命令设置成功'))
|
.then(() => this.log.info('命令设置成功'))
|
||||||
.catch(e => this.log.error('命令设置错误', e));
|
.catch(e => this.log.error('命令设置错误', e));
|
||||||
|
|
|
@ -16,6 +16,7 @@ export class Pair {
|
||||||
constructor(
|
constructor(
|
||||||
public readonly qq: Friend | Group,
|
public readonly qq: Friend | Group,
|
||||||
private _tg: TelegramChat,
|
private _tg: TelegramChat,
|
||||||
|
public readonly tgUser: TelegramChat,
|
||||||
public dbId: number,
|
public dbId: number,
|
||||||
private _flags: number,
|
private _flags: number,
|
||||||
) {
|
) {
|
||||||
|
|
|
@ -200,7 +200,7 @@ export default class ConfigService {
|
||||||
// 关联写入数据库
|
// 关联写入数据库
|
||||||
const chatForBot = await this.tgBot.getChat(chat.id);
|
const chatForBot = await this.tgBot.getChat(chat.id);
|
||||||
status && await status.edit({ text: '正在写数据库…' });
|
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;
|
isFinish = true;
|
||||||
|
|
||||||
// 更新头像
|
// 更新头像
|
||||||
|
@ -253,7 +253,8 @@ export default class ConfigService {
|
||||||
try {
|
try {
|
||||||
const qGroup = this.oicq.getChat(qqRoomId) as Group;
|
const qGroup = this.oicq.getChat(qqRoomId) as Group;
|
||||||
const tgChat = await this.tgBot.getChat(tgChatId);
|
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>)已与 ` +
|
await tgChat.sendMessage(`QQ群:${qGroup.name} (<code>${qGroup.group_id}</code>)已与 ` +
|
||||||
`Telegram 群 ${(tgChat.entity as Api.Channel).title} (<code>${tgChatId}</code>)关联`);
|
`Telegram 群 ${(tgChat.entity as Api.Channel).title} (<code>${tgChatId}</code>)关联`);
|
||||||
if (!(tgChat.entity instanceof Api.Channel)) {
|
if (!(tgChat.entity instanceof Api.Channel)) {
|
||||||
|
|
|
@ -415,6 +415,7 @@ export default class ForwardService {
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
if (richHeaderUsed) {
|
if (richHeaderUsed) {
|
||||||
|
richHeaderUsed = false;
|
||||||
this.log.warn('Rich Header 发送错误', messageToSend.file, e);
|
this.log.warn('Rich Header 发送错误', messageToSend.file, e);
|
||||||
delete messageToSend.file;
|
delete messageToSend.file;
|
||||||
delete messageToSend.linkPreview;
|
delete messageToSend.linkPreview;
|
||||||
|
@ -425,6 +426,23 @@ export default class ForwardService {
|
||||||
else throw e;
|
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) {
|
if (this.instance.workMode === 'personal' && event.message_type === 'group' && event.atall) {
|
||||||
await tgMessage.pin({ notify: false });
|
await tgMessage.pin({ notify: false });
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue