grammy-broadcast 2.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.
- package/LICENSE +21 -0
- package/Readme.md +603 -0
- package/dist/index.d.mts +180 -0
- package/dist/index.d.ts +180 -0
- package/dist/index.js +884 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +884 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +33 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import * as grammy from 'grammy';
|
|
2
|
+
import { Context, Api } from 'grammy';
|
|
3
|
+
import { Redis } from 'ioredis';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Abstract Storage class that defines the interface for storage operations
|
|
7
|
+
* Used for broadcast queue management
|
|
8
|
+
*/
|
|
9
|
+
declare abstract class Storage {
|
|
10
|
+
/**
|
|
11
|
+
* Get a range of items from a list
|
|
12
|
+
* @param key List key
|
|
13
|
+
* @param start Start index (0-based)
|
|
14
|
+
* @param end End index (-1 for all)
|
|
15
|
+
* @returns Array of string values
|
|
16
|
+
*/
|
|
17
|
+
abstract lrange(key: string, start: number, end: number): Promise<string[]>;
|
|
18
|
+
/**
|
|
19
|
+
* Get all fields and values from a hash
|
|
20
|
+
* @param key Hash key
|
|
21
|
+
* @returns Object with field-value pairs
|
|
22
|
+
*/
|
|
23
|
+
abstract hgetall(key: string): Promise<Record<string, string>>;
|
|
24
|
+
/**
|
|
25
|
+
* Remove items from a list
|
|
26
|
+
* @param key List key
|
|
27
|
+
* @param count Number of items to remove (0 for all, positive for first N, negative for last N)
|
|
28
|
+
* @param value Value to remove
|
|
29
|
+
* @returns Promise that resolves when done
|
|
30
|
+
*/
|
|
31
|
+
abstract lrem(key: string, count: number, value: string): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Pop items from the left side of a list
|
|
34
|
+
* @param key List key
|
|
35
|
+
* @param count Number of items to pop
|
|
36
|
+
* @returns Array of popped values or null if list is empty
|
|
37
|
+
*/
|
|
38
|
+
abstract lpop(key: string, count: number): Promise<string[] | null>;
|
|
39
|
+
/**
|
|
40
|
+
* Delete one or more keys
|
|
41
|
+
* @param keys Key(s) to delete
|
|
42
|
+
* @returns Promise that resolves when done
|
|
43
|
+
*/
|
|
44
|
+
abstract del(...keys: string[]): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Increment a hash field by a value
|
|
47
|
+
* @param key Hash key
|
|
48
|
+
* @param field Field name
|
|
49
|
+
* @param increment Increment value
|
|
50
|
+
* @returns Promise that resolves when done
|
|
51
|
+
*/
|
|
52
|
+
abstract hincrby(key: string, field: string, increment: number): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Set hash field(s)
|
|
55
|
+
* @param key Hash key
|
|
56
|
+
* @param fieldOrObject Field name or object with field-value pairs
|
|
57
|
+
* @param value Value (if field is a string)
|
|
58
|
+
* @returns Promise that resolves when done
|
|
59
|
+
*/
|
|
60
|
+
abstract hset(key: string, fieldOrObject: string | Record<string, string>, value?: string): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Get a hash field value
|
|
63
|
+
* @param key Hash key
|
|
64
|
+
* @param field Field name
|
|
65
|
+
* @returns Field value or null if not found
|
|
66
|
+
*/
|
|
67
|
+
abstract hget(key: string, field: string): Promise<string | null>;
|
|
68
|
+
/**
|
|
69
|
+
* Delete one or more hash fields
|
|
70
|
+
* @param key Hash key
|
|
71
|
+
* @param fields Field name(s) to delete
|
|
72
|
+
* @returns Promise that resolves when done
|
|
73
|
+
*/
|
|
74
|
+
abstract hdel(key: string, ...fields: string[]): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Push values to the right side of a list
|
|
77
|
+
* @param key List key
|
|
78
|
+
* @param values Values to push
|
|
79
|
+
* @returns Promise that resolves when done
|
|
80
|
+
*/
|
|
81
|
+
abstract rpush(key: string, ...values: string[]): Promise<void>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
type getBroadcastChats = (botId: number, offset: number, limit: number, filter?: string) => Promise<string[] | number[]>;
|
|
85
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
86
|
+
type setRestricted = (botId: number, chatId: string, type: /*Users: */ 'block' | 'deactivated' | /*Groups: */ 'banned' | 'restricted') => Promise<void>;
|
|
87
|
+
type progressCallback = (id: string, sent: number, error: number, total: number) => void;
|
|
88
|
+
interface BroadcastOptions {
|
|
89
|
+
storage: Storage;
|
|
90
|
+
getBroadcastChats: getBroadcastChats;
|
|
91
|
+
setRestricted?: setRestricted | null;
|
|
92
|
+
chunkSize?: number;
|
|
93
|
+
keyPrefix?: string;
|
|
94
|
+
sudoUsers: number[];
|
|
95
|
+
hasPermission?: (ctx: Context) => MaybePromise<boolean>;
|
|
96
|
+
getApi: (botId: number) => MaybePromise<Api>;
|
|
97
|
+
isMainInstance: boolean;
|
|
98
|
+
reportFrequency?: number;
|
|
99
|
+
checkQueueInterval?: number;
|
|
100
|
+
progressCallback?: progressCallback | null;
|
|
101
|
+
allowPaidBroadcast?: boolean;
|
|
102
|
+
cmds?: {
|
|
103
|
+
broadcast?: string;
|
|
104
|
+
copy?: string;
|
|
105
|
+
forward?: string;
|
|
106
|
+
addmsg?: string;
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Redis storage implementation using ioredis
|
|
112
|
+
*/
|
|
113
|
+
declare class RedisStorage extends Storage {
|
|
114
|
+
private redis;
|
|
115
|
+
constructor(redis: Redis);
|
|
116
|
+
lrange(key: string, start: number, end: number): Promise<string[]>;
|
|
117
|
+
hgetall(key: string): Promise<Record<string, string>>;
|
|
118
|
+
lrem(key: string, count: number, value: string): Promise<void>;
|
|
119
|
+
lpop(key: string, count: number): Promise<string[] | null>;
|
|
120
|
+
del(...keys: string[]): Promise<void>;
|
|
121
|
+
hincrby(key: string, field: string, increment: number): Promise<void>;
|
|
122
|
+
hset(key: string, fieldOrObject: string | Record<string, string>, value?: string): Promise<void>;
|
|
123
|
+
hget(key: string, field: string): Promise<string | null>;
|
|
124
|
+
hdel(key: string, ...fields: string[]): Promise<void>;
|
|
125
|
+
rpush(key: string, ...values: string[]): Promise<void>;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Memory storage implementation using in-memory data structures
|
|
130
|
+
*/
|
|
131
|
+
declare class MemoryStorage extends Storage {
|
|
132
|
+
private lists;
|
|
133
|
+
private hashes;
|
|
134
|
+
lrange(key: string, start: number, end: number): Promise<string[]>;
|
|
135
|
+
hgetall(key: string): Promise<Record<string, string>>;
|
|
136
|
+
lrem(key: string, count: number, value: string): Promise<void>;
|
|
137
|
+
lpop(key: string, count: number): Promise<string[] | null>;
|
|
138
|
+
del(...keys: string[]): Promise<void>;
|
|
139
|
+
hincrby(key: string, field: string, increment: number): Promise<void>;
|
|
140
|
+
hset(key: string, fieldOrObject: string | Record<string, string>, value?: string): Promise<void>;
|
|
141
|
+
hget(key: string, field: string): Promise<string | null>;
|
|
142
|
+
hdel(key: string, ...fields: string[]): Promise<void>;
|
|
143
|
+
rpush(key: string, ...values: string[]): Promise<void>;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* File storage implementation using filesystem
|
|
148
|
+
*/
|
|
149
|
+
declare class FileStorage extends Storage {
|
|
150
|
+
private baseDir;
|
|
151
|
+
constructor(baseDir?: string);
|
|
152
|
+
private ensureDir;
|
|
153
|
+
private getListPath;
|
|
154
|
+
private getHashPath;
|
|
155
|
+
private readList;
|
|
156
|
+
private writeList;
|
|
157
|
+
private readHash;
|
|
158
|
+
private writeHash;
|
|
159
|
+
lrange(key: string, start: number, end: number): Promise<string[]>;
|
|
160
|
+
hgetall(key: string): Promise<Record<string, string>>;
|
|
161
|
+
lrem(key: string, count: number, value: string): Promise<void>;
|
|
162
|
+
lpop(key: string, count: number): Promise<string[] | null>;
|
|
163
|
+
del(...keys: string[]): Promise<void>;
|
|
164
|
+
hincrby(key: string, field: string, increment: number): Promise<void>;
|
|
165
|
+
hset(key: string, fieldOrObject: string | Record<string, string>, value?: string): Promise<void>;
|
|
166
|
+
hget(key: string, field: string): Promise<string | null>;
|
|
167
|
+
hdel(key: string, ...fields: string[]): Promise<void>;
|
|
168
|
+
rpush(key: string, ...values: string[]): Promise<void>;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
declare class Broadcaster {
|
|
172
|
+
private options;
|
|
173
|
+
static _instance?: Broadcaster;
|
|
174
|
+
private constructor();
|
|
175
|
+
static getInstance(options: BroadcastOptions): Broadcaster;
|
|
176
|
+
getMiddleware(): grammy.Composer<grammy.Context>;
|
|
177
|
+
}
|
|
178
|
+
declare function createBroadcaster(options: BroadcastOptions): Broadcaster;
|
|
179
|
+
|
|
180
|
+
export { FileStorage, MemoryStorage, RedisStorage, Storage, createBroadcaster };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import * as grammy from 'grammy';
|
|
2
|
+
import { Context, Api } from 'grammy';
|
|
3
|
+
import { Redis } from 'ioredis';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Abstract Storage class that defines the interface for storage operations
|
|
7
|
+
* Used for broadcast queue management
|
|
8
|
+
*/
|
|
9
|
+
declare abstract class Storage {
|
|
10
|
+
/**
|
|
11
|
+
* Get a range of items from a list
|
|
12
|
+
* @param key List key
|
|
13
|
+
* @param start Start index (0-based)
|
|
14
|
+
* @param end End index (-1 for all)
|
|
15
|
+
* @returns Array of string values
|
|
16
|
+
*/
|
|
17
|
+
abstract lrange(key: string, start: number, end: number): Promise<string[]>;
|
|
18
|
+
/**
|
|
19
|
+
* Get all fields and values from a hash
|
|
20
|
+
* @param key Hash key
|
|
21
|
+
* @returns Object with field-value pairs
|
|
22
|
+
*/
|
|
23
|
+
abstract hgetall(key: string): Promise<Record<string, string>>;
|
|
24
|
+
/**
|
|
25
|
+
* Remove items from a list
|
|
26
|
+
* @param key List key
|
|
27
|
+
* @param count Number of items to remove (0 for all, positive for first N, negative for last N)
|
|
28
|
+
* @param value Value to remove
|
|
29
|
+
* @returns Promise that resolves when done
|
|
30
|
+
*/
|
|
31
|
+
abstract lrem(key: string, count: number, value: string): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Pop items from the left side of a list
|
|
34
|
+
* @param key List key
|
|
35
|
+
* @param count Number of items to pop
|
|
36
|
+
* @returns Array of popped values or null if list is empty
|
|
37
|
+
*/
|
|
38
|
+
abstract lpop(key: string, count: number): Promise<string[] | null>;
|
|
39
|
+
/**
|
|
40
|
+
* Delete one or more keys
|
|
41
|
+
* @param keys Key(s) to delete
|
|
42
|
+
* @returns Promise that resolves when done
|
|
43
|
+
*/
|
|
44
|
+
abstract del(...keys: string[]): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Increment a hash field by a value
|
|
47
|
+
* @param key Hash key
|
|
48
|
+
* @param field Field name
|
|
49
|
+
* @param increment Increment value
|
|
50
|
+
* @returns Promise that resolves when done
|
|
51
|
+
*/
|
|
52
|
+
abstract hincrby(key: string, field: string, increment: number): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Set hash field(s)
|
|
55
|
+
* @param key Hash key
|
|
56
|
+
* @param fieldOrObject Field name or object with field-value pairs
|
|
57
|
+
* @param value Value (if field is a string)
|
|
58
|
+
* @returns Promise that resolves when done
|
|
59
|
+
*/
|
|
60
|
+
abstract hset(key: string, fieldOrObject: string | Record<string, string>, value?: string): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Get a hash field value
|
|
63
|
+
* @param key Hash key
|
|
64
|
+
* @param field Field name
|
|
65
|
+
* @returns Field value or null if not found
|
|
66
|
+
*/
|
|
67
|
+
abstract hget(key: string, field: string): Promise<string | null>;
|
|
68
|
+
/**
|
|
69
|
+
* Delete one or more hash fields
|
|
70
|
+
* @param key Hash key
|
|
71
|
+
* @param fields Field name(s) to delete
|
|
72
|
+
* @returns Promise that resolves when done
|
|
73
|
+
*/
|
|
74
|
+
abstract hdel(key: string, ...fields: string[]): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Push values to the right side of a list
|
|
77
|
+
* @param key List key
|
|
78
|
+
* @param values Values to push
|
|
79
|
+
* @returns Promise that resolves when done
|
|
80
|
+
*/
|
|
81
|
+
abstract rpush(key: string, ...values: string[]): Promise<void>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
type getBroadcastChats = (botId: number, offset: number, limit: number, filter?: string) => Promise<string[] | number[]>;
|
|
85
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
86
|
+
type setRestricted = (botId: number, chatId: string, type: /*Users: */ 'block' | 'deactivated' | /*Groups: */ 'banned' | 'restricted') => Promise<void>;
|
|
87
|
+
type progressCallback = (id: string, sent: number, error: number, total: number) => void;
|
|
88
|
+
interface BroadcastOptions {
|
|
89
|
+
storage: Storage;
|
|
90
|
+
getBroadcastChats: getBroadcastChats;
|
|
91
|
+
setRestricted?: setRestricted | null;
|
|
92
|
+
chunkSize?: number;
|
|
93
|
+
keyPrefix?: string;
|
|
94
|
+
sudoUsers: number[];
|
|
95
|
+
hasPermission?: (ctx: Context) => MaybePromise<boolean>;
|
|
96
|
+
getApi: (botId: number) => MaybePromise<Api>;
|
|
97
|
+
isMainInstance: boolean;
|
|
98
|
+
reportFrequency?: number;
|
|
99
|
+
checkQueueInterval?: number;
|
|
100
|
+
progressCallback?: progressCallback | null;
|
|
101
|
+
allowPaidBroadcast?: boolean;
|
|
102
|
+
cmds?: {
|
|
103
|
+
broadcast?: string;
|
|
104
|
+
copy?: string;
|
|
105
|
+
forward?: string;
|
|
106
|
+
addmsg?: string;
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Redis storage implementation using ioredis
|
|
112
|
+
*/
|
|
113
|
+
declare class RedisStorage extends Storage {
|
|
114
|
+
private redis;
|
|
115
|
+
constructor(redis: Redis);
|
|
116
|
+
lrange(key: string, start: number, end: number): Promise<string[]>;
|
|
117
|
+
hgetall(key: string): Promise<Record<string, string>>;
|
|
118
|
+
lrem(key: string, count: number, value: string): Promise<void>;
|
|
119
|
+
lpop(key: string, count: number): Promise<string[] | null>;
|
|
120
|
+
del(...keys: string[]): Promise<void>;
|
|
121
|
+
hincrby(key: string, field: string, increment: number): Promise<void>;
|
|
122
|
+
hset(key: string, fieldOrObject: string | Record<string, string>, value?: string): Promise<void>;
|
|
123
|
+
hget(key: string, field: string): Promise<string | null>;
|
|
124
|
+
hdel(key: string, ...fields: string[]): Promise<void>;
|
|
125
|
+
rpush(key: string, ...values: string[]): Promise<void>;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Memory storage implementation using in-memory data structures
|
|
130
|
+
*/
|
|
131
|
+
declare class MemoryStorage extends Storage {
|
|
132
|
+
private lists;
|
|
133
|
+
private hashes;
|
|
134
|
+
lrange(key: string, start: number, end: number): Promise<string[]>;
|
|
135
|
+
hgetall(key: string): Promise<Record<string, string>>;
|
|
136
|
+
lrem(key: string, count: number, value: string): Promise<void>;
|
|
137
|
+
lpop(key: string, count: number): Promise<string[] | null>;
|
|
138
|
+
del(...keys: string[]): Promise<void>;
|
|
139
|
+
hincrby(key: string, field: string, increment: number): Promise<void>;
|
|
140
|
+
hset(key: string, fieldOrObject: string | Record<string, string>, value?: string): Promise<void>;
|
|
141
|
+
hget(key: string, field: string): Promise<string | null>;
|
|
142
|
+
hdel(key: string, ...fields: string[]): Promise<void>;
|
|
143
|
+
rpush(key: string, ...values: string[]): Promise<void>;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* File storage implementation using filesystem
|
|
148
|
+
*/
|
|
149
|
+
declare class FileStorage extends Storage {
|
|
150
|
+
private baseDir;
|
|
151
|
+
constructor(baseDir?: string);
|
|
152
|
+
private ensureDir;
|
|
153
|
+
private getListPath;
|
|
154
|
+
private getHashPath;
|
|
155
|
+
private readList;
|
|
156
|
+
private writeList;
|
|
157
|
+
private readHash;
|
|
158
|
+
private writeHash;
|
|
159
|
+
lrange(key: string, start: number, end: number): Promise<string[]>;
|
|
160
|
+
hgetall(key: string): Promise<Record<string, string>>;
|
|
161
|
+
lrem(key: string, count: number, value: string): Promise<void>;
|
|
162
|
+
lpop(key: string, count: number): Promise<string[] | null>;
|
|
163
|
+
del(...keys: string[]): Promise<void>;
|
|
164
|
+
hincrby(key: string, field: string, increment: number): Promise<void>;
|
|
165
|
+
hset(key: string, fieldOrObject: string | Record<string, string>, value?: string): Promise<void>;
|
|
166
|
+
hget(key: string, field: string): Promise<string | null>;
|
|
167
|
+
hdel(key: string, ...fields: string[]): Promise<void>;
|
|
168
|
+
rpush(key: string, ...values: string[]): Promise<void>;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
declare class Broadcaster {
|
|
172
|
+
private options;
|
|
173
|
+
static _instance?: Broadcaster;
|
|
174
|
+
private constructor();
|
|
175
|
+
static getInstance(options: BroadcastOptions): Broadcaster;
|
|
176
|
+
getMiddleware(): grammy.Composer<grammy.Context>;
|
|
177
|
+
}
|
|
178
|
+
declare function createBroadcaster(options: BroadcastOptions): Broadcaster;
|
|
179
|
+
|
|
180
|
+
export { FileStorage, MemoryStorage, RedisStorage, Storage, createBroadcaster };
|