Add inline button telegram (#3)

* Update utils.ts

add type inline button

* Update dokploy-restart.ts

fixing format massage and adding [] for inline button type

* Update docker-cleanup.ts

fixing telegram message

* Update database-backup.ts

fixing telegram message

* Update build-error.ts

fixing message and adding button logs view

* Update build-success.ts

fixing message, adding domains props, adding inline button

* Update compose.ts

adding get domains compose and send to notif

* Update application.ts

adding get domains and send it to notif
This commit is contained in:
shiqocred
2025-01-12 00:55:31 +07:00
committed by GitHub
parent 9db979e43f
commit dd3618bfd9
8 changed files with 68 additions and 44 deletions

View File

@@ -176,6 +176,7 @@ export const deployApplication = async ({
}) => { }) => {
const application = await findApplicationById(applicationId); const application = await findApplicationById(applicationId);
const buildLink = `${await getDokployUrl()}/dashboard/project/${application.projectId}/services/application/${application.applicationId}?tab=deployments`; const buildLink = `${await getDokployUrl()}/dashboard/project/${application.projectId}/services/application/${application.applicationId}?tab=deployments`;
const domains = application.domains.map(({ host, https }) => ({ host, https }));
const deployment = await createDeployment({ const deployment = await createDeployment({
applicationId: applicationId, applicationId: applicationId,
title: titleLog, title: titleLog,
@@ -213,6 +214,7 @@ export const deployApplication = async ({
applicationType: "application", applicationType: "application",
buildLink, buildLink,
adminId: application.project.adminId, adminId: application.project.adminId,
domains
}); });
} catch (error) { } catch (error) {
await updateDeploymentStatus(deployment.deploymentId, "error"); await updateDeploymentStatus(deployment.deploymentId, "error");
@@ -285,6 +287,7 @@ export const deployRemoteApplication = async ({
}) => { }) => {
const application = await findApplicationById(applicationId); const application = await findApplicationById(applicationId);
const buildLink = `${await getDokployUrl()}/dashboard/project/${application.projectId}/services/application/${application.applicationId}?tab=deployments`; const buildLink = `${await getDokployUrl()}/dashboard/project/${application.projectId}/services/application/${application.applicationId}?tab=deployments`;
const domains = application.domains.map(({ host, https }) => ({ host, https }));
const deployment = await createDeployment({ const deployment = await createDeployment({
applicationId: applicationId, applicationId: applicationId,
title: titleLog, title: titleLog,
@@ -332,6 +335,7 @@ export const deployRemoteApplication = async ({
applicationType: "application", applicationType: "application",
buildLink, buildLink,
adminId: application.project.adminId, adminId: application.project.adminId,
domains
}); });
} catch (error) { } catch (error) {
// @ts-ignore // @ts-ignore

View File

@@ -209,6 +209,7 @@ export const deployCompose = async ({
const buildLink = `${await getDokployUrl()}/dashboard/project/${ const buildLink = `${await getDokployUrl()}/dashboard/project/${
compose.projectId compose.projectId
}/services/compose/${compose.composeId}?tab=deployments`; }/services/compose/${compose.composeId}?tab=deployments`;
const domains = compose.domains.map(({ host, https }) => ({ host, https }));
const deployment = await createDeploymentCompose({ const deployment = await createDeploymentCompose({
composeId: composeId, composeId: composeId,
title: titleLog, title: titleLog,
@@ -243,6 +244,7 @@ export const deployCompose = async ({
applicationType: "compose", applicationType: "compose",
buildLink, buildLink,
adminId: compose.project.adminId, adminId: compose.project.adminId,
domains
}); });
} catch (error) { } catch (error) {
await updateDeploymentStatus(deployment.deploymentId, "error"); await updateDeploymentStatus(deployment.deploymentId, "error");
@@ -313,6 +315,7 @@ export const deployRemoteCompose = async ({
const buildLink = `${await getDokployUrl()}/dashboard/project/${ const buildLink = `${await getDokployUrl()}/dashboard/project/${
compose.projectId compose.projectId
}/services/compose/${compose.composeId}?tab=deployments`; }/services/compose/${compose.composeId}?tab=deployments`;
const domains = compose.domains.map(({ host, https }) => ({ host, https }));
const deployment = await createDeploymentCompose({ const deployment = await createDeploymentCompose({
composeId: composeId, composeId: composeId,
title: titleLog, title: titleLog,
@@ -366,6 +369,7 @@ export const deployRemoteCompose = async ({
applicationType: "compose", applicationType: "compose",
buildLink, buildLink,
adminId: compose.project.adminId, adminId: compose.project.adminId,
domains
}); });
} catch (error) { } catch (error) {
// @ts-ignore // @ts-ignore

View File

@@ -3,6 +3,7 @@ import { notifications } from "@dokploy/server/db/schema";
import BuildFailedEmail from "@dokploy/server/emails/emails/build-failed"; import BuildFailedEmail from "@dokploy/server/emails/emails/build-failed";
import { renderAsync } from "@react-email/components"; import { renderAsync } from "@react-email/components";
import { and, eq } from "drizzle-orm"; import { and, eq } from "drizzle-orm";
import { format } from "date-fns";
import { import {
sendDiscordNotification, sendDiscordNotification,
sendEmailNotification, sendEmailNotification,
@@ -113,21 +114,19 @@ export const sendBuildErrorNotifications = async ({
} }
if (telegram) { if (telegram) {
const inlineButton = [
[
{
text: "Deployment Logs",
url: buildLink,
},
],
];
await sendTelegramNotification( await sendTelegramNotification(
telegram, telegram,
` `<b>⚠️ Build Failed</b>\n\n<b>Project:</b> ${projectName}\n<b>Application:</b> ${applicationName}\n<b>Type:</b> ${applicationType}\n<b>Date:</b> ${format(date, "PP")}\n<b>Time:</b> ${format(date, "pp")}\n\n<b>Error:</b>\n<pre>${errorMessage}</pre>`,
<b>⚠️ Build Failed</b> inlineButton
<b>Project:</b> ${projectName}
<b>Application:</b> ${applicationName}
<b>Type:</b> ${applicationType}
<b>Time:</b> ${date.toLocaleString()}
<b>Error:</b>
<pre>${errorMessage}</pre>
<b>Build Details:</b> ${buildLink}
`,
); );
} }

View File

@@ -16,6 +16,10 @@ interface Props {
applicationType: string; applicationType: string;
buildLink: string; buildLink: string;
adminId: string; adminId: string;
domains: {
host: string;
https: boolean;
}[];
} }
export const sendBuildSuccessNotifications = async ({ export const sendBuildSuccessNotifications = async ({
@@ -24,6 +28,7 @@ export const sendBuildSuccessNotifications = async ({
applicationType, applicationType,
buildLink, buildLink,
adminId, adminId,
domains
}: Props) => { }: Props) => {
const date = new Date(); const date = new Date();
const unixDate = ~~(Number(date) / 1000); const unixDate = ~~(Number(date) / 1000);
@@ -107,18 +112,29 @@ export const sendBuildSuccessNotifications = async ({
} }
if (telegram) { if (telegram) {
const chunkArray = <T>(array: T[], chunkSize: number): T[][] =>
Array.from({ length: Math.ceil(array.length / chunkSize) }, (_, i) => array.slice(i * chunkSize, i * chunkSize + chunkSize)
);
const inlineButton = [
[
{
text: "Deployment Logs",
url: buildLink,
},
],
...chunkArray(domains, 2).map((chunk) =>
chunk.map((data) => ({
text: data.host,
url: `${data.https ? "https" : "http"}://${data.host}`,
}))
),
];
await sendTelegramNotification( await sendTelegramNotification(
telegram, telegram,
` `<b>✅ Build Success</b>\n\n<b>Project:</b> ${projectName}\n<b>Application:</b> ${applicationName}<b>Type:</b> ${applicationType}\n<b>Date:</b> ${format(date, "PP")}\n<b>Time:</b> ${format(date, "pp")}`,
<b>✅ Build Success</b> inlineButton
<b>Project:</b> ${projectName}
<b>Application:</b> ${applicationName}
<b>Type:</b> ${applicationType}
<b>Time:</b> ${date.toLocaleString()}
<b>Build Details:</b> ${buildLink}
`,
); );
} }

View File

@@ -3,6 +3,7 @@ import { notifications } from "@dokploy/server/db/schema";
import DatabaseBackupEmail from "@dokploy/server/emails/emails/database-backup"; import DatabaseBackupEmail from "@dokploy/server/emails/emails/database-backup";
import { renderAsync } from "@react-email/components"; import { renderAsync } from "@react-email/components";
import { and, eq } from "drizzle-orm"; import { and, eq } from "drizzle-orm";
import { format } from "date-fns";
import { import {
sendDiscordNotification, sendDiscordNotification,
sendEmailNotification, sendEmailNotification,
@@ -121,19 +122,15 @@ export const sendDatabaseBackupNotifications = async ({
} }
if (telegram) { if (telegram) {
const isError = type === "error" && errorMessage;
const statusEmoji = type === "success" ? "✅" : "❌"; const statusEmoji = type === "success" ? "✅" : "❌";
const messageText = ` const typeStatus = type === "success" ? "Successful" : "Failed";
<b>${statusEmoji} Database Backup ${type === "success" ? "Successful" : "Failed"}</b> const errorMsg = isError ? `\n\n<b>Error:</b>\n<pre>${errorMessage}</pre>` : "";
<b>Project:</b> ${projectName} const messageText = `<b>${statusEmoji} Database Backup ${typeStatus}</b>\n\n<b>Project:</b> ${projectName}\n<b>Application:</b> ${applicationName}\n<b>Type:</b> ${databaseType}\n<b>Date:</b> ${format(date, "PP")}\n<b>Time:</b> ${format(date, "pp")}${isError ? errorMsg : ""}`;
<b>Application:</b> ${applicationName}
<b>Type:</b> ${databaseType} await sendTelegramNotification(telegram, messageText, []);
<b>Time:</b> ${date.toLocaleString()}
<b>Status:</b> ${type === "success" ? "Successful" : "Failed"}
${type === "error" && errorMessage ? `<b>Error:</b> ${errorMessage}` : ""}
`;
await sendTelegramNotification(telegram, messageText);
} }
if (slack) { if (slack) {

View File

@@ -3,6 +3,7 @@ import { notifications } from "@dokploy/server/db/schema";
import DockerCleanupEmail from "@dokploy/server/emails/emails/docker-cleanup"; import DockerCleanupEmail from "@dokploy/server/emails/emails/docker-cleanup";
import { renderAsync } from "@react-email/components"; import { renderAsync } from "@react-email/components";
import { and, eq } from "drizzle-orm"; import { and, eq } from "drizzle-orm";
import { format } from "date-fns";
import { import {
sendDiscordNotification, sendDiscordNotification,
sendEmailNotification, sendEmailNotification,
@@ -82,11 +83,8 @@ export const sendDockerCleanupNotifications = async (
if (telegram) { if (telegram) {
await sendTelegramNotification( await sendTelegramNotification(
telegram, telegram,
` `<b>✅ Docker Cleanup</b>\n\n<b>Message:</b> ${message}\n<b>Date:</b> ${format(date, "PP")}\n<b>Time:</b> ${format(date, "pp")}`,
<b>✅ Docker Cleanup</b> []
<b>Message:</b> ${message}
<b>Time:</b> ${date.toLocaleString()}
`,
); );
} }

View File

@@ -9,6 +9,7 @@ import {
sendSlackNotification, sendSlackNotification,
sendTelegramNotification, sendTelegramNotification,
} from "./utils"; } from "./utils";
import { format } from "date-fns";
export const sendDokployRestartNotifications = async () => { export const sendDokployRestartNotifications = async () => {
const date = new Date(); const date = new Date();
@@ -67,10 +68,8 @@ export const sendDokployRestartNotifications = async () => {
if (telegram) { if (telegram) {
await sendTelegramNotification( await sendTelegramNotification(
telegram, telegram,
` `<b>✅ Dokploy Serverd Restarted</b>\n<b>Date:</b> ${format(date, "PP")}\n<b>Time:</b> ${format(date, "pp")}`,
<b>✅ Dokploy Serverd Restarted</b> []
<b>Time:</b> ${date.toLocaleString()}
`,
); );
} }

View File

@@ -55,6 +55,10 @@ export const sendDiscordNotification = async (
export const sendTelegramNotification = async ( export const sendTelegramNotification = async (
connection: typeof telegram.$inferInsert, connection: typeof telegram.$inferInsert,
messageText: string, messageText: string,
inlineButton: {
text: string;
url: string;
}[][]
) => { ) => {
try { try {
const url = `https://api.telegram.org/bot${connection.botToken}/sendMessage`; const url = `https://api.telegram.org/bot${connection.botToken}/sendMessage`;
@@ -66,6 +70,9 @@ export const sendTelegramNotification = async (
text: messageText, text: messageText,
parse_mode: "HTML", parse_mode: "HTML",
disable_web_page_preview: true, disable_web_page_preview: true,
reply_markup: {
inline_keyboard: inlineButton,
},
}), }),
}); });
} catch (err) { } catch (err) {