evogram-gramjs 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import { Command, CommandContext, InlineQueryContext, MessageContext } from 'evogram';
2
2
  export declare class AccountsCommand extends Command {
3
- execute(context: CommandContext, sessionId?: string): Promise<any>;
3
+ execute(context: CommandContext, sessionId?: string, page?: number): Promise<any>;
4
4
  handleMessage(context: CommandContext, message: MessageContext): Promise<any>;
5
5
  inlineExecute(context: InlineQueryContext): Promise<void>;
6
6
  }
@@ -7,10 +7,11 @@ const evogram_1 = require("evogram");
7
7
  const EvogramGramJS_1 = require("../EvogramGramJS");
8
8
  const AddAccount_command_1 = require("./AddAccount.command");
9
9
  let AccountsCommand = AccountsCommand_1 = class AccountsCommand extends evogram_1.Command {
10
- execute(context, sessionId) {
10
+ execute(context, sessionId, page) {
11
11
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
12
12
  var _a, _b;
13
- if (!sessionId)
13
+ if (!sessionId) {
14
+ const sessions = yield EvogramGramJS_1.EvogramGramJS.sessionManager.getAllLoadedSessions();
14
15
  //@ts-ignore
15
16
  return (_a = context[context.callbackQuery ? 'edit' : 'send']) === null || _a === void 0 ? void 0 : _a.call(context, {
16
17
  // prettier-ignore
@@ -20,12 +21,15 @@ let AccountsCommand = AccountsCommand_1 = class AccountsCommand extends evogram_
20
21
  reply_markup: {
21
22
  inline_keyboard: [
22
23
  [
23
- { text: 'Добавить', command: AddAccount_command_1.AddAccountCommand, payload: { phone: null, session: null, code: null, password: null } },
24
- { text: 'Список', command: AccountsCommand_1, switch_inline_query_current_chat: ' ' },
24
+ { text: '📋 Список аккаунтов', switch_inline_query_current_chat: ' ', command: AccountsCommand_1 },
25
+ { text: '➕ Добавить аккаунт', command: AddAccount_command_1.AddAccountCommand },
25
26
  ],
27
+ ...sessions.slice((Number(page) || 0) * 10, (Number(page) || 0) * 10 + 10).map((x) => { var _a, _b, _c; return [{ text: `[${((_a = x.db) === null || _a === void 0 ? void 0 : _a.error) ? '❌' : '✅'}] ${((_b = x.db) === null || _b === void 0 ? void 0 : _b.firstName) || ((_c = x.db) === null || _c === void 0 ? void 0 : _c.username) || 'Безымянный'}`, command: AccountsCommand_1, payload: { sessionId: x.sessionId } }]; }),
28
+ (0, evogram_1.Pagination)({ pages: Math.ceil(sessions.length / 10), currentPage: Number(page) || 0, command: AccountsCommand_1 }),
26
29
  ],
27
30
  },
28
31
  });
32
+ }
29
33
  const session = EvogramGramJS_1.EvogramGramJS.sessionManager.getSession(sessionId);
30
34
  const db = yield (session === null || session === void 0 ? void 0 : session.db());
31
35
  const me = yield (session === null || session === void 0 ? void 0 : session.client.getMe().then((x) => x).catch(() => null));
@@ -75,8 +79,9 @@ let AccountsCommand = AccountsCommand_1 = class AccountsCommand extends evogram_
75
79
  exports.AccountsCommand = AccountsCommand;
76
80
  tslib_1.__decorate([
77
81
  tslib_1.__param(1, (0, evogram_1.CommandArgument)('sessionId?')),
82
+ tslib_1.__param(2, (0, evogram_1.CommandArgument)('page?')),
78
83
  tslib_1.__metadata("design:type", Function),
79
- tslib_1.__metadata("design:paramtypes", [evogram_1.CommandContext, String]),
84
+ tslib_1.__metadata("design:paramtypes", [evogram_1.CommandContext, String, Number]),
80
85
  tslib_1.__metadata("design:returntype", Promise)
81
86
  ], AccountsCommand.prototype, "execute", null);
82
87
  tslib_1.__decorate([
@@ -86,5 +91,5 @@ tslib_1.__decorate([
86
91
  tslib_1.__metadata("design:returntype", Promise)
87
92
  ], AccountsCommand.prototype, "handleMessage", null);
88
93
  exports.AccountsCommand = AccountsCommand = AccountsCommand_1 = tslib_1.__decorate([
89
- (0, evogram_1.CommandD)({ name: 'accounts' })
94
+ (0, evogram_1.CommandD)({ name: 'accounts', backButton: 'Главное меню' })
90
95
  ], AccountsCommand);
@@ -1,5 +1,6 @@
1
1
  import { EventEmitter } from 'events';
2
- import { TelegramClient } from 'telegram';
2
+ import { Api, TelegramClient } from 'telegram';
3
+ import { Session as SessionEntity } from '../entities/Session.entity';
3
4
  import { DatabaseService } from '../services/DatabaseService';
4
5
  import { SessionConfig, SessionInfo } from '../types/session.types';
5
6
  import { SessionLogger } from './SessionLogger';
@@ -57,6 +58,10 @@ export declare class SessionManager extends EventEmitter {
57
58
  * @returns Массив информации о всех сессиях
58
59
  */
59
60
  getAllSessions(): SessionInfo[];
61
+ getAllLoadedSessions(): Promise<({
62
+ db: SessionEntity | null;
63
+ user: Api.User | null;
64
+ } & Omit<SessionInfo, 'db' | 'user'>)[]>;
60
65
  /**
61
66
  * Получает список всех ID сессий
62
67
  *
@@ -67,9 +67,11 @@ class SessionManager extends events_1.EventEmitter {
67
67
  });
68
68
  yield client.connect();
69
69
  client.addEventHandler((event) => tslib_1.__awaiter(this, void 0, void 0, function* () {
70
- var _a, _b;
70
+ var _a, _b, _c, _d;
71
71
  if ((_b = (yield ((_a = this.getSession(config.sessionId)) === null || _a === void 0 ? void 0 : _a.db()))) === null || _b === void 0 ? void 0 : _b.error)
72
72
  return;
73
+ else if (!((_d = (yield ((_c = this.getSession(config.sessionId)) === null || _c === void 0 ? void 0 : _c.db()))) === null || _d === void 0 ? void 0 : _d.sessionString))
74
+ return;
73
75
  if (event instanceof network_1.UpdateConnectionState) {
74
76
  const error = yield client
75
77
  .getMe()
@@ -155,6 +157,19 @@ class SessionManager extends events_1.EventEmitter {
155
157
  getAllSessions() {
156
158
  return Array.from(this.sessions.values());
157
159
  }
160
+ getAllLoadedSessions() {
161
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
162
+ const sessions = Array.from(this.sessions.values());
163
+ const results = yield Promise.all(sessions.map((session) => tslib_1.__awaiter(this, void 0, void 0, function* () {
164
+ //@ts-ignore
165
+ ;
166
+ [session.db, session.user] = [yield session.db().catch(() => null), yield session.user().catch(() => null)];
167
+ return session;
168
+ })));
169
+ //@ts-ignore
170
+ return results;
171
+ });
172
+ }
158
173
  /**
159
174
  * Получает список всех ID сессий
160
175
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "evogram-gramjs",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "main": "./lib/index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {
@@ -1,11 +1,13 @@
1
- import { Command, CommandArgument, CommandContext, CommandD, CommandUpdate, InlineQueryContext, MessageContext } from 'evogram'
1
+ import { Command, CommandArgument, CommandContext, CommandD, CommandUpdate, InlineQueryContext, MessageContext, Pagination } from 'evogram'
2
2
  import { EvogramGramJS } from '../EvogramGramJS'
3
3
  import { AddAccountCommand } from './AddAccount.command'
4
4
 
5
- @CommandD({ name: 'accounts' })
5
+ @CommandD({ name: 'accounts', backButton: 'Главное меню' })
6
6
  export class AccountsCommand extends Command {
7
- public async execute(context: CommandContext, @CommandArgument('sessionId?') sessionId?: string) {
8
- if (!sessionId)
7
+ public async execute(context: CommandContext, @CommandArgument('sessionId?') sessionId?: string, @CommandArgument('page?') page?: number) {
8
+ if (!sessionId) {
9
+ const sessions = await EvogramGramJS.sessionManager.getAllLoadedSessions()
10
+
9
11
  //@ts-ignore
10
12
  return context[context.callbackQuery ? 'edit' : 'send']?.({
11
13
  // prettier-ignore
@@ -17,12 +19,15 @@ export class AccountsCommand extends Command {
17
19
  reply_markup: {
18
20
  inline_keyboard: [
19
21
  [
20
- { text: 'Добавить', command: AddAccountCommand, payload: { phone: null, session: null, code: null, password: null } },
21
- { text: 'Список', command: AccountsCommand, switch_inline_query_current_chat: ' ' },
22
+ { text: '📋 Список аккаунтов', switch_inline_query_current_chat: ' ', command: AccountsCommand },
23
+ { text: '➕ Добавить аккаунт', command: AddAccountCommand },
22
24
  ],
25
+ ...sessions.slice((Number(page) || 0) * 10, (Number(page) || 0) * 10 + 10).map((x) => [{ text: `[${x.db?.error ? '❌' : '✅'}] ${x.db?.firstName || x.db?.username || 'Безымянный'}`, command: AccountsCommand, payload: { sessionId: x.sessionId } }]),
26
+ Pagination({ pages: Math.ceil(sessions.length / 10), currentPage: Number(page) || 0, command: AccountsCommand }),
23
27
  ],
24
28
  },
25
29
  })
30
+ }
26
31
 
27
32
  const session = EvogramGramJS.sessionManager.getSession(sessionId)
28
33
  const db = await session?.db()
@@ -1,8 +1,9 @@
1
1
  import { EventEmitter } from 'events'
2
- import { TelegramClient } from 'telegram'
2
+ import { Api, TelegramClient } from 'telegram'
3
3
  import { UpdateConnectionState } from 'telegram/network'
4
4
  import { StringSession } from 'telegram/sessions'
5
5
  import { EvogramGramJS } from '../EvogramGramJS'
6
+ import { Session as SessionEntity } from '../entities/Session.entity'
6
7
  import { DatabaseService } from '../services/DatabaseService'
7
8
  import { SessionConfig, SessionEvent, SessionEventData, SessionInfo, SessionInvalidEventData } from '../types/session.types'
8
9
  import { Session } from './Session'
@@ -64,6 +65,7 @@ export class SessionManager extends EventEmitter {
64
65
 
65
66
  client.addEventHandler(async (event) => {
66
67
  if ((await this.getSession(config.sessionId)?.db())?.error) return
68
+ else if (!(await this.getSession(config.sessionId)?.db())?.sessionString) return
67
69
 
68
70
  if (event instanceof UpdateConnectionState) {
69
71
  const error = await client
@@ -160,6 +162,20 @@ export class SessionManager extends EventEmitter {
160
162
  return Array.from(this.sessions.values())
161
163
  }
162
164
 
165
+ async getAllLoadedSessions(): Promise<({ db: SessionEntity | null; user: Api.User | null } & Omit<SessionInfo, 'db' | 'user'>)[]> {
166
+ const sessions = Array.from(this.sessions.values())
167
+ const results = await Promise.all(
168
+ sessions.map(async (session) => {
169
+ //@ts-ignore
170
+ ;[session.db, session.user] = [await session.db().catch(() => null), await session.user().catch(() => null)]
171
+ return session
172
+ })
173
+ )
174
+
175
+ //@ts-ignore
176
+ return results
177
+ }
178
+
163
179
  /**
164
180
  * Получает список всех ID сессий
165
181
  *