Free & Open-Source Invoice Generator - No Sign-Up, No Ads, Instant PDF Export, 100% In-Browser, and Fully Customizable Templates.
TypeScript fintechinvoiceinvoice-generatorinvoice-pdfinvoice-templateinvoicesnextjsopen-sourcequotesreactreact-pdf-rendererself-hostedshadcn-uitailwindcsstypescript
chore: updates to tg-webhook workflow and remove cache configuration from Node.js setup in GitHub workflows (#227)
Vlad Sazonau845fe858a3d0
diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.ymlindex 4d1d3d9..360d4da 100644--- a/.github/workflows/e2e.yml+++ b/.github/workflows/e2e.yml@@ -37,7 +37,6 @@ jobs:name: π Setup Node.jswith:node-version: lts/*- cache: "pnpm"- name: π Install dependenciesrun: pnpm installdiff --git a/.github/workflows/knip.yml b/.github/workflows/knip.ymlindex b57918b..3998c48 100644--- a/.github/workflows/knip.yml+++ b/.github/workflows/knip.yml@@ -26,7 +26,6 @@ jobs:name: π Setup Node.jswith:node-version: lts/*- cache: "pnpm"- name: π Install dependenciesrun: pnpm installdiff --git a/.github/workflows/lint.yml b/.github/workflows/lint.ymlindex 37f9f8c..a26bf16 100644--- a/.github/workflows/lint.yml+++ b/.github/workflows/lint.yml@@ -26,7 +26,6 @@ jobs:name: π Setup Node.jswith:node-version: lts/*- cache: "pnpm"- name: π Install dependenciesrun: pnpm installdiff --git a/.github/workflows/type-check.yml b/.github/workflows/type-check.ymlindex f149a51..f500531 100644--- a/.github/workflows/type-check.yml+++ b/.github/workflows/type-check.yml@@ -26,7 +26,6 @@ jobs:name: π Setup Node.jswith:node-version: lts/*- cache: "pnpm"- name: π Install dependenciesrun: pnpm installdiff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.ymlindex 8ff5d67..cb00c11 100644--- a/.github/workflows/unit-tests.yml+++ b/.github/workflows/unit-tests.yml@@ -26,7 +26,6 @@ jobs:name: π Setup Node.jswith:node-version: lts/*- cache: "pnpm"- name: π Install dependenciesrun: pnpm installdiff --git a/src/app/api/telegram-webhook/route.ts b/src/app/api/telegram-webhook/route.tsindex 5ced427..37b9dcb 100644--- a/src/app/api/telegram-webhook/route.ts+++ b/src/app/api/telegram-webhook/route.ts@@ -1,31 +1,13 @@+import { telegramUpdateSchema } from "@/app/api/telegram-webhook/schema/telegram-schema";import { env } from "@/env";import { sendTelegramMessage } from "@/lib/telegram";+import { NextResponse, type NextRequest } from "next/server";export const dynamic = "force-dynamic";-interface TelegramUpdate {- update_id: number;- message?: {- message_id: number;- from: {- id: number;- is_bot: boolean;- first_name: string;- };- chat: {- id: number;- type: string;- };- date: number;- text?: string;- entities?: Array<{- offset: number;- length: number;- type: string;- }>;- };-}+// serverless function can run for max 30 seconds+export const maxDuration = 30; // Set to 30 seconds/*** Handles incoming Telegram webhook updates.@@ -38,24 +20,20 @@ export async function POST(req: NextRequest) {const body = await req.text();// Parse and validate incoming Telegram update- const update = JSON.parse(body) as TelegramUpdate;+ const parseResult = telegramUpdateSchema.safeParse(JSON.parse(body));- // Guard: ignore updates without message text- if (!update.message?.text) {+ if (!parseResult.success) {+ console.error(+ "[telegram-webhook] Invalid webhook payload:",+ parseResult.error.errors,+ );return new NextResponse(JSON.stringify({ ok: true }), {status: 200,headers: { "Content-Type": "application/json" },});}- const isGenerateInvoiceCommand = update.message.text === "/generate";-- if (!isGenerateInvoiceCommand) {- return new NextResponse(JSON.stringify({ ok: true }), {- status: 200,- headers: { "Content-Type": "application/json" },- });- }+ const update = parseResult.data;const senderChatId = update.message.from.id;const allowedChatId = parseInt(env.TELEGRAM_CHAT_ID, 10);diff --git a/src/app/api/telegram-webhook/schema/telegram-schema.test.ts b/src/app/api/telegram-webhook/schema/telegram-schema.test.tsnew file mode 100644index 0000000..2fe6553--- /dev/null+++ b/src/app/api/telegram-webhook/schema/telegram-schema.test.ts@@ -0,0 +1,253 @@+import { describe, it, expect } from "vitest";+import { telegramUpdateSchema } from "./telegram-schema";++describe("Telegram Schema Validation", () => {+ describe("telegramUpdateSchema", () => {+ it("should validate a valid Telegram update", () => {+ const validUpdate = {+ update_id: 123456789,+ message: {+ message_id: 1,+ from: {+ id: 987654321,+ is_bot: false,+ first_name: "John",+ },+ chat: {+ id: 987654321,+ type: "private",+ },+ date: 1234567890,+ text: "/generate",+ },+ } as const;++ const result = telegramUpdateSchema.safeParse(validUpdate);++ expect(result.success).toBe(true);++ if (result.success) {+ expect(result.data.update_id).toBe(123456789);+ expect(result.data.message?.text).toBe("/generate");+ }+ });++ it("should validate message without entities", () => {+ const updateWithoutEntities = {+ update_id: 123456789,+ message: {+ message_id: 1,+ from: {+ id: 987654321,+ is_bot: false,+ first_name: "John",+ },+ chat: {+ id: 987654321,+ type: "private",+ },+ date: 1234567890,+ text: "/generate",+ },+ } as const;++ const result = telegramUpdateSchema.safeParse(updateWithoutEntities);++ expect(result.success).toBe(true);++ if (result.success) {+ expect(result.data.message?.entities).toBeUndefined();+ }+ });++ it("should reject update with missing update_id", () => {+ const invalidUpdate = {+ message: {+ message_id: 1,+ from: {+ id: 987654321,+ is_bot: false,+ first_name: "John",+ },+ chat: {+ id: 987654321,+ type: "private",+ },+ date: 1234567890,+ text: "/generate",+ },+ } as const;++ const result = telegramUpdateSchema.safeParse(invalidUpdate);++ expect(result.success).toBe(false);+ });++ it("should reject update with missing message", () => {+ const updateWithoutMessage = {+ update_id: 123456789,+ } as const;++ const result = telegramUpdateSchema.safeParse(updateWithoutMessage);++ expect(result.success).toBe(false);+ });++ it("should reject update with invalid from object", () => {+ const invalidUpdate = {+ update_id: 123456789,+ message: {+ message_id: 1,+ from: {+ id: "not-a-number",+ is_bot: false,+ first_name: "John",+ },+ chat: {+ id: 987654321,+ type: "private",+ },+ date: 1234567890,+ text: "/generate",+ },+ } as const;++ const result = telegramUpdateSchema.safeParse(invalidUpdate);+ expect(result.success).toBe(false);+ });++ it("should reject update with extra unknown fields", () => {+ const updateWithExtra = {+ update_id: 123456789,+ message: {+ message_id: 1,+ from: {+ id: 987654321,+ is_bot: false,+ first_name: "John",+ },+ chat: {+ id: 987654321,+ type: "private",+ },+ date: 1234567890,+ text: "/generate",+ },+ extra_field: "should fail",+ } as const;++ const result = telegramUpdateSchema.safeParse(updateWithExtra);+ expect(result.success).toBe(false);+ });++ it("should validate update with entities", () => {+ const updateWithEntities = {+ update_id: 123456789,+ message: {+ message_id: 1,+ from: {+ id: 987654321,+ is_bot: false,+ first_name: "John",+ },+ chat: {+ id: 987654321,+ type: "private",+ },+ date: 1234567890,+ text: "/generate",+ entities: [+ {+ offset: 0,+ length: 9,+ type: "bot_command",+ },+ ],+ },+ } as const;++ const result = telegramUpdateSchema.safeParse(updateWithEntities);++ expect(result.success).toBe(true);++ if (result.success) {+ expect(result.data.message?.entities).toHaveLength(1);+ expect(result.data.message?.entities?.[0].type).toBe("bot_command");+ }+ });++ it("should reject message with text other than '/generate'", () => {+ const updateWithInvalidCommand = {+ update_id: 123456789,+ message: {+ message_id: 1,+ from: {+ id: 987654321,+ is_bot: false,+ first_name: "John",+ },+ chat: {+ id: 987654321,+ type: "private",+ },+ date: 1234567890,+ text: "/start",+ },+ } as const;++ const result = telegramUpdateSchema.safeParse(updateWithInvalidCommand);+ expect(result.success).toBe(false);+ });++ it("should reject message with empty text", () => {+ const updateWithEmptyText = {+ update_id: 123456789,+ message: {+ message_id: 1,+ from: {+ id: 987654321,+ is_bot: false,+ first_name: "John",+ },+ chat: {+ id: 987654321,+ type: "private",+ },+ date: 1234567890,+ text: "",+ },+ } as const;++ const result = telegramUpdateSchema.safeParse(updateWithEmptyText);+ expect(result.success).toBe(false);+ });++ it("should accept message with exactly '/generate' text", () => {+ const updateWithGenerateCommand = {+ update_id: 123456789,+ message: {+ message_id: 1,+ from: {+ id: 987654321,+ is_bot: false,+ first_name: "John",+ },+ chat: {+ id: 987654321,+ type: "private",+ },+ date: 1234567890,+ text: "/generate",+ },+ } as const;++ const result = telegramUpdateSchema.safeParse(updateWithGenerateCommand);++ expect(result.success).toBe(true);++ if (result.success) {+ expect(result.data.message?.text).toBe("/generate");+ }+ });+ });+});diff --git a/src/app/api/telegram-webhook/schema/telegram-schema.ts b/src/app/api/telegram-webhook/schema/telegram-schema.tsnew file mode 100644index 0000000..5507b04--- /dev/null+++ b/src/app/api/telegram-webhook/schema/telegram-schema.ts@@ -0,0 +1,46 @@+import { z } from "zod";++const telegramMessageSchema = z+ .object({+ message_id: z.number().int(),+ from: z+ .object({+ id: z.number().int(),+ is_bot: z.boolean(),+ first_name: z.string(),+ })+ .strict(),+ chat: z+ .object({+ id: z.number().int(),+ type: z.string(),+ })+ .strict(),+ date: z.number().int().nonnegative(),+ text: z+ .string()+ .min(1, "Text cannot be empty")+ .refine((val) => val === "/generate", {+ message: "Only '/generate' command is supported",+ })+ .optional(),+ entities: z+ .array(+ z+ .object({+ offset: z.number().int().nonnegative(),+ length: z.number().int().positive(),+ type: z.string(),+ })+ .strict(),+ )+ .optional(),+ })+ .strict();++export const telegramUpdateSchema = z+ .object({+ update_id: z.number().int(),+ message: telegramMessageSchema,+ })+ .strict();