BOT_WEBHOOK_SECRET |
diff --git a/api/index.ts b/api/index.ts
new file mode 100644
index 00000000..441d71e6
--- /dev/null
+++ b/api/index.ts
@@ -0,0 +1,9 @@
+import { handle } from "@hono/node-server/vercel";
+import { createBot } from "#root/bot/index.js";
+import { config as configuration } from "#root/config.js";
+import { createServer } from "#root/server/index.js";
+
+const bot = createBot(configuration.BOT_TOKEN);
+const server = await createServer(bot);
+
+export default handle(server);
diff --git a/src/config.ts b/src/config.ts
index 0ff44397..a6c2208f 100644
--- a/src/config.ts
+++ b/src/config.ts
@@ -24,7 +24,6 @@ function createConfigFromEnvironment(environment: NodeJS.ProcessEnv) {
},
},
BOT_TOKEN: z.string(),
- BOT_WEBHOOK: z.string().default(''),
BOT_WEBHOOK_SECRET: z.string().default(''),
BOT_SERVER_HOST: z.string().default('0.0.0.0'),
BOT_SERVER_PORT: port().default(80),
@@ -35,12 +34,6 @@ function createConfigFromEnvironment(environment: NodeJS.ProcessEnv) {
})
if (config.BOT_MODE === 'webhook') {
- // validate webhook url in webhook mode
- z.string()
- .url()
- .parse(config.BOT_WEBHOOK, {
- path: ['BOT_WEBHOOK'],
- })
// validate webhook secret in webhook mode
z.string()
.min(1)
diff --git a/src/main.ts b/src/main.ts
index 7b677b46..913b3ed7 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -63,16 +63,6 @@ async function startWebhook() {
? `http://[${info.address}]:${info.port}`
: `http://${info.address}:${info.port}`,
})
-
- // set webhook
- await bot.api.setWebhook(config.BOT_WEBHOOK, {
- allowed_updates: config.BOT_ALLOWED_UPDATES,
- secret_token: config.BOT_WEBHOOK_SECRET,
- })
- logger.info({
- msg: 'Webhook was set',
- url: config.BOT_WEBHOOK,
- })
}
try {
diff --git a/src/server/index.ts b/src/server/index.ts
index 4fadd096..3e9536bf 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -46,6 +46,24 @@ export function createServer(bot: Bot) {
server.get('/', c => c.json({ status: true }))
+ server.get(`/${bot.token}`, async (c) => {
+ const hostname = c.req.header("x-forwarded-host");
+ if (typeof hostname === "string") {
+ const webhookUrl = new URL("webhook", `https://${hostname}`).href;
+ await bot.api.setWebhook(webhookUrl, {
+ allowed_updates: config.BOT_ALLOWED_UPDATES,
+ secret_token: config.BOT_WEBHOOK_SECRET,
+ });
+ return c.json({
+ status: true,
+ });
+ }
+ c.status(500);
+ return c.json({
+ status: false,
+ });
+ });
+
server.post(
'/webhook',
webhookCallback(bot, 'hono', {
diff --git a/tsconfig.json b/tsconfig.json
index fca6c24c..65886b8c 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -18,6 +18,7 @@
"preserveWatchOutput": true
},
"include": [
+ "api/**/*",
"src/**/*"
]
}
diff --git a/vercel.json b/vercel.json
new file mode 100644
index 00000000..f96d4935
--- /dev/null
+++ b/vercel.json
@@ -0,0 +1,12 @@
+{
+ "installCommand": "npm install",
+ "buildCommand": "npm run build",
+ "devCommand": "npm run dev",
+ "outputDirectory": "build",
+ "rewrites": [
+ {
+ "source": "/(.*)",
+ "destination": "/api"
+ }
+ ]
+}
\ No newline at end of file
|