@effect-ak/tg-bot 1.2.4 → 1.3.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/dist/index.d.ts CHANGED
@@ -1,32 +1,11 @@
1
- import * as effect_Types from 'effect/Types';
2
- import { Api, Update } from '@effect-ak/tg-bot-api';
3
- import * as Context from 'effect/Context';
4
- import * as Data from 'effect/Data';
5
- import { TgBotClient } from '@effect-ak/tg-bot-client';
6
- import * as effect_Cause from 'effect/Cause';
7
- import * as Micro from 'effect/Micro';
1
+ import { Api, Update, Message, CallbackQuery, InlineQuery } from '@effect-ak/tg-bot-api';
8
2
 
9
- type BotResult = {
10
- [K in keyof Api]: K extends `send_${infer R}` ? {
11
- type: R;
12
- } & Omit<Parameters<Api[K]>[0], "chat_id"> : never;
13
- }[keyof Api];
14
- declare const BotResponse_base: new <A extends Record<string, any> = {}>(args: effect_Types.Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => Readonly<A> & {
15
- readonly _tag: "BotResponse";
16
- };
17
- declare class BotResponse extends BotResponse_base<{
18
- response?: BotResult;
19
- }> {
20
- static make(result: BotResult): BotResponse;
21
- static readonly ignore: BotResponse;
22
- }
3
+ /**
4
+ * @module polling
5
+ * Long-polling infrastructure: settings validation and the UpdateFetcher
6
+ * that pulls updates from the Telegram API in a loop.
7
+ */
23
8
 
24
- declare const BotUpdateHandlersTag_base: Context.TagClass<BotUpdateHandlersTag, "BotUpdateHandlers", BotMode>;
25
- declare class BotUpdateHandlersTag extends BotUpdateHandlersTag_base {
26
- }
27
- declare const BotTgClientTag_base: Context.TagClass<BotTgClientTag, "BotTgClient", TgBotClient>;
28
- declare class BotTgClientTag extends BotTgClientTag_base {
29
- }
30
9
  interface PollSettings {
31
10
  log_level: "info" | "debug";
32
11
  on_error: "stop" | "continue";
@@ -34,29 +13,57 @@ interface PollSettings {
34
13
  poll_timeout: number;
35
14
  max_empty_responses: number | undefined;
36
15
  }
37
- declare class BotPollSettings extends Data.Class<PollSettings> {
38
- static make(input: Partial<PollSettings>): BotPollSettings;
16
+
17
+ type BotResult = {
18
+ [K in keyof Api]: K extends `send_${infer R}` ? {
19
+ type: R;
20
+ } & Omit<Parameters<Api[K]>[0], "chat_id"> : never;
21
+ }[keyof Api];
22
+ declare class BotResponse {
23
+ readonly response: BotResult | undefined;
24
+ constructor(response?: BotResult);
25
+ static make(result: BotResult): BotResponse;
26
+ static readonly ignore: BotResponse;
39
27
  }
40
- declare const BotPollSettingsTag_base: Context.ReferenceClass<BotPollSettings, "BotSettings", BotPollSettings>;
41
- declare class BotPollSettingsTag extends BotPollSettingsTag_base {
28
+ interface HandleResult {
29
+ update: Update;
30
+ updateType: string;
31
+ status: "handled" | "ignored" | "no_handler" | "error";
32
+ responseType?: string;
33
+ error?: string;
34
+ duration: number;
35
+ }
36
+ interface BotLogger {
37
+ debug: (message: string, data?: unknown) => void;
38
+ info: (message: string, data?: unknown) => void;
39
+ warn: (message: string, data?: unknown) => void;
40
+ error: (message: string, data?: unknown) => void;
42
41
  }
43
-
44
42
  type RunBotInput = RunBotInputSingle | RunBotInputBatch;
45
43
  interface RunBotInputSingle extends BotUpdatesHandlers {
46
44
  bot_token: string;
47
45
  mode: "single";
48
46
  poll?: Partial<PollSettings>;
47
+ onUpdate?: (update: Update) => void;
48
+ onHandleResult?: (result: HandleResult) => void;
49
+ logger?: BotLogger;
49
50
  }
50
51
  interface RunBotInputBatch extends HandleBatchUpdateFunction {
51
52
  bot_token: string;
52
53
  mode: "batch";
53
54
  poll?: Partial<PollSettings>;
55
+ onUpdate?: (update: Update) => void;
56
+ onHandleResult?: (result: HandleResult) => void;
57
+ logger?: BotLogger;
54
58
  }
55
59
  type ExtractedUpdate<K extends AvailableUpdateTypes> = {
56
60
  type: K;
57
61
  } & Update[K];
58
62
  type AvailableUpdateTypes = Exclude<keyof Update, "update_id">;
59
63
  type HandleUpdateFunction<U> = (update: U) => BotResponse | PromiseLike<BotResponse>;
64
+ type BotResponseParams<T extends string> = Extract<Parameters<typeof BotResponse.make>[0], {
65
+ type: T;
66
+ }>;
60
67
  interface BotContext {
61
68
  readonly command: string | undefined;
62
69
  readonly reply: (text: string, options?: Omit<BotResponseParams<"message">, "text" | "type">) => BotResponse;
@@ -64,9 +71,6 @@ interface BotContext {
64
71
  readonly replyWithPhoto: (photo: BotResponseParams<"photo">["photo"], options?: Omit<BotResponseParams<"photo">, "photo" | "type">) => BotResponse;
65
72
  readonly ignore: BotResponse;
66
73
  }
67
- type BotResponseParams<T extends string> = Extract<Parameters<typeof BotResponse.make>[0], {
68
- type: T;
69
- }>;
70
74
  interface HandlerInput<U> {
71
75
  readonly update: U;
72
76
  readonly ctx: BotContext;
@@ -82,68 +86,93 @@ type BotUpdatesHandlers = {
82
86
  interface HandleBatchUpdateFunction {
83
87
  readonly on_batch: (update: Update[]) => boolean | PromiseLike<boolean>;
84
88
  }
85
- interface BotSingleMode extends BotUpdatesHandlers {
89
+ interface BotSingleBehavior extends BotUpdatesHandlers {
86
90
  type: "single";
87
91
  }
88
- interface BotBatchMode extends HandleBatchUpdateFunction {
92
+ interface BotBatchBehavior extends HandleBatchUpdateFunction {
89
93
  type: "batch";
90
94
  }
91
- type BotMode = BotSingleMode | BotBatchMode;
92
-
95
+ type BotBehavior = BotSingleBehavior | BotBatchBehavior;
93
96
  declare const createBotContext: (update: unknown) => BotContext;
94
97
 
98
+ /**
99
+ * @module bot-processor
100
+ * Update processing pipeline: extracts the update type, matches it to
101
+ * the registered handler (function, guard, or guard array), executes
102
+ * the handler, and sends the response back to Telegram.
103
+ */
104
+
95
105
  declare const extractUpdate: <U extends AvailableUpdateTypes>(input: Update) => ExtractedUpdate<U> | undefined;
96
- declare class BatchUpdateResult extends Data.Class<{
97
- hasErrors: boolean;
98
- updates: Update[];
99
- }> {
100
- }
101
- declare const handleUpdates: (updates: Update[]) => Micro.Micro<BatchUpdateResult, never, BotUpdateHandlersTag | BotTgClientTag>;
102
- declare const handleEntireBatch: (updates: Update[], handlers: HandleBatchUpdateFunction) => Micro.Micro<BatchUpdateResult, never, never>;
103
- declare const HandleUpdateError_base: new <A extends Record<string, any> = {}>(args: effect_Types.Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => effect_Cause.YieldableError & {
104
- readonly _tag: "HandleUpdateError";
105
- } & Readonly<A>;
106
- declare class HandleUpdateError extends HandleUpdateError_base<{
107
- name: "UnknownUpdate" | "HandlerNotDefined" | "BotHandlerError";
108
- update: Update;
109
- cause?: unknown;
110
- }> {
111
- logInfo(): {
112
- error?: string;
113
- updateId: number;
114
- updateKey: string | undefined;
115
- name: "HandleUpdateError";
116
- };
117
- }
118
- declare const handleOneByOne: (updates: Update[], handlers: BotUpdatesHandlers, pollSettings: PollSettings) => Micro.Micro<BatchUpdateResult, never, BotTgClientTag>;
119
- declare const handleOneUpdate: (updateObject: Update, handlers: BotUpdatesHandlers) => Micro.Micro<HandleUpdateError | undefined, unknown, BotTgClientTag>;
120
106
 
121
- type BotInstance = Micro.Micro.Success<ReturnType<typeof launchBot>>;
122
- declare const launchBot: (input: RunBotInput) => Micro.Micro<{
123
- readonly reload: (mode: BotMode) => Promise<void>;
124
- readonly fiber: () => Micro.MicroFiber<BatchUpdateResult, unknown> | undefined;
125
- }, never, never>;
107
+ /**
108
+ * @module run
109
+ * Bot execution entry points: long-polling runner ({@link runBot})
110
+ * and webhook handler ({@link createWebhook}).
111
+ */
126
112
 
127
- declare const BotRunService_base: Context.ReferenceClass<BotRunService, "BotRunService", {
128
- readonly runBotInBackground: Micro.Micro<void, never, BotUpdateHandlersTag | BotTgClientTag>;
129
- readonly getFiber: () => Micro.MicroFiber<BatchUpdateResult, unknown> | undefined;
130
- }>;
131
- declare class BotRunService extends BotRunService_base {
113
+ interface BotInstance {
114
+ stop(): void;
115
+ reload(behavior: BotBehavior): void;
132
116
  }
133
-
134
- declare const runTgChatBot: (input: RunBotInput) => Promise<{
135
- readonly reload: (mode: BotMode) => Promise<void>;
136
- readonly fiber: () => Micro.MicroFiber<BatchUpdateResult, unknown> | undefined;
137
- }>;
117
+ declare const runBot: (input: RunBotInput) => Promise<BotInstance>;
138
118
  declare const defineBot: (input: BotUpdatesHandlers) => BotUpdatesHandlers;
139
-
140
119
  interface WebhookBotConfig extends BotUpdatesHandlers {
141
120
  bot_token: string;
121
+ onHandleResult?: (result: HandleResult) => void;
122
+ logger?: BotLogger;
142
123
  }
143
124
  interface WebhookHandler {
144
125
  (request: Request): Promise<Response>;
145
126
  handleUpdate: (update: Update) => Promise<void>;
146
127
  }
147
- declare const createWebhookHandler: (config: WebhookBotConfig) => WebhookHandler;
128
+ declare const createWebhook: (config: WebhookBotConfig) => WebhookHandler;
129
+
130
+ type HandlerFn<U> = (input: HandlerInput<U>) => BotResponse | PromiseLike<BotResponse>;
131
+ interface MessageHelpers {
132
+ command: (cmd: string, handler: HandlerFn<Message>) => GuardedHandler<Message>;
133
+ text: (handler: HandlerFn<Message>) => GuardedHandler<Message>;
134
+ photo: (handler: HandlerFn<Message>) => GuardedHandler<Message>;
135
+ document: (handler: HandlerFn<Message>) => GuardedHandler<Message>;
136
+ sticker: (handler: HandlerFn<Message>) => GuardedHandler<Message>;
137
+ fallback: (handler: HandlerFn<Message>) => GuardedHandler<Message>;
138
+ }
139
+ interface CallbackQueryHelpers {
140
+ data: (pattern: string | RegExp, handler: HandlerFn<CallbackQuery>) => GuardedHandler<CallbackQuery>;
141
+ fallback: (handler: HandlerFn<CallbackQuery>) => GuardedHandler<CallbackQuery>;
142
+ }
143
+ interface InlineQueryHelpers {
144
+ query: (pattern: string | RegExp, handler: HandlerFn<InlineQuery>) => GuardedHandler<InlineQuery>;
145
+ fallback: (handler: HandlerFn<InlineQuery>) => GuardedHandler<InlineQuery>;
146
+ }
147
+ interface GenericHelpers<U> {
148
+ fallback: (handler: HandlerFn<U>) => GuardedHandler<U>;
149
+ }
150
+ type HandlerRegistration<U, H> = ((helpers: H) => GuardedHandler<U>[]) | GuardedHandler<U>[];
151
+ interface BotRunConfig {
152
+ bot_token: string;
153
+ poll?: Partial<PollSettings>;
154
+ onUpdate?: (update: Update) => void;
155
+ onHandleResult?: (result: HandleResult) => void;
156
+ logger?: BotLogger;
157
+ }
158
+ interface BotWebhookConfig {
159
+ bot_token: string;
160
+ onHandleResult?: (result: HandleResult) => void;
161
+ logger?: BotLogger;
162
+ }
163
+ interface Bot {
164
+ onMessage(input: HandlerRegistration<Message, MessageHelpers>): Bot;
165
+ onEditedMessage(input: HandlerRegistration<Message, GenericHelpers<Message>>): Bot;
166
+ onChannelPost(input: HandlerRegistration<Message, GenericHelpers<Message>>): Bot;
167
+ onEditedChannelPost(input: HandlerRegistration<Message, GenericHelpers<Message>>): Bot;
168
+ onBusinessMessage(input: HandlerRegistration<Message, GenericHelpers<Message>>): Bot;
169
+ onEditedBusinessMessage(input: HandlerRegistration<Message, GenericHelpers<Message>>): Bot;
170
+ onCallbackQuery(input: HandlerRegistration<CallbackQuery, CallbackQueryHelpers>): Bot;
171
+ onInlineQuery(input: HandlerRegistration<InlineQuery, InlineQueryHelpers>): Bot;
172
+ on<K extends AvailableUpdateTypes>(type: K, input: HandlerRegistration<NonNullable<Update[K]>, GenericHelpers<NonNullable<Update[K]>>>): Bot;
173
+ run(config: BotRunConfig): Promise<BotInstance>;
174
+ webhook(config: BotWebhookConfig): WebhookHandler;
175
+ }
176
+ declare function createBot(): Bot;
148
177
 
149
- export { type AvailableUpdateTypes, BatchUpdateResult, type BotBatchMode, type BotContext, type BotInstance, type BotMode, BotPollSettings, BotPollSettingsTag, BotResponse, BotRunService, type BotSingleMode, BotTgClientTag, BotUpdateHandlersTag, type BotUpdatesHandlers, type ExtractedUpdate, type GuardedHandler, type HandleBatchUpdateFunction, HandleUpdateError, type HandleUpdateFunction, type HandlerInput, type PollSettings, type RunBotInput, type RunBotInputBatch, type RunBotInputSingle, type UpdateHandler, type WebhookBotConfig, type WebhookHandler, createBotContext, createWebhookHandler, defineBot, extractUpdate, handleEntireBatch, handleOneByOne, handleOneUpdate, handleUpdates, launchBot, runTgChatBot };
178
+ export { type Bot, type BotContext, type BotInstance, type BotLogger, BotResponse, type BotUpdatesHandlers, type GuardedHandler, type HandleResult, type HandleUpdateFunction, type HandlerInput, type PollSettings, type RunBotInput, type UpdateHandler, type WebhookBotConfig, type WebhookHandler, createBot, createBotContext, createWebhook, defineBot, extractUpdate, runBot };