import { type Update } from "../types.ts"; const SECRET_HEADER = "X-Telegram-Bot-Api-Secret-Token"; const SECRET_HEADER_LOWERCASE = SECRET_HEADER.toLowerCase(); const WRONG_TOKEN_ERROR = "secret token is wrong"; const ok = () => new Response(null, { status: 200 }); const okJson = (json: string) => new Response(json, { status: 200, headers: { "Content-Type": "application/json" }, }); const unauthorized = () => new Response('"unauthorized"', { status: 401, statusText: WRONG_TOKEN_ERROR, }); type MaybePromise = T | Promise; /** * Abstraction over a request-response cycle, providing access to the update, as * well as a mechanism for responding to the request and to end it. */ export interface ReqResHandler { /** * The update object sent from Telegram, usually resolves the request's JSON * body */ update: MaybePromise; /** * X-Telegram-Bot-Api-Secret-Token header of the request, or undefined if * not present */ header?: string; /** * Ends the request immediately without body, called after every request * unless a webhook reply was performed */ end?: () => void; /** * Sends the specified JSON as a payload in the body, used for webhook * replies */ respond: (json: string) => unknown | Promise; /** * Responds that the request is unauthorized due to mismatching * X-Telegram-Bot-Api-Secret-Token headers */ unauthorized: () => unknown | Promise; /** * Some frameworks (e.g. Deno's std/http `listenAndServe`) assume that * handler returns something */ handlerReturn?: Promise; } /** * Middleware for a web framework. Creates a request-response handler for a * request. The handler will be used to integrate with the compatible framework. */ // deno-lint-ignore no-explicit-any export type FrameworkAdapter = (...args: any[]) => ReqResHandler; export type LambdaAdapter = ( event: { body?: string; headers: Record; }, _context: unknown, callback: ( arg0: unknown, arg1: Record, ) => Promise, ) => ReqResHandler; export type LambdaAsyncAdapter = ( event: { body?: string; headers: Record; }, _context: unknown, ) => ReqResHandler; export type AzureAdapter = (context: { res?: { // deno-lint-ignore no-explicit-any [key: string]: any; }; }, request: { body?: unknown; headers?: Record; }) => ReqResHandler; export type AzureAdapterV4 = ( request: { headers: { get(name: string): string | null }; json(): Promise; }, ) => ReqResHandler< | { status: number; body?: string; headers?: Record; } | { jsonBody: string } >; export type BunAdapter = (request: { headers: Headers; json: () => Promise; }) => ReqResHandler; export type CloudflareAdapter = (event: { request: Body & { method: string; url: string; headers: Headers; }; respondWith: (response: Promise) => void; }) => ReqResHandler; export type CloudflareModuleAdapter = ( request: Body & { method: string; url: string; headers: Headers; }, ) => ReqResHandler; export type ElysiaAdapter = (ctx: { body: unknown; headers: Record; set: { headers: Record; status?: string | number; }; }) => ReqResHandler; export type ExpressAdapter = (req: { body: Update; header: (header: string) => string | undefined; }, res: { end: (cb?: () => void) => typeof res; set: (field: string, value?: string | string[]) => typeof res; send: (json: string) => typeof res; status: (code: number) => typeof res; }) => ReqResHandler; export type FastifyAdapter = (request: { body: unknown; // deno-lint-ignore no-explicit-any headers: any; }, reply: { status: (code: number) => typeof reply; headers: (headers: Record) => typeof reply; code: (code: number) => typeof reply; send: { (): typeof reply; (json: string): typeof reply; }; }) => ReqResHandler; export type HonoAdapter = (c: { req: { json: () => Promise; header: (header: string) => string | undefined; }; header: (name: string, value: string) => void; body(data: string): Response; body(data: null, status: 204): Response; // deno-lint-ignore no-explicit-any status: (status: any) => void; json?: (json: string) => Response; }) => ReqResHandler; export type HttpAdapter = (req: { headers: Record; on: (event: string, listener: (chunk: unknown) => void) => typeof req; once: (event: string, listener: () => void) => typeof req; }, res: { writeHead: { (status: number): typeof res; (status: number, headers: Record): typeof res; }; end: (json?: string) => void; }) => ReqResHandler; export type KoaAdapter = (ctx: { get: (header: string) => string | undefined; set: (key: string, value: string) => void; status: number; body: string; request: { body?: unknown; }; response: { body: unknown; status: number; }; }) => ReqResHandler; export type NextAdapter = (req: { body: Update; headers: Record; }, res: { end: (cb?: () => void) => typeof res; status: (code: number) => typeof res; // deno-lint-ignore no-explicit-any json?: (json: string) => any; // deno-lint-ignore no-explicit-any send: (json: string) => any; setHeader?: ( name: string, value: string | number | readonly string[], ) => typeof res | void; }) => ReqResHandler; export type NHttpAdapter = (rev: { body: unknown; headers: { get: (header: string) => string | null; }; response: { sendStatus: (status: number) => void; setHeader?: (header: string, value: string) => void; status: (status: number) => { send: (json: string) => void; }; }; }) => ReqResHandler; export type OakAdapter = (ctx: { request: { body: { json: () => Promise; }; headers: { get: (header: string) => string | null; }; }; response: { status: number; type: string | undefined; body: unknown; }; }) => ReqResHandler; export type ServeHttpAdapter = ( requestEvent: { request: Request; respondWith: (response: Response) => void; }, ) => ReqResHandler; export type StdHttpAdapter = ( req: Request, ) => ReqResHandler; export type SveltekitAdapter = ( { request }: { request: Request }, ) => ReqResHandler; export type WorktopAdapter = (req: { json: () => Promise; headers: { get: (header: string) => string | null; }; }, res: { end: (data: BodyInit | null) => void; send: ( status: number, json: string, headers?: Record, ) => void; }) => ReqResHandler; /** AWS lambda serverless functions */ const awsLambda: LambdaAdapter = (event, _context, callback) => ({ get update() { return JSON.parse(event.body ?? "{}"); }, header: event.headers[SECRET_HEADER] ?? event.headers[SECRET_HEADER_LOWERCASE], end: () => callback(null, { statusCode: 200 }), respond: (json) => callback(null, { statusCode: 200, headers: { "Content-Type": "application/json" }, body: json, }), unauthorized: () => callback(null, { statusCode: 401 }), }); /** AWS lambda async/await serverless functions */ const awsLambdaAsync: LambdaAsyncAdapter = (event, _context) => { // deno-lint-ignore no-explicit-any let resolveResponse: (response: any) => void; return { get update() { return JSON.parse(event.body ?? "{}"); }, header: event.headers[SECRET_HEADER] ?? event.headers[SECRET_HEADER_LOWERCASE], end: () => resolveResponse({ statusCode: 200 }), respond: (json) => resolveResponse({ statusCode: 200, headers: { "Content-Type": "application/json" }, body: json, }), unauthorized: () => resolveResponse({ statusCode: 401 }), handlerReturn: new Promise((res) => resolveResponse = res), }; }; /** Azure Functions v3 and v4 */ const azure: AzureAdapter = (context, request) => ({ get update() { return request.body as Update; }, header: request.headers?.[SECRET_HEADER_LOWERCASE], end: () => (context.res = { status: 200, body: "", }), respond: (json) => { if (typeof context.res?.send === "function") { context.res?.set?.("Content-Type", "application/json"); context.res.send(json); } else { context.res = { status: 200, headers: { "Content-Type": "application/json" }, body: json, }; } }, unauthorized: () => { if (typeof context.res?.send === "function") { context.res.send(401, WRONG_TOKEN_ERROR); } else { context.res = { status: 401, body: WRONG_TOKEN_ERROR, }; } }, }); const azureV4: AzureAdapterV4 = (request) => { type Res = NonNullable< Awaited["handlerReturn"]> >; let resolveResponse: (response: Res) => void; return { get update() { return request.json() as Promise; }, header: request.headers.get(SECRET_HEADER) || undefined, end: () => resolveResponse({ status: 204 }), respond: (json) => resolveResponse({ status: 200, headers: { "Content-Type": "application/json" }, body: json, }), unauthorized: () => resolveResponse({ status: 401, body: WRONG_TOKEN_ERROR }), handlerReturn: new Promise((resolve) => resolveResponse = resolve), }; }; /** Bun.serve */ const bun: BunAdapter = (request) => { let resolveResponse: (response: Response) => void; return { get update() { return request.json() as Promise; }, header: request.headers.get(SECRET_HEADER) || undefined, end: () => { resolveResponse(ok()); }, respond: (json) => { resolveResponse(okJson(json)); }, unauthorized: () => { resolveResponse(unauthorized()); }, handlerReturn: new Promise((res) => resolveResponse = res), }; }; /** Native CloudFlare workers (service worker) */ const cloudflare: CloudflareAdapter = (event) => { let resolveResponse: (response: Response) => void; event.respondWith( new Promise((resolve) => { resolveResponse = resolve; }), ); return { get update() { return event.request.json() as Promise; }, header: event.request.headers.get(SECRET_HEADER) || undefined, end: () => { resolveResponse(ok()); }, respond: (json) => { resolveResponse(okJson(json)); }, unauthorized: () => { resolveResponse(unauthorized()); }, }; }; /** Native CloudFlare workers (module worker) */ const cloudflareModule: CloudflareModuleAdapter = (request) => { let resolveResponse: (res: Response) => void; return { get update() { return request.json() as Promise; }, header: request.headers.get(SECRET_HEADER) || undefined, end: () => { resolveResponse(ok()); }, respond: (json) => { resolveResponse(okJson(json)); }, unauthorized: () => { resolveResponse(unauthorized()); }, handlerReturn: new Promise((res) => resolveResponse = res), }; }; /** express web framework */ const express: ExpressAdapter = (req, res) => ({ get update() { return req.body as Update; }, header: req.header(SECRET_HEADER), end: () => res.end(), respond: (json) => { res.set("Content-Type", "application/json"); res.send(json); }, unauthorized: () => { res.status(401).send(WRONG_TOKEN_ERROR); }, }); /** fastify web framework */ const fastify: FastifyAdapter = (request, reply) => ({ get update() { return request.body as Update; }, header: request.headers[SECRET_HEADER_LOWERCASE], end: () => reply.send(""), respond: (json) => reply.headers({ "Content-Type": "application/json" }).send(json), unauthorized: () => reply.code(401).send(WRONG_TOKEN_ERROR), }); /** hono web framework */ const hono: HonoAdapter = (c) => { let resolveResponse: (response: Response) => void; return { get update() { return c.req.json() as Promise; }, header: c.req.header(SECRET_HEADER), end: () => { resolveResponse(c.body("")); }, respond: (json) => { c.header("Content-Type", "application/json"); resolveResponse(c.body(json)); }, unauthorized: () => { c.status(401); resolveResponse(c.body("")); }, handlerReturn: new Promise((res) => resolveResponse = res), }; }; /** Node.js native 'http' and 'https' modules */ const http: HttpAdapter = (req, res) => { const secretHeaderFromRequest = req.headers[SECRET_HEADER_LOWERCASE]; return { get update() { return new Promise((resolve, reject) => { // deno-lint-ignore no-explicit-any type Chunk = any; const chunks: Chunk[] = []; req.on("data", (chunk: Chunk) => chunks.push(chunk)) .once("end", () => { // @ts-ignore `Buffer` is Node-only // deno-lint-ignore no-node-globals const raw = Buffer.concat(chunks).toString("utf-8"); try { resolve(JSON.parse(raw)); } catch (err) { reject(err); } }) .once("error", reject); }) as Promise; }, header: Array.isArray(secretHeaderFromRequest) ? secretHeaderFromRequest[0] : secretHeaderFromRequest, end: () => res.end(), respond: (json) => res .writeHead(200, { "Content-Type": "application/json" }) .end(json), unauthorized: () => res.writeHead(401).end(WRONG_TOKEN_ERROR), }; }; /** koa web framework */ const koa: KoaAdapter = (ctx) => ({ get update() { return ctx.request.body as Update; }, header: ctx.get(SECRET_HEADER) || undefined, end: () => { ctx.body = ""; }, respond: (json) => { ctx.set("Content-Type", "application/json"); ctx.response.body = json; }, unauthorized: () => { ctx.status = 401; }, }); /** Next.js Serverless Functions */ const nextJs: NextAdapter = (request, response) => ({ get update() { return request.body as Update; }, header: request.headers[SECRET_HEADER_LOWERCASE] as string, end: () => response.end(), respond: (json) => { response.status(200); response.setHeader?.("Content-Type", "application/json"); response.send(json); }, unauthorized: () => response.status(401).send(WRONG_TOKEN_ERROR), }); /** nhttp web framework */ const nhttp: NHttpAdapter = (rev) => ({ get update() { return rev.body as Update; }, header: rev.headers.get(SECRET_HEADER) || undefined, end: () => rev.response.sendStatus(200), respond: (json) => { rev.response.setHeader?.("content-type", "application/json"); return rev.response.status(200).send(json); }, unauthorized: () => rev.response.status(401).send(WRONG_TOKEN_ERROR), }); /** oak web framework */ const oak: OakAdapter = (ctx) => ({ get update() { return ctx.request.body.json() as Promise; }, header: ctx.request.headers.get(SECRET_HEADER) || undefined, end: () => { ctx.response.status = 200; }, respond: (json) => { ctx.response.type = "json"; ctx.response.body = json; }, unauthorized: () => { ctx.response.status = 401; }, }); /** Deno.serve */ const serveHttp: ServeHttpAdapter = (requestEvent) => ({ get update() { return requestEvent.request.json() as Promise; }, header: requestEvent.request.headers.get(SECRET_HEADER) || undefined, end: () => requestEvent.respondWith(ok()), respond: (json) => requestEvent.respondWith(okJson(json)), unauthorized: () => requestEvent.respondWith(unauthorized()), }); /** std/http web server */ const stdHttp: StdHttpAdapter = (req) => { let resolveResponse: (response: Response) => void; return { get update() { return req.json() as Promise; }, header: req.headers.get(SECRET_HEADER) || undefined, end: () => { if (resolveResponse) resolveResponse(ok()); }, respond: (json) => { if (resolveResponse) resolveResponse(okJson(json)); }, unauthorized: () => { if (resolveResponse) resolveResponse(unauthorized()); }, handlerReturn: new Promise((res) => resolveResponse = res), }; }; /** Sveltekit Serverless Functions */ const sveltekit: SveltekitAdapter = ({ request }) => { let resolveResponse: (res: Response) => void; return { get update() { return request.json() as Promise; }, header: request.headers.get(SECRET_HEADER) || undefined, end: () => { if (resolveResponse) resolveResponse(ok()); }, respond: (json) => { if (resolveResponse) resolveResponse(okJson(json)); }, unauthorized: () => { if (resolveResponse) resolveResponse(unauthorized()); }, handlerReturn: new Promise((res) => resolveResponse = res), }; }; /** worktop Cloudflare workers framework */ const worktop: WorktopAdapter = (req, res) => ({ get update() { return req.json() as Promise; }, header: req.headers.get(SECRET_HEADER) ?? undefined, end: () => res.end(null), respond: (json) => res.send(200, json, { "content-type": "application/json" }), unauthorized: () => res.send(401, WRONG_TOKEN_ERROR), }); const elysia: ElysiaAdapter = (ctx) => { // @note upgrade target to use modern code? // const { promise, resolve } = Promise.withResolvers(); let resolveResponse: (result: string) => void; return { // @note technically the type shouldn't be limited to Promise, because it's fine to await plain values as well get update() { return ctx.body as Update; }, header: ctx.headers[SECRET_HEADER_LOWERCASE], end() { resolveResponse(""); }, respond(json) { // @note since json is passed as string here, we gotta define proper content-type ctx.set.headers["content-type"] = "application/json"; resolveResponse(json); }, unauthorized() { ctx.set.status = 401; resolveResponse(""); }, handlerReturn: new Promise((res) => resolveResponse = res), }; }; // Please open a pull request if you want to add another adapter export const adapters = { "aws-lambda": awsLambda, "aws-lambda-async": awsLambdaAsync, azure, "azure-v4": azureV4, bun, cloudflare, "cloudflare-mod": cloudflareModule, elysia, express, fastify, hono, http, https: http, koa, "next-js": nextJs, nhttp, oak, serveHttp, "std/http": stdHttp, sveltekit, worktop, };