feat: 自动创建保存所有 tg chat 的文件夹

This commit is contained in:
凌莞 2022-02-23 11:30:49 +08:00
parent d2753328ce
commit f75fa06f25
No known key found for this signature in database
GPG Key ID: 05F8479BA63A8E92
3 changed files with 54 additions and 1 deletions

View File

@ -111,6 +111,10 @@ export class Telegram {
public registerCallback(cb: () => any) { public registerCallback(cb: () => any) {
return this.callbackQueryHelper.registerCallback(cb); return this.callbackQueryHelper.registerCallback(cb);
} }
public invoke<R extends Api.AnyRequest>(req: R) {
return this.client.invoke(req);
}
} }
export class TelegramChat { export class TelegramChat {
@ -120,7 +124,10 @@ export class TelegramChat {
private readonly waitForInputHelper: WaitForMessageHelper) { private readonly waitForInputHelper: WaitForMessageHelper) {
} }
public async sendMessage(params: SendMessageParams) { public async sendMessage(params: SendMessageParams | string) {
if (typeof params === 'string') {
params = { message: params };
}
return await this.client.sendMessage(this.entity, params); return await this.client.sendMessage(this.entity, params);
} }

View File

@ -14,6 +14,7 @@ export default class ConfigController {
this.configService = new ConfigService(tgBot, tgUser, oicq); this.configService = new ConfigService(tgBot, tgUser, oicq);
tgBot.addNewMessageEventHandler(this.handleMessage); tgBot.addNewMessageEventHandler(this.handleMessage);
this.configService.configCommands(); this.configService.configCommands();
config.workMode === 'personal' && this.configService.setupFilter();
} }
private handleMessage = async (message: Api.Message) => { private handleMessage = async (message: Api.Message) => {

View File

@ -10,9 +10,12 @@ import db from '../providers/db';
import { Api, utils } from 'telegram'; import { Api, utils } from 'telegram';
import commands from '../constants/commands'; import commands from '../constants/commands';
const DEFAULT_FILTER_ID = 114; // 514
export default class ConfigService { export default class ConfigService {
private owner: TelegramChat; private owner: TelegramChat;
private log = getLogger('ConfigService'); private log = getLogger('ConfigService');
private filter;
constructor(private readonly tgBot: Telegram, constructor(private readonly tgBot: Telegram,
private readonly tgUser: Telegram, private readonly tgUser: Telegram,
@ -122,4 +125,46 @@ export default class ConfigService {
} }
await this.owner.sendMessage({ message }); await this.owner.sendMessage({ message });
} }
// 创建 QQ 群组的文件夹
public async setupFilter() {
const result = await this.tgUser.invoke(new Api.messages.GetDialogFilters());
this.filter = result.find(e => e.id === DEFAULT_FILTER_ID);
this.log.debug(this.filter);
if (!this.filter) {
this.log.info('创建 TG 文件夹');
// 要自己计算新的 id随意 id 也是可以的
// https://github.com/morethanwords/tweb/blob/7d646bc9a87d943426d831f30b69d61b743f51e0/src/lib/storages/filters.ts#L251
// 创建
this.filter = new Api.DialogFilter({
id: DEFAULT_FILTER_ID,
title: 'QQ',
pinnedPeers: [
utils.getInputPeer((await this.tgUser.getChat(this.tgBot.me.username)).entity),
],
includePeers: [],
excludePeers: [],
emoticon: '🐧',
});
let errorText = '设置文件夹失败';
try {
const isSuccess = await this.tgUser.invoke(
new Api.messages.UpdateDialogFilter({
id: DEFAULT_FILTER_ID,
filter: this.filter,
}),
);
if (!isSuccess) {
this.filter = null;
this.log.error(errorText);
await this.owner.sendMessage(errorText);
}
}
catch (e) {
this.filter = null;
this.log.error(errorText, e);
await this.owner.sendMessage(errorText + `\n<code>${e}</code>`);
}
}
}
} }