Skip to content

Commit

Permalink
notify telegram webhook (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
technophile-04 authored Jan 1, 2025
1 parent 742f3bd commit 22cfc17
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/nextjs/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ NEXT_PUBLIC_ALCHEMY_API_KEY=
NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID=
# will be used in production
NEXT_PUBLIC_PONDER_URL=


POSTGRES_URL="postgresql://postgres:mysecretpassword@localhost:5432/postgres"
NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_SECRET=somereallysecretsecret

TELEGRAM_BOT_URL=http://localhost:8080
TELEGRAM_WEBHOOK_SECRET=your_secret_here
7 changes: 7 additions & 0 deletions packages/nextjs/app/api/grants/new/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { parseEther } from "viem";
import { applyFormSchema } from "~~/app/apply/schema";
import { GrantInsert, createGrant } from "~~/services/database/repositories/grants";
import { createStage } from "~~/services/database/repositories/stages";
import { notifyTelegramBot } from "~~/services/notifications/telegram";
import { authOptions } from "~~/utils/auth";

export type CreateNewGrantReqBody = Omit<GrantInsert, "requestedFunds" | "builderAddress"> & {
Expand Down Expand Up @@ -36,6 +37,12 @@ export async function POST(req: Request) {
grantId: createdGrant.id,
});

await notifyTelegramBot("grant", {
id: createdGrant.id,
...body,
builderAddress,
});

return NextResponse.json({ grantId: createdGrant.id, stageId: createdStage.id }, { status: 201 });
} catch (error) {
console.error(error);
Expand Down
6 changes: 6 additions & 0 deletions packages/nextjs/app/api/stages/new/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import deployedContracts from "~~/contracts/deployedContracts";
import scaffoldConfig from "~~/scaffold.config";
import { getGrantById } from "~~/services/database/repositories/grants";
import { StageInsert, createStage, updateStageStatusToCompleted } from "~~/services/database/repositories/stages";
import { notifyTelegramBot } from "~~/services/notifications/telegram";
import { authOptions } from "~~/utils/auth";
import { EIP_712_DOMAIN, EIP_712_TYPES__APPLY_FOR_STAGE } from "~~/utils/eip712";
import { getAlchemyHttpUrl } from "~~/utils/scaffold-eth";
Expand Down Expand Up @@ -93,6 +94,11 @@ export async function POST(req: Request) {
stageNumber: latestStage.stageNumber + 1,
});

await notifyTelegramBot("stage", {
newStage: body,
grant: grant,
});

return NextResponse.json({ grantId: newStage.grantId, stageId: createdStage.id }, { status: 201 });
} catch (error) {
console.error(error);
Expand Down
49 changes: 49 additions & 0 deletions packages/nextjs/services/notifications/telegram.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { CreateNewGrantReqBody } from "~~/app/api/grants/new/route";
import { CreateNewStageReqBody } from "~~/app/api/stages/new/route";
import { GrantWithStages } from "~~/app/grants/[grantId]/page";

const TELEGRAM_BOT_URL = process.env.TELEGRAM_BOT_URL;
const TELEGRAM_WEBHOOK_SECRET = process.env.TELEGRAM_WEBHOOK_SECRET;

type StageData = {
newStage: CreateNewStageReqBody;
grant: GrantWithStages;
};

type GrantData = CreateNewGrantReqBody & { builderAddress: string };

export async function notifyTelegramBot<T extends "grant" | "stage">(
endpoint: T,
data: T extends "grant" ? GrantData : StageData,
) {
if (!TELEGRAM_BOT_URL || !TELEGRAM_WEBHOOK_SECRET) {
if (!TELEGRAM_BOT_URL) {
console.warn("TELEGRAM_BOT_URL is not set. Telegram notifications will be disabled.");
}

if (!TELEGRAM_WEBHOOK_SECRET) {
console.warn("TELEGRAM_WEBHOOK_SECRET is not set. Telegram notifications will be disabled.");
}
return;
}

try {
const response = await fetch(`${TELEGRAM_BOT_URL}/webhook/${endpoint}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Webhook-Secret": TELEGRAM_WEBHOOK_SECRET,
},
body: JSON.stringify(data),
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

return response.json();
} catch (error) {
// We don't throw here to prevent the main flow from failing if notifications fail
console.error(`Error notifying Telegram bot (${endpoint}):`, error);
}
}

0 comments on commit 22cfc17

Please sign in to comment.