teleproto 1.221.2 → 1.221.4
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/Version.d.ts +1 -1
- package/Version.js +1 -1
- package/client/TelegramClient.d.ts +66 -4
- package/client/TelegramClient.js +73 -0
- package/client/auth.d.ts +101 -4
- package/client/auth.js +178 -3
- package/client/telegramBaseClient.d.ts +7 -0
- package/client/telegramBaseClient.js +1 -0
- package/client/updates.d.ts +5 -2
- package/client/updates.js +195 -48
- package/client/uploads.d.ts +1 -1
- package/client/uploads.js +9 -3
- package/index.d.ts +2 -1
- package/index.js +3 -1
- package/network/MTProtoSender.js +2 -0
- package/package.json +1 -1
package/Version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const version = "1.221.
|
|
1
|
+
export declare const version = "1.221.4";
|
package/Version.js
CHANGED
|
@@ -161,10 +161,62 @@ export declare class TelegramClient extends TelegramBaseClient {
|
|
|
161
161
|
* @param reCaptchaCallback - callback to handle reCAPTCHA verification
|
|
162
162
|
* @return the phone code hash and whether it was sent via app
|
|
163
163
|
*/
|
|
164
|
-
sendCode(apiCredentials: authMethods.ApiCredentials, phoneNumber: string, forceSMS?: boolean, reCaptchaCallback?: (siteKey: string) => Promise<string>): Promise<
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
164
|
+
sendCode(apiCredentials: authMethods.ApiCredentials, phoneNumber: string, forceSMS?: boolean, reCaptchaCallback?: (siteKey: string) => Promise<string>): Promise<authMethods.SendCodeResult>;
|
|
165
|
+
/**
|
|
166
|
+
* Sends an email verification code for login setup.
|
|
167
|
+
* This is used when Telegram requires email verification during login.
|
|
168
|
+
* @param phoneNumber - The phone number being used for login
|
|
169
|
+
* @param phoneCodeHash - The phone code hash from sendCode
|
|
170
|
+
* @param email - The email address to verify
|
|
171
|
+
* @returns The email pattern and code length
|
|
172
|
+
* @example
|
|
173
|
+
* ```ts
|
|
174
|
+
* const result = await client.sendVerifyEmailCode(
|
|
175
|
+
* "+1234567890",
|
|
176
|
+
* "abc123hash",
|
|
177
|
+
* "user@example.com"
|
|
178
|
+
* );
|
|
179
|
+
* console.log(`Code sent to ${result.emailPattern}, length: ${result.length}`);
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
sendVerifyEmailCode(phoneNumber: string, phoneCodeHash: string, email: string): Promise<authMethods.SentEmailCodeResult>;
|
|
183
|
+
/**
|
|
184
|
+
* Verifies an email address during login setup.
|
|
185
|
+
* @param phoneNumber - The phone number being used for login
|
|
186
|
+
* @param phoneCodeHash - The phone code hash from sendCode
|
|
187
|
+
* @param verification - The verification result (code, Google token, or Apple token)
|
|
188
|
+
* @returns The verified email and the new sent code for phone verification
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* // Verify with email code
|
|
192
|
+
* const result = await client.verifyEmail(
|
|
193
|
+
* "+1234567890",
|
|
194
|
+
* "abc123hash",
|
|
195
|
+
* { type: "code", code: "12345" }
|
|
196
|
+
* );
|
|
197
|
+
*
|
|
198
|
+
* // Or verify with Google Sign-In
|
|
199
|
+
* const result = await client.verifyEmail(
|
|
200
|
+
* "+1234567890",
|
|
201
|
+
* "abc123hash",
|
|
202
|
+
* { type: "google", token: "google-id-token" }
|
|
203
|
+
* );
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
verifyEmail(phoneNumber: string, phoneCodeHash: string, verification: authMethods.EmailVerificationResult): Promise<authMethods.EmailVerifiedLoginResult>;
|
|
207
|
+
/**
|
|
208
|
+
* Resets the login email when the user cannot access their current email.
|
|
209
|
+
* This will cancel the current email verification and allow setting up a new one.
|
|
210
|
+
* @param phoneNumber - The phone number being used for login
|
|
211
|
+
* @param phoneCodeHash - The phone code hash from sendCode
|
|
212
|
+
* @returns The new sent code result
|
|
213
|
+
* @example
|
|
214
|
+
* ```ts
|
|
215
|
+
* // User can't access their email, reset it
|
|
216
|
+
* const newSentCode = await client.resetLoginEmail("+1234567890", "abc123hash");
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
resetLoginEmail(phoneNumber: string, phoneCodeHash: string): Promise<Api.auth.TypeSentCode>;
|
|
168
220
|
/**
|
|
169
221
|
* Uses the 2FA password to sign in the account.<br/>
|
|
170
222
|
* This function should be used after the user has signed in with the code they received.
|
|
@@ -810,6 +862,16 @@ export declare class TelegramClient extends TelegramBaseClient {
|
|
|
810
862
|
* @return pair of [eventBuilder,CallableFunction]
|
|
811
863
|
*/
|
|
812
864
|
listEventHandlers(): [EventBuilder, CallableFunction][];
|
|
865
|
+
/**
|
|
866
|
+
* Fetches and processes any updates that were missed while disconnected.
|
|
867
|
+
* Call this after reconnecting to ensure no updates are lost.
|
|
868
|
+
* @example
|
|
869
|
+
* ```ts
|
|
870
|
+
* await client.connect();
|
|
871
|
+
* await client.catchUp(); // Fetch missed updates
|
|
872
|
+
* ```
|
|
873
|
+
*/
|
|
874
|
+
catchUp(): Promise<void>;
|
|
813
875
|
/**
|
|
814
876
|
* Uploads a file to Telegram's servers, without sending it.
|
|
815
877
|
* @remarks generally it's better to use {@link sendFile} instead.
|
package/client/TelegramClient.js
CHANGED
|
@@ -211,6 +211,67 @@ class TelegramClient extends telegramBaseClient_1.TelegramBaseClient {
|
|
|
211
211
|
sendCode(apiCredentials, phoneNumber, forceSMS = false, reCaptchaCallback) {
|
|
212
212
|
return authMethods.sendCode(this, apiCredentials, phoneNumber, forceSMS, reCaptchaCallback);
|
|
213
213
|
}
|
|
214
|
+
/**
|
|
215
|
+
* Sends an email verification code for login setup.
|
|
216
|
+
* This is used when Telegram requires email verification during login.
|
|
217
|
+
* @param phoneNumber - The phone number being used for login
|
|
218
|
+
* @param phoneCodeHash - The phone code hash from sendCode
|
|
219
|
+
* @param email - The email address to verify
|
|
220
|
+
* @returns The email pattern and code length
|
|
221
|
+
* @example
|
|
222
|
+
* ```ts
|
|
223
|
+
* const result = await client.sendVerifyEmailCode(
|
|
224
|
+
* "+1234567890",
|
|
225
|
+
* "abc123hash",
|
|
226
|
+
* "user@example.com"
|
|
227
|
+
* );
|
|
228
|
+
* console.log(`Code sent to ${result.emailPattern}, length: ${result.length}`);
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
sendVerifyEmailCode(phoneNumber, phoneCodeHash, email) {
|
|
232
|
+
return authMethods.sendVerifyEmailCode(this, phoneNumber, phoneCodeHash, email);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Verifies an email address during login setup.
|
|
236
|
+
* @param phoneNumber - The phone number being used for login
|
|
237
|
+
* @param phoneCodeHash - The phone code hash from sendCode
|
|
238
|
+
* @param verification - The verification result (code, Google token, or Apple token)
|
|
239
|
+
* @returns The verified email and the new sent code for phone verification
|
|
240
|
+
* @example
|
|
241
|
+
* ```ts
|
|
242
|
+
* // Verify with email code
|
|
243
|
+
* const result = await client.verifyEmail(
|
|
244
|
+
* "+1234567890",
|
|
245
|
+
* "abc123hash",
|
|
246
|
+
* { type: "code", code: "12345" }
|
|
247
|
+
* );
|
|
248
|
+
*
|
|
249
|
+
* // Or verify with Google Sign-In
|
|
250
|
+
* const result = await client.verifyEmail(
|
|
251
|
+
* "+1234567890",
|
|
252
|
+
* "abc123hash",
|
|
253
|
+
* { type: "google", token: "google-id-token" }
|
|
254
|
+
* );
|
|
255
|
+
* ```
|
|
256
|
+
*/
|
|
257
|
+
verifyEmail(phoneNumber, phoneCodeHash, verification) {
|
|
258
|
+
return authMethods.verifyEmail(this, phoneNumber, phoneCodeHash, verification);
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Resets the login email when the user cannot access their current email.
|
|
262
|
+
* This will cancel the current email verification and allow setting up a new one.
|
|
263
|
+
* @param phoneNumber - The phone number being used for login
|
|
264
|
+
* @param phoneCodeHash - The phone code hash from sendCode
|
|
265
|
+
* @returns The new sent code result
|
|
266
|
+
* @example
|
|
267
|
+
* ```ts
|
|
268
|
+
* // User can't access their email, reset it
|
|
269
|
+
* const newSentCode = await client.resetLoginEmail("+1234567890", "abc123hash");
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
resetLoginEmail(phoneNumber, phoneCodeHash) {
|
|
273
|
+
return authMethods.resetLoginEmail(this, phoneNumber, phoneCodeHash);
|
|
274
|
+
}
|
|
214
275
|
/**
|
|
215
276
|
* Uses the 2FA password to sign in the account.<br/>
|
|
216
277
|
* This function should be used after the user has signed in with the code they received.
|
|
@@ -862,6 +923,18 @@ class TelegramClient extends telegramBaseClient_1.TelegramBaseClient {
|
|
|
862
923
|
listEventHandlers() {
|
|
863
924
|
return updateMethods.listEventHandlers(this);
|
|
864
925
|
}
|
|
926
|
+
/**
|
|
927
|
+
* Fetches and processes any updates that were missed while disconnected.
|
|
928
|
+
* Call this after reconnecting to ensure no updates are lost.
|
|
929
|
+
* @example
|
|
930
|
+
* ```ts
|
|
931
|
+
* await client.connect();
|
|
932
|
+
* await client.catchUp(); // Fetch missed updates
|
|
933
|
+
* ```
|
|
934
|
+
*/
|
|
935
|
+
async catchUp() {
|
|
936
|
+
return (0, updates_1.catchUp)(this);
|
|
937
|
+
}
|
|
865
938
|
// region uploads
|
|
866
939
|
/**
|
|
867
940
|
* Uploads a file to Telegram's servers, without sending it.
|
package/client/auth.d.ts
CHANGED
|
@@ -1,5 +1,35 @@
|
|
|
1
1
|
import { Api } from "../tl";
|
|
2
2
|
import type { TelegramClient } from "./TelegramClient";
|
|
3
|
+
/**
|
|
4
|
+
* Email verification options passed to the email callback.
|
|
5
|
+
*/
|
|
6
|
+
export interface EmailVerificationOptions {
|
|
7
|
+
/** Whether Google Sign-In is allowed for email verification. */
|
|
8
|
+
googleSigninAllowed?: boolean;
|
|
9
|
+
/** Whether Apple Sign-In is allowed for email verification. */
|
|
10
|
+
appleSigninAllowed?: boolean;
|
|
11
|
+
/** The email pattern (masked email) when code was sent to existing email. */
|
|
12
|
+
emailPattern?: string;
|
|
13
|
+
/** The code length when email code is expected. */
|
|
14
|
+
codeLength?: number;
|
|
15
|
+
/** Period in seconds after which the email can be reset. */
|
|
16
|
+
resetAvailablePeriod?: number;
|
|
17
|
+
/** Date when the pending reset will complete. */
|
|
18
|
+
resetPendingDate?: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Result from the email callback - can be code, Google token, or Apple token.
|
|
22
|
+
*/
|
|
23
|
+
export type EmailVerificationResult = {
|
|
24
|
+
type: "code";
|
|
25
|
+
code: string;
|
|
26
|
+
} | {
|
|
27
|
+
type: "google";
|
|
28
|
+
token: string;
|
|
29
|
+
} | {
|
|
30
|
+
type: "apple";
|
|
31
|
+
token: string;
|
|
32
|
+
};
|
|
3
33
|
/**
|
|
4
34
|
* For when you want to login as a {@link Api.User}<br/>
|
|
5
35
|
* this should handle all needed steps for authorization as a user.<br/>
|
|
@@ -29,6 +59,14 @@ export interface UserAuthParams {
|
|
|
29
59
|
forceSMS?: boolean;
|
|
30
60
|
/** optional callback for handling reCAPTCHA. */
|
|
31
61
|
reCaptchaCallback?: (siteKey: string) => Promise<string>;
|
|
62
|
+
/** callback for email verification when Telegram requires email setup or code.<br/>
|
|
63
|
+
* Called when `auth.SentCodeTypeSetUpEmailRequired` or `auth.SentCodeTypeEmailCode` is received.<br/>
|
|
64
|
+
* For setup: should return email address to use and then handle verification code.<br/>
|
|
65
|
+
* For existing email: should return the verification result (code, Google token, or Apple token). */
|
|
66
|
+
emailVerification?: (options: EmailVerificationOptions) => Promise<EmailVerificationResult>;
|
|
67
|
+
/** callback to get email address when email setup is required.<br/>
|
|
68
|
+
* Called first when `auth.SentCodeTypeSetUpEmailRequired` is received. */
|
|
69
|
+
emailAddress?: () => Promise<string>;
|
|
32
70
|
}
|
|
33
71
|
export interface UserPasswordAuthParams {
|
|
34
72
|
/** optional string or callback that should return the 2FA password if present.<br/>
|
|
@@ -80,16 +118,75 @@ export declare function checkAuthorization(client: TelegramClient): Promise<bool
|
|
|
80
118
|
export declare function signInUser(client: TelegramClient, apiCredentials: ApiCredentials, authParams: UserAuthParams): Promise<Api.TypeUser>;
|
|
81
119
|
/** @hidden */
|
|
82
120
|
export declare function signInUserWithQrCode(client: TelegramClient, apiCredentials: ApiCredentials, authParams: QrCodeAuthParams): Promise<Api.TypeUser>;
|
|
83
|
-
/**
|
|
84
|
-
|
|
85
|
-
|
|
121
|
+
/**
|
|
122
|
+
* Result from sendCode containing info about how to proceed with verification.
|
|
123
|
+
*/
|
|
124
|
+
export interface SendCodeResult {
|
|
125
|
+
/** The phone code hash needed for sign in. */
|
|
86
126
|
phoneCodeHash: string;
|
|
127
|
+
/** Whether the code was sent via Telegram app (true) or SMS (false). */
|
|
87
128
|
isCodeViaApp: boolean;
|
|
88
|
-
|
|
129
|
+
/** If true, email setup is required before phone code. */
|
|
130
|
+
emailRequired?: boolean;
|
|
131
|
+
/** If true, code was sent to existing email. */
|
|
132
|
+
emailCodeSent?: boolean;
|
|
133
|
+
/** Email verification options when email is involved. */
|
|
134
|
+
emailOptions?: EmailVerificationOptions;
|
|
135
|
+
}
|
|
136
|
+
/** @hidden */
|
|
137
|
+
export declare function sendCode(client: TelegramClient, apiCredentials: ApiCredentials, phoneNumber: string, forceSMS?: boolean, reCaptchaCallback?: (siteKey: string) => Promise<string>): Promise<SendCodeResult>;
|
|
89
138
|
/** @hidden */
|
|
90
139
|
export declare function signInWithPassword(client: TelegramClient, apiCredentials: ApiCredentials, authParams: UserPasswordAuthParams): Promise<Api.TypeUser>;
|
|
91
140
|
/** @hidden */
|
|
92
141
|
export declare function signInBot(client: TelegramClient, apiCredentials: ApiCredentials, authParams: BotAuthParams): Promise<Api.TypeUser>;
|
|
93
142
|
/** @hidden */
|
|
94
143
|
export declare function _authFlow(client: TelegramClient, apiCredentials: ApiCredentials, authParams: UserAuthParams | BotAuthParams): Promise<void>;
|
|
144
|
+
/**
|
|
145
|
+
* Result from sendVerifyEmailCode.
|
|
146
|
+
*/
|
|
147
|
+
export interface SentEmailCodeResult {
|
|
148
|
+
/** The masked email pattern where the code was sent. */
|
|
149
|
+
emailPattern: string;
|
|
150
|
+
/** The length of the verification code. */
|
|
151
|
+
length: number;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Sends an email verification code for login setup.
|
|
155
|
+
* @param client - The telegram client
|
|
156
|
+
* @param phoneNumber - The phone number being used for login
|
|
157
|
+
* @param phoneCodeHash - The phone code hash from sendCode
|
|
158
|
+
* @param email - The email address to verify
|
|
159
|
+
* @returns The email pattern and code length
|
|
160
|
+
*/
|
|
161
|
+
/** @hidden */
|
|
162
|
+
export declare function sendVerifyEmailCode(client: TelegramClient, phoneNumber: string, phoneCodeHash: string, email: string): Promise<SentEmailCodeResult>;
|
|
163
|
+
/**
|
|
164
|
+
* Result from verifyEmail for login.
|
|
165
|
+
*/
|
|
166
|
+
export interface EmailVerifiedLoginResult {
|
|
167
|
+
/** The verified email address. */
|
|
168
|
+
email: string;
|
|
169
|
+
/** The new sent code result to continue with phone verification. */
|
|
170
|
+
sentCode: Api.auth.TypeSentCode;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Verifies an email address during login setup.
|
|
174
|
+
* @param client - The telegram client
|
|
175
|
+
* @param phoneNumber - The phone number being used for login
|
|
176
|
+
* @param phoneCodeHash - The phone code hash from sendCode
|
|
177
|
+
* @param verification - The verification (code, Google token, or Apple token)
|
|
178
|
+
* @returns The verified email and the new sent code for phone verification
|
|
179
|
+
*/
|
|
180
|
+
/** @hidden */
|
|
181
|
+
export declare function verifyEmail(client: TelegramClient, phoneNumber: string, phoneCodeHash: string, verification: EmailVerificationResult): Promise<EmailVerifiedLoginResult>;
|
|
182
|
+
/**
|
|
183
|
+
* Resets the login email when the user cannot access their current email.
|
|
184
|
+
* This will cancel the current email verification and allow setting up a new one.
|
|
185
|
+
* @param client - The telegram client
|
|
186
|
+
* @param phoneNumber - The phone number being used for login
|
|
187
|
+
* @param phoneCodeHash - The phone code hash from sendCode
|
|
188
|
+
* @returns The new sent code result
|
|
189
|
+
*/
|
|
190
|
+
/** @hidden */
|
|
191
|
+
export declare function resetLoginEmail(client: TelegramClient, phoneNumber: string, phoneCodeHash: string): Promise<Api.auth.TypeSentCode>;
|
|
95
192
|
export {};
|
package/client/auth.js
CHANGED
|
@@ -41,6 +41,9 @@ exports.sendCode = sendCode;
|
|
|
41
41
|
exports.signInWithPassword = signInWithPassword;
|
|
42
42
|
exports.signInBot = signInBot;
|
|
43
43
|
exports._authFlow = _authFlow;
|
|
44
|
+
exports.sendVerifyEmailCode = sendVerifyEmailCode;
|
|
45
|
+
exports.verifyEmail = verifyEmail;
|
|
46
|
+
exports.resetLoginEmail = resetLoginEmail;
|
|
44
47
|
const tl_1 = require("../tl");
|
|
45
48
|
const utils = __importStar(require("../Utils"));
|
|
46
49
|
const Helpers_1 = require("../Helpers");
|
|
@@ -73,8 +76,8 @@ async function checkAuthorization(client) {
|
|
|
73
76
|
}
|
|
74
77
|
/** @hidden */
|
|
75
78
|
async function signInUser(client, apiCredentials, authParams) {
|
|
76
|
-
let phoneNumber;
|
|
77
|
-
let phoneCodeHash;
|
|
79
|
+
let phoneNumber = "";
|
|
80
|
+
let phoneCodeHash = "";
|
|
78
81
|
let isCodeViaApp = false;
|
|
79
82
|
while (1) {
|
|
80
83
|
try {
|
|
@@ -98,6 +101,45 @@ async function signInUser(client, apiCredentials, authParams) {
|
|
|
98
101
|
if (typeof phoneCodeHash !== "string") {
|
|
99
102
|
throw new Error("Failed to retrieve phone code hash");
|
|
100
103
|
}
|
|
104
|
+
// Handle email verification if required
|
|
105
|
+
if (sendCodeResult.emailRequired) {
|
|
106
|
+
// Email setup is required before phone code
|
|
107
|
+
if (!authParams.emailAddress || !authParams.emailVerification) {
|
|
108
|
+
throw new Error("Email verification required but emailAddress or emailVerification callback not provided");
|
|
109
|
+
}
|
|
110
|
+
// Get email address from user
|
|
111
|
+
const email = await authParams.emailAddress();
|
|
112
|
+
// Send verification code to email
|
|
113
|
+
const emailCodeResult = await sendVerifyEmailCode(client, phoneNumber, phoneCodeHash, email);
|
|
114
|
+
// Get verification from user
|
|
115
|
+
const verification = await authParams.emailVerification(Object.assign(Object.assign({}, sendCodeResult.emailOptions), { emailPattern: emailCodeResult.emailPattern, codeLength: emailCodeResult.length }));
|
|
116
|
+
// Verify email
|
|
117
|
+
const verifyResult = await verifyEmail(client, phoneNumber, phoneCodeHash, verification);
|
|
118
|
+
// Update phone code hash from the new sent code
|
|
119
|
+
if (verifyResult.sentCode instanceof tl_1.Api.auth.SentCode) {
|
|
120
|
+
phoneCodeHash = verifyResult.sentCode.phoneCodeHash;
|
|
121
|
+
isCodeViaApp =
|
|
122
|
+
verifyResult.sentCode.type instanceof
|
|
123
|
+
tl_1.Api.auth.SentCodeTypeApp;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
else if (sendCodeResult.emailCodeSent) {
|
|
127
|
+
// Code was sent to existing email
|
|
128
|
+
if (!authParams.emailVerification) {
|
|
129
|
+
throw new Error("Email code sent but emailVerification callback not provided");
|
|
130
|
+
}
|
|
131
|
+
// Get verification from user for existing email
|
|
132
|
+
const verification = await authParams.emailVerification(sendCodeResult.emailOptions || {});
|
|
133
|
+
// Verify with existing email
|
|
134
|
+
const verifyResult = await verifyEmail(client, phoneNumber, phoneCodeHash, verification);
|
|
135
|
+
// Update phone code hash from the new sent code
|
|
136
|
+
if (verifyResult.sentCode instanceof tl_1.Api.auth.SentCode) {
|
|
137
|
+
phoneCodeHash = verifyResult.sentCode.phoneCodeHash;
|
|
138
|
+
isCodeViaApp =
|
|
139
|
+
verifyResult.sentCode.type instanceof
|
|
140
|
+
tl_1.Api.auth.SentCodeTypeApp;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
101
143
|
break;
|
|
102
144
|
}
|
|
103
145
|
catch (err) {
|
|
@@ -273,7 +315,6 @@ async function signInUserWithQrCode(client, apiCredentials, authParams) {
|
|
|
273
315
|
throw new Error("QR auth failed");
|
|
274
316
|
}
|
|
275
317
|
/** @hidden */
|
|
276
|
-
/** @hidden */
|
|
277
318
|
async function sendCode(client, apiCredentials, phoneNumber, forceSMS = false, reCaptchaCallback) {
|
|
278
319
|
var _a;
|
|
279
320
|
try {
|
|
@@ -310,6 +351,33 @@ async function sendCode(client, apiCredentials, phoneNumber, forceSMS = false, r
|
|
|
310
351
|
isCodeViaApp: false,
|
|
311
352
|
};
|
|
312
353
|
}
|
|
354
|
+
// Handle email verification types
|
|
355
|
+
if (sendResult.type instanceof tl_1.Api.auth.SentCodeTypeSetUpEmailRequired) {
|
|
356
|
+
return {
|
|
357
|
+
phoneCodeHash: sendResult.phoneCodeHash,
|
|
358
|
+
isCodeViaApp: false,
|
|
359
|
+
emailRequired: true,
|
|
360
|
+
emailOptions: {
|
|
361
|
+
googleSigninAllowed: sendResult.type.googleSigninAllowed,
|
|
362
|
+
appleSigninAllowed: sendResult.type.appleSigninAllowed,
|
|
363
|
+
},
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
if (sendResult.type instanceof tl_1.Api.auth.SentCodeTypeEmailCode) {
|
|
367
|
+
return {
|
|
368
|
+
phoneCodeHash: sendResult.phoneCodeHash,
|
|
369
|
+
isCodeViaApp: false,
|
|
370
|
+
emailCodeSent: true,
|
|
371
|
+
emailOptions: {
|
|
372
|
+
googleSigninAllowed: sendResult.type.googleSigninAllowed,
|
|
373
|
+
appleSigninAllowed: sendResult.type.appleSigninAllowed,
|
|
374
|
+
emailPattern: sendResult.type.emailPattern,
|
|
375
|
+
codeLength: sendResult.type.length,
|
|
376
|
+
resetAvailablePeriod: sendResult.type.resetAvailablePeriod,
|
|
377
|
+
resetPendingDate: sendResult.type.resetPendingDate,
|
|
378
|
+
},
|
|
379
|
+
};
|
|
380
|
+
}
|
|
313
381
|
// If we already sent a SMS, do not resend the phoneCode (hash may be empty)
|
|
314
382
|
if (!forceSMS || sendResult.type instanceof tl_1.Api.auth.SentCodeTypeSms) {
|
|
315
383
|
return {
|
|
@@ -329,6 +397,33 @@ async function sendCode(client, apiCredentials, phoneNumber, forceSMS = false, r
|
|
|
329
397
|
isCodeViaApp: false,
|
|
330
398
|
};
|
|
331
399
|
}
|
|
400
|
+
// Handle email types in resend result as well
|
|
401
|
+
if (resendResult.type instanceof tl_1.Api.auth.SentCodeTypeSetUpEmailRequired) {
|
|
402
|
+
return {
|
|
403
|
+
phoneCodeHash: resendResult.phoneCodeHash,
|
|
404
|
+
isCodeViaApp: false,
|
|
405
|
+
emailRequired: true,
|
|
406
|
+
emailOptions: {
|
|
407
|
+
googleSigninAllowed: resendResult.type.googleSigninAllowed,
|
|
408
|
+
appleSigninAllowed: resendResult.type.appleSigninAllowed,
|
|
409
|
+
},
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
if (resendResult.type instanceof tl_1.Api.auth.SentCodeTypeEmailCode) {
|
|
413
|
+
return {
|
|
414
|
+
phoneCodeHash: resendResult.phoneCodeHash,
|
|
415
|
+
isCodeViaApp: false,
|
|
416
|
+
emailCodeSent: true,
|
|
417
|
+
emailOptions: {
|
|
418
|
+
googleSigninAllowed: resendResult.type.googleSigninAllowed,
|
|
419
|
+
appleSigninAllowed: resendResult.type.appleSigninAllowed,
|
|
420
|
+
emailPattern: resendResult.type.emailPattern,
|
|
421
|
+
codeLength: resendResult.type.length,
|
|
422
|
+
resetAvailablePeriod: resendResult.type.resetAvailablePeriod,
|
|
423
|
+
resetPendingDate: resendResult.type.resetPendingDate,
|
|
424
|
+
},
|
|
425
|
+
};
|
|
426
|
+
}
|
|
332
427
|
return {
|
|
333
428
|
phoneCodeHash: resendResult.phoneCodeHash,
|
|
334
429
|
isCodeViaApp: resendResult.type instanceof tl_1.Api.auth.SentCodeTypeApp,
|
|
@@ -406,3 +501,83 @@ async function _authFlow(client, apiCredentials, authParams) {
|
|
|
406
501
|
: await client.signInBot(apiCredentials, authParams);
|
|
407
502
|
client._log.info("Signed in successfully as " + utils.getDisplayName(me));
|
|
408
503
|
}
|
|
504
|
+
/**
|
|
505
|
+
* Sends an email verification code for login setup.
|
|
506
|
+
* @param client - The telegram client
|
|
507
|
+
* @param phoneNumber - The phone number being used for login
|
|
508
|
+
* @param phoneCodeHash - The phone code hash from sendCode
|
|
509
|
+
* @param email - The email address to verify
|
|
510
|
+
* @returns The email pattern and code length
|
|
511
|
+
*/
|
|
512
|
+
/** @hidden */
|
|
513
|
+
async function sendVerifyEmailCode(client, phoneNumber, phoneCodeHash, email) {
|
|
514
|
+
const result = await client.invoke(new tl_1.Api.account.SendVerifyEmailCode({
|
|
515
|
+
purpose: new tl_1.Api.EmailVerifyPurposeLoginSetup({
|
|
516
|
+
phoneNumber,
|
|
517
|
+
phoneCodeHash,
|
|
518
|
+
}),
|
|
519
|
+
email,
|
|
520
|
+
}));
|
|
521
|
+
return {
|
|
522
|
+
emailPattern: result.emailPattern,
|
|
523
|
+
length: result.length,
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* Verifies an email address during login setup.
|
|
528
|
+
* @param client - The telegram client
|
|
529
|
+
* @param phoneNumber - The phone number being used for login
|
|
530
|
+
* @param phoneCodeHash - The phone code hash from sendCode
|
|
531
|
+
* @param verification - The verification (code, Google token, or Apple token)
|
|
532
|
+
* @returns The verified email and the new sent code for phone verification
|
|
533
|
+
*/
|
|
534
|
+
/** @hidden */
|
|
535
|
+
async function verifyEmail(client, phoneNumber, phoneCodeHash, verification) {
|
|
536
|
+
let emailVerification;
|
|
537
|
+
switch (verification.type) {
|
|
538
|
+
case "code":
|
|
539
|
+
emailVerification = new tl_1.Api.EmailVerificationCode({
|
|
540
|
+
code: verification.code,
|
|
541
|
+
});
|
|
542
|
+
break;
|
|
543
|
+
case "google":
|
|
544
|
+
emailVerification = new tl_1.Api.EmailVerificationGoogle({
|
|
545
|
+
token: verification.token,
|
|
546
|
+
});
|
|
547
|
+
break;
|
|
548
|
+
case "apple":
|
|
549
|
+
emailVerification = new tl_1.Api.EmailVerificationApple({
|
|
550
|
+
token: verification.token,
|
|
551
|
+
});
|
|
552
|
+
break;
|
|
553
|
+
}
|
|
554
|
+
const result = await client.invoke(new tl_1.Api.account.VerifyEmail({
|
|
555
|
+
purpose: new tl_1.Api.EmailVerifyPurposeLoginSetup({
|
|
556
|
+
phoneNumber,
|
|
557
|
+
phoneCodeHash,
|
|
558
|
+
}),
|
|
559
|
+
verification: emailVerification,
|
|
560
|
+
}));
|
|
561
|
+
if (!(result instanceof tl_1.Api.account.EmailVerifiedLogin)) {
|
|
562
|
+
throw new Error("Expected EmailVerifiedLogin but got " + result.className);
|
|
563
|
+
}
|
|
564
|
+
return {
|
|
565
|
+
email: result.email,
|
|
566
|
+
sentCode: result.sentCode,
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* Resets the login email when the user cannot access their current email.
|
|
571
|
+
* This will cancel the current email verification and allow setting up a new one.
|
|
572
|
+
* @param client - The telegram client
|
|
573
|
+
* @param phoneNumber - The phone number being used for login
|
|
574
|
+
* @param phoneCodeHash - The phone code hash from sendCode
|
|
575
|
+
* @returns The new sent code result
|
|
576
|
+
*/
|
|
577
|
+
/** @hidden */
|
|
578
|
+
async function resetLoginEmail(client, phoneNumber, phoneCodeHash) {
|
|
579
|
+
return await client.invoke(new tl_1.Api.auth.ResetLoginEmail({
|
|
580
|
+
phoneNumber,
|
|
581
|
+
phoneCodeHash,
|
|
582
|
+
}));
|
|
583
|
+
}
|
|
@@ -181,6 +181,13 @@ export declare abstract class TelegramBaseClient {
|
|
|
181
181
|
/** @hidden */
|
|
182
182
|
protected _loopStarted: boolean;
|
|
183
183
|
/** @hidden */
|
|
184
|
+
_updateState?: {
|
|
185
|
+
pts: number;
|
|
186
|
+
qts: number;
|
|
187
|
+
date: number;
|
|
188
|
+
seq: number;
|
|
189
|
+
};
|
|
190
|
+
/** @hidden */
|
|
184
191
|
_reconnecting: boolean;
|
|
185
192
|
/** @hidden */
|
|
186
193
|
_destroyed: boolean;
|
package/client/updates.d.ts
CHANGED
|
@@ -19,8 +19,11 @@ export declare function addEventHandler(client: TelegramClient, callback: Callab
|
|
|
19
19
|
export declare function removeEventHandler(client: TelegramClient, callback: CallableFunction, event: EventBuilder): void;
|
|
20
20
|
/** @hidden */
|
|
21
21
|
export declare function listEventHandlers(client: TelegramClient): [EventBuilder, CallableFunction][];
|
|
22
|
-
/**
|
|
23
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Fetches and processes any updates that were missed while disconnected.
|
|
24
|
+
* Call this after reconnecting to ensure no updates are lost.
|
|
25
|
+
*/
|
|
26
|
+
export declare function catchUp(client: TelegramClient): Promise<void>;
|
|
24
27
|
/** @hidden */
|
|
25
28
|
export declare function _handleUpdate(client: TelegramClient, update: Api.TypeUpdate | number): void;
|
|
26
29
|
/** @hidden */
|
package/client/updates.js
CHANGED
|
@@ -14,7 +14,6 @@ const tl_1 = require("../tl");
|
|
|
14
14
|
const network_1 = require("../network");
|
|
15
15
|
const index_1 = require("../index");
|
|
16
16
|
const Helpers_1 = require("../Helpers");
|
|
17
|
-
const Logger_1 = require("../extensions/Logger");
|
|
18
17
|
const PING_INTERVAL = 9000; // 9 sec
|
|
19
18
|
const PING_TIMEOUT = 10000; // 10 sec
|
|
20
19
|
const PING_FAIL_ATTEMPTS = 3;
|
|
@@ -61,39 +60,151 @@ function removeEventHandler(client, callback, event) {
|
|
|
61
60
|
function listEventHandlers(client) {
|
|
62
61
|
return client._eventBuilders;
|
|
63
62
|
}
|
|
64
|
-
/**
|
|
65
|
-
|
|
66
|
-
|
|
63
|
+
/**
|
|
64
|
+
* Fetches and processes any updates that were missed while disconnected.
|
|
65
|
+
* Call this after reconnecting to ensure no updates are lost.
|
|
66
|
+
*/
|
|
67
|
+
async function catchUp(client) {
|
|
68
|
+
try {
|
|
69
|
+
// Get current state if we don't have one
|
|
70
|
+
if (!client._updateState) {
|
|
71
|
+
const state = await client.invoke(new tl_1.Api.updates.GetState());
|
|
72
|
+
client._updateState = {
|
|
73
|
+
pts: state.pts,
|
|
74
|
+
qts: state.qts,
|
|
75
|
+
date: state.date,
|
|
76
|
+
seq: state.seq,
|
|
77
|
+
};
|
|
78
|
+
client._log.debug("Initialized update state");
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
// Fetch missed updates
|
|
82
|
+
client._log.debug("Catching up on missed updates...");
|
|
83
|
+
let fetching = true;
|
|
84
|
+
while (fetching) {
|
|
85
|
+
const diff = await client.invoke(new tl_1.Api.updates.GetDifference({
|
|
86
|
+
pts: client._updateState.pts,
|
|
87
|
+
date: client._updateState.date,
|
|
88
|
+
qts: client._updateState.qts,
|
|
89
|
+
}));
|
|
90
|
+
if (diff instanceof tl_1.Api.updates.DifferenceEmpty) {
|
|
91
|
+
client._updateState.date = diff.date;
|
|
92
|
+
client._updateState.seq = diff.seq;
|
|
93
|
+
fetching = false;
|
|
94
|
+
}
|
|
95
|
+
else if (diff instanceof tl_1.Api.updates.Difference) {
|
|
96
|
+
// Process all missed updates
|
|
97
|
+
await _processDifference(client, diff);
|
|
98
|
+
client._updateState = {
|
|
99
|
+
pts: diff.state.pts,
|
|
100
|
+
qts: diff.state.qts,
|
|
101
|
+
date: diff.state.date,
|
|
102
|
+
seq: diff.state.seq,
|
|
103
|
+
};
|
|
104
|
+
fetching = false;
|
|
105
|
+
}
|
|
106
|
+
else if (diff instanceof tl_1.Api.updates.DifferenceSlice) {
|
|
107
|
+
// Process partial updates, continue fetching
|
|
108
|
+
await _processDifference(client, diff);
|
|
109
|
+
client._updateState = {
|
|
110
|
+
pts: diff.intermediateState.pts,
|
|
111
|
+
qts: diff.intermediateState.qts,
|
|
112
|
+
date: diff.intermediateState.date,
|
|
113
|
+
seq: diff.intermediateState.seq,
|
|
114
|
+
};
|
|
115
|
+
// Continue loop to fetch more
|
|
116
|
+
}
|
|
117
|
+
else if (diff instanceof tl_1.Api.updates.DifferenceTooLong) {
|
|
118
|
+
// Too many updates missed, just update pts and continue
|
|
119
|
+
client._updateState.pts = diff.pts;
|
|
120
|
+
fetching = false;
|
|
121
|
+
client._log.warn("Too many updates missed, some may be lost");
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
client._log.debug("Catch up complete");
|
|
125
|
+
}
|
|
126
|
+
catch (e) {
|
|
127
|
+
client._log.error(`Error during catch up: ${e}`);
|
|
128
|
+
}
|
|
67
129
|
}
|
|
68
130
|
/** @hidden */
|
|
69
|
-
function
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
131
|
+
async function _processDifference(client, diff) {
|
|
132
|
+
// Build entities map
|
|
133
|
+
const entities = new Map();
|
|
134
|
+
for (const user of diff.users) {
|
|
135
|
+
try {
|
|
136
|
+
entities.set(index_1.utils.getPeerId(user), user);
|
|
137
|
+
}
|
|
138
|
+
catch (e) {
|
|
139
|
+
// Skip invalid
|
|
76
140
|
}
|
|
77
141
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
if (update instanceof tl_1.Api.Updates ||
|
|
82
|
-
update instanceof tl_1.Api.UpdatesCombined) {
|
|
83
|
-
// TODO deal with entities
|
|
84
|
-
const entities = new Map();
|
|
85
|
-
for (const x of [...update.users, ...update.chats]) {
|
|
86
|
-
entities.set(index_1.utils.getPeerId(x), x);
|
|
142
|
+
for (const chat of diff.chats) {
|
|
143
|
+
try {
|
|
144
|
+
entities.set(index_1.utils.getPeerId(chat), chat);
|
|
87
145
|
}
|
|
88
|
-
|
|
89
|
-
|
|
146
|
+
catch (e) {
|
|
147
|
+
// Skip invalid
|
|
90
148
|
}
|
|
91
149
|
}
|
|
92
|
-
|
|
93
|
-
|
|
150
|
+
// Process entities
|
|
151
|
+
client._entityCache.add(diff);
|
|
152
|
+
client.session.processEntities(diff);
|
|
153
|
+
// Process new messages as updates
|
|
154
|
+
for (const message of diff.newMessages) {
|
|
155
|
+
if (message instanceof tl_1.Api.Message || message instanceof tl_1.Api.MessageService) {
|
|
156
|
+
const update = new tl_1.Api.UpdateNewMessage({
|
|
157
|
+
message: message,
|
|
158
|
+
pts: 0,
|
|
159
|
+
ptsCount: 0,
|
|
160
|
+
});
|
|
161
|
+
_processUpdate(client, update, null, entities);
|
|
162
|
+
}
|
|
94
163
|
}
|
|
95
|
-
|
|
96
|
-
|
|
164
|
+
// Process other updates
|
|
165
|
+
for (const update of diff.otherUpdates) {
|
|
166
|
+
_processUpdate(client, update, diff.otherUpdates, entities);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/** @hidden */
|
|
170
|
+
function _handleUpdate(client, update) {
|
|
171
|
+
try {
|
|
172
|
+
if (typeof update === "number") {
|
|
173
|
+
if ([-1, 0, 1].includes(update)) {
|
|
174
|
+
_dispatchUpdate(client, {
|
|
175
|
+
update: new network_1.UpdateConnectionState(update),
|
|
176
|
+
}).catch((e) => {
|
|
177
|
+
client._log.error(`Error dispatching connection state: ${e}`);
|
|
178
|
+
});
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
client._entityCache.add(update);
|
|
183
|
+
client.session.processEntities(update);
|
|
184
|
+
if (update instanceof tl_1.Api.Updates ||
|
|
185
|
+
update instanceof tl_1.Api.UpdatesCombined) {
|
|
186
|
+
const entities = new Map();
|
|
187
|
+
for (const x of [...update.users, ...update.chats]) {
|
|
188
|
+
try {
|
|
189
|
+
entities.set(index_1.utils.getPeerId(x), x);
|
|
190
|
+
}
|
|
191
|
+
catch (e) {
|
|
192
|
+
// Skip invalid entity
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
for (const u of update.updates) {
|
|
196
|
+
_processUpdate(client, u, update.updates, entities);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
else if (update instanceof tl_1.Api.UpdateShort) {
|
|
200
|
+
_processUpdate(client, update.update, null);
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
_processUpdate(client, update, null);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
catch (e) {
|
|
207
|
+
client._log.error(`Error handling update: ${e}`);
|
|
97
208
|
}
|
|
98
209
|
}
|
|
99
210
|
/** @hidden */
|
|
@@ -103,7 +214,9 @@ function _processUpdate(client, update, others, entities) {
|
|
|
103
214
|
update: update,
|
|
104
215
|
others: others,
|
|
105
216
|
};
|
|
106
|
-
_dispatchUpdate(client, args)
|
|
217
|
+
_dispatchUpdate(client, args).catch((e) => {
|
|
218
|
+
client._log.error(`Error dispatching update: ${e}`);
|
|
219
|
+
});
|
|
107
220
|
}
|
|
108
221
|
/** @hidden */
|
|
109
222
|
async function _dispatchUpdate(client, args) {
|
|
@@ -111,8 +224,14 @@ async function _dispatchUpdate(client, args) {
|
|
|
111
224
|
if (!builder || !callback) {
|
|
112
225
|
continue;
|
|
113
226
|
}
|
|
114
|
-
|
|
115
|
-
|
|
227
|
+
try {
|
|
228
|
+
if (!builder.resolved) {
|
|
229
|
+
await builder.resolve(client);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
catch (e) {
|
|
233
|
+
client._log.error(`Error resolving event builder: ${e}`);
|
|
234
|
+
continue;
|
|
116
235
|
}
|
|
117
236
|
let event = args.update;
|
|
118
237
|
if (event) {
|
|
@@ -128,9 +247,15 @@ async function _dispatchUpdate(client, args) {
|
|
|
128
247
|
// TODO fix me
|
|
129
248
|
}
|
|
130
249
|
// TODO fix others not being passed
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
250
|
+
try {
|
|
251
|
+
event = builder.build(event, callback, client._selfInputPeer
|
|
252
|
+
? (0, Helpers_1.returnBigInt)(client._selfInputPeer.userId)
|
|
253
|
+
: undefined);
|
|
254
|
+
}
|
|
255
|
+
catch (e) {
|
|
256
|
+
client._log.error(`Error building event: ${e}`);
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
134
259
|
if (event) {
|
|
135
260
|
event._client = client;
|
|
136
261
|
if ("_eventName" in event) {
|
|
@@ -138,7 +263,14 @@ async function _dispatchUpdate(client, args) {
|
|
|
138
263
|
event.originalUpdate = args.update;
|
|
139
264
|
event._entities = args.update._entities;
|
|
140
265
|
}
|
|
141
|
-
|
|
266
|
+
let filter;
|
|
267
|
+
try {
|
|
268
|
+
filter = await builder.filter(event);
|
|
269
|
+
}
|
|
270
|
+
catch (e) {
|
|
271
|
+
client._log.error(`Error in event filter: ${e}`);
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
142
274
|
if (!filter) {
|
|
143
275
|
continue;
|
|
144
276
|
}
|
|
@@ -152,12 +284,7 @@ async function _dispatchUpdate(client, args) {
|
|
|
152
284
|
if (client._errorHandler) {
|
|
153
285
|
await client._errorHandler(e);
|
|
154
286
|
}
|
|
155
|
-
|
|
156
|
-
console.error(e);
|
|
157
|
-
}
|
|
158
|
-
else if (client._log) {
|
|
159
|
-
console.error(e);
|
|
160
|
-
}
|
|
287
|
+
client._log.error(`Error in event handler: ${e}`);
|
|
161
288
|
}
|
|
162
289
|
}
|
|
163
290
|
}
|
|
@@ -165,10 +292,26 @@ async function _dispatchUpdate(client, args) {
|
|
|
165
292
|
}
|
|
166
293
|
/** @hidden */
|
|
167
294
|
async function _updateLoop(client) {
|
|
295
|
+
// Initialize update state on first run
|
|
296
|
+
if (!client._updateState) {
|
|
297
|
+
try {
|
|
298
|
+
const state = await client.invoke(new tl_1.Api.updates.GetState());
|
|
299
|
+
client._updateState = {
|
|
300
|
+
pts: state.pts,
|
|
301
|
+
qts: state.qts,
|
|
302
|
+
date: state.date,
|
|
303
|
+
seq: state.seq,
|
|
304
|
+
};
|
|
305
|
+
client._log.debug("Initialized update state");
|
|
306
|
+
}
|
|
307
|
+
catch (e) {
|
|
308
|
+
client._log.error(`Failed to get initial update state: ${e}`);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
168
311
|
let lastPongAt;
|
|
169
312
|
while (!client._destroyed) {
|
|
170
313
|
await (0, Helpers_1.sleep)(PING_INTERVAL, true);
|
|
171
|
-
if (client._destroyed)
|
|
314
|
+
if (client._destroyed || client.disconnected)
|
|
172
315
|
break;
|
|
173
316
|
if (client._sender.isReconnecting || client._isSwitchingDc) {
|
|
174
317
|
lastPongAt = undefined;
|
|
@@ -201,17 +344,14 @@ async function _updateLoop(client) {
|
|
|
201
344
|
lastPongAt = Date.now();
|
|
202
345
|
}
|
|
203
346
|
catch (err) {
|
|
204
|
-
// eslint-disable-next-line no-console
|
|
205
347
|
if (client._errorHandler) {
|
|
206
348
|
await client._errorHandler(err);
|
|
207
349
|
}
|
|
208
|
-
|
|
209
|
-
console.error(err);
|
|
210
|
-
}
|
|
211
|
-
else if (client._log) {
|
|
212
|
-
console.error(err);
|
|
213
|
-
}
|
|
350
|
+
client._log.error(`Ping failed: ${err}`);
|
|
214
351
|
lastPongAt = undefined;
|
|
352
|
+
if (client.disconnected || client._destroyed) {
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
215
355
|
if (client._sender.isReconnecting || client._isSwitchingDc) {
|
|
216
356
|
continue;
|
|
217
357
|
}
|
|
@@ -222,7 +362,14 @@ async function _updateLoop(client) {
|
|
|
222
362
|
// just stop even if we're connected. Do so every 30 minutes.
|
|
223
363
|
if (Date.now() - (client._lastRequest || 0) > 30 * 60 * 1000) {
|
|
224
364
|
try {
|
|
225
|
-
await client.invoke(new tl_1.Api.updates.GetState());
|
|
365
|
+
const state = await client.invoke(new tl_1.Api.updates.GetState());
|
|
366
|
+
// Update our state to stay in sync
|
|
367
|
+
if (client._updateState) {
|
|
368
|
+
client._updateState.pts = state.pts;
|
|
369
|
+
client._updateState.qts = state.qts;
|
|
370
|
+
client._updateState.date = state.date;
|
|
371
|
+
client._updateState.seq = state.seq;
|
|
372
|
+
}
|
|
226
373
|
}
|
|
227
374
|
catch (e) {
|
|
228
375
|
// we don't care about errors here
|
package/client/uploads.d.ts
CHANGED
|
@@ -130,7 +130,7 @@ export declare function _fileToMedia(client: TelegramClient, { file, forceDocume
|
|
|
130
130
|
image?: boolean;
|
|
131
131
|
}>;
|
|
132
132
|
/** @hidden */
|
|
133
|
-
export declare function _sendAlbum(client: TelegramClient, entity: EntityLike, { file, caption, forceDocument, fileSize, clearDraft, progressCallback, replyTo, attributes, thumb, parseMode, voiceNote, videoNote, silent, supportsStreaming, scheduleDate, workers, noforwards, commentTo, topMsgId, }: SendFileInterface): Promise<Api.Message>;
|
|
133
|
+
export declare function _sendAlbum(client: TelegramClient, entity: EntityLike, { file, caption, formattingEntities, forceDocument, fileSize, clearDraft, progressCallback, replyTo, attributes, thumb, parseMode, voiceNote, videoNote, silent, supportsStreaming, scheduleDate, workers, noforwards, commentTo, topMsgId, }: SendFileInterface): Promise<Api.Message>;
|
|
134
134
|
/** @hidden */
|
|
135
135
|
export declare function sendFile(client: TelegramClient, entity: EntityLike, { file, caption, forceDocument, fileSize, clearDraft, progressCallback, replyTo, attributes, thumb, parseMode, formattingEntities, voiceNote, videoNote, buttons, silent, supportsStreaming, scheduleDate, workers, noforwards, commentTo, topMsgId, }: SendFileInterface): Promise<Api.Message>;
|
|
136
136
|
export {};
|
package/client/uploads.js
CHANGED
|
@@ -318,7 +318,7 @@ async function _fileToMedia(client, { file, forceDocument, fileSize, progressCal
|
|
|
318
318
|
};
|
|
319
319
|
}
|
|
320
320
|
/** @hidden */
|
|
321
|
-
async function _sendAlbum(client, entity, { file, caption, forceDocument = false, fileSize, clearDraft = false, progressCallback, replyTo, attributes, thumb, parseMode, voiceNote = false, videoNote = false, silent, supportsStreaming = false, scheduleDate, workers = 1, noforwards, commentTo, topMsgId, }) {
|
|
321
|
+
async function _sendAlbum(client, entity, { file, caption, formattingEntities, forceDocument = false, fileSize, clearDraft = false, progressCallback, replyTo, attributes, thumb, parseMode, voiceNote = false, videoNote = false, silent, supportsStreaming = false, scheduleDate, workers = 1, noforwards, commentTo, topMsgId, }) {
|
|
322
322
|
entity = await client.getInputEntity(entity);
|
|
323
323
|
let files = [];
|
|
324
324
|
if (!Array.isArray(file)) {
|
|
@@ -334,8 +334,13 @@ async function _sendAlbum(client, entity, { file, caption, forceDocument = false
|
|
|
334
334
|
caption = [caption];
|
|
335
335
|
}
|
|
336
336
|
const captions = [];
|
|
337
|
-
for (const c of caption) {
|
|
338
|
-
|
|
337
|
+
for (const [i, c] of caption.entries()) {
|
|
338
|
+
if (i === 0 && formattingEntities) {
|
|
339
|
+
captions.push([c || "", formattingEntities]);
|
|
340
|
+
}
|
|
341
|
+
else {
|
|
342
|
+
captions.push(await (0, messageParse_1._parseMessageText)(client, c, parseMode));
|
|
343
|
+
}
|
|
339
344
|
}
|
|
340
345
|
if (commentTo != undefined) {
|
|
341
346
|
const discussionData = await (0, messages_1.getCommentData)(client, entity, commentTo);
|
|
@@ -440,6 +445,7 @@ async function sendFile(client, entity, { file, caption, forceDocument = false,
|
|
|
440
445
|
return await _sendAlbum(client, entity, {
|
|
441
446
|
file: file,
|
|
442
447
|
caption: caption,
|
|
448
|
+
formattingEntities: formattingEntities,
|
|
443
449
|
replyTo: replyTo,
|
|
444
450
|
parseMode: parseMode,
|
|
445
451
|
attributes: attributes,
|
package/index.d.ts
CHANGED
|
@@ -11,4 +11,5 @@ import * as extensions from "./extensions";
|
|
|
11
11
|
import * as helpers from "./Helpers";
|
|
12
12
|
import * as client from "./client";
|
|
13
13
|
import * as password from "./Password";
|
|
14
|
-
|
|
14
|
+
import * as events from "./events";
|
|
15
|
+
export { utils, errors, sessions, extensions, helpers, tl, password, client, events };
|
package/index.js
CHANGED
|
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.client = exports.password = exports.tl = exports.helpers = exports.extensions = exports.sessions = exports.errors = exports.utils = exports.Logger = exports.version = exports.Connection = exports.TelegramClient = exports.Api = void 0;
|
|
36
|
+
exports.events = exports.client = exports.password = exports.tl = exports.helpers = exports.extensions = exports.sessions = exports.errors = exports.utils = exports.Logger = exports.version = exports.Connection = exports.TelegramClient = exports.Api = void 0;
|
|
37
37
|
var tl_1 = require("./tl");
|
|
38
38
|
Object.defineProperty(exports, "Api", { enumerable: true, get: function () { return tl_1.Api; } });
|
|
39
39
|
const tl = __importStar(require("./tl"));
|
|
@@ -60,3 +60,5 @@ const client = __importStar(require("./client"));
|
|
|
60
60
|
exports.client = client;
|
|
61
61
|
const password = __importStar(require("./Password"));
|
|
62
62
|
exports.password = password;
|
|
63
|
+
const events = __importStar(require("./events"));
|
|
64
|
+
exports.events = events;
|
package/network/MTProtoSender.js
CHANGED
|
@@ -254,6 +254,7 @@ class MTProtoSender {
|
|
|
254
254
|
this._log.debug("Already have an auth key ...");
|
|
255
255
|
}
|
|
256
256
|
this._userConnected = true;
|
|
257
|
+
this._disconnected = false;
|
|
257
258
|
this.isReconnecting = false;
|
|
258
259
|
if (!this._sendLoopHandle) {
|
|
259
260
|
this._log.debug("Starting send loop");
|
|
@@ -279,6 +280,7 @@ class MTProtoSender {
|
|
|
279
280
|
}
|
|
280
281
|
this._log.info("Disconnecting from %s...".replace("%s", connection.toString()));
|
|
281
282
|
this._userConnected = false;
|
|
283
|
+
this._disconnected = true;
|
|
282
284
|
this._log.debug("Closing current connection...");
|
|
283
285
|
// Signal abort to any ongoing operations
|
|
284
286
|
this._abortController.abort();
|