Initial commit: Telegram Management System
Some checks failed
Deploy / deploy (push) Has been cancelled
Some checks failed
Deploy / deploy (push) Has been cancelled
Full-stack web application for Telegram management - Frontend: Vue 3 + Vben Admin - Backend: NestJS - Features: User management, group broadcast, statistics 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
91
backend/src/client/MtTgClientBus.js
Normal file
91
backend/src/client/MtTgClientBus.js
Normal file
@@ -0,0 +1,91 @@
|
||||
const ClientBus = require("@src/client/ClientBus")
|
||||
const MApiDataService = require("@src/service/MApiDataService")
|
||||
const MTgAccountService = require("@src/service/MTgAccountService");
|
||||
const config=require("@src/config/Config");
|
||||
const {TelegramClient, Api} = require('telegram');
|
||||
const MtTgClient = require("@src/client/MtTgClient")
|
||||
const Logger = require("@src/util/Log4jUtil")
|
||||
|
||||
class MtTgClientBus {
|
||||
|
||||
constructor(clientBus) {
|
||||
this.clientBus = clientBus
|
||||
this.apiDataService = MApiDataService.getInstance()
|
||||
this.accountService = MTgAccountService.getInstance()
|
||||
this.clientMap = {}
|
||||
this.logger = Logger.getLogger('MtTgClientBus');
|
||||
}
|
||||
|
||||
static getInstance(clientBus){
|
||||
if (!MtTgClientBus.instance) {
|
||||
MtTgClientBus.instance = new MtTgClientBus(clientBus);
|
||||
}
|
||||
return MtTgClientBus.instance;
|
||||
}
|
||||
|
||||
async getClient(phoneNumber, opts) {
|
||||
let client = this.clientMap[phoneNumber]
|
||||
if (!client || !(await client.isValid()) || (opts['sessionString'] && client.sessionString !== opts['sessionString'])) {
|
||||
|
||||
if (client && !(await client.isValid())) {
|
||||
await this.clientBus.deleteCache(client.wClient)
|
||||
}
|
||||
|
||||
this.logger.debug("init new client", phoneNumber, opts)
|
||||
let wClient = this.clientBus.getClientByCacheAndPhone(phoneNumber)
|
||||
client = new MtTgClient(phoneNumber, opts)
|
||||
if (wClient) {
|
||||
this.logger.debug("Found client in clientBus cache")
|
||||
client.client = wClient.tggClient
|
||||
client.sessionString = wClient.tggClient.session.save()
|
||||
} else {
|
||||
// 根据手机号获取相关的用户信息
|
||||
const account = await this.accountService.findByPhone(phoneNumber)
|
||||
if (account) {
|
||||
this.logger.debug("Found Db account", account.phone)
|
||||
let item = await this.apiDataService.getCanWorkRandomItem();
|
||||
client.apiId = item.apiId
|
||||
client.apiHash = item.apiHash
|
||||
wClient = await this.clientBus.getClient(account, client.apiId, client.apiHash, false)
|
||||
client.client = wClient.tggClient
|
||||
await wClient.connect()
|
||||
client.sessionString = wClient.tggClient.session.save()
|
||||
} else {
|
||||
this.logger.debug("New client initiating")
|
||||
await client.initClient();
|
||||
}
|
||||
}
|
||||
client.wClient = wClient
|
||||
this.clientMap[phoneNumber] = client
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
async getClient22(ph) {
|
||||
let account = await this.accountService.findByPhone(ph)
|
||||
let client;
|
||||
if (!account) {
|
||||
// throw new Error("not found account")
|
||||
// 数据库中没有数据,去缓存中查看是否有
|
||||
client = this.clientBus.getClientByCacheAndPhone(ph)
|
||||
if (!client) {
|
||||
// 如果没有, 则去登录
|
||||
console.log("RRst:", rst)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!client) {
|
||||
let item = await this.apiDataService.getCanWorkRandomItem();
|
||||
client = await this.clientBus.getClient(account, item.apiId, item.apiHash, false)
|
||||
}
|
||||
|
||||
let user = await client.invokeFun(new Api.users.GetFullUser({
|
||||
id: new Api.InputUserSelf()
|
||||
}
|
||||
))
|
||||
return user
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MtTgClientBus;
|
||||
Reference in New Issue
Block a user