feat: 支持启动关联指定的 QQ 群组

This commit is contained in:
凌莞 2022-02-20 18:01:07 +08:00
parent da8b70266f
commit 2ad2d41cad
No known key found for this signature in database
GPG Key ID: 05F8479BA63A8E92
6 changed files with 89 additions and 6 deletions

View File

@ -16,6 +16,7 @@
},
"dependencies": {
"@prisma/client": "^3.9.2",
"axios": "^0.26.0",
"log4js": "^6.4.1",
"oicq": "^2.2.0",
"telegram": "^2.5.0"

3
src/constants/regExps.ts Normal file
View File

@ -0,0 +1,3 @@
export default {
qq: /^[1-9]\d{4,10}$/,
};

View File

@ -3,6 +3,7 @@ import { Telegram } from '../client/Telegram';
import { Client as OicqClient } from 'oicq';
import ConfigService from '../services/ConfigService';
import { config } from '../providers/userConfig';
import regExps from '../constants/regExps';
export default class ConfigController {
private readonly configService: ConfigService;
@ -16,13 +17,24 @@ export default class ConfigController {
}
private handleMessage = async (message: Api.Message) => {
if (!message.chat.id.eq(config.owner)) {
if (!message.sender.id.eq(config.owner)) {
return false;
}
switch (message.message){
case '/add':
this.configService.add()
return true
const messageSplit = message.message.split(' ');
if (message.isGroup) {
}
else if (message.isPrivate) {
switch (messageSplit[0]) {
case '/add':
if (messageSplit[1] && regExps.qq.test(messageSplit[1])) {
await this.configService.addExact(Number(messageSplit[1]));
}
else {
await this.configService.add();
}
return true;
}
}
};
}

View File

@ -2,9 +2,14 @@ import { Telegram, TelegramChat } from '../client/Telegram';
import { Client as OicqClient } from 'oicq';
import { config } from '../providers/userConfig';
import { Button } from 'telegram/tl/custom/button';
import { getLogger } from 'log4js';
import axios from 'axios';
import { getAvatarUrl } from '../utils/urls';
import { CustomFile } from 'telegram/client/uploads';
export default class ConfigService {
private owner: TelegramChat;
private log = getLogger('ConfigService');
constructor(private readonly tgBot: Telegram,
private readonly tgUser: Telegram,
@ -12,13 +17,38 @@ export default class ConfigService {
tgBot.getChat(config.owner).then(e => this.owner = e);
}
private getAssociateLink(gin: number) {
return `https://t.me/${this.tgBot.me.username}?startgroup=${gin}`;
}
// 开始添加转发群组流程
public async add() {
const qGroups = Array.from(this.oicq.gl).map(e => e[1]);
await this.owner.createPaginatedInlineSelector('选择 QQ 群组\n然后选择在 TG 中的群组',
qGroups.map(e => [Button.url(
`${e.group_name} (${e.group_id})`,
`https://t.me/${this.tgBot.me.username}?startgroup=${e.group_id}`,
this.getAssociateLink(e.group_id),
)]));
}
public async addExact(gin: number) {
const group = this.oicq.gl.get(gin);
let avatar: Buffer;
try {
const res = await axios.get(getAvatarUrl(-group.group_id), {
responseType: 'arraybuffer',
});
avatar = res.data;
}
catch (e) {
avatar = null;
this.log.error(`加载 ${group.group_name} (${gin}) 的头像失败`, e);
}
const message = `${group.group_name}\n${group.group_id}\n${group.member_count} 名成员`;
await this.owner.sendMessage({
message,
file: avatar ? new CustomFile('avatar.png', avatar.length, '', avatar) : undefined,
buttons: Button.url('关联 Telegram 群组', this.getAssociateLink(group.group_id)),
});
}
}

17
src/utils/urls.ts Normal file
View File

@ -0,0 +1,17 @@
export function getAvatarUrl(roomId: number, large = false): string {
if (!roomId) return '';
return roomId < 0 ?
`https://p.qlogo.cn/gh/${-roomId}/${-roomId}/0` :
`https://q1.qlogo.cn/g?b=qq&nk=${roomId}&s=${large ? 0 : 140}`;
}
export function getImageUrlByMd5(md5: string) {
return 'https://gchat.qpic.cn/gchatpic_new/0/0-0-' + md5.toUpperCase() + '/0';
}
export function getBigFaceUrl(file: string) {
return `https://gxh.vip.qq.com/club/item/parcel/item/${file.substring(
0,
2,
)}/${file.substring(0, 32)}/300x300.png`;
}

View File

@ -234,6 +234,15 @@ __metadata:
languageName: node
linkType: hard
"axios@npm:^0.26.0":
version: 0.26.0
resolution: "axios@npm:0.26.0"
dependencies:
follow-redirects: ^1.14.8
checksum: d7a8b898f4157bedeb2e06c03b16133b91b354c041205bea732ce58b7a21f373d22057b0eea0d482838145ce6ff482b359750d9bcb8dd19d45e3928e3c65c280
languageName: node
linkType: hard
"balanced-match@npm:^1.0.0":
version: 1.0.2
resolution: "balanced-match@npm:1.0.2"
@ -621,6 +630,16 @@ __metadata:
languageName: node
linkType: hard
"follow-redirects@npm:^1.14.8":
version: 1.14.9
resolution: "follow-redirects@npm:1.14.9"
peerDependenciesMeta:
debug:
optional: true
checksum: f5982e0eb481818642492d3ca35a86989c98af1128b8e1a62911a3410621bc15d2b079e8170b35b19d3bdee770b73ed431a257ed86195af773771145baa57845
languageName: node
linkType: hard
"foreach@npm:^2.0.5":
version: 2.0.5
resolution: "foreach@npm:2.0.5"
@ -1494,6 +1513,7 @@ __metadata:
dependencies:
"@prisma/client": ^3.9.2
"@types/node": ^17.0.18
axios: ^0.26.0
log4js: ^6.4.1
oicq: ^2.2.0
prisma: ^3.9.2