-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
742f3bd
commit 22cfc17
Showing
4 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |