@effect-ak/tg-bot-client 0.2.0 → 0.2.2

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.
Files changed (80) hide show
  1. package/dist/index.d.ts +2821 -0
  2. package/dist/index.js +462 -0
  3. package/dist/index.mjs +418 -0
  4. package/package.json +15 -16
  5. package/readme.md +1 -1
  6. package/dist/cjs/bot/factory/_service.js +0 -20
  7. package/dist/cjs/bot/factory/client-config.js +0 -29
  8. package/dist/cjs/bot/factory/make-bot.js +0 -20
  9. package/dist/cjs/bot/message-handler/_service.js +0 -12
  10. package/dist/cjs/bot/message-handler/types.js +0 -5
  11. package/dist/cjs/bot/message-handler/utils.js +0 -19
  12. package/dist/cjs/bot/run.js +0 -10
  13. package/dist/cjs/bot/update-poller/_service.js +0 -39
  14. package/dist/cjs/bot/update-poller/errors.js +0 -5
  15. package/dist/cjs/bot/update-poller/fetch-updates.js +0 -70
  16. package/dist/cjs/bot/update-poller/poll-and-handle.js +0 -50
  17. package/dist/cjs/bot/update-poller/settings.js +0 -23
  18. package/dist/cjs/client/_client.js +0 -25
  19. package/dist/cjs/client/config.js +0 -17
  20. package/dist/cjs/client/errors.js +0 -18
  21. package/dist/cjs/client/execute-request/_service.js +0 -20
  22. package/dist/cjs/client/execute-request/execute.js +0 -59
  23. package/dist/cjs/client/execute-request/payload.js +0 -24
  24. package/dist/cjs/client/file/_service.js +0 -22
  25. package/dist/cjs/client/file/get-file.js +0 -38
  26. package/dist/cjs/client/guards.js +0 -12
  27. package/dist/cjs/const.js +0 -20
  28. package/dist/cjs/index.js +0 -60
  29. package/dist/cjs/specification/api.js +0 -5
  30. package/dist/cjs/specification/types.js +0 -5
  31. package/dist/dts/bot/factory/_service.d.ts +0 -37
  32. package/dist/dts/bot/factory/client-config.d.ts +0 -3
  33. package/dist/dts/bot/factory/make-bot.d.ts +0 -7
  34. package/dist/dts/bot/message-handler/_service.d.ts +0 -6
  35. package/dist/dts/bot/message-handler/types.d.ts +0 -16
  36. package/dist/dts/bot/message-handler/utils.d.ts +0 -6
  37. package/dist/dts/bot/run.d.ts +0 -7
  38. package/dist/dts/bot/update-poller/_service.d.ts +0 -21
  39. package/dist/dts/bot/update-poller/errors.d.ts +0 -1
  40. package/dist/dts/bot/update-poller/fetch-updates.d.ts +0 -17
  41. package/dist/dts/bot/update-poller/poll-and-handle.d.ts +0 -16
  42. package/dist/dts/bot/update-poller/settings.d.ts +0 -6
  43. package/dist/dts/client/_client.d.ts +0 -8
  44. package/dist/dts/client/config.d.ts +0 -8
  45. package/dist/dts/client/errors.d.ts +0 -23
  46. package/dist/dts/client/execute-request/_service.d.ts +0 -15
  47. package/dist/dts/client/execute-request/execute.d.ts +0 -5
  48. package/dist/dts/client/execute-request/payload.d.ts +0 -1
  49. package/dist/dts/client/file/_service.d.ts +0 -16
  50. package/dist/dts/client/file/get-file.d.ts +0 -5
  51. package/dist/dts/client/guards.d.ts +0 -17
  52. package/dist/dts/const.d.ts +0 -12
  53. package/dist/dts/index.d.ts +0 -5
  54. package/dist/dts/specification/api.d.ts +0 -1030
  55. package/dist/dts/specification/types.d.ts +0 -1663
  56. package/dist/esm/bot/factory/_service.js +0 -12
  57. package/dist/esm/bot/factory/client-config.js +0 -20
  58. package/dist/esm/bot/factory/make-bot.js +0 -9
  59. package/dist/esm/bot/message-handler/_service.js +0 -4
  60. package/dist/esm/bot/message-handler/types.js +0 -1
  61. package/dist/esm/bot/message-handler/utils.js +0 -12
  62. package/dist/esm/bot/run.js +0 -6
  63. package/dist/esm/bot/update-poller/_service.js +0 -31
  64. package/dist/esm/bot/update-poller/errors.js +0 -1
  65. package/dist/esm/bot/update-poller/fetch-updates.js +0 -49
  66. package/dist/esm/bot/update-poller/poll-and-handle.js +0 -37
  67. package/dist/esm/bot/update-poller/settings.js +0 -15
  68. package/dist/esm/client/_client.js +0 -16
  69. package/dist/esm/client/config.js +0 -8
  70. package/dist/esm/client/errors.js +0 -9
  71. package/dist/esm/client/execute-request/_service.js +0 -12
  72. package/dist/esm/client/execute-request/execute.js +0 -37
  73. package/dist/esm/client/execute-request/payload.js +0 -21
  74. package/dist/esm/client/file/_service.js +0 -14
  75. package/dist/esm/client/file/get-file.js +0 -24
  76. package/dist/esm/client/guards.js +0 -7
  77. package/dist/esm/const.js +0 -13
  78. package/dist/esm/index.js +0 -5
  79. package/dist/esm/specification/api.js +0 -1
  80. package/dist/esm/specification/types.js +0 -1
@@ -1,12 +0,0 @@
1
- import * as Micro from "effect/Micro";
2
- import * as Context from "effect/Context";
3
- import { TgBotClientConfig } from "../../client/config.js";
4
- import { makeClientConfigFrom } from "./client-config.js";
5
- import { makeBot } from "./make-bot.js";
6
- export class BotFactoryService extends Context.Tag("BotFactoryService")() {
7
- }
8
- ;
9
- export const BotFactoryServiceDefault = {
10
- makeBot,
11
- runBot: (input) => makeBot(input).pipe(Micro.provideServiceEffect(TgBotClientConfig, makeClientConfigFrom(input)))
12
- };
@@ -1,20 +0,0 @@
1
- import * as Micro from "effect/Micro";
2
- import { readFileSync } from "fs";
3
- import { makeTgBotClientConfig } from "../../client/config.js";
4
- import { isTgBotClientSettingsInput } from "../../client/guards.js";
5
- export const makeClientConfigFrom = (input) => Micro.gen(function* () {
6
- if (input.type == "config") {
7
- return makeTgBotClientConfig(input);
8
- }
9
- const config = yield* Micro.try({
10
- try: () => JSON.parse(readFileSync("config.json").toString("utf-8")),
11
- catch: error => {
12
- console.warn(error);
13
- return "ReadingConfigError";
14
- }
15
- });
16
- if (!isTgBotClientSettingsInput(config)) {
17
- return yield* Micro.fail("InvalidConfig");
18
- }
19
- return makeTgBotClientConfig(config);
20
- });
@@ -1,9 +0,0 @@
1
- import * as Micro from "effect/Micro";
2
- import { BotUpdatePollerService, BotUpdatesPollerServiceDefault } from "../../bot/update-poller/_service.js";
3
- export const makeBot = (messageHandler) => Micro.gen(function* () {
4
- const { runBot } = yield* Micro.service(BotUpdatePollerService);
5
- return yield* runBot(messageHandler);
6
- }).pipe(Micro.provideServiceEffect(BotUpdatePollerService, BotUpdatesPollerServiceDefault), Micro.tapError(error => {
7
- console.error(error);
8
- return Micro.void;
9
- }));
@@ -1,4 +0,0 @@
1
- import * as Context from "effect/Context";
2
- export class BotMessageHandler extends Context.Tag("BotMessageHandler")() {
3
- }
4
- ;
@@ -1 +0,0 @@
1
- export {};
@@ -1,12 +0,0 @@
1
- export const extractUpdate = (input) => {
2
- for (const [field, value] of Object.entries(input)) {
3
- if (field == "update_id") {
4
- continue;
5
- }
6
- return {
7
- type: field,
8
- ...value
9
- };
10
- }
11
- return undefined;
12
- };
@@ -1,6 +0,0 @@
1
- import { Micro } from "effect";
2
- import { BotFactoryServiceDefault } from "./factory/_service.js";
3
- export const runTgChatBot = (input) => BotFactoryServiceDefault
4
- .runBot(input)
5
- .pipe(Micro.runPromise)
6
- .finally(() => "Telegram chat bot has been shutdown");
@@ -1,31 +0,0 @@
1
- import * as Micro from "effect/Micro";
2
- import * as Context from "effect/Context";
3
- import { ClientExecuteRequestService, ClientExecuteRequestServiceDefault } from "../../client/execute-request/_service.js";
4
- import { pollAndHandle } from "./poll-and-handle.js";
5
- export class BotUpdatePollerService extends Context.Tag("BotUpdatePollerService")() {
6
- }
7
- ;
8
- export const BotUpdatesPollerServiceDefault = Micro.gen(function* () {
9
- const state = {
10
- isActive: false,
11
- };
12
- const client = yield* Micro.service(ClientExecuteRequestService);
13
- const runBot = (messageHandler) => Micro.gen(function* () {
14
- if (state.isActive) {
15
- return yield* Micro.fail("AlreadyRunning");
16
- }
17
- const fiber = yield* pollAndHandle({
18
- settings: messageHandler,
19
- execute: client.execute
20
- }).pipe(Micro.forkDaemon);
21
- fiber.addObserver((exit) => {
22
- console.log("bot's fiber has been closed", exit);
23
- state.isActive = false;
24
- });
25
- console.log("Reading bot's updates...");
26
- return fiber;
27
- });
28
- return {
29
- runBot
30
- };
31
- }).pipe(Micro.provideServiceEffect(ClientExecuteRequestService, ClientExecuteRequestServiceDefault));
@@ -1 +0,0 @@
1
- export {};
@@ -1,49 +0,0 @@
1
- import * as Micro from "effect/Micro";
2
- import { extractUpdate } from "../../bot/message-handler/utils.js";
3
- export const fetchUpdates = ({ state, settings, execute, handlers }) => Micro.gen(function* () {
4
- const updateId = state.lastUpdateId;
5
- console.info("getting updates", state);
6
- const updates = yield* execute("get_updates", {
7
- ...settings,
8
- ...(updateId ? { offset: updateId } : undefined)
9
- }).pipe(Micro.andThen(_ => _.sort(_ => _.update_id)));
10
- let lastSuccessId = undefined;
11
- let hasError = false;
12
- for (const updateObject of updates) {
13
- const update = extractUpdate(updateObject);
14
- if (!update) {
15
- console.warn("Unknown update", update);
16
- hasError = true;
17
- break;
18
- }
19
- const handler = handlers[`on_${update.type}`];
20
- if (!handler) {
21
- console.warn("Handler for update not defined", update);
22
- hasError = true;
23
- break;
24
- }
25
- const handleResult = handler(update);
26
- if ("chat" in update) {
27
- const response = yield* execute(`send_${handleResult.type}`, {
28
- ...handleResult,
29
- chat_id: update.chat.id
30
- });
31
- console.log("bot response", response);
32
- }
33
- if (!handleResult) {
34
- hasError = true;
35
- console.log(handleResult);
36
- break;
37
- }
38
- ;
39
- lastSuccessId = updateObject.update_id;
40
- }
41
- if (hasError && lastSuccessId) {
42
- const resp = //commit successfully handled messages
43
- yield* execute("get_updates", {
44
- offset: lastSuccessId,
45
- limit: 0
46
- });
47
- }
48
- return { updates, lastSuccessId, hasError };
49
- });
@@ -1,37 +0,0 @@
1
- import * as Micro from "effect/Micro";
2
- import { makeSettingsFrom } from "./settings.js";
3
- import { fetchUpdates } from "./fetch-updates.js";
4
- export const pollAndHandle = (input) => {
5
- const state = {
6
- lastUpdateId: undefined,
7
- emptyResponses: 0
8
- };
9
- const settings = makeSettingsFrom(input.settings);
10
- return Micro.delay(1000)(fetchUpdates({
11
- state, settings,
12
- execute: input.execute,
13
- handlers: input.settings,
14
- })).pipe(Micro.repeat({
15
- while: ({ updates, lastSuccessId, hasError }) => {
16
- if (hasError) {
17
- console.warn("error in handler, quitting");
18
- return false;
19
- }
20
- if (updates.length == 0) {
21
- state.emptyResponses += 1;
22
- if (state.emptyResponses > 200) {
23
- console.info("too many empty responses, quitting");
24
- return false;
25
- }
26
- }
27
- else {
28
- state.emptyResponses = 0;
29
- }
30
- ;
31
- if (lastSuccessId) {
32
- state.lastUpdateId = lastSuccessId + 1;
33
- }
34
- return true;
35
- }
36
- }));
37
- };
@@ -1,15 +0,0 @@
1
- export const makeSettingsFrom = (input) => {
2
- let limit = input.batch_size ?? 10;
3
- let timeout = input.timeout ?? 10;
4
- if (limit < 10 || limit > 100) {
5
- console.warn("Wrong limit, must be in [10..100], using 10 instead");
6
- limit = 10;
7
- }
8
- if (timeout < 2 || timeout > 10) {
9
- console.warn("Wrong timeout, must be in [2..10], using 2 instead");
10
- limit = 10;
11
- }
12
- return {
13
- limit, timeout
14
- };
15
- };
@@ -1,16 +0,0 @@
1
- import * as Micro from "effect/Micro";
2
- import { makeTgBotClientConfig, TgBotClientConfig } from "./config.js";
3
- import { ClientExecuteRequestService, ClientExecuteRequestServiceDefault } from "./execute-request/_service.js";
4
- import { ClientFileService, ClientFileServiceDefault } from "./file/_service.js";
5
- export const makeTgBotClient = (input) => {
6
- const config = makeTgBotClientConfig(input);
7
- const client = Micro.gen(function* () {
8
- const execute = yield* Micro.service(ClientExecuteRequestService);
9
- const file = yield* Micro.service(ClientFileService);
10
- return {
11
- execute: (method, input) => execute.execute(method, input).pipe(Micro.runPromise),
12
- getFile: (input) => file.getFile(input).pipe(Micro.runPromise)
13
- };
14
- }).pipe(Micro.provideServiceEffect(ClientExecuteRequestService, ClientExecuteRequestServiceDefault), Micro.provideServiceEffect(ClientFileService, ClientFileServiceDefault), Micro.provideService(TgBotClientConfig, config), Micro.runSync);
15
- return client;
16
- };
@@ -1,8 +0,0 @@
1
- import * as Context from "effect/Context";
2
- import { defaultBaseUrl } from "../const.js";
3
- export const makeTgBotClientConfig = (input) => TgBotClientConfig.of({
4
- ...input,
5
- ["base-url"]: input["base-url"] ?? defaultBaseUrl
6
- });
7
- export class TgBotClientConfig extends Context.Tag("TgBotClientConfig")() {
8
- }
@@ -1,9 +0,0 @@
1
- import * as Data from "effect/Data";
2
- export class TgBotClientError extends Data.TaggedError("TgBotClientError") {
3
- static missingSuccess = new TgBotClientError({
4
- reason: {
5
- type: "ClientInternalError",
6
- cause: "Expected 'success' to be defined"
7
- },
8
- });
9
- }
@@ -1,12 +0,0 @@
1
- import * as Micro from "effect/Micro";
2
- import * as Context from "effect/Context";
3
- import { TgBotClientConfig } from "../config.js";
4
- import { execute } from "./execute.js";
5
- export class ClientExecuteRequestService extends Context.Tag("ClientExecuteRequestService")() {
6
- }
7
- export const ClientExecuteRequestServiceDefault = Micro.gen(function* () {
8
- const config = yield* Micro.service(TgBotClientConfig);
9
- return {
10
- execute: (method, input) => execute(config, method, input)
11
- };
12
- });
@@ -1,37 +0,0 @@
1
- import * as Micro from "effect/Micro";
2
- import * as String from "effect/String";
3
- import { TgBotClientError } from "../errors.js";
4
- import { makePayload } from "./payload.js";
5
- import { isTgBotApiResponse } from "../guards.js";
6
- export const execute = (config, method, input) => Micro.gen(function* () {
7
- const httpResponse = yield* Micro.tryPromise({
8
- try: () => fetch(`${config["base-url"]}/bot${config["bot-token"]}/${String.snakeToCamel(method)}`, {
9
- body: makePayload(input) ?? null,
10
- method: "POST",
11
- }),
12
- catch: cause => new TgBotClientError({
13
- reason: { type: "ClientInternalError", cause }
14
- })
15
- });
16
- const response = yield* Micro.tryPromise({
17
- try: () => httpResponse.json(),
18
- catch: () => new TgBotClientError({
19
- reason: { type: "UnexpectedResponse", response: httpResponse }
20
- })
21
- });
22
- if (!isTgBotApiResponse(response)) {
23
- return yield* Micro.fail(new TgBotClientError({
24
- reason: { type: "UnexpectedResponse", response }
25
- }));
26
- }
27
- if (!httpResponse.ok) {
28
- return yield* Micro.fail(new TgBotClientError({
29
- reason: {
30
- type: "NotOkResponse",
31
- ...(response.error_code ? { errorCode: response.error_code } : undefined),
32
- ...(response.description ? { details: response.description } : undefined)
33
- }
34
- }));
35
- }
36
- return response.result;
37
- });
@@ -1,21 +0,0 @@
1
- import { isFileContent } from "../guards.js";
2
- export const makePayload = (body) => {
3
- const entries = Object.entries(body);
4
- if (entries.length == 0)
5
- return undefined;
6
- const result = new FormData();
7
- for (const [key, value] of entries) {
8
- if (!value)
9
- continue;
10
- if (typeof value != "object") {
11
- result.append(key, `${value}`);
12
- }
13
- else if (isFileContent(value)) {
14
- result.append(key, new Blob([value.file_content]), value.file_name);
15
- }
16
- else {
17
- result.append(key, JSON.stringify(value));
18
- }
19
- }
20
- return result;
21
- };
@@ -1,14 +0,0 @@
1
- import * as Micro from "effect/Micro";
2
- import * as Context from "effect/Context";
3
- import { TgBotClientConfig } from "../config.js";
4
- import { ClientExecuteRequestService, ClientExecuteRequestServiceDefault } from "../execute-request/_service.js";
5
- import { getFile } from "./get-file.js";
6
- export class ClientFileService extends Context.Tag("ClientFileService")() {
7
- }
8
- export const ClientFileServiceDefault = Micro.gen(function* () {
9
- const config = yield* Micro.service(TgBotClientConfig);
10
- const execute = yield* Micro.service(ClientExecuteRequestService);
11
- return {
12
- getFile: (input) => getFile(input.file_id, config, execute)
13
- };
14
- }).pipe(Micro.provideServiceEffect(ClientExecuteRequestService, ClientExecuteRequestServiceDefault));
@@ -1,24 +0,0 @@
1
- import * as Micro from "effect/Micro";
2
- import { TgBotClientError } from "../errors.js";
3
- export const getFile = (fileId, config, execute) => Micro.gen(function* () {
4
- const response = yield* execute.execute("get_file", { file_id: fileId });
5
- const file_path = response.file_path;
6
- if (!file_path || file_path.length == 0) {
7
- return yield* Micro.fail(new TgBotClientError({
8
- reason: {
9
- type: "UnableToGetFile",
10
- cause: "File path not defined"
11
- }
12
- }));
13
- }
14
- const file_name = file_path.replaceAll("/", "-");
15
- const url = `${config["base-url"]}/file/bot${config["bot-token"]}/${file_path}`;
16
- const fileContent = yield* Micro.tryPromise({
17
- try: () => fetch(url).then(_ => _.arrayBuffer()),
18
- catch: cause => new TgBotClientError({
19
- reason: { type: "UnableToGetFile", cause }
20
- })
21
- });
22
- const file = new File([new Uint8Array(fileContent)], file_name);
23
- return file;
24
- });
@@ -1,7 +0,0 @@
1
- export const isFileContent = (input) => (typeof input == "object" && input != null) &&
2
- ("file_content" in input && input.file_content instanceof Uint8Array) &&
3
- ("file_name" in input && typeof input.file_name == "string");
4
- export const isTgBotApiResponse = (input) => (typeof input == "object" && input != null) &&
5
- ("ok" in input && typeof input.ok == "boolean");
6
- export const isTgBotClientSettingsInput = (input) => (typeof input == "object" && input != null) &&
7
- ("bot-token" in input && typeof input["bot-token"] == "string");
package/dist/esm/const.js DELETED
@@ -1,13 +0,0 @@
1
- export const defaultBaseUrl = "https://api.telegram.org";
2
- export const MESSAGE_EFFECTS = {
3
- "🔥": "5104841245755180586",
4
- "👍": "5107584321108051014",
5
- "👎": "5104858069142078462",
6
- "❤️": "5159385139981059251",
7
- "🎉": "5046509860389126442",
8
- "💩": "5046589136895476101"
9
- };
10
- export const messageEffectIdCodes = Object.keys(MESSAGE_EFFECTS);
11
- export const isMessageEffect = (input) => {
12
- return typeof input === "string" && input in MESSAGE_EFFECTS;
13
- };
package/dist/esm/index.js DELETED
@@ -1,5 +0,0 @@
1
- export * from "./client/_client.js";
2
- export * from "./bot/run.js";
3
- export * from "./specification/api.js";
4
- export * from "./specification/types.js";
5
- export * from "./const.js";
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};