@mtkruto/node 0.1.114 → 0.1.116

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.
@@ -5,7 +5,7 @@ export declare const PUBLIC_KEYS: PublicKeys;
5
5
  export declare const VECTOR_CONSTRUCTOR = 481674261;
6
6
  export declare const INITIAL_DC: DC;
7
7
  export declare const LAYER = 166;
8
- export declare const APP_VERSION = "MTKruto 0.1.114";
8
+ export declare const APP_VERSION = "MTKruto 0.1.116";
9
9
  export declare const DEVICE_MODEL: string;
10
10
  export declare const LANG_CODE: string;
11
11
  export declare const LANG_PACK = "";
@@ -54,7 +54,7 @@ export const PUBLIC_KEYS = Object.freeze([
54
54
  export const VECTOR_CONSTRUCTOR = 0x1CB5C415;
55
55
  export const INITIAL_DC = "2";
56
56
  export const LAYER = 166;
57
- export const APP_VERSION = "MTKruto 0.1.114";
57
+ export const APP_VERSION = "MTKruto 0.1.116";
58
58
  // @ts-ignore: lib
59
59
  export const DEVICE_MODEL = typeof dntShim.Deno === "undefined" ? typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : process.platform + "-" + process.arch : navigator.userAgent.split(" ")[0] : dntShim.Deno.build.os + "-" + dntShim.Deno.build.arch;
60
60
  export const LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];
@@ -9,3 +9,4 @@ export declare function isChannelPtsUpdate(v: types.TypeUpdate | types.TypeUpdat
9
9
  export type FileSource = string | URL | Uint8Array;
10
10
  export declare function getFileContents(source: FileSource, fileName?: string): Promise<readonly [Uint8Array, string]>;
11
11
  export declare function isHttpUrl(string: string): boolean;
12
+ export declare function getUsername(string: string): string;
@@ -2,6 +2,7 @@ import * as dntShim from "../_dnt.shims.js";
2
2
  import { path } from "../0_deps.js";
3
3
  import { UNREACHABLE } from "../1_utilities.js";
4
4
  import { types } from "../2_tl.js";
5
+ import { Username } from "../tl/2_types.js";
5
6
  export const resolve = () => Promise.resolve();
6
7
  export function isPtsUpdate(v) {
7
8
  return v instanceof types.UpdateShortMessage ||
@@ -74,3 +75,66 @@ export function isHttpUrl(string) {
74
75
  return false;
75
76
  }
76
77
  }
78
+ function isAlpha(string) {
79
+ const c = string.charCodeAt(0) | 0x20;
80
+ return "a".charCodeAt(0) <= c && c <= "z".charCodeAt(0);
81
+ }
82
+ function isDigit(string) {
83
+ const c = string.charCodeAt(0);
84
+ return "0".charCodeAt(0) <= c && c <= "9".charCodeAt(0);
85
+ }
86
+ const errInvalidUsername = (u) => new Error("Invalid username: " + u);
87
+ function validateUsername(string, ignoreAt = false) {
88
+ string = string.trim();
89
+ if (ignoreAt && string.startsWith("@")) {
90
+ string = string.slice(1);
91
+ }
92
+ if (string.length == 0 || string.length > 32) {
93
+ throw errInvalidUsername(string);
94
+ }
95
+ if (!isAlpha(string[0])) {
96
+ throw errInvalidUsername(string);
97
+ }
98
+ for (const c of string) {
99
+ if (!isAlpha(c) && !isDigit(c) && c != "_") {
100
+ throw errInvalidUsername(string);
101
+ }
102
+ }
103
+ if (string[Username.length - 1] == "_") {
104
+ throw errInvalidUsername(string);
105
+ }
106
+ for (let i = 1; i < string.length; ++i) {
107
+ if (string[i - 1] == "_" && string[i] == "_") {
108
+ throw errInvalidUsername(string);
109
+ }
110
+ }
111
+ return string;
112
+ }
113
+ export function getUsername(string) {
114
+ let url = null;
115
+ try {
116
+ url = new URL(string);
117
+ }
118
+ catch {
119
+ try {
120
+ url = new URL("https://" + string);
121
+ }
122
+ catch {
123
+ //
124
+ }
125
+ }
126
+ if (url === null || (url.protocol != "http:" && url.protocol != "https:")) {
127
+ return validateUsername(string, true);
128
+ }
129
+ if (url.hostname != "telegram.dog" && url.hostname != "telegram.me" && url.hostname != "t.me" && !url.hostname.endsWith(".t.me")) {
130
+ return validateUsername(string, true);
131
+ }
132
+ if (url.hostname == "telegram.dog" || url.hostname == "telegram.me" || url.hostname == "t.me") {
133
+ return validateUsername(url.pathname.split("/")[1]);
134
+ }
135
+ const parts = url.hostname.split(".");
136
+ if (parts.length != 3) {
137
+ return validateUsername(string);
138
+ }
139
+ return validateUsername(parts[0]);
140
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -41,6 +41,10 @@ export interface ClientParams extends ClientPlainParams {
41
41
  * Whether to use default handlers. Defaults to `true`.
42
42
  */
43
43
  defaultHandlers?: boolean;
44
+ /**
45
+ * Whether to ignore outgoing messages. Defaults to `true` for bots, and `false` for users.
46
+ */
47
+ ignoreOutgoing?: boolean;
44
48
  }
45
49
  export interface AnswerCallbackQueryParams {
46
50
  /** A text to be shown to the user. */
@@ -1,4 +1,8 @@
1
- import { FilterableUpdates, FilterUpdate, Update } from "./3_types.js";
1
+ import { User } from "../3_types.js";
2
+ import { FilterableUpdates, FilterUpdate, Update as Update_ } from "./3_types.js";
3
+ interface Update extends Update_ {
4
+ me: undefined extends this["connectionState"] ? undefined extends this["authorizationState"] ? User : (User | undefined) : (User | undefined);
5
+ }
2
6
  type MaybePromise<T> = T | Promise<T>;
3
7
  export type NextFunction = () => Promise<void>;
4
8
  export type MiddlewareFn<C extends Update = Update> = (ctx: C, next: NextFunction) => MaybePromise<unknown>;
@@ -18,5 +22,6 @@ export declare class Composer<C extends Update> implements MiddlewareObj<C> {
18
22
  filter<D extends C>(predicate: (ctx: C) => ctx is D, ...middleware: Middleware<D>[]): Composer<D>;
19
23
  filter(predicate: (ctx: C) => MaybePromise<boolean>, ...middleware: Middleware<C>[]): Composer<C>;
20
24
  on<T extends keyof Update, F extends keyof NonNullable<Update[T]>>(filter: T extends FilterableUpdates ? T | [T, F, ...F[]] : T, ...middleawre: Middleware<FilterUpdate<C, T, F>>[]): Composer<FilterUpdate<C, T, F>>;
25
+ command(commands: string | RegExp | (string | RegExp)[], ...middleawre: Middleware<FilterUpdate<C, "message", "text">>[]): Composer<FilterUpdate<C, "message", "text">>;
21
26
  }
22
27
  export {};
@@ -54,7 +54,9 @@ export class Composer {
54
54
  });
55
55
  }
56
56
  filter(predicate, ...middleware) {
57
- return this.branch(predicate, middleware.length == 0 ? skip : middleware.map(flatten).reduce(concat), skip);
57
+ const composer = new Composer(...middleware);
58
+ this.branch(predicate, composer, skip);
59
+ return composer;
58
60
  }
59
61
  on(filter, ...middleawre) {
60
62
  const type = typeof filter === "string" ? filter : filter[0];
@@ -77,5 +79,31 @@ export class Composer {
77
79
  }
78
80
  }, ...middleawre);
79
81
  }
82
+ command(commands, ...middleawre) {
83
+ const commands_ = Array.isArray(commands) ? commands : [commands];
84
+ return this.on(["message", "text"]).filter((ctx) => {
85
+ const botCommand = ctx.message.entities?.find((v) => v.type == "botCommand");
86
+ if (!botCommand) {
87
+ return false;
88
+ }
89
+ const cmd = ctx.message.text.slice(botCommand.offset, botCommand.offset + botCommand.length);
90
+ if (cmd.includes("@")) {
91
+ const username = cmd.split("@")[1];
92
+ if (username.toLowerCase() !== ctx.me.username?.toLowerCase()) {
93
+ return false;
94
+ }
95
+ }
96
+ const command_ = cmd.split("@")[0].split("/")[1].toLowerCase();
97
+ for (const command of commands_) {
98
+ if (typeof command === "string" && (command.toLowerCase() == command_)) {
99
+ return true;
100
+ }
101
+ else if (command instanceof RegExp && command.test(command_)) {
102
+ return true;
103
+ }
104
+ }
105
+ return false;
106
+ }, ...middleawre);
107
+ }
80
108
  }
81
109
  _Composer_handle = new WeakMap();
@@ -15,6 +15,7 @@ declare const getMessageWithReply: unique symbol;
15
15
  export interface Context extends Update {
16
16
  /** The client that received the update. */
17
17
  client: Client;
18
+ me: undefined extends this["connectionState"] ? undefined extends this["authorizationState"] ? User : (User | undefined) : (User | undefined);
18
19
  /** Resolves to `ctx.message ?? ctx.editedMessage ?? ctx.callbackQuery?.message`. */
19
20
  msg: undefined extends this["message"] ? undefined extends this["editedMessage"] ? undefined extends this["callbackQuery"] ? never : this["callbackQuery"] extends With<CallbackQuery, "message"> ? this["callbackQuery"]["message"] : this["callbackQuery"] extends With<CallbackQuery, "inlineMessageId"> ? never : (Message | undefined) : this["editedMessage"] : this["message"];
20
21
  /** Resolves to `effectiveMessage?.chat`. */
@@ -135,7 +136,7 @@ export declare class Client<C extends Context = Context> extends ClientAbstract
135
136
  * Alias for `invoke` with its second parameter being `true`.
136
137
  */
137
138
  send<T extends (functions.Function<unknown> | types.Type) = functions.Function<unknown>>(function_: T): Promise<void>;
138
- checkGap(pts: number, ptsCount: number, assertNoGap: boolean): Promise<void>;
139
+ checkGap(pts: number, ptsCount: number): Promise<void>;
139
140
  getUserAccessHash(userId: bigint): Promise<bigint>;
140
141
  getInputPeer(id: ChatID): Promise<types.InputPeerChat | types.InputPeerUser | types.InputPeerChannel>;
141
142
  private [getEntity];
@@ -282,9 +283,10 @@ export declare class Client<C extends Context = Context> extends ClientAbstract
282
283
  answerInlineQuery(id: string, results: InlineQueryResult[], params?: AnswerInlineQueryParams): Promise<void>;
283
284
  use(...middleware: Middleware<C>[]): Composer<C>;
284
285
  branch(predicate: (ctx: C) => MaybePromise<boolean>, trueHandler_: Middleware<C>, falseHandler_: Middleware<C>): Composer<C>;
285
- filter<D extends C>(predicate: (ctx: C) => ctx is D, ...middleware: Middleware<D>[]): void;
286
- filter(predicate: (ctx: C) => MaybePromise<boolean>, ...middleware: Middleware<C>[]): void;
287
- on<T extends keyof Update, F extends keyof NonNullable<Update[T]>>(filter: T extends FilterableUpdates ? T | [T, F, ...F[]] : T, ...middleawre: Middleware<FilterUpdate<C, T, F>>[]): void;
286
+ filter<D extends C>(predicate: (ctx: C) => ctx is D, ...middleware: Middleware<D>[]): Composer<D>;
287
+ filter(predicate: (ctx: C) => MaybePromise<boolean>, ...middleware: Middleware<C>[]): Composer<C>;
288
+ on<T extends keyof Update, F extends keyof NonNullable<Update[T]>>(filter: T extends FilterableUpdates ? T | [T, F, ...F[]] : T, ...middleawre: Middleware<FilterUpdate<C, T, F>>[]): Composer<FilterUpdate<C, T, F>>;
289
+ command(commands: string | RegExp | (string | RegExp)[], ...middleawre: Middleware<FilterUpdate<C, "message", "text">>[]): Composer<FilterUpdate<C, "message", "text">>;
288
290
  /**
289
291
  * Set the bot's description in the given language. Bot-only.
290
292
  *