@grom.js/effect-tg 0.3.1 → 0.4.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/Bot.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import type * as Effect from 'effect/Effect';
2
+ import type { BotApi } from './index.ts';
3
+ import * as Context from 'effect/Context';
4
+ export type Bot<E = never, R = never> = Effect.Effect<void, E, R | Update>;
5
+ declare const Update_base: Context.TagClass<Update, "@grom.js/effect-tg/Bot/Update", BotApi.Types.Update>;
6
+ export declare class Update extends Update_base {
7
+ }
8
+ export {};
9
+ //# sourceMappingURL=Bot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Bot.d.ts","sourceRoot":"","sources":["../src/Bot.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,MAAM,eAAe,CAAA;AAC5C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACxC,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AAEzC,MAAM,MAAM,GAAG,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC,GAAG,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,CAAA;;AAE1E,qBAAa,MAAO,SAAQ,WAGzB;CAAG"}
package/dist/Bot.js ADDED
@@ -0,0 +1,3 @@
1
+ import * as Context from 'effect/Context';
2
+ export class Update extends Context.Tag('@grom.js/effect-tg/Bot/Update')() {
3
+ }
@@ -0,0 +1,6 @@
1
+ import type { Bot } from './Bot.ts';
2
+ export interface Middleware {
3
+ <E, R>(self: Bot<E, R>): Bot<any, any>;
4
+ }
5
+ export declare const make: <M extends Middleware>(middleware: M) => M;
6
+ //# sourceMappingURL=Middleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Middleware.d.ts","sourceRoot":"","sources":["../src/Middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA;AAEnC,MAAM,WAAW,UAAU;IACzB,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;CACvC;AAED,eAAO,MAAM,IAAI,EAAE,CAAC,CAAC,SAAS,UAAU,EAAE,UAAU,EAAE,CAAC,KAAK,CAAU,CAAA"}
@@ -0,0 +1 @@
1
+ export const make = m => m;
@@ -0,0 +1,18 @@
1
+ import type * as Effect from 'effect/Effect';
2
+ import type { Bot, Update } from './Bot.ts';
3
+ /**
4
+ * Runner runs a bot using long polling.
5
+ */
6
+ export interface Runner<E = never, R = never> {
7
+ readonly run: {
8
+ <E1, R1>(bot: Bot<E1, R1>): Effect.Effect<never, E | E1, R | Exclude<R1, Update>>;
9
+ };
10
+ }
11
+ /**
12
+ * Creates a simple runner that fetches updates by calling `BotApi.getUpdates`
13
+ * method and handles them one by one.
14
+ */
15
+ export declare const makeSimple: (options: void | {
16
+ allowedUpdates?: string[];
17
+ }) => Runner<import("./BotApi.ts").BotApiError | import("./BotApiTransport.ts").BotApiTransportError, import("./BotApi.ts").BotApi>;
18
+ //# sourceMappingURL=Runner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Runner.d.ts","sourceRoot":"","sources":["../src/Runner.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,MAAM,eAAe,CAAA;AAC5C,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAG3C;;GAEG;AACH,MAAM,WAAW,MAAM,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC,GAAG,KAAK;IAC1C,QAAQ,CAAC,GAAG,EAAE;QACZ,CAAC,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,CACvC,KAAK,EACL,CAAC,GAAG,EAAE,EACN,CAAC,GAAG,OAAO,CAAC,EAAE,EAAE,MAAM,CAAC,CACxB,CAAA;KACF,CAAA;CACF;AAED;;;GAGG;AACH,eAAO,MAAM,UAAU;kBAFnB,CAAC;mIAEwC,CAAA"}
package/dist/Runner.js ADDED
@@ -0,0 +1,6 @@
1
+ import * as internal from "./internal/runner.js";
2
+ /**
3
+ * Creates a simple runner that fetches updates by calling `BotApi.getUpdates`
4
+ * method and handles them one by one.
5
+ */
6
+ export const makeSimple = internal.makeSimple;
package/dist/Text.d.ts CHANGED
@@ -1,9 +1,10 @@
1
+ import type { TgxElement } from '@grom.js/tgx/types';
1
2
  import type { MethodParams, Types } from './BotApi.ts';
2
3
  import * as Data from 'effect/Data';
3
4
  /**
4
5
  * Formatted text.
5
6
  */
6
- export type Text = Plain | Html | Markdown;
7
+ export type Text = Plain | Html | Markdown | Tgx;
7
8
  export declare class Plain extends Data.Class<{
8
9
  text: string;
9
10
  entities?: Array<Types.MessageEntity>;
@@ -20,6 +21,11 @@ export declare class Markdown extends Data.Class<{
20
21
  }> {
21
22
  sendParams(): SendParams;
22
23
  }
24
+ export declare class Tgx extends Data.Class<{
25
+ tgx: TgxElement;
26
+ }> {
27
+ sendParams(): SendParams;
28
+ }
23
29
  type SendParams = Pick<MethodParams['sendMessage'], 'text' | 'entities' | 'parse_mode'>;
24
30
  export {};
25
31
  //# sourceMappingURL=Text.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Text.d.ts","sourceRoot":"","sources":["../src/Text.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AACtD,OAAO,KAAK,IAAI,MAAM,aAAa,CAAA;AAEnC;;GAEG;AACH,MAAM,MAAM,IAAI,GACV,KAAK,GACL,IAAI,GACJ,QAAQ,CAAA;AAEd,qBAAa,KAAM,SAAQ,IAAI,CAAC,KAAK,CAAC;IACpC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IAAI,UAAU;CAGzB;AAED,qBAAa,GAAI,SAAQ,IAAI,CAAC,KAAK,CAAC;IAAE,GAAG,EAAE,UAAU,CAAA;CAAE,CAAC;IACtD,UAAU,IAAI,UAAU;CAGzB;AAED,KAAK,UAAU,GAAG,IAAI,CAAC,YAAY,CAAC,aAAa,CAAC,EAAE,MAAM,GAAG,UAAU,GAAG,YAAY,CAAC,CAAA"}
package/dist/Text.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { html as tgxToHtml } from '@grom.js/tgx';
1
2
  import * as Data from 'effect/Data';
2
3
  export class Plain extends Data.Class {
3
4
  sendParams() {
@@ -14,3 +15,8 @@ export class Markdown extends Data.Class {
14
15
  return { text: this.markdown, parse_mode: 'MarkdownV2' };
15
16
  }
16
17
  }
18
+ export class Tgx extends Data.Class {
19
+ sendParams() {
20
+ return { text: tgxToHtml(this.tgx), parse_mode: 'HTML' };
21
+ }
22
+ }
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * as Bot from './Bot.ts';
1
2
  export * as BotApi from './BotApi.ts';
2
3
  export * as BotApiTransport from './BotApiTransport.ts';
3
4
  export * as BotApiWebhook from './BotApiWebhook.ts';
@@ -5,6 +6,8 @@ export * as Chat from './Chat.ts';
5
6
  export * as Content from './Content.ts';
6
7
  export * as InputFile from './InputFile.ts';
7
8
  export * as LinkPreview from './LinkPreview.ts';
9
+ export * as Middleware from './Middleware.ts';
10
+ export * as Runner from './Runner.ts';
8
11
  export * as Send from './Send.ts';
9
12
  export * as Text from './Text.ts';
10
13
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC,OAAO,KAAK,eAAe,MAAM,sBAAsB,CAAA;AACvD,OAAO,KAAK,aAAa,MAAM,oBAAoB,CAAA;AACnD,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AACjC,OAAO,KAAK,OAAO,MAAM,cAAc,CAAA;AACvC,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAA;AAC3C,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAC/C,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AACjC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,UAAU,CAAA;AAC/B,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC,OAAO,KAAK,eAAe,MAAM,sBAAsB,CAAA;AACvD,OAAO,KAAK,aAAa,MAAM,oBAAoB,CAAA;AACnD,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AACjC,OAAO,KAAK,OAAO,MAAM,cAAc,CAAA;AACvC,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAA;AAC3C,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAC/C,OAAO,KAAK,UAAU,MAAM,iBAAiB,CAAA;AAC7C,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AACjC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA"}
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ export * as Bot from "./Bot.js";
1
2
  export * as BotApi from "./BotApi.js";
2
3
  export * as BotApiTransport from "./BotApiTransport.js";
3
4
  export * as BotApiWebhook from "./BotApiWebhook.js";
@@ -5,5 +6,7 @@ export * as Chat from "./Chat.js";
5
6
  export * as Content from "./Content.js";
6
7
  export * as InputFile from "./InputFile.js";
7
8
  export * as LinkPreview from "./LinkPreview.js";
9
+ export * as Middleware from "./Middleware.js";
10
+ export * as Runner from "./Runner.js";
8
11
  export * as Send from "./Send.js";
9
12
  export * as Text from "./Text.js";
@@ -0,0 +1,8 @@
1
+ import type { BotApiError } from '../BotApi.ts';
2
+ import type { BotApiTransportError } from '../BotApiTransport.ts';
3
+ import type { Runner } from '../Runner.ts';
4
+ import { BotApi } from '../BotApi.ts';
5
+ export declare const makeSimple: (options: void | {
6
+ allowedUpdates?: string[];
7
+ }) => Runner<BotApiError | BotApiTransportError, BotApi>;
8
+ //# sourceMappingURL=runner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runner.d.ts","sourceRoot":"","sources":["../../src/internal/runner.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAC/C,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAA;AACjE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAM1C,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAErC,eAAO,MAAM,UAAU,GAAI,SAAS,IAAI,GAAG;IACzC,cAAc,CAAC,EAAE,MAAM,EAAE,CAAA;CAC1B,KAAG,MAAM,CAAC,WAAW,GAAG,oBAAoB,EAAE,MAAM,CA+CnD,CAAA"}
@@ -0,0 +1,34 @@
1
+ import * as Duration from 'effect/Duration';
2
+ import * as Effect from 'effect/Effect';
3
+ import * as Match from 'effect/Match';
4
+ import * as Schedule from 'effect/Schedule';
5
+ import { Update } from "../Bot.js";
6
+ import { BotApi } from "../BotApi.js";
7
+ export const makeSimple = (options) => ({
8
+ run: bot => Effect.gen(function* () {
9
+ const { allowedUpdates } = options ?? {};
10
+ const api = yield* BotApi;
11
+ let lastUpdateId;
12
+ while (true) {
13
+ const [update] = yield* api
14
+ .getUpdates({
15
+ offset: lastUpdateId == null ? undefined : lastUpdateId + 1,
16
+ allowed_updates: allowedUpdates,
17
+ timeout: 30,
18
+ limit: 1,
19
+ })
20
+ .pipe(Effect.retry({
21
+ schedule: Schedule.spaced(Duration.seconds(3)),
22
+ while: error => Match.value(error).pipe(Match.tag('@grom.js/effect-tg/BotApiError', error => Effect.succeed(error.code >= 500 || (error.code !== 401
23
+ && error.code !== 403
24
+ && error.code !== 404))), Match.tag('@grom.js/effect-tg/BotApiTransportError', () => Effect.succeed(true)), Match.exhaustive),
25
+ }));
26
+ if (update) {
27
+ yield* Effect
28
+ .provideService(bot, Update, update)
29
+ .pipe(Effect.catchAll(error => (Effect.logError('Error in bot:', error))));
30
+ lastUpdateId = update.update_id;
31
+ }
32
+ }
33
+ }),
34
+ });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@grom.js/effect-tg",
3
3
  "type": "module",
4
- "version": "0.3.1",
4
+ "version": "0.4.0",
5
5
  "description": "Effectful library for crafting Telegram bots.",
6
6
  "author": {
7
7
  "name": "Vladislav Deryabkin",
@@ -31,6 +31,7 @@
31
31
  },
32
32
  "peerDependencies": {
33
33
  "@effect/platform": "^0.93.1",
34
+ "@grom.js/tgx": "^0.5.2",
34
35
  "effect": "^3.19.3"
35
36
  },
36
37
  "devDependencies": {