@effect-ak/tg-bot 1.2.0 → 1.2.1
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.cjs +87 -0
- package/dist/index.d.ts +10 -1
- package/dist/index.js +86 -0
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -29,6 +29,7 @@ __export(index_exports, {
|
|
|
29
29
|
BotUpdateHandlersTag: () => BotUpdateHandlersTag,
|
|
30
30
|
HandleUpdateError: () => HandleUpdateError,
|
|
31
31
|
createBotContext: () => createBotContext,
|
|
32
|
+
createWebhookHandler: () => createWebhookHandler,
|
|
32
33
|
defineBot: () => defineBot,
|
|
33
34
|
extractUpdate: () => extractUpdate,
|
|
34
35
|
handleEntireBatch: () => handleEntireBatch,
|
|
@@ -3961,6 +3962,91 @@ var defineBot = (input) => {
|
|
|
3961
3962
|
console.warn("No handlers are defined for bot");
|
|
3962
3963
|
return input;
|
|
3963
3964
|
};
|
|
3965
|
+
|
|
3966
|
+
// src/webhook.ts
|
|
3967
|
+
var import_tg_bot_client3 = require("@effect-ak/tg-bot-client");
|
|
3968
|
+
var isGuardedHandler2 = (handler) => typeof handler === "object" && handler !== null && "handle" in handler;
|
|
3969
|
+
var executeSingleGuard2 = async (guard, update, ctx) => {
|
|
3970
|
+
const input = { update, ctx };
|
|
3971
|
+
if (guard.match) {
|
|
3972
|
+
const matched = await guard.match(input);
|
|
3973
|
+
if (!matched) return null;
|
|
3974
|
+
}
|
|
3975
|
+
return await guard.handle(input);
|
|
3976
|
+
};
|
|
3977
|
+
var executeGuards2 = async (guards, update, ctx) => {
|
|
3978
|
+
for (const guard of guards) {
|
|
3979
|
+
const result = await executeSingleGuard2(guard, update, ctx);
|
|
3980
|
+
if (result !== null) return result;
|
|
3981
|
+
}
|
|
3982
|
+
return BotResponse.ignore;
|
|
3983
|
+
};
|
|
3984
|
+
var executeHandler2 = async (handler, update, ctx) => {
|
|
3985
|
+
if (typeof handler === "function") {
|
|
3986
|
+
return await handler(update);
|
|
3987
|
+
}
|
|
3988
|
+
if (Array.isArray(handler)) {
|
|
3989
|
+
return await executeGuards2(handler, update, ctx);
|
|
3990
|
+
}
|
|
3991
|
+
if (isGuardedHandler2(handler)) {
|
|
3992
|
+
const result = await executeSingleGuard2(handler, update, ctx);
|
|
3993
|
+
return result ?? BotResponse.ignore;
|
|
3994
|
+
}
|
|
3995
|
+
return BotResponse.ignore;
|
|
3996
|
+
};
|
|
3997
|
+
var extractUpdate2 = (input) => {
|
|
3998
|
+
for (const [field, value] of Object.entries(input)) {
|
|
3999
|
+
if (field === "update_id") continue;
|
|
4000
|
+
return { type: field, ...value };
|
|
4001
|
+
}
|
|
4002
|
+
return void 0;
|
|
4003
|
+
};
|
|
4004
|
+
var processUpdate = async (updateObject, handlers, client) => {
|
|
4005
|
+
const update = extractUpdate2(updateObject);
|
|
4006
|
+
if (!update) {
|
|
4007
|
+
console.warn("Unknown update format", updateObject);
|
|
4008
|
+
return;
|
|
4009
|
+
}
|
|
4010
|
+
const handlerKey = `on_${update.type}`;
|
|
4011
|
+
const handler = handlers[handlerKey];
|
|
4012
|
+
if (!handler) {
|
|
4013
|
+
return;
|
|
4014
|
+
}
|
|
4015
|
+
const ctx = createBotContext(update);
|
|
4016
|
+
try {
|
|
4017
|
+
const result = await executeHandler2(handler, update, ctx);
|
|
4018
|
+
if (result.response && "chat" in update) {
|
|
4019
|
+
const responsePayload = result.response;
|
|
4020
|
+
await client.execute(`send_${responsePayload.type}`, {
|
|
4021
|
+
...responsePayload,
|
|
4022
|
+
chat_id: update.chat.id
|
|
4023
|
+
});
|
|
4024
|
+
}
|
|
4025
|
+
} catch (error) {
|
|
4026
|
+
console.error("Error handling update", {
|
|
4027
|
+
updateId: updateObject.update_id,
|
|
4028
|
+
error: error instanceof Error ? error.message : error
|
|
4029
|
+
});
|
|
4030
|
+
}
|
|
4031
|
+
};
|
|
4032
|
+
var createWebhookHandler = (config) => {
|
|
4033
|
+
const client = (0, import_tg_bot_client3.makeTgBotClient)({ bot_token: config.bot_token });
|
|
4034
|
+
const handleUpdate = async (update) => {
|
|
4035
|
+
await processUpdate(update, config, client);
|
|
4036
|
+
};
|
|
4037
|
+
const handler = async (request) => {
|
|
4038
|
+
try {
|
|
4039
|
+
const update = await request.json();
|
|
4040
|
+
await handleUpdate(update);
|
|
4041
|
+
return new Response("ok", { status: 200 });
|
|
4042
|
+
} catch (error) {
|
|
4043
|
+
console.error("Webhook error", error);
|
|
4044
|
+
return new Response("error", { status: 500 });
|
|
4045
|
+
}
|
|
4046
|
+
};
|
|
4047
|
+
handler.handleUpdate = handleUpdate;
|
|
4048
|
+
return handler;
|
|
4049
|
+
};
|
|
3964
4050
|
// Annotate the CommonJS export names for ESM import in node:
|
|
3965
4051
|
0 && (module.exports = {
|
|
3966
4052
|
BatchUpdateResult,
|
|
@@ -3972,6 +4058,7 @@ var defineBot = (input) => {
|
|
|
3972
4058
|
BotUpdateHandlersTag,
|
|
3973
4059
|
HandleUpdateError,
|
|
3974
4060
|
createBotContext,
|
|
4061
|
+
createWebhookHandler,
|
|
3975
4062
|
defineBot,
|
|
3976
4063
|
extractUpdate,
|
|
3977
4064
|
handleEntireBatch,
|
package/dist/index.d.ts
CHANGED
|
@@ -137,4 +137,13 @@ declare const runTgChatBot: (input: RunBotInput) => Promise<{
|
|
|
137
137
|
}>;
|
|
138
138
|
declare const defineBot: (input: BotUpdatesHandlers) => BotUpdatesHandlers;
|
|
139
139
|
|
|
140
|
-
|
|
140
|
+
interface WebhookBotConfig extends BotUpdatesHandlers {
|
|
141
|
+
bot_token: string;
|
|
142
|
+
}
|
|
143
|
+
interface WebhookHandler {
|
|
144
|
+
(request: Request): Promise<Response>;
|
|
145
|
+
handleUpdate: (update: Update) => Promise<void>;
|
|
146
|
+
}
|
|
147
|
+
declare const createWebhookHandler: (config: WebhookBotConfig) => WebhookHandler;
|
|
148
|
+
|
|
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 };
|
package/dist/index.js
CHANGED
|
@@ -3919,6 +3919,91 @@ var defineBot = (input) => {
|
|
|
3919
3919
|
console.warn("No handlers are defined for bot");
|
|
3920
3920
|
return input;
|
|
3921
3921
|
};
|
|
3922
|
+
|
|
3923
|
+
// src/webhook.ts
|
|
3924
|
+
import { makeTgBotClient as makeTgBotClient2 } from "@effect-ak/tg-bot-client";
|
|
3925
|
+
var isGuardedHandler2 = (handler) => typeof handler === "object" && handler !== null && "handle" in handler;
|
|
3926
|
+
var executeSingleGuard2 = async (guard, update, ctx) => {
|
|
3927
|
+
const input = { update, ctx };
|
|
3928
|
+
if (guard.match) {
|
|
3929
|
+
const matched = await guard.match(input);
|
|
3930
|
+
if (!matched) return null;
|
|
3931
|
+
}
|
|
3932
|
+
return await guard.handle(input);
|
|
3933
|
+
};
|
|
3934
|
+
var executeGuards2 = async (guards, update, ctx) => {
|
|
3935
|
+
for (const guard of guards) {
|
|
3936
|
+
const result = await executeSingleGuard2(guard, update, ctx);
|
|
3937
|
+
if (result !== null) return result;
|
|
3938
|
+
}
|
|
3939
|
+
return BotResponse.ignore;
|
|
3940
|
+
};
|
|
3941
|
+
var executeHandler2 = async (handler, update, ctx) => {
|
|
3942
|
+
if (typeof handler === "function") {
|
|
3943
|
+
return await handler(update);
|
|
3944
|
+
}
|
|
3945
|
+
if (Array.isArray(handler)) {
|
|
3946
|
+
return await executeGuards2(handler, update, ctx);
|
|
3947
|
+
}
|
|
3948
|
+
if (isGuardedHandler2(handler)) {
|
|
3949
|
+
const result = await executeSingleGuard2(handler, update, ctx);
|
|
3950
|
+
return result ?? BotResponse.ignore;
|
|
3951
|
+
}
|
|
3952
|
+
return BotResponse.ignore;
|
|
3953
|
+
};
|
|
3954
|
+
var extractUpdate2 = (input) => {
|
|
3955
|
+
for (const [field, value] of Object.entries(input)) {
|
|
3956
|
+
if (field === "update_id") continue;
|
|
3957
|
+
return { type: field, ...value };
|
|
3958
|
+
}
|
|
3959
|
+
return void 0;
|
|
3960
|
+
};
|
|
3961
|
+
var processUpdate = async (updateObject, handlers, client) => {
|
|
3962
|
+
const update = extractUpdate2(updateObject);
|
|
3963
|
+
if (!update) {
|
|
3964
|
+
console.warn("Unknown update format", updateObject);
|
|
3965
|
+
return;
|
|
3966
|
+
}
|
|
3967
|
+
const handlerKey = `on_${update.type}`;
|
|
3968
|
+
const handler = handlers[handlerKey];
|
|
3969
|
+
if (!handler) {
|
|
3970
|
+
return;
|
|
3971
|
+
}
|
|
3972
|
+
const ctx = createBotContext(update);
|
|
3973
|
+
try {
|
|
3974
|
+
const result = await executeHandler2(handler, update, ctx);
|
|
3975
|
+
if (result.response && "chat" in update) {
|
|
3976
|
+
const responsePayload = result.response;
|
|
3977
|
+
await client.execute(`send_${responsePayload.type}`, {
|
|
3978
|
+
...responsePayload,
|
|
3979
|
+
chat_id: update.chat.id
|
|
3980
|
+
});
|
|
3981
|
+
}
|
|
3982
|
+
} catch (error) {
|
|
3983
|
+
console.error("Error handling update", {
|
|
3984
|
+
updateId: updateObject.update_id,
|
|
3985
|
+
error: error instanceof Error ? error.message : error
|
|
3986
|
+
});
|
|
3987
|
+
}
|
|
3988
|
+
};
|
|
3989
|
+
var createWebhookHandler = (config) => {
|
|
3990
|
+
const client = makeTgBotClient2({ bot_token: config.bot_token });
|
|
3991
|
+
const handleUpdate = async (update) => {
|
|
3992
|
+
await processUpdate(update, config, client);
|
|
3993
|
+
};
|
|
3994
|
+
const handler = async (request) => {
|
|
3995
|
+
try {
|
|
3996
|
+
const update = await request.json();
|
|
3997
|
+
await handleUpdate(update);
|
|
3998
|
+
return new Response("ok", { status: 200 });
|
|
3999
|
+
} catch (error) {
|
|
4000
|
+
console.error("Webhook error", error);
|
|
4001
|
+
return new Response("error", { status: 500 });
|
|
4002
|
+
}
|
|
4003
|
+
};
|
|
4004
|
+
handler.handleUpdate = handleUpdate;
|
|
4005
|
+
return handler;
|
|
4006
|
+
};
|
|
3922
4007
|
export {
|
|
3923
4008
|
BatchUpdateResult,
|
|
3924
4009
|
BotPollSettings,
|
|
@@ -3929,6 +4014,7 @@ export {
|
|
|
3929
4014
|
BotUpdateHandlersTag,
|
|
3930
4015
|
HandleUpdateError,
|
|
3931
4016
|
createBotContext,
|
|
4017
|
+
createWebhookHandler,
|
|
3932
4018
|
defineBot,
|
|
3933
4019
|
extractUpdate,
|
|
3934
4020
|
handleEntireBatch,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effect-ak/tg-bot",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Telegram Bot runner",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"dist/*.d.ts"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@effect-ak/tg-bot-client": "^1.1
|
|
38
|
-
"@effect-ak/tg-bot-api": "0.
|
|
37
|
+
"@effect-ak/tg-bot-client": "^1.3.1",
|
|
38
|
+
"@effect-ak/tg-bot-api": "^1.0.0"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"effect": "^3.12.7"
|