feat: 在转发群里可以使用 info 命令查看消息和聊天的信息

This commit is contained in:
Clansty 2022-03-12 12:57:18 +08:00
parent 7e6acbb66d
commit 5f2c42545c
No known key found for this signature in database
GPG Key ID: 3A6BE8BAF2EDE134
5 changed files with 141 additions and 26 deletions

View File

@ -0,0 +1,37 @@
import InChatCommandsService from '../services/InChatCommandsService';
import { getLogger, Logger } from 'log4js';
import Instance from '../models/Instance';
import Telegram from '../client/Telegram';
import OicqClient from '../client/OicqClient';
import { Api } from 'telegram';
export default class InChatCommandsController {
private readonly service: InChatCommandsService;
private readonly log: Logger;
constructor(private readonly instance: Instance,
private readonly tgBot: Telegram,
private readonly oicq: OicqClient) {
this.log = getLogger(`InChatCommandsController - ${instance.id}`);
this.service = new InChatCommandsService(instance, tgBot, oicq);
tgBot.addNewMessageEventHandler(this.onTelegramMessage);
}
private onTelegramMessage = async (message: Api.Message) => {
if (!message.message) return;
const messageParts = message.message.split(' ');
if (!messageParts.length || !messageParts[0].startsWith('/')) return;
let command: string = messageParts[0];
if (command.includes('@')) {
let target: string;
[command, target] = command.split('@');
if (target !== this.tgBot.me.username) return false;
}
const pair = this.instance.forwardPairs.find(message.chat);
switch (command) {
case '/info':
await this.service.info(message, pair);
return true;
}
};
}

View File

@ -11,6 +11,7 @@ import OicqClient from '../client/OicqClient';
import { getLogger, Logger } from 'log4js';
import ForwardPairs from './ForwardPairs';
import InstanceManageController from '../controllers/InstanceManageController';
import InChatCommandsController from '../controllers/InChatCommandsController';
export default class Instance {
private _owner = 0;
@ -32,6 +33,7 @@ export default class Instance {
private instanceManageController: InstanceManageController;
private configController: ConfigController;
private deleteMessageController: DeleteMessageController;
private inChatCommandsController: InChatCommandsController;
private forwardController: ForwardController;
private fileAndFlashPhotoController: FileAndFlashPhotoController;
@ -99,6 +101,7 @@ export default class Instance {
}
this.configController = new ConfigController(this, this.tgBot, this.tgUser, this.oicq);
this.deleteMessageController = new DeleteMessageController(this, this.tgBot, this.tgUser, this.oicq);
this.inChatCommandsController = new InChatCommandsController(this, this.tgBot, this.oicq);
this.forwardController = new ForwardController(this, this.tgBot, this.tgUser, this.oicq);
this.fileAndFlashPhotoController = new FileAndFlashPhotoController(this, this.tgBot, this.oicq);
})()

View File

@ -11,6 +11,7 @@ import OicqClient from '../client/OicqClient';
import { md5 } from '../utils/hashing';
import TelegramChat from '../client/TelegramChat';
import Instance from '../models/Instance';
import getAboutText from '../utils/getAboutText';
const DEFAULT_FILTER_ID = 114; // 514
@ -97,7 +98,7 @@ export default class ConfigService {
const entity = this.oicq.getChat(roomId);
const avatar = await getAvatar(roomId);
const message = await (await this.owner).sendMessage({
message: await this.getAboutText(entity),
message: await getAboutText(entity),
buttons: [
[Button.inline('自动创建群组', this.tgBot.registerCallback(
async () => {
@ -166,7 +167,7 @@ export default class ConfigService {
if (!chat) {
// 创建群聊,拿到的是 user 的 chat
chat = await this.tgUser.createChat(title, await this.getAboutText(qEntity));
chat = await this.tgUser.createChat(title, await getAboutText(qEntity));
// 添加机器人
status && await status.edit({ text: '正在添加机器人…' });
@ -233,7 +234,7 @@ export default class ConfigService {
public async promptNewGroup(group: Group) {
const message = await (await this.owner).sendMessage({
message: '你加入了一个新的群:\n' +
await this.getAboutText(group) + '\n' +
await getAboutText(group) + '\n' +
'要创建关联群吗',
buttons: Button.inline('创建', this.tgBot.registerCallback(async () => {
await message.delete({ revoke: true });
@ -300,29 +301,6 @@ export default class ConfigService {
}
}
private async getAboutText(entity: Friend | Group) {
let text: string;
if (entity instanceof Friend) {
text = `备注:${entity.remark}\n` +
`昵称:${entity.nickname}\n` +
`账号:${entity.user_id}`;
}
else {
const owner = entity.pickMember(entity.info.owner_id);
await owner.renew();
const self = entity.pickMember(this.oicq.uin);
await self.renew();
text = `群名称:${entity.name}\n` +
`${entity.info.member_count} 名成员\n` +
`群号:${entity.group_id}\n` +
(self ? `我的群名片:${self.title ? `${self.title}` : ''}${self.card}\n` : '') +
(owner ? `群主:${owner.title ? `${owner.title}` : ''}${owner.card || owner.info.nickname} (${owner.user_id})` : '') +
((entity.is_admin || entity.is_owner) ? '\n可管理' : '');
}
return text;
}
public async migrateAllChats() {
const dbPairs = await db.forwardPair.findMany();
for (const forwardPair of dbPairs) {

View File

@ -0,0 +1,72 @@
import { getLogger, Logger } from 'log4js';
import Instance from '../models/Instance';
import Telegram from '../client/Telegram';
import OicqClient from '../client/OicqClient';
import { Api } from 'telegram';
import getAboutText from '../utils/getAboutText';
import { Pair } from '../models/Pair';
import { CustomFile } from 'telegram/client/uploads';
import { getAvatar } from '../utils/urls';
import db from '../models/db';
import { Friend } from 'oicq';
import { format } from 'date-and-time';
export default class InChatCommandsService {
private readonly log: Logger;
constructor(private readonly instance: Instance,
private readonly tgBot: Telegram,
private readonly oicq: OicqClient) {
this.log = getLogger(`InChatCommandsService - ${instance.id}`);
}
public async info(message: Api.Message, pair: Pair) {
const replyMessageId = message.replyToMsgId;
if (replyMessageId) {
const messageInfo = await db.message.findFirst({
where: {
tgChatId: Number(message.chat.id),
tgMsgId: replyMessageId,
},
});
if (messageInfo) {
let textToSend = '';
if (pair.qq instanceof Friend) {
if (Number(messageInfo.qqSenderId) === pair.qqRoomId) {
textToSend += `<b>发送者:</b>${pair.qq.remark || pair.qq.nickname}(<code>${pair.qq.user_id}</code>)\n`;
}
else {
textToSend += `<b>发送者:</b>${this.oicq.nickname}(<code>${this.oicq.uin}</code>)\n`;
}
}
else {
const sender = pair.qq.pickMember(Number(messageInfo.qqSenderId));
await sender.renew();
textToSend += `<b>发送者:</b>${sender.title ? `「<i>${sender.title}</i>」` : ''}` +
`${sender.card || sender.info.nickname}(<code>${sender.user_id}</code>)\n`;
if (sender.info.role !== 'member') {
textToSend += `<b>职务:</b>${sender.info.role === 'owner' ? '群主' : '管理员'}\n`;
}
}
textToSend += `<b>发送时间:</b>${format(new Date(messageInfo.time * 1000), 'YYYY-M-D hh:mm:ss')}`;
const avatar = await getAvatar(Number(messageInfo.qqSenderId));
await message.reply({
message: textToSend,
file: new CustomFile('avatar.png', avatar.length, '', avatar),
});
}
else {
await message.reply({
message: '获取消息信息失败',
});
}
}
else {
const avatar = await getAvatar(pair.qqRoomId);
await message.reply({
message: await getAboutText(pair.qq),
file: new CustomFile('avatar.png', avatar.length, '', avatar),
});
}
}
}

25
src/utils/getAboutText.ts Normal file
View File

@ -0,0 +1,25 @@
import { Friend, Group } from 'oicq';
export default async function getAboutText(entity: Friend | Group) {
let text: string;
if (entity instanceof Friend) {
text = `<b>备注:</b>${entity.remark}\n` +
`<b>昵称:</b>${entity.nickname}\n` +
`<b>账号:</b>${entity.user_id}`;
}
else {
const owner = entity.pickMember(entity.info.owner_id);
await owner.renew();
const self = entity.pickMember(entity.client.uin);
await self.renew();
text = `<b>群名称:</b>${entity.name}\n` +
`<b>${entity.info.member_count} 名成员</b>\n` +
`<b>群号:</b><code>${entity.group_id}</code>\n` +
(self ? `<b>我的群名片:</b>${self.title ? `「<i>${self.title}</i>」` : ''}${self.card}\n` : '') +
(owner ? `<b>群主:</b>${owner.title ? `「<i>${owner.title}</i>」` : ''}` +
`${owner.card || owner.info.nickname} (<code>${owner.user_id}</code>)` : '') +
((entity.is_admin || entity.is_owner) ? '\n<b>可管理</b>' : '');
}
return text;
}