@mtcute/dispatcher 0.1.0
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 +8 -0
- package/README.md +26 -0
- package/cjs/callback-data-builder.d.ts +43 -0
- package/cjs/callback-data-builder.js +99 -0
- package/cjs/callback-data-builder.js.map +1 -0
- package/cjs/context/base.d.ts +8 -0
- package/cjs/context/base.js +3 -0
- package/cjs/context/base.js.map +1 -0
- package/cjs/context/callback-query.d.ts +27 -0
- package/cjs/context/callback-query.js +56 -0
- package/cjs/context/callback-query.js.map +1 -0
- package/cjs/context/chat-join-request.d.ts +16 -0
- package/cjs/context/chat-join-request.js +34 -0
- package/cjs/context/chat-join-request.js.map +1 -0
- package/cjs/context/chosen-inline-result.d.ts +21 -0
- package/cjs/context/chosen-inline-result.js +35 -0
- package/cjs/context/chosen-inline-result.js.map +1 -0
- package/cjs/context/index.d.ts +8 -0
- package/cjs/context/index.js +24 -0
- package/cjs/context/index.js.map +1 -0
- package/cjs/context/inline-query.d.ts +14 -0
- package/cjs/context/inline-query.js +22 -0
- package/cjs/context/inline-query.js.map +1 -0
- package/cjs/context/message.d.ts +72 -0
- package/cjs/context/message.js +142 -0
- package/cjs/context/message.js.map +1 -0
- package/cjs/context/parse.d.ts +1 -0
- package/cjs/context/parse.js +34 -0
- package/cjs/context/parse.js.map +1 -0
- package/cjs/context/pre-checkout-query.d.ts +16 -0
- package/cjs/context/pre-checkout-query.js +26 -0
- package/cjs/context/pre-checkout-query.js.map +1 -0
- package/cjs/dispatcher.d.ts +641 -0
- package/cjs/dispatcher.js +765 -0
- package/cjs/dispatcher.js.map +1 -0
- package/cjs/filters/bots.d.ts +70 -0
- package/cjs/filters/bots.js +129 -0
- package/cjs/filters/bots.js.map +1 -0
- package/cjs/filters/bundle.d.ts +10 -0
- package/cjs/filters/bundle.js +27 -0
- package/cjs/filters/bundle.js.map +1 -0
- package/cjs/filters/chat.d.ts +27 -0
- package/cjs/filters/chat.js +55 -0
- package/cjs/filters/chat.js.map +1 -0
- package/cjs/filters/group.d.ts +25 -0
- package/cjs/filters/group.js +72 -0
- package/cjs/filters/group.js.map +1 -0
- package/cjs/filters/index.d.ts +3 -0
- package/cjs/filters/index.js +29 -0
- package/cjs/filters/index.js.map +1 -0
- package/cjs/filters/logic.d.ts +29 -0
- package/cjs/filters/logic.js +114 -0
- package/cjs/filters/logic.js.map +1 -0
- package/cjs/filters/message.d.ts +295 -0
- package/cjs/filters/message.js +150 -0
- package/cjs/filters/message.js.map +1 -0
- package/cjs/filters/state.d.ts +15 -0
- package/cjs/filters/state.js +32 -0
- package/cjs/filters/state.js.map +1 -0
- package/cjs/filters/text.d.ts +64 -0
- package/cjs/filters/text.js +132 -0
- package/cjs/filters/text.js.map +1 -0
- package/cjs/filters/types.d.ts +91 -0
- package/cjs/filters/types.js +6 -0
- package/cjs/filters/types.js.map +1 -0
- package/cjs/filters/updates.d.ts +46 -0
- package/cjs/filters/updates.js +46 -0
- package/cjs/filters/updates.js.map +1 -0
- package/cjs/filters/user.d.ts +24 -0
- package/cjs/filters/user.js +76 -0
- package/cjs/filters/user.js.map +1 -0
- package/cjs/handler.d.ts +31 -0
- package/cjs/handler.js +4 -0
- package/cjs/handler.js.map +1 -0
- package/cjs/index.d.ts +8 -0
- package/cjs/index.js +25 -0
- package/cjs/index.js.map +1 -0
- package/cjs/package.json +3 -0
- package/cjs/propagation.d.ts +21 -0
- package/cjs/propagation.js +26 -0
- package/cjs/propagation.js.map +1 -0
- package/cjs/state/index.d.ts +3 -0
- package/cjs/state/index.js +20 -0
- package/cjs/state/index.js.map +1 -0
- package/cjs/state/key.d.ts +23 -0
- package/cjs/state/key.js +45 -0
- package/cjs/state/key.js.map +1 -0
- package/cjs/state/storage.d.ts +75 -0
- package/cjs/state/storage.js +17 -0
- package/cjs/state/storage.js.map +1 -0
- package/cjs/state/update-state.d.ts +151 -0
- package/cjs/state/update-state.js +211 -0
- package/cjs/state/update-state.js.map +1 -0
- package/cjs/wizard.d.ts +60 -0
- package/cjs/wizard.js +103 -0
- package/cjs/wizard.js.map +1 -0
- package/esm/callback-data-builder.d.ts +43 -0
- package/esm/callback-data-builder.js +95 -0
- package/esm/callback-data-builder.js.map +1 -0
- package/esm/context/base.d.ts +8 -0
- package/esm/context/base.js +2 -0
- package/esm/context/base.js.map +1 -0
- package/esm/context/callback-query.d.ts +27 -0
- package/esm/context/callback-query.js +52 -0
- package/esm/context/callback-query.js.map +1 -0
- package/esm/context/chat-join-request.d.ts +16 -0
- package/esm/context/chat-join-request.js +30 -0
- package/esm/context/chat-join-request.js.map +1 -0
- package/esm/context/chosen-inline-result.d.ts +21 -0
- package/esm/context/chosen-inline-result.js +31 -0
- package/esm/context/chosen-inline-result.js.map +1 -0
- package/esm/context/index.d.ts +8 -0
- package/esm/context/index.js +8 -0
- package/esm/context/index.js.map +1 -0
- package/esm/context/inline-query.d.ts +14 -0
- package/esm/context/inline-query.js +18 -0
- package/esm/context/inline-query.js.map +1 -0
- package/esm/context/message.d.ts +72 -0
- package/esm/context/message.js +138 -0
- package/esm/context/message.js.map +1 -0
- package/esm/context/parse.d.ts +1 -0
- package/esm/context/parse.js +30 -0
- package/esm/context/parse.js.map +1 -0
- package/esm/context/pre-checkout-query.d.ts +16 -0
- package/esm/context/pre-checkout-query.js +22 -0
- package/esm/context/pre-checkout-query.js.map +1 -0
- package/esm/dispatcher.d.ts +641 -0
- package/esm/dispatcher.js +761 -0
- package/esm/dispatcher.js.map +1 -0
- package/esm/filters/bots.d.ts +70 -0
- package/esm/filters/bots.js +125 -0
- package/esm/filters/bots.js.map +1 -0
- package/esm/filters/bundle.d.ts +10 -0
- package/esm/filters/bundle.js +11 -0
- package/esm/filters/bundle.js.map +1 -0
- package/esm/filters/chat.d.ts +27 -0
- package/esm/filters/chat.js +50 -0
- package/esm/filters/chat.js.map +1 -0
- package/esm/filters/group.d.ts +25 -0
- package/esm/filters/group.js +67 -0
- package/esm/filters/group.js.map +1 -0
- package/esm/filters/index.d.ts +3 -0
- package/esm/filters/index.js +3 -0
- package/esm/filters/index.js.map +1 -0
- package/esm/filters/logic.d.ts +29 -0
- package/esm/filters/logic.js +107 -0
- package/esm/filters/logic.js.map +1 -0
- package/esm/filters/message.d.ts +295 -0
- package/esm/filters/message.js +130 -0
- package/esm/filters/message.js.map +1 -0
- package/esm/filters/state.d.ts +15 -0
- package/esm/filters/state.js +27 -0
- package/esm/filters/state.js.map +1 -0
- package/esm/filters/text.d.ts +64 -0
- package/esm/filters/text.js +124 -0
- package/esm/filters/text.js.map +1 -0
- package/esm/filters/types.d.ts +91 -0
- package/esm/filters/types.js +5 -0
- package/esm/filters/types.js.map +1 -0
- package/esm/filters/updates.d.ts +46 -0
- package/esm/filters/updates.js +39 -0
- package/esm/filters/updates.js.map +1 -0
- package/esm/filters/user.d.ts +24 -0
- package/esm/filters/user.js +70 -0
- package/esm/filters/user.js.map +1 -0
- package/esm/handler.d.ts +31 -0
- package/esm/handler.js +3 -0
- package/esm/handler.js.map +1 -0
- package/esm/index.d.ts +8 -0
- package/esm/index.js +9 -0
- package/esm/index.js.map +1 -0
- package/esm/propagation.d.ts +21 -0
- package/esm/propagation.js +23 -0
- package/esm/propagation.js.map +1 -0
- package/esm/state/index.d.ts +3 -0
- package/esm/state/index.js +4 -0
- package/esm/state/index.js.map +1 -0
- package/esm/state/key.d.ts +23 -0
- package/esm/state/key.js +41 -0
- package/esm/state/key.js.map +1 -0
- package/esm/state/storage.d.ts +75 -0
- package/esm/state/storage.js +13 -0
- package/esm/state/storage.js.map +1 -0
- package/esm/state/update-state.d.ts +151 -0
- package/esm/state/update-state.js +206 -0
- package/esm/state/update-state.js.map +1 -0
- package/esm/wizard.d.ts +60 -0
- package/esm/wizard.js +99 -0
- package/esm/wizard.js.map +1 -0
- package/package.json +21 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message.js","sourceRoot":"","sources":["../../../src/filters/message.ts"],"names":[],"mappings":";;;AAAA,uDA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1C,IAAI,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC,CAAA;QAExC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAE,GAAG,CAAC,MAAM,EAAE,IAAY,IAAI,KAAK,CAAA;KACrD;IAED,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,KAAK,IAAI,CAAA;AAC7C,CAAC,CAAA;AAnBY,QAAA,MAAM,UAmBlB;AAEM,MAAM,MAAM,GACf,CACI,IAAO,EACmE,EAAE,CAC5E,CAAC,GAAG,EAAE,EAAE,CACJ,GAAG,CAAC,MAAM,CAAC,IAAI,KAAK,IAAI,CAAA;AALvB,QAAA,MAAM,UAKiB;AAEpC;;;;GAIG;AACI,MAAM,OAAO,GAChB,CACI,MAA0C,EACuC,EAAE,CACnF,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE;IACjB,IAAI,CAAC,GAAG,CAAC,cAAc,EAAE,EAAE;QAAE,OAAO,KAAK,CAAA;IAEzC,MAAM,KAAK,GAAG,MAAM,GAAG,CAAC,UAAU,EAAE,CAAA;IACpC,IAAI,CAAC,KAAK;QAAE,OAAO,KAAK,CAAA;IAExB,GAAG,CAAC,UAAU,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAE7C,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAA;IAExB,OAAO,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;AAC/B,CAAC,CAAA;AAfI,QAAA,OAAO,WAeX","sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n// ^^ will be looked into in MTQ-29\nimport {\n Chat,\n MaybeArray,\n Message,\n MessageAction,\n MessageMediaType,\n MessageReplyInfo,\n RawDocument,\n RawLocation,\n Sticker,\n StickerSourceType,\n StickerType,\n User,\n Video,\n} from '@mtcute/client'\n\nimport { MessageContext } from '../index.js'\nimport { Modify, UpdateFilter } from './types.js'\n\n/**\n * Filter incoming messages.\n *\n * Messages sent to yourself (i.e. Saved Messages) are also \"incoming\"\n */\nexport const incoming: UpdateFilter<Message, { isOutgoing: false }> = (msg) => !msg.isOutgoing\n\n/**\n * Filter outgoing messages.\n *\n * Messages sent to yourself (i.e. Saved Messages) are **not** \"outgoing\"\n */\nexport const outgoing: UpdateFilter<Message, { isOutgoing: true }> = (msg) => msg.isOutgoing\n\n/**\n * Filter messages that are replies to some other message\n */\nexport const reply: UpdateFilter<Message, { replyToMessage: MessageReplyInfo }> = (msg) => msg.replyToMessage !== null\n\n/**\n * Filter messages containing some media\n */\nexport const media: UpdateFilter<Message, { media: Exclude<Message['media'], null> }> = (msg) => msg.media !== null\n\n/**\n * Filter messages containing media of given type\n */\nexport const mediaOf =\n <T extends MessageMediaType>(type: T): UpdateFilter<Message, { media: Extract<Message['media'], { type: T }> }> =>\n (msg) =>\n msg.media?.type === type\n\n/** Filter messages containing a photo */\nexport const photo = mediaOf('photo')\n/** Filter messages containing a dice */\nexport const dice = mediaOf('dice')\n/** Filter messages containing a contact */\nexport const contact = mediaOf('contact')\n/** Filter messages containing an audio file */\nexport const audio = mediaOf('audio')\n/** Filter messages containing a voice message (audio-only) */\nexport const voice = mediaOf('voice')\n/** Filter messages containing a sticker */\nexport const sticker = mediaOf('sticker')\n/** Filter messages containing a document (a file) */\nexport const document = mediaOf('document')\n/** Filter messages containing any video (videos, round messages and animations) */\nexport const anyVideo = mediaOf('video')\n/** Filter messages containing a static location */\nexport const location = mediaOf('location')\n/** Filter messages containing a live location */\nexport const liveLocation = mediaOf('live_location')\n/** Filter messages containing a game */\nexport const game = mediaOf('game')\n/** Filter messages containing a web page */\nexport const webpage = mediaOf('web_page')\n/** Filter messages containing a venue */\nexport const venue = mediaOf('venue')\n/** Filter messages containing a poll */\nexport const poll = mediaOf('poll')\n/** Filter messages containing an invoice */\nexport const invoice = mediaOf('invoice')\n\n/**\n * Filter messages containing any location (live or static).\n */\nexport const anyLocation: UpdateFilter<Message, { media: Location }> = (msg) => msg.media instanceof RawLocation\n\n/**\n * Filter messages containing a document\n *\n * This will also match media like audio, video, voice\n * that also use Documents\n */\nexport const anyDocument: UpdateFilter<Message, { media: RawDocument }> = (msg) => msg.media instanceof RawDocument\n\n/**\n * Filter messages containing a simple video.\n *\n * This does not include round messages and animations\n */\nexport const video: UpdateFilter<\n Message,\n {\n media: Modify<\n Video,\n {\n isRound: false\n isAnimation: false\n }\n >\n }\n> = (msg) => msg.media?.type === 'video' && !msg.media.isAnimation && !msg.media.isRound\n\n/**\n * Filter messages containing an animation.\n *\n * > **Note**: Legacy GIFs (i.e. documents with `image/gif` MIME)\n * > are also considered animations.\n */\nexport const animation: UpdateFilter<\n Message,\n {\n media: Modify<\n Video,\n {\n isRound: false\n isAnimation: true\n }\n >\n }\n> = (msg) => msg.media?.type === 'video' && msg.media.isAnimation && !msg.media.isRound\n\n/**\n * Filter messages containing a round message (aka video note).\n */\nexport const roundMessage: UpdateFilter<\n Message,\n {\n media: Modify<\n Video,\n {\n isRound: true\n isAnimation: false\n }\n >\n }\n> = (msg) => msg.media?.type === 'video' && !msg.media.isAnimation && msg.media.isRound\n\n/**\n * Filter messages containing a sticker by its type\n */\nexport const stickerByType =\n (type: StickerType): UpdateFilter<Message, { media: Sticker }> =>\n (msg) =>\n msg.media?.type === 'sticker' && msg.media.stickerType === type\n\n/**\n * Filter messages containing a sticker by its source file type\n */\nexport const stickerBySourceType =\n (type: StickerSourceType): UpdateFilter<Message, { media: Sticker }> =>\n (msg) =>\n msg.media?.type === 'sticker' && msg.media.sourceType === type\n\n/**\n * Filter text-only messages non-service messages\n */\nexport const text: UpdateFilter<\n Message,\n {\n media: null\n isService: false\n }\n> = (msg) => msg.media === null && !msg.isService\n\n/**\n * Filter service messages\n */\nexport const service: UpdateFilter<Message, { isService: true }> = (msg) => msg.isService\n\n/**\n * Filter service messages by action type\n */\nexport const action = <T extends Exclude<MessageAction, null>['type']>(\n type: MaybeArray<T>,\n): UpdateFilter<\n Message,\n {\n action: Extract<MessageAction, { type: T }>\n sender: T extends 'user_joined_link' | 'user_removed' | 'history_cleared' | 'contact_joined' | 'bot_allowed'\n ? User\n : User | Chat\n }\n> => {\n if (Array.isArray(type)) {\n const index: Partial<Record<T, true>> = {}\n type.forEach((it) => (index[it] = true))\n\n return (msg) => (msg.action?.type as any) in index\n }\n\n return (msg) => msg.action?.type === type\n}\n\nexport const sender =\n <T extends Message['sender']['type']>(\n type: T,\n ): UpdateFilter<Message, { sender: Extract<Message['sender'], { type: T }> }> =>\n (msg) =>\n msg.sender.type === type\n\n/**\n * Filter that matches messages that are replies to some other message.\n *\n * Optionally, you can pass a filter that will be applied to the replied message.\n */\nexport const replyTo =\n <Mod, State extends object>(\n filter?: UpdateFilter<Message, Mod, State>,\n ): UpdateFilter<MessageContext, { getReplyTo: () => Promise<Message & Mod> }, State> =>\n async (msg, state) => {\n if (!msg.replyToMessage?.id) return false\n\n const reply = await msg.getReplyTo()\n if (!reply) return false\n\n msg.getReplyTo = () => Promise.resolve(reply)\n\n if (!filter) return true\n\n return filter(reply, state)\n }\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { MaybeAsync } from '@mtcute/client';
|
|
2
|
+
import { UpdateFilter } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Create a filter for the cases when the state is empty
|
|
5
|
+
*/
|
|
6
|
+
export declare const stateEmpty: UpdateFilter<any>;
|
|
7
|
+
/**
|
|
8
|
+
* Create a filter based on state predicate
|
|
9
|
+
*
|
|
10
|
+
* If state exists and matches `predicate`, update passes
|
|
11
|
+
* this filter, otherwise it doesn't
|
|
12
|
+
*
|
|
13
|
+
* @param predicate State predicate
|
|
14
|
+
*/
|
|
15
|
+
export declare const state: <T extends object>(predicate: (state: T) => MaybeAsync<boolean>) => UpdateFilter<any, {}, T>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.state = exports.stateEmpty = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Create a filter for the cases when the state is empty
|
|
6
|
+
*/
|
|
7
|
+
const stateEmpty = async (upd, state) => {
|
|
8
|
+
if (!state)
|
|
9
|
+
return false;
|
|
10
|
+
return !(await state.get());
|
|
11
|
+
};
|
|
12
|
+
exports.stateEmpty = stateEmpty;
|
|
13
|
+
/**
|
|
14
|
+
* Create a filter based on state predicate
|
|
15
|
+
*
|
|
16
|
+
* If state exists and matches `predicate`, update passes
|
|
17
|
+
* this filter, otherwise it doesn't
|
|
18
|
+
*
|
|
19
|
+
* @param predicate State predicate
|
|
20
|
+
*/
|
|
21
|
+
const state = (predicate) => {
|
|
22
|
+
return async (upd, state) => {
|
|
23
|
+
if (!state)
|
|
24
|
+
return false;
|
|
25
|
+
const data = await state.get();
|
|
26
|
+
if (!data)
|
|
27
|
+
return false;
|
|
28
|
+
return predicate(data);
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
exports.state = state;
|
|
32
|
+
//# sourceMappingURL=state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.js","sourceRoot":"","sources":["../../../src/filters/state.ts"],"names":[],"mappings":";;;AAKA;;GAEG;AACI,MAAM,UAAU,GAAsB,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE;IAC9D,IAAI,CAAC,KAAK;QAAE,OAAO,KAAK,CAAA;IAExB,OAAO,CAAC,CAAC,MAAM,KAAK,CAAC,GAAG,EAAE,CAAC,CAAA;AAC/B,CAAC,CAAA;AAJY,QAAA,UAAU,cAItB;AAED;;;;;;;GAOG;AACI,MAAM,KAAK,GAAG,CACjB,SAA4C,EAEpB,EAAE;IAC1B,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE;QACxB,IAAI,CAAC,KAAK;YAAE,OAAO,KAAK,CAAA;QACxB,MAAM,IAAI,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE,CAAA;QAC9B,IAAI,CAAC,IAAI;YAAE,OAAO,KAAK,CAAA;QAEvB,OAAO,SAAS,CAAC,IAAI,CAAC,CAAA;IAC1B,CAAC,CAAA;AACL,CAAC,CAAA;AAXY,QAAA,KAAK,SAWjB","sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { MaybeAsync } from '@mtcute/client'\n\nimport { UpdateFilter } from './types.js'\n\n/**\n * Create a filter for the cases when the state is empty\n */\nexport const stateEmpty: UpdateFilter<any> = async (upd, state) => {\n if (!state) return false\n\n return !(await state.get())\n}\n\n/**\n * Create a filter based on state predicate\n *\n * If state exists and matches `predicate`, update passes\n * this filter, otherwise it doesn't\n *\n * @param predicate State predicate\n */\nexport const state = <T extends object>(\n predicate: (state: T) => MaybeAsync<boolean>,\n // eslint-disable-next-line @typescript-eslint/ban-types\n): UpdateFilter<any, {}, T> => {\n return async (upd, state) => {\n if (!state) return false\n const data = await state.get()\n if (!data) return false\n\n return predicate(data)\n }\n}\n"]}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { CallbackQuery, ChosenInlineResult, InlineQuery, Message } from '@mtcute/client';
|
|
2
|
+
import { UpdateContextDistributed } from '../context/base.js';
|
|
3
|
+
import { UpdateFilter } from './types.js';
|
|
4
|
+
type UpdatesWithText = UpdateContextDistributed<Message | InlineQuery | ChosenInlineResult | CallbackQuery>;
|
|
5
|
+
/**
|
|
6
|
+
* Filter objects that match a given regular expression
|
|
7
|
+
* - for `Message`, `Message.text` is used
|
|
8
|
+
* - for `InlineQuery`, `InlineQuery.query` is used
|
|
9
|
+
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult#id} is used
|
|
10
|
+
* - for `CallbackQuery`, `CallbackQuery.dataStr` is used
|
|
11
|
+
*
|
|
12
|
+
* When a regex matches, the match array is stored in a
|
|
13
|
+
* type-safe extension field `.match` of the object
|
|
14
|
+
*
|
|
15
|
+
* @param regex Regex to be matched
|
|
16
|
+
*/
|
|
17
|
+
export declare const regex: (regex: RegExp) => UpdateFilter<UpdatesWithText, {
|
|
18
|
+
match: RegExpMatchArray;
|
|
19
|
+
}>;
|
|
20
|
+
/**
|
|
21
|
+
* Filter objects which contain the exact text given
|
|
22
|
+
* - for `Message`, `Message.text` is used
|
|
23
|
+
* - for `InlineQuery`, `InlineQuery.query` is used
|
|
24
|
+
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
|
25
|
+
* - for `CallbackQuery`, `CallbackQuery.dataStr` is used
|
|
26
|
+
*
|
|
27
|
+
* @param str String to be matched
|
|
28
|
+
* @param ignoreCase Whether string case should be ignored
|
|
29
|
+
*/
|
|
30
|
+
export declare const equals: (str: string, ignoreCase?: boolean) => UpdateFilter<UpdatesWithText>;
|
|
31
|
+
/**
|
|
32
|
+
* Filter objects which contain the text given (as a substring)
|
|
33
|
+
* - for `Message`, `Message.text` is used
|
|
34
|
+
* - for `InlineQuery`, `InlineQuery.query` is used
|
|
35
|
+
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
|
36
|
+
* - for `CallbackQuery`, `CallbackQuery.dataStr` is used
|
|
37
|
+
*
|
|
38
|
+
* @param str Substring to be matched
|
|
39
|
+
* @param ignoreCase Whether string case should be ignored
|
|
40
|
+
*/
|
|
41
|
+
export declare const contains: (str: string, ignoreCase?: boolean) => UpdateFilter<UpdatesWithText>;
|
|
42
|
+
/**
|
|
43
|
+
* Filter objects which contain the text starting with a given string
|
|
44
|
+
* - for `Message`, `Message.text` is used
|
|
45
|
+
* - for `InlineQuery`, `InlineQuery.query` is used
|
|
46
|
+
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
|
47
|
+
* - for `CallbackQuery`, `CallbackQuery.dataStr` is used
|
|
48
|
+
*
|
|
49
|
+
* @param str Substring to be matched
|
|
50
|
+
* @param ignoreCase Whether string case should be ignored
|
|
51
|
+
*/
|
|
52
|
+
export declare const startsWith: (str: string, ignoreCase?: boolean) => UpdateFilter<UpdatesWithText>;
|
|
53
|
+
/**
|
|
54
|
+
* Filter objects which contain the text ending with a given string
|
|
55
|
+
* - for `Message`, `Message.text` is used
|
|
56
|
+
* - for `InlineQuery`, `InlineQuery.query` is used
|
|
57
|
+
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
|
58
|
+
* - for `CallbackQuery`, `CallbackQuery.dataStr` is used
|
|
59
|
+
*
|
|
60
|
+
* @param str Substring to be matched
|
|
61
|
+
* @param ignoreCase Whether string case should be ignored
|
|
62
|
+
*/
|
|
63
|
+
export declare const endsWith: (str: string, ignoreCase?: boolean) => UpdateFilter<UpdatesWithText>;
|
|
64
|
+
export {};
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.endsWith = exports.startsWith = exports.contains = exports.equals = exports.regex = void 0;
|
|
4
|
+
function extractText(obj) {
|
|
5
|
+
switch (obj._name) {
|
|
6
|
+
case 'new_message':
|
|
7
|
+
return obj.text;
|
|
8
|
+
case 'inline_query':
|
|
9
|
+
return obj.query;
|
|
10
|
+
case 'chosen_inline_result':
|
|
11
|
+
return obj.id;
|
|
12
|
+
case 'callback_query':
|
|
13
|
+
if (obj.raw.data)
|
|
14
|
+
return obj.dataStr;
|
|
15
|
+
}
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Filter objects that match a given regular expression
|
|
20
|
+
* - for `Message`, `Message.text` is used
|
|
21
|
+
* - for `InlineQuery`, `InlineQuery.query` is used
|
|
22
|
+
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult#id} is used
|
|
23
|
+
* - for `CallbackQuery`, `CallbackQuery.dataStr` is used
|
|
24
|
+
*
|
|
25
|
+
* When a regex matches, the match array is stored in a
|
|
26
|
+
* type-safe extension field `.match` of the object
|
|
27
|
+
*
|
|
28
|
+
* @param regex Regex to be matched
|
|
29
|
+
*/
|
|
30
|
+
const regex = (regex) => (obj) => {
|
|
31
|
+
const txt = extractText(obj);
|
|
32
|
+
if (!txt)
|
|
33
|
+
return false;
|
|
34
|
+
const m = txt.match(regex);
|
|
35
|
+
if (m) {
|
|
36
|
+
obj.match = m;
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
};
|
|
41
|
+
exports.regex = regex;
|
|
42
|
+
/**
|
|
43
|
+
* Filter objects which contain the exact text given
|
|
44
|
+
* - for `Message`, `Message.text` is used
|
|
45
|
+
* - for `InlineQuery`, `InlineQuery.query` is used
|
|
46
|
+
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
|
47
|
+
* - for `CallbackQuery`, `CallbackQuery.dataStr` is used
|
|
48
|
+
*
|
|
49
|
+
* @param str String to be matched
|
|
50
|
+
* @param ignoreCase Whether string case should be ignored
|
|
51
|
+
*/
|
|
52
|
+
const equals = (str, ignoreCase = false) => {
|
|
53
|
+
if (ignoreCase) {
|
|
54
|
+
str = str.toLowerCase();
|
|
55
|
+
return (obj) => extractText(obj)?.toLowerCase() === str;
|
|
56
|
+
}
|
|
57
|
+
return (obj) => extractText(obj) === str;
|
|
58
|
+
};
|
|
59
|
+
exports.equals = equals;
|
|
60
|
+
/**
|
|
61
|
+
* Filter objects which contain the text given (as a substring)
|
|
62
|
+
* - for `Message`, `Message.text` is used
|
|
63
|
+
* - for `InlineQuery`, `InlineQuery.query` is used
|
|
64
|
+
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
|
65
|
+
* - for `CallbackQuery`, `CallbackQuery.dataStr` is used
|
|
66
|
+
*
|
|
67
|
+
* @param str Substring to be matched
|
|
68
|
+
* @param ignoreCase Whether string case should be ignored
|
|
69
|
+
*/
|
|
70
|
+
const contains = (str, ignoreCase = false) => {
|
|
71
|
+
if (ignoreCase) {
|
|
72
|
+
str = str.toLowerCase();
|
|
73
|
+
return (obj) => {
|
|
74
|
+
const txt = extractText(obj);
|
|
75
|
+
return txt != null && txt.toLowerCase().includes(str);
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
return (obj) => {
|
|
79
|
+
const txt = extractText(obj);
|
|
80
|
+
return txt != null && txt.includes(str);
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
exports.contains = contains;
|
|
84
|
+
/**
|
|
85
|
+
* Filter objects which contain the text starting with a given string
|
|
86
|
+
* - for `Message`, `Message.text` is used
|
|
87
|
+
* - for `InlineQuery`, `InlineQuery.query` is used
|
|
88
|
+
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
|
89
|
+
* - for `CallbackQuery`, `CallbackQuery.dataStr` is used
|
|
90
|
+
*
|
|
91
|
+
* @param str Substring to be matched
|
|
92
|
+
* @param ignoreCase Whether string case should be ignored
|
|
93
|
+
*/
|
|
94
|
+
const startsWith = (str, ignoreCase = false) => {
|
|
95
|
+
if (ignoreCase) {
|
|
96
|
+
str = str.toLowerCase();
|
|
97
|
+
return (obj) => {
|
|
98
|
+
const txt = extractText(obj);
|
|
99
|
+
return txt != null && txt.toLowerCase().substring(0, str.length) === str;
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
return (obj) => {
|
|
103
|
+
const txt = extractText(obj);
|
|
104
|
+
return txt != null && txt.substring(0, str.length) === str;
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
exports.startsWith = startsWith;
|
|
108
|
+
/**
|
|
109
|
+
* Filter objects which contain the text ending with a given string
|
|
110
|
+
* - for `Message`, `Message.text` is used
|
|
111
|
+
* - for `InlineQuery`, `InlineQuery.query` is used
|
|
112
|
+
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
|
113
|
+
* - for `CallbackQuery`, `CallbackQuery.dataStr` is used
|
|
114
|
+
*
|
|
115
|
+
* @param str Substring to be matched
|
|
116
|
+
* @param ignoreCase Whether string case should be ignored
|
|
117
|
+
*/
|
|
118
|
+
const endsWith = (str, ignoreCase = false) => {
|
|
119
|
+
if (ignoreCase) {
|
|
120
|
+
str = str.toLowerCase();
|
|
121
|
+
return (obj) => {
|
|
122
|
+
const txt = extractText(obj);
|
|
123
|
+
return txt != null && txt.toLowerCase().substring(0, str.length) === str;
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
return (obj) => {
|
|
127
|
+
const txt = extractText(obj);
|
|
128
|
+
return txt != null && txt.substring(0, str.length) === str;
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
exports.endsWith = endsWith;
|
|
132
|
+
//# sourceMappingURL=text.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text.js","sourceRoot":"","sources":["../../../src/filters/text.ts"],"names":[],"mappings":";;;AAOA,SAAS,WAAW,CAAC,GAAoB;IACrC,QAAQ,GAAG,CAAC,KAAK,EAAE;QACf,KAAK,aAAa;YACd,OAAO,GAAG,CAAC,IAAI,CAAA;QACnB,KAAK,cAAc;YACf,OAAO,GAAG,CAAC,KAAK,CAAA;QACpB,KAAK,sBAAsB;YACvB,OAAO,GAAG,CAAC,EAAE,CAAA;QACjB,KAAK,gBAAgB;YACjB,IAAI,GAAG,CAAC,GAAG,CAAC,IAAI;gBAAE,OAAO,GAAG,CAAC,OAAO,CAAA;KAC3C;IAED,OAAO,IAAI,CAAA;AACf,CAAC;AAED;;;;;;;;;;;GAWG;AACI,MAAM,KAAK,GACd,CAAC,KAAa,EAA8D,EAAE,CAC1E,CAAC,GAAG,EAAE,EAAE;IACJ,MAAM,GAAG,GAAG,WAAW,CAAC,GAAG,CAAC,CAAA;IAC5B,IAAI,CAAC,GAAG;QAAE,OAAO,KAAK,CAAA;IAEtB,MAAM,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;IAE1B,IAAI,CAAC,EAAE;QACF,GAAgD,CAAC,KAAK,GAAG,CAAC,CAAA;QAE3D,OAAO,IAAI,CAAA;KACd;IAED,OAAO,KAAK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, Message } from '@mtcute/client'\n\nimport { UpdateContextDistributed } from '../context/base.js'\nimport { UpdateFilter } from './types.js'\n\ntype UpdatesWithText = UpdateContextDistributed<Message | InlineQuery | ChosenInlineResult | CallbackQuery>\n\nfunction extractText(obj: UpdatesWithText): string | null {\n switch (obj._name) {\n case 'new_message':\n return obj.text\n case 'inline_query':\n return obj.query\n case 'chosen_inline_result':\n return obj.id\n case 'callback_query':\n if (obj.raw.data) return obj.dataStr\n }\n\n return null\n}\n\n/**\n * Filter objects that match a given regular expression\n * - for `Message`, `Message.text` is used\n * - for `InlineQuery`, `InlineQuery.query` is used\n * - for {@link ChosenInlineResult}, {@link ChosenInlineResult#id} is used\n * - for `CallbackQuery`, `CallbackQuery.dataStr` is used\n *\n * When a regex matches, the match array is stored in a\n * type-safe extension field `.match` of the object\n *\n * @param regex Regex to be matched\n */\nexport const regex =\n (regex: RegExp): UpdateFilter<UpdatesWithText, { match: RegExpMatchArray }> =>\n (obj) => {\n const txt = extractText(obj)\n if (!txt) return false\n\n const m = txt.match(regex)\n\n if (m) {\n (obj as typeof obj & { match: RegExpMatchArray }).match = m\n\n return true\n }\n\n return false\n }\n\n/**\n * Filter objects which contain the exact text given\n * - for `Message`, `Message.text` is used\n * - for `InlineQuery`, `InlineQuery.query` is used\n * - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used\n * - for `CallbackQuery`, `CallbackQuery.dataStr` is used\n *\n * @param str String to be matched\n * @param ignoreCase Whether string case should be ignored\n */\nexport const equals = (str: string, ignoreCase = false): UpdateFilter<UpdatesWithText> => {\n if (ignoreCase) {\n str = str.toLowerCase()\n\n return (obj) => extractText(obj)?.toLowerCase() === str\n }\n\n return (obj) => extractText(obj) === str\n}\n\n/**\n * Filter objects which contain the text given (as a substring)\n * - for `Message`, `Message.text` is used\n * - for `InlineQuery`, `InlineQuery.query` is used\n * - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used\n * - for `CallbackQuery`, `CallbackQuery.dataStr` is used\n *\n * @param str Substring to be matched\n * @param ignoreCase Whether string case should be ignored\n */\nexport const contains = (str: string, ignoreCase = false): UpdateFilter<UpdatesWithText> => {\n if (ignoreCase) {\n str = str.toLowerCase()\n\n return (obj) => {\n const txt = extractText(obj)\n\n return txt != null && txt.toLowerCase().includes(str)\n }\n }\n\n return (obj) => {\n const txt = extractText(obj)\n\n return txt != null && txt.includes(str)\n }\n}\n\n/**\n * Filter objects which contain the text starting with a given string\n * - for `Message`, `Message.text` is used\n * - for `InlineQuery`, `InlineQuery.query` is used\n * - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used\n * - for `CallbackQuery`, `CallbackQuery.dataStr` is used\n *\n * @param str Substring to be matched\n * @param ignoreCase Whether string case should be ignored\n */\nexport const startsWith = (str: string, ignoreCase = false): UpdateFilter<UpdatesWithText> => {\n if (ignoreCase) {\n str = str.toLowerCase()\n\n return (obj) => {\n const txt = extractText(obj)\n\n return txt != null && txt.toLowerCase().substring(0, str.length) === str\n }\n }\n\n return (obj) => {\n const txt = extractText(obj)\n\n return txt != null && txt.substring(0, str.length) === str\n }\n}\n\n/**\n * Filter objects which contain the text ending with a given string\n * - for `Message`, `Message.text` is used\n * - for `InlineQuery`, `InlineQuery.query` is used\n * - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used\n * - for `CallbackQuery`, `CallbackQuery.dataStr` is used\n *\n * @param str Substring to be matched\n * @param ignoreCase Whether string case should be ignored\n */\nexport const endsWith = (str: string, ignoreCase = false): UpdateFilter<UpdatesWithText> => {\n if (ignoreCase) {\n str = str.toLowerCase()\n\n return (obj) => {\n const txt = extractText(obj)\n\n return txt != null && txt.toLowerCase().substring(0, str.length) === str\n }\n }\n\n return (obj) => {\n const txt = extractText(obj)\n\n return txt != null && txt.substring(0, str.length) === str\n }\n}\n"]}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { MaybeAsync } from '@mtcute/client';
|
|
2
|
+
import { UpdateState } from '../state/update-state.js';
|
|
3
|
+
/**
|
|
4
|
+
* Type describing a primitive filter, which is a function taking some `Base`
|
|
5
|
+
* and a {@link TelegramClient}, checking it against some condition
|
|
6
|
+
* and returning a boolean.
|
|
7
|
+
*
|
|
8
|
+
* If `true` is returned, the filter is considered
|
|
9
|
+
* to be matched, and the appropriate update handler function is called,
|
|
10
|
+
* otherwise next registered handler is checked.
|
|
11
|
+
*
|
|
12
|
+
* Additionally, filter might contain a type modification
|
|
13
|
+
* to `Base` for better code insights. If it is present,
|
|
14
|
+
* it is used to overwrite types (!) of some of the `Base` fields
|
|
15
|
+
* to given (note that this is entirely compile-time! object is not modified)
|
|
16
|
+
*
|
|
17
|
+
* For parametrized filters (like {@link filters.regex}),
|
|
18
|
+
* type modification can also be used to add additional fields
|
|
19
|
+
* (in case of `regex`, its match array is added to `.match`)
|
|
20
|
+
*
|
|
21
|
+
* Example without type mod:
|
|
22
|
+
* ```typescript
|
|
23
|
+
*
|
|
24
|
+
* const hasPhoto: UpdateFilter<Message> = msg => msg.media?.type === 'photo'
|
|
25
|
+
*
|
|
26
|
+
* // ..later..
|
|
27
|
+
* tg.onNewMessage(hasPhoto, async (msg) => {
|
|
28
|
+
* // `hasPhoto` filter matched, so we can safely assume
|
|
29
|
+
* // that `msg.media` is a Photo.
|
|
30
|
+
* //
|
|
31
|
+
* // but it is very redundant, verbose and error-rome,
|
|
32
|
+
* // wonder if we could make typescript do this automagically and safely...
|
|
33
|
+
* await (msg.media as Photo).downloadToFile(`${msg.id}.jpg`)
|
|
34
|
+
* })
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* Example with type mod:
|
|
38
|
+
* ```typescript
|
|
39
|
+
*
|
|
40
|
+
* const hasPhoto: UpdateFilter<Message, { media: Photo }> = msg => msg.media?.type === 'photo'
|
|
41
|
+
*
|
|
42
|
+
* // ..later..
|
|
43
|
+
* tg.onNewMessage(hasPhoto, async (msg) => {
|
|
44
|
+
* // since `hasPhoto` filter matched,
|
|
45
|
+
* // we have applied the modification to `msg`,
|
|
46
|
+
* // and `msg.media` now has type `Photo`
|
|
47
|
+
* //
|
|
48
|
+
* // no more redundancy and type casts!
|
|
49
|
+
* await msg.media.downloadToFile(`${msg.id}.jpg`)
|
|
50
|
+
* })
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* > **Note**: Type modification can contain anything, even totally unrelated types
|
|
54
|
+
* > and it is *your* task to keep track that everything is correct.
|
|
55
|
+
* >
|
|
56
|
+
* > Bad example:
|
|
57
|
+
* > ```typescript
|
|
58
|
+
* > // we check for `Photo`, but type contains `Audio`. this will be a problem!
|
|
59
|
+
* > const hasPhoto: UpdateFilter<Message, { media: Audio }> = msg => msg.media?.type === 'photo'
|
|
60
|
+
* >
|
|
61
|
+
* > // ..later..
|
|
62
|
+
* > tg.onNewMessage(hasPhoto, async (msg) => {
|
|
63
|
+
* > // oops! `msg.media` is `Audio` and does not have `.width`!
|
|
64
|
+
* > console.log(msg.media.width)
|
|
65
|
+
* > })
|
|
66
|
+
* > ```
|
|
67
|
+
*
|
|
68
|
+
* > **Warning!** Do not use the generics provided in functions
|
|
69
|
+
* > like `and`, `or`, etc. Those are meant to be inferred by the compiler!
|
|
70
|
+
*/
|
|
71
|
+
export type UpdateFilter<Base, Mod = {}, State extends object = never> = (update: Base, state?: UpdateState<State>) => MaybeAsync<boolean>;
|
|
72
|
+
export type Modify<Base, Mod> = Omit<Base, keyof Mod> & Mod;
|
|
73
|
+
export type Invert<Base, Mod> = {
|
|
74
|
+
[P in keyof Mod & keyof Base]: Exclude<Base[P], Mod[P]>;
|
|
75
|
+
};
|
|
76
|
+
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
77
|
+
export type ExtractBase<Filter> = Filter extends UpdateFilter<infer I, any> ? I : never;
|
|
78
|
+
export type ExtractMod<Filter> = Filter extends UpdateFilter<any, infer I> ? I : never;
|
|
79
|
+
export type ExtractState<Filter> = Filter extends UpdateFilter<any, any, infer I> ? I : never;
|
|
80
|
+
export type TupleKeys<T extends any[]> = Exclude<keyof T, keyof []>;
|
|
81
|
+
export type WrapBase<T extends any[]> = {
|
|
82
|
+
[K in TupleKeys<T>]: {
|
|
83
|
+
base: ExtractBase<T[K]>;
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
export type Values<T> = T[keyof T];
|
|
87
|
+
export type UnwrapBase<T> = T extends {
|
|
88
|
+
base: any;
|
|
89
|
+
} ? T['base'] : never;
|
|
90
|
+
export type ExtractBaseMany<Filters extends any[]> = UnwrapBase<UnionToIntersection<Values<WrapBase<Filters>>>>;
|
|
91
|
+
export type EmptyObject = Record<never, never>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/filters/types.ts"],"names":[],"mappings":";AAAA,iDAAiD;AACjD,uDAAuD;AACvD,mCAAmC","sourcesContent":["/* eslint-disable @typescript-eslint/ban-types */\n/* eslint-disable @typescript-eslint/no-explicit-any */\n// ^^ will be looked into in MTQ-29\n\nimport { MaybeAsync } from '@mtcute/client'\n\nimport { UpdateState } from '../state/update-state.js'\n/**\n * Type describing a primitive filter, which is a function taking some `Base`\n * and a {@link TelegramClient}, checking it against some condition\n * and returning a boolean.\n *\n * If `true` is returned, the filter is considered\n * to be matched, and the appropriate update handler function is called,\n * otherwise next registered handler is checked.\n *\n * Additionally, filter might contain a type modification\n * to `Base` for better code insights. If it is present,\n * it is used to overwrite types (!) of some of the `Base` fields\n * to given (note that this is entirely compile-time! object is not modified)\n *\n * For parametrized filters (like {@link filters.regex}),\n * type modification can also be used to add additional fields\n * (in case of `regex`, its match array is added to `.match`)\n *\n * Example without type mod:\n * ```typescript\n *\n * const hasPhoto: UpdateFilter<Message> = msg => msg.media?.type === 'photo'\n *\n * // ..later..\n * tg.onNewMessage(hasPhoto, async (msg) => {\n * // `hasPhoto` filter matched, so we can safely assume\n * // that `msg.media` is a Photo.\n * //\n * // but it is very redundant, verbose and error-rome,\n * // wonder if we could make typescript do this automagically and safely...\n * await (msg.media as Photo).downloadToFile(`${msg.id}.jpg`)\n * })\n * ```\n *\n * Example with type mod:\n * ```typescript\n *\n * const hasPhoto: UpdateFilter<Message, { media: Photo }> = msg => msg.media?.type === 'photo'\n *\n * // ..later..\n * tg.onNewMessage(hasPhoto, async (msg) => {\n * // since `hasPhoto` filter matched,\n * // we have applied the modification to `msg`,\n * // and `msg.media` now has type `Photo`\n * //\n * // no more redundancy and type casts!\n * await msg.media.downloadToFile(`${msg.id}.jpg`)\n * })\n * ```\n *\n * > **Note**: Type modification can contain anything, even totally unrelated types\n * > and it is *your* task to keep track that everything is correct.\n * >\n * > Bad example:\n * > ```typescript\n * > // we check for `Photo`, but type contains `Audio`. this will be a problem!\n * > const hasPhoto: UpdateFilter<Message, { media: Audio }> = msg => msg.media?.type === 'photo'\n * >\n * > // ..later..\n * > tg.onNewMessage(hasPhoto, async (msg) => {\n * > // oops! `msg.media` is `Audio` and does not have `.width`!\n * > console.log(msg.media.width)\n * > })\n * > ```\n *\n * > **Warning!** Do not use the generics provided in functions\n * > like `and`, `or`, etc. Those are meant to be inferred by the compiler!\n */\n// we need the second parameter because it carries meta information\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nexport type UpdateFilter<Base, Mod = {}, State extends object = never> = (\n update: Base,\n state?: UpdateState<State>,\n) => MaybeAsync<boolean>\n\nexport type Modify<Base, Mod> = Omit<Base, keyof Mod> & Mod\nexport type Invert<Base, Mod> = {\n [P in keyof Mod & keyof Base]: Exclude<Base[P], Mod[P]>\n}\n\nexport type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never\n\nexport type ExtractBase<Filter> = Filter extends UpdateFilter<infer I, any> ? I : never\n\nexport type ExtractMod<Filter> = Filter extends UpdateFilter<any, infer I> ? I : never\n\nexport type ExtractState<Filter> = Filter extends UpdateFilter<any, any, infer I> ? I : never\n\nexport type TupleKeys<T extends any[]> = Exclude<keyof T, keyof []>\nexport type WrapBase<T extends any[]> = {\n [K in TupleKeys<T>]: { base: ExtractBase<T[K]> }\n}\nexport type Values<T> = T[keyof T]\nexport type UnwrapBase<T> = T extends { base: any } ? T['base'] : never\nexport type ExtractBaseMany<Filters extends any[]> = UnwrapBase<UnionToIntersection<Values<WrapBase<Filters>>>>\n\nexport type EmptyObject = Record<never, never>\n"]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { CallbackQuery, ChatMemberUpdate, ChatMemberUpdateType, UserStatus, UserStatusUpdate } from '@mtcute/client';
|
|
2
|
+
import { UpdateFilter } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Create a filter for {@link ChatMemberUpdate} by update type
|
|
5
|
+
*
|
|
6
|
+
* @param types Update type(s)
|
|
7
|
+
* @link ChatMemberUpdate.Type
|
|
8
|
+
*/
|
|
9
|
+
export declare const chatMember: {
|
|
10
|
+
<T extends ChatMemberUpdateType>(type: T): UpdateFilter<ChatMemberUpdate, {
|
|
11
|
+
type: T;
|
|
12
|
+
}>;
|
|
13
|
+
<T extends ChatMemberUpdateType[]>(types: T): UpdateFilter<ChatMemberUpdate, {
|
|
14
|
+
type: T[number];
|
|
15
|
+
}>;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Create a filter for {@link UserStatusUpdate} by new user status
|
|
19
|
+
*
|
|
20
|
+
* @param statuses Update type(s)
|
|
21
|
+
* @link User.Status
|
|
22
|
+
*/
|
|
23
|
+
export declare const userStatus: {
|
|
24
|
+
<T extends UserStatus>(status: T): UpdateFilter<UserStatusUpdate, {
|
|
25
|
+
type: T;
|
|
26
|
+
lastOnline: T extends 'offline' ? Date : null;
|
|
27
|
+
nextOffline: T extends 'online' ? Date : null;
|
|
28
|
+
}>;
|
|
29
|
+
<T extends UserStatus[]>(statuses: T): UpdateFilter<UserStatusUpdate, {
|
|
30
|
+
type: T[number];
|
|
31
|
+
}>;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Create a filter for {@link ChatMemberUpdate} for updates
|
|
35
|
+
* regarding current user
|
|
36
|
+
*/
|
|
37
|
+
export declare const chatMemberSelf: UpdateFilter<ChatMemberUpdate, {
|
|
38
|
+
isSelf: true;
|
|
39
|
+
}>;
|
|
40
|
+
/**
|
|
41
|
+
* Create a filter for callback queries that
|
|
42
|
+
* originated from an inline message
|
|
43
|
+
*/
|
|
44
|
+
export declare const callbackInline: UpdateFilter<CallbackQuery, {
|
|
45
|
+
isInline: true;
|
|
46
|
+
}>;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.callbackInline = exports.chatMemberSelf = exports.userStatus = exports.chatMember = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Create a filter for {@link ChatMemberUpdate} by update type
|
|
6
|
+
*
|
|
7
|
+
* @param types Update type(s)
|
|
8
|
+
* @link ChatMemberUpdate.Type
|
|
9
|
+
*/
|
|
10
|
+
const chatMember = (types) => {
|
|
11
|
+
if (Array.isArray(types)) {
|
|
12
|
+
const index = {};
|
|
13
|
+
types.forEach((typ) => (index[typ] = true));
|
|
14
|
+
return (upd) => upd.type in index;
|
|
15
|
+
}
|
|
16
|
+
return (upd) => upd.type === types;
|
|
17
|
+
};
|
|
18
|
+
exports.chatMember = chatMember;
|
|
19
|
+
/**
|
|
20
|
+
* Create a filter for {@link UserStatusUpdate} by new user status
|
|
21
|
+
*
|
|
22
|
+
* @param statuses Update type(s)
|
|
23
|
+
* @link User.Status
|
|
24
|
+
*/
|
|
25
|
+
const userStatus = (statuses) => {
|
|
26
|
+
if (Array.isArray(statuses)) {
|
|
27
|
+
const index = {};
|
|
28
|
+
statuses.forEach((typ) => (index[typ] = true));
|
|
29
|
+
return (upd) => upd.status in index;
|
|
30
|
+
}
|
|
31
|
+
return (upd) => upd.status === statuses;
|
|
32
|
+
};
|
|
33
|
+
exports.userStatus = userStatus;
|
|
34
|
+
/**
|
|
35
|
+
* Create a filter for {@link ChatMemberUpdate} for updates
|
|
36
|
+
* regarding current user
|
|
37
|
+
*/
|
|
38
|
+
const chatMemberSelf = (upd) => upd.isSelf;
|
|
39
|
+
exports.chatMemberSelf = chatMemberSelf;
|
|
40
|
+
/**
|
|
41
|
+
* Create a filter for callback queries that
|
|
42
|
+
* originated from an inline message
|
|
43
|
+
*/
|
|
44
|
+
const callbackInline = (q) => q.isInline;
|
|
45
|
+
exports.callbackInline = callbackInline;
|
|
46
|
+
//# sourceMappingURL=updates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"updates.js","sourceRoot":"","sources":["../../../src/filters/updates.ts"],"names":[],"mappings":";;;AAIA;;;;;GAKG;AACI,MAAM,UAAU,GAGnB,CAAC,KAAuC,EAAkC,EAAE;IAC5E,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;QACtB,MAAM,KAAK,GAAgD,EAAE,CAAA;QAC7D,KAAK,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC,CAAA;QAE3C,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,IAAI,KAAK,CAAA;KACpC;IAED,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,KAAK,KAAK,CAAA;AACtC,CAAC,CAAA;AAZY,QAAA,UAAU,cAYtB;AAED;;;;;GAKG;AACI,MAAM,UAAU,GAYnB,CAAC,QAAgC,EAAkC,EAAE;IACrE,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;QACzB,MAAM,KAAK,GAAsC,EAAE,CAAA;QACnD,QAAQ,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC,CAAA;QAE9C,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,IAAI,KAAK,CAAA;KACtC;IAED,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,KAAK,QAAQ,CAAA;AAC3C,CAAC,CAAA;AArBY,QAAA,UAAU,cAqBtB;AAED;;;GAGG;AACI,MAAM,cAAc,GAAqD,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAA;AAAtF,QAAA,cAAc,kBAAwE;AAEnG;;;GAGG;AACI,MAAM,cAAc,GAAoD,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAA;AAAnF,QAAA,cAAc,kBAAqE","sourcesContent":["import { CallbackQuery, ChatMemberUpdate, ChatMemberUpdateType, MaybeArray, UserStatus, UserStatusUpdate } from '@mtcute/client'\n\nimport { UpdateFilter } from './types.js'\n\n/**\n * Create a filter for {@link ChatMemberUpdate} by update type\n *\n * @param types Update type(s)\n * @link ChatMemberUpdate.Type\n */\nexport const chatMember: {\n <T extends ChatMemberUpdateType>(type: T): UpdateFilter<ChatMemberUpdate, { type: T }>\n <T extends ChatMemberUpdateType[]>(types: T): UpdateFilter<ChatMemberUpdate, { type: T[number] }>\n} = (types: MaybeArray<ChatMemberUpdateType>): UpdateFilter<ChatMemberUpdate> => {\n if (Array.isArray(types)) {\n const index: Partial<Record<ChatMemberUpdateType, true>> = {}\n types.forEach((typ) => (index[typ] = true))\n\n return (upd) => upd.type in index\n }\n\n return (upd) => upd.type === types\n}\n\n/**\n * Create a filter for {@link UserStatusUpdate} by new user status\n *\n * @param statuses Update type(s)\n * @link User.Status\n */\nexport const userStatus: {\n <T extends UserStatus>(\n status: T,\n ): UpdateFilter<\n UserStatusUpdate,\n {\n type: T\n lastOnline: T extends 'offline' ? Date : null\n nextOffline: T extends 'online' ? Date : null\n }\n >\n <T extends UserStatus[]>(statuses: T): UpdateFilter<UserStatusUpdate, { type: T[number] }>\n} = (statuses: MaybeArray<UserStatus>): UpdateFilter<UserStatusUpdate> => {\n if (Array.isArray(statuses)) {\n const index: Partial<Record<UserStatus, true>> = {}\n statuses.forEach((typ) => (index[typ] = true))\n\n return (upd) => upd.status in index\n }\n\n return (upd) => upd.status === statuses\n}\n\n/**\n * Create a filter for {@link ChatMemberUpdate} for updates\n * regarding current user\n */\nexport const chatMemberSelf: UpdateFilter<ChatMemberUpdate, { isSelf: true }> = (upd) => upd.isSelf\n\n/**\n * Create a filter for callback queries that\n * originated from an inline message\n */\nexport const callbackInline: UpdateFilter<CallbackQuery, { isInline: true }> = (q) => q.isInline\n"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { BotChatJoinRequestUpdate, CallbackQuery, ChatMemberUpdate, ChosenInlineResult, DeleteStoryUpdate, HistoryReadUpdate, InlineQuery, MaybeArray, Message, PollVoteUpdate, StoryUpdate, User, UserStatusUpdate, UserTypingUpdate } from '@mtcute/client';
|
|
2
|
+
import { UpdateContextDistributed } from '../context/base.js';
|
|
3
|
+
import { UpdateFilter } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Filter messages generated by yourself (including Saved Messages)
|
|
6
|
+
*/
|
|
7
|
+
export declare const me: UpdateFilter<Message, {
|
|
8
|
+
sender: User;
|
|
9
|
+
}>;
|
|
10
|
+
/**
|
|
11
|
+
* Filter messages sent by bots
|
|
12
|
+
*/
|
|
13
|
+
export declare const bot: UpdateFilter<Message, {
|
|
14
|
+
sender: User;
|
|
15
|
+
}>;
|
|
16
|
+
/**
|
|
17
|
+
* Filter updates by user ID(s) or username(s)
|
|
18
|
+
*
|
|
19
|
+
* Note that only some updates support filtering by username.
|
|
20
|
+
*/
|
|
21
|
+
export declare const userId: {
|
|
22
|
+
(id: MaybeArray<number>): UpdateFilter<UpdateContextDistributed<Message | StoryUpdate | DeleteStoryUpdate | InlineQuery | ChatMemberUpdate | ChosenInlineResult | CallbackQuery | PollVoteUpdate | BotChatJoinRequestUpdate>>;
|
|
23
|
+
(id: MaybeArray<number | string>): UpdateFilter<UpdateContextDistributed<Message | UserStatusUpdate | UserTypingUpdate | StoryUpdate | HistoryReadUpdate | DeleteStoryUpdate | InlineQuery | ChatMemberUpdate | ChosenInlineResult | CallbackQuery | PollVoteUpdate | BotChatJoinRequestUpdate>>;
|
|
24
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.userId = exports.bot = exports.me = void 0;
|
|
4
|
+
const client_1 = require("@mtcute/client");
|
|
5
|
+
/**
|
|
6
|
+
* Filter messages generated by yourself (including Saved Messages)
|
|
7
|
+
*/
|
|
8
|
+
const me = (msg) => (msg.sender.type === 'user' && msg.sender.isSelf) || msg.isOutgoing;
|
|
9
|
+
exports.me = me;
|
|
10
|
+
/**
|
|
11
|
+
* Filter messages sent by bots
|
|
12
|
+
*/
|
|
13
|
+
const bot = (msg) => msg.sender.constructor === client_1.User && msg.sender.isBot;
|
|
14
|
+
exports.bot = bot;
|
|
15
|
+
// prettier-ignore
|
|
16
|
+
/**
|
|
17
|
+
* Filter updates by user ID(s) or username(s)
|
|
18
|
+
*
|
|
19
|
+
* Note that only some updates support filtering by username.
|
|
20
|
+
*/
|
|
21
|
+
const userId = (id) => {
|
|
22
|
+
const indexId = new Set();
|
|
23
|
+
const indexUsername = new Set();
|
|
24
|
+
let matchSelf = false;
|
|
25
|
+
if (!Array.isArray(id))
|
|
26
|
+
id = [id];
|
|
27
|
+
id.forEach((id) => {
|
|
28
|
+
if (id === 'me' || id === 'self') {
|
|
29
|
+
matchSelf = true;
|
|
30
|
+
}
|
|
31
|
+
else if (typeof id === 'string') {
|
|
32
|
+
indexUsername.add(id);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
indexId.add(id);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return (upd) => {
|
|
39
|
+
switch (upd._name) {
|
|
40
|
+
case 'new_message':
|
|
41
|
+
case 'edit_message': {
|
|
42
|
+
const sender = upd.sender;
|
|
43
|
+
return (matchSelf && sender.isSelf) ||
|
|
44
|
+
indexId.has(sender.id) ||
|
|
45
|
+
indexUsername.has(sender.username);
|
|
46
|
+
}
|
|
47
|
+
case 'user_status':
|
|
48
|
+
case 'user_typing': {
|
|
49
|
+
const id = upd.userId;
|
|
50
|
+
return (matchSelf && id === upd.client.getAuthState().userId) ||
|
|
51
|
+
indexId.has(id);
|
|
52
|
+
}
|
|
53
|
+
case 'poll_vote':
|
|
54
|
+
case 'story':
|
|
55
|
+
case 'delete_story': {
|
|
56
|
+
const peer = upd.peer;
|
|
57
|
+
if (peer.type !== 'user')
|
|
58
|
+
return false;
|
|
59
|
+
return (matchSelf && peer.isSelf) ||
|
|
60
|
+
indexId.has(peer.id) ||
|
|
61
|
+
Boolean(peer.usernames?.some((u) => indexUsername.has(u.username)));
|
|
62
|
+
}
|
|
63
|
+
case 'history_read': {
|
|
64
|
+
const id = upd.chatId;
|
|
65
|
+
return (matchSelf && id === upd.client.getAuthState().userId) ||
|
|
66
|
+
indexId.has(id);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const user = upd.user;
|
|
70
|
+
return ((matchSelf && user.isSelf) ||
|
|
71
|
+
indexId.has(user.id) ||
|
|
72
|
+
Boolean(user.usernames?.some((u) => indexUsername.has(u.username))));
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
exports.userId = userId;
|
|
76
|
+
//# sourceMappingURL=user.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user.js","sourceRoot":"","sources":["../../../src/filters/user.ts"],"names":[],"mappings":";;;AAAA,2CAeuB;AAKvB;;GAEG;AACI,MAAM,EAAE,GAA4C,CAAC,GAAG,EAAE,EAAE,CAC/D,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,KAAK,MAAM,IAAI,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,GAAG,CAAC,UAAU,CAAA;AAD1D,QAAA,EAAE,MACwD;AAE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AACL,CAAC,CAAA;AArFY,QAAA,MAAM,UAqFlB","sourcesContent":["import {\n BotChatJoinRequestUpdate,\n CallbackQuery,\n ChatMemberUpdate,\n ChosenInlineResult,\n DeleteStoryUpdate,\n HistoryReadUpdate,\n InlineQuery,\n MaybeArray,\n Message,\n PollVoteUpdate,\n StoryUpdate,\n User,\n UserStatusUpdate,\n UserTypingUpdate,\n} from '@mtcute/client'\n\nimport { UpdateContextDistributed } from '../context/base.js'\nimport { UpdateFilter } from './types.js'\n\n/**\n * Filter messages generated by yourself (including Saved Messages)\n */\nexport const me: UpdateFilter<Message, { sender: User }> = (msg) =>\n (msg.sender.type === 'user' && msg.sender.isSelf) || msg.isOutgoing\n\n/**\n * Filter messages sent by bots\n */\nexport const bot: UpdateFilter<Message, { sender: User }> = (msg) => msg.sender.constructor === User && msg.sender.isBot\n\n// prettier-ignore\n/**\n * Filter updates by user ID(s) or username(s)\n *\n * Note that only some updates support filtering by username.\n */\nexport const userId: {\n (id: MaybeArray<number>): UpdateFilter<UpdateContextDistributed<\n | Message\n | StoryUpdate\n | DeleteStoryUpdate\n | InlineQuery\n | ChatMemberUpdate\n | ChosenInlineResult\n | CallbackQuery\n | PollVoteUpdate\n | BotChatJoinRequestUpdate\n >>\n (id: MaybeArray<number | string>): UpdateFilter<UpdateContextDistributed<\n | Message\n | UserStatusUpdate\n | UserTypingUpdate\n | StoryUpdate\n | HistoryReadUpdate\n | DeleteStoryUpdate\n | InlineQuery\n | ChatMemberUpdate\n | ChosenInlineResult\n | CallbackQuery\n | PollVoteUpdate\n | BotChatJoinRequestUpdate\n >>\n} = (id) => {\n const indexId = new Set<number>()\n const indexUsername = new Set<string>()\n let matchSelf = false\n\n if (!Array.isArray(id)) id = [id]\n id.forEach((id) => {\n if (id === 'me' || id === 'self') {\n matchSelf = true\n } else if (typeof id === 'string') {\n indexUsername.add(id)\n } else {\n indexId.add(id)\n }\n })\n\n return (upd) => {\n switch (upd._name) {\n case 'new_message':\n case 'edit_message': {\n const sender = upd.sender\n\n return (matchSelf && sender.isSelf) ||\n indexId.has(sender.id) ||\n indexUsername.has(sender.username!)\n }\n case 'user_status':\n case 'user_typing': {\n const id = upd.userId\n\n return (matchSelf && id === upd.client.getAuthState().userId) ||\n indexId.has(id)\n }\n case 'poll_vote':\n case 'story':\n case 'delete_story': {\n const peer = upd.peer\n if (peer.type !== 'user') return false\n\n return (matchSelf && peer.isSelf) ||\n indexId.has(peer.id) ||\n Boolean(peer.usernames?.some((u) => indexUsername.has(u.username)))\n }\n case 'history_read': {\n const id = upd.chatId\n\n return (matchSelf && id === upd.client.getAuthState().userId) ||\n indexId.has(id)\n }\n }\n\n const user = upd.user\n\n return (\n (matchSelf && user.isSelf) ||\n indexId.has(user.id) ||\n Boolean(user.usernames?.some((u) => indexUsername.has(u.username)))\n )\n }\n}\n"]}
|