@effect/chat-telegram 0.0.0-stage → 4.1.5
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/AGENTS.md +434 -0
- package/CLAUDE.md +434 -0
- package/LICENSE +21 -0
- package/README.md +121 -2
- package/ai-docs/README.md +44 -0
- package/ai-docs/package.json +36 -0
- package/ai-docs/src/01_effect/01_basics/01_effect-gen.ts +30 -0
- package/ai-docs/src/01_effect/01_basics/02_effect-fn.ts +52 -0
- package/ai-docs/src/01_effect/01_basics/10_creating-effects.ts +74 -0
- package/ai-docs/src/01_effect/01_basics/index.md +7 -0
- package/ai-docs/src/01_effect/02_schema/10_schema-basics.ts +43 -0
- package/ai-docs/src/01_effect/02_schema/index.md +7 -0
- package/ai-docs/src/01_effect/03_services/01_service.ts +45 -0
- package/ai-docs/src/01_effect/03_services/10_reference.ts +10 -0
- package/ai-docs/src/01_effect/03_services/20_layer-composition.ts +70 -0
- package/ai-docs/src/01_effect/03_services/20_layer-unwrap.ts +66 -0
- package/ai-docs/src/01_effect/03_services/index.md +5 -0
- package/ai-docs/src/01_effect/04_errors/01_error-handling.ts +30 -0
- package/ai-docs/src/01_effect/04_errors/10_catch-tags.ts +24 -0
- package/ai-docs/src/01_effect/04_errors/20_reason-errors.ts +64 -0
- package/ai-docs/src/01_effect/04_errors/index.md +1 -0
- package/ai-docs/src/01_effect/05_resources/10_acquire-release.ts +105 -0
- package/ai-docs/src/01_effect/05_resources/20_layer-side-effects.ts +31 -0
- package/ai-docs/src/01_effect/05_resources/30_layer-map.ts +86 -0
- package/ai-docs/src/01_effect/05_resources/index.md +3 -0
- package/ai-docs/src/01_effect/06_running/10_run-main.ts +30 -0
- package/ai-docs/src/01_effect/06_running/20_layer-launch.ts +27 -0
- package/ai-docs/src/01_effect/06_running/index.md +1 -0
- package/ai-docs/src/01_effect/07_pubsub/10_pubsub.ts +56 -0
- package/ai-docs/src/01_effect/07_pubsub/index.md +3 -0
- package/ai-docs/src/03_stream/10_creating-streams.ts +103 -0
- package/ai-docs/src/03_stream/20_consuming-streams.ts +137 -0
- package/ai-docs/src/03_stream/30_encoding.ts +163 -0
- package/ai-docs/src/03_stream/index.md +4 -0
- package/ai-docs/src/04_integration/10_managed-runtime.ts +129 -0
- package/ai-docs/src/04_integration/index.md +5 -0
- package/ai-docs/src/05_batching/10_request-resolver.ts +89 -0
- package/ai-docs/src/05_batching/index.md +3 -0
- package/ai-docs/src/06_schedule/10_schedules.ts +111 -0
- package/ai-docs/src/06_schedule/index.md +3 -0
- package/ai-docs/src/07_datetime/10_creating-and-formatting.ts +30 -0
- package/ai-docs/src/07_datetime/20_time-zones.ts +44 -0
- package/ai-docs/src/07_datetime/index.md +5 -0
- package/ai-docs/src/08_observability/10_logging.ts +66 -0
- package/ai-docs/src/08_observability/20_otlp-tracing.ts +95 -0
- package/ai-docs/src/08_observability/index.md +7 -0
- package/ai-docs/src/09_testing/10_effect-tests.ts +55 -0
- package/ai-docs/src/09_testing/20_layer-tests.ts +138 -0
- package/ai-docs/src/09_testing/index.md +1 -0
- package/ai-docs/src/10_predicate/01_basics.ts +14 -0
- package/ai-docs/src/10_predicate/index.md +9 -0
- package/ai-docs/src/40_sql/10_basics.ts +175 -0
- package/ai-docs/src/40_sql/index.md +6 -0
- package/ai-docs/src/50_http-client/10_basics.ts +102 -0
- package/ai-docs/src/50_http-client/index.md +3 -0
- package/ai-docs/src/51_http-server/10_basics.ts +118 -0
- package/ai-docs/src/51_http-server/20_testing.ts +100 -0
- package/ai-docs/src/51_http-server/fixtures/api/Api.ts +14 -0
- package/ai-docs/src/51_http-server/fixtures/api/Authorization.ts +36 -0
- package/ai-docs/src/51_http-server/fixtures/api/System.ts +10 -0
- package/ai-docs/src/51_http-server/fixtures/api/Users.ts +107 -0
- package/ai-docs/src/51_http-server/fixtures/domain/User.ts +21 -0
- package/ai-docs/src/51_http-server/fixtures/domain/UserErrors.ts +22 -0
- package/ai-docs/src/51_http-server/fixtures/server/Authorization.ts +37 -0
- package/ai-docs/src/51_http-server/fixtures/server/Users/http.ts +80 -0
- package/ai-docs/src/51_http-server/fixtures/server/Users.ts +183 -0
- package/ai-docs/src/51_http-server/index.md +3 -0
- package/ai-docs/src/52_graphql/10_basics.ts +117 -0
- package/ai-docs/src/52_graphql/20_testing.ts +45 -0
- package/ai-docs/src/52_graphql/30_batching.ts +118 -0
- package/ai-docs/src/52_graphql/40_aliased-batching.ts +124 -0
- package/ai-docs/src/52_graphql/50_errors-and-retries.ts +74 -0
- package/ai-docs/src/52_graphql/index.md +6 -0
- package/ai-docs/src/60_child-process/10_working-with-child-processes.ts +117 -0
- package/ai-docs/src/60_child-process/index.md +3 -0
- package/ai-docs/src/70_cli/10_basics.ts +162 -0
- package/ai-docs/src/70_cli/index.md +5 -0
- package/ai-docs/src/71_ai/10_language-model.ts +156 -0
- package/ai-docs/src/71_ai/20_tools.ts +226 -0
- package/ai-docs/src/71_ai/30_chat.ts +158 -0
- package/ai-docs/src/71_ai/fixtures/domain/LaunchPlan.ts +9 -0
- package/ai-docs/src/71_ai/index.md +5 -0
- package/ai-docs/src/80_cluster/10_entities.ts +97 -0
- package/ai-docs/src/80_cluster/index.md +4 -0
- package/ai-docs/src/index.md +10 -0
- package/ai-docs/tsconfig.json +28 -0
- package/dist/TelegramBotApi.d.ts +153 -0
- package/dist/TelegramBotApi.d.ts.map +1 -0
- package/dist/TelegramBotApi.js +219 -0
- package/dist/TelegramBotApi.js.map +1 -0
- package/dist/TelegramChat.d.ts +390 -0
- package/dist/TelegramChat.d.ts.map +1 -0
- package/dist/TelegramChat.js +359 -0
- package/dist/TelegramChat.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/actions.d.ts +2 -0
- package/dist/internal/actions.d.ts.map +1 -0
- package/dist/internal/actions.js +88 -0
- package/dist/internal/actions.js.map +1 -0
- package/dist/internal/adapter.d.ts +2 -0
- package/dist/internal/adapter.d.ts.map +1 -0
- package/dist/internal/adapter.js +309 -0
- package/dist/internal/adapter.js.map +1 -0
- package/dist/internal/address.d.ts +2 -0
- package/dist/internal/address.d.ts.map +1 -0
- package/dist/internal/address.js +26 -0
- package/dist/internal/address.js.map +1 -0
- package/dist/internal/commands.d.ts +2 -0
- package/dist/internal/commands.d.ts.map +1 -0
- package/dist/internal/commands.js +30 -0
- package/dist/internal/commands.js.map +1 -0
- package/dist/internal/errors.d.ts +2 -0
- package/dist/internal/errors.d.ts.map +1 -0
- package/dist/internal/errors.js +136 -0
- package/dist/internal/errors.js.map +1 -0
- package/dist/internal/markdown.d.ts +9 -0
- package/dist/internal/markdown.d.ts.map +1 -0
- package/dist/internal/markdown.js +708 -0
- package/dist/internal/markdown.js.map +1 -0
- package/dist/internal/normalize.d.ts +2 -0
- package/dist/internal/normalize.d.ts.map +1 -0
- package/dist/internal/normalize.js +276 -0
- package/dist/internal/normalize.js.map +1 -0
- package/dist/internal/options.d.ts +2 -0
- package/dist/internal/options.d.ts.map +1 -0
- package/dist/internal/options.js +6 -0
- package/dist/internal/options.js.map +1 -0
- package/dist/internal/polling.d.ts +2 -0
- package/dist/internal/polling.d.ts.map +1 -0
- package/dist/internal/polling.js +68 -0
- package/dist/internal/polling.js.map +1 -0
- package/dist/internal/receive.d.ts +2 -0
- package/dist/internal/receive.d.ts.map +1 -0
- package/dist/internal/receive.js +183 -0
- package/dist/internal/receive.js.map +1 -0
- package/dist/internal/webhook.d.ts +2 -0
- package/dist/internal/webhook.d.ts.map +1 -0
- package/dist/internal/webhook.js +61 -0
- package/dist/internal/webhook.js.map +1 -0
- package/package.json +62 -3
- package/src/TelegramBotApi.ts +282 -0
- package/src/TelegramChat.ts +542 -0
- package/src/index.ts +18 -0
- package/src/internal/actions.ts +95 -0
- package/src/internal/adapter.ts +319 -0
- package/src/internal/address.ts +33 -0
- package/src/internal/commands.ts +37 -0
- package/src/internal/errors.ts +148 -0
- package/src/internal/markdown.ts +805 -0
- package/src/internal/normalize.ts +283 -0
- package/src/internal/options.ts +12 -0
- package/src/internal/polling.ts +86 -0
- package/src/internal/receive.ts +268 -0
- package/src/internal/webhook.ts +85 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import * as ByteSize from "effect/ByteSize"
|
|
2
|
+
import type { ChatError } from "effect/chat/ChatError"
|
|
3
|
+
import { timingSafeEqual } from "effect/crypto/Mac"
|
|
4
|
+
import * as Effect from "effect/Effect"
|
|
5
|
+
import * as HttpIncomingMessage from "effect/http/HttpIncomingMessage"
|
|
6
|
+
import type * as HttpServerRequest from "effect/http/HttpServerRequest"
|
|
7
|
+
import * as HttpServerResponse from "effect/http/HttpServerResponse"
|
|
8
|
+
import * as Predicate from "effect/Predicate"
|
|
9
|
+
import * as Redacted from "effect/Redacted"
|
|
10
|
+
import * as Errors from "./errors.ts"
|
|
11
|
+
import type { Outcome } from "./receive.ts"
|
|
12
|
+
|
|
13
|
+
/** The header Telegram sends the webhook secret in. @internal */
|
|
14
|
+
export const secretHeader = "x-telegram-bot-api-secret-token"
|
|
15
|
+
|
|
16
|
+
/** A secret token's value, if it keeps Telegram's rule with the adapter's floor of 32 characters. @internal */
|
|
17
|
+
export const secretToken = (op: string, token: Redacted.Redacted<string>): Effect.Effect<string, ChatError> => {
|
|
18
|
+
const secret = Redacted.value(token)
|
|
19
|
+
return /^[A-Za-z0-9_-]{32,256}$/.test(secret)
|
|
20
|
+
? Effect.succeed(secret)
|
|
21
|
+
: Errors.fail(op, Errors.invalidRequest("invalid_secret_token"))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const encoder = new TextEncoder()
|
|
25
|
+
const decoder = new TextDecoder()
|
|
26
|
+
|
|
27
|
+
const parseJson = (text: string): unknown => {
|
|
28
|
+
try {
|
|
29
|
+
return JSON.parse(text)
|
|
30
|
+
} catch {
|
|
31
|
+
return undefined
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const status = (code: number) => HttpServerResponse.empty({ status: code })
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* One webhook request.
|
|
39
|
+
*
|
|
40
|
+
* - 401 without the secret, before the body is read. 413 past
|
|
41
|
+
* `maxBodySize`, 400 for a body that can't be read or is not an update.
|
|
42
|
+
* - 200 once the update was accepted, answered or sent to `onUnhandled`.
|
|
43
|
+
* - 503 when its event was not accepted in time, or could not be read.
|
|
44
|
+
*
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
export const handleRequest = (
|
|
48
|
+
request: HttpServerRequest.HttpServerRequest,
|
|
49
|
+
options: {
|
|
50
|
+
readonly secret: Uint8Array
|
|
51
|
+
readonly maxBodySize: ByteSize.ByteSize
|
|
52
|
+
readonly receive: (update: unknown) => Effect.Effect<Outcome>
|
|
53
|
+
}
|
|
54
|
+
): Effect.Effect<HttpServerResponse.HttpServerResponse> =>
|
|
55
|
+
Effect.gen(function*() {
|
|
56
|
+
const header = request.headers[secretHeader]
|
|
57
|
+
// Compared in constant time.
|
|
58
|
+
if (header === undefined || !timingSafeEqual(encoder.encode(header), options.secret)) return status(401)
|
|
59
|
+
const body = yield* Effect.result(
|
|
60
|
+
Effect.provideService(request.arrayBuffer, HttpIncomingMessage.MaxBodySize, options.maxBodySize)
|
|
61
|
+
)
|
|
62
|
+
if (body._tag === "Failure") {
|
|
63
|
+
const reason = body.failure.reason
|
|
64
|
+
return status(reason._tag === "RequestParseError" && reason.reason === "BodyTooLarge" ? 413 : 400)
|
|
65
|
+
}
|
|
66
|
+
// Measured again: the request caches its body, so one read earlier under a larger limit comes back whole.
|
|
67
|
+
if (body.success.byteLength > ByteSize.toNumberUnsafe(options.maxBodySize)) return status(413)
|
|
68
|
+
const update = parseJson(decoder.decode(body.success))
|
|
69
|
+
if (!Predicate.isObject(update) || typeof update.update_id !== "number") return status(400)
|
|
70
|
+
switch (yield* options.receive(update)) {
|
|
71
|
+
case "accepted":
|
|
72
|
+
case "unhandled":
|
|
73
|
+
return status(200)
|
|
74
|
+
case "invalid":
|
|
75
|
+
return yield* Effect.as(
|
|
76
|
+
Effect.logWarning("telegram webhook: the update could not be read, answered 503"),
|
|
77
|
+
status(503)
|
|
78
|
+
)
|
|
79
|
+
case "notAccepted":
|
|
80
|
+
return yield* Effect.as(
|
|
81
|
+
Effect.logWarning("telegram webhook: the handler refused the event or passed the deadline, answered 503"),
|
|
82
|
+
status(503)
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
})
|