|
@@ -59,12 +59,27 @@ export class TgClientService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const stringSession = new StringSession(sessionString)
|
|
const stringSession = new StringSession(sessionString)
|
|
|
- this.client = new TelegramClient(stringSession, this.apiId, this.apiHash, {
|
|
|
|
|
- connectionRetries: 5
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ const connectionOptions = {
|
|
|
|
|
+ connectionRetries: 5,
|
|
|
|
|
+ retryDelay: 2000,
|
|
|
|
|
+ requestRetries: 5,
|
|
|
|
|
+ useWSS: true
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ this.client = new TelegramClient(stringSession, this.apiId, this.apiHash, connectionOptions)
|
|
|
|
|
|
|
|
- this.app.log.info('正在建立连接...')
|
|
|
|
|
- await this.client.connect()
|
|
|
|
|
|
|
+ this.app.log.info(
|
|
|
|
|
+ `正在建立连接... useWSS=${connectionOptions.useWSS ? 'true' : 'false'} (443 优先), retries=${
|
|
|
|
|
+ connectionOptions.connectionRetries
|
|
|
|
|
+ }`
|
|
|
|
|
+ )
|
|
|
|
|
+ try {
|
|
|
|
|
+ await this.client.connect()
|
|
|
|
|
+ } catch (err) {
|
|
|
|
|
+ const errorMessage = err instanceof Error ? err.message : String(err)
|
|
|
|
|
+ this.app.log.error({ msg: '连接 Telegram 失败', error: errorMessage, useWSS: connectionOptions.useWSS })
|
|
|
|
|
+ throw err
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
if (!this.client.connected) {
|
|
if (!this.client.connected) {
|
|
|
throw new Error('TelegramClient 连接失败,请检查网络或 Session 是否有效')
|
|
throw new Error('TelegramClient 连接失败,请检查网络或 Session 是否有效')
|
|
@@ -117,7 +132,6 @@ export class TgClientService {
|
|
|
|
|
|
|
|
if (typeof parsedTarget === 'string' && parsedTarget.startsWith('+')) {
|
|
if (typeof parsedTarget === 'string' && parsedTarget.startsWith('+')) {
|
|
|
this.app.log.info('手机号导入联系人...')
|
|
this.app.log.info('手机号导入联系人...')
|
|
|
- // 手机号
|
|
|
|
|
const result = await client.invoke(
|
|
const result = await client.invoke(
|
|
|
new Api.contacts.ImportContacts({
|
|
new Api.contacts.ImportContacts({
|
|
|
contacts: [
|
|
contacts: [
|
|
@@ -136,7 +150,6 @@ export class TgClientService {
|
|
|
|
|
|
|
|
targetPeer = user
|
|
targetPeer = user
|
|
|
} else {
|
|
} else {
|
|
|
- // 用户名 用户 id
|
|
|
|
|
targetPeer = await client.getEntity(parsedTarget)
|
|
targetPeer = await client.getEntity(parsedTarget)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -225,19 +238,16 @@ export class TgClientService {
|
|
|
className: entityInfo.className || '未知'
|
|
className: entityInfo.className || '未知'
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 记录 ID
|
|
|
|
|
if (entityInfo.id !== undefined) {
|
|
if (entityInfo.id !== undefined) {
|
|
|
logData.id = entityInfo.id.toString()
|
|
logData.id = entityInfo.id.toString()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 记录名称信息
|
|
|
|
|
if (entityInfo.title) {
|
|
if (entityInfo.title) {
|
|
|
logData.title = entityInfo.title
|
|
logData.title = entityInfo.title
|
|
|
} else if (entityInfo.firstName) {
|
|
} else if (entityInfo.firstName) {
|
|
|
logData.name = `${entityInfo.firstName} ${entityInfo.lastName || ''}`.trim()
|
|
logData.name = `${entityInfo.firstName} ${entityInfo.lastName || ''}`.trim()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 记录用户名
|
|
|
|
|
if (entityInfo.username) {
|
|
if (entityInfo.username) {
|
|
|
logData.username = entityInfo.username
|
|
logData.username = entityInfo.username
|
|
|
}
|
|
}
|