From d28ada1907328de156eeab4543d3519d53053bc0 Mon Sep 17 00:00:00 2001 From: Vladislav Ponomarev Date: Sun, 17 Mar 2024 22:40:52 +0500 Subject: [PATCH] New template --- .nvmrc | 1 + api/update.mjs | 4 ++-- api/webhook.mjs | 8 ++++---- package.json | 20 ++++++++------------ public/index.html | 2 +- scripts/build.mjs | 6 +++++- scripts/start.mjs | 3 +++ 7 files changed, 24 insertions(+), 20 deletions(-) create mode 100644 .nvmrc diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 0000000..9a2a0e2 --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +v20 diff --git a/api/update.mjs b/api/update.mjs index a4b96aa..2cfb280 100644 --- a/api/update.mjs +++ b/api/update.mjs @@ -1,10 +1,10 @@ -import {webhookStream} from "vercel-grammy"; import {bot, secretToken} from "../src/bot.mjs"; +import {webhookStream} from "vercel-grammy"; export const config = {runtime: "edge"}; // Custom grammY handler for incoming updates via webhooks with streaming response -export default webhookStream(bot, { +export const POST = webhookStream(bot, { timeoutMilliseconds: 59_000, secretToken, }); diff --git a/api/webhook.mjs b/api/webhook.mjs index 53aca5e..afd3b30 100644 --- a/api/webhook.mjs +++ b/api/webhook.mjs @@ -1,11 +1,11 @@ -import {setWebhookCallback} from "vercel-grammy"; import {bot, secretToken} from "../src/bot.mjs"; +import {setWebhookCallback} from "vercel-grammy"; export const config = {runtime: "edge"}; // Handler to set webhook url based on request headers -export default setWebhookCallback(bot, { - secret_token: secretToken, +export const POST = setWebhookCallback(bot, { path: "api/update", - onError: "return" + onError: "return", + secretToken, }); diff --git a/package.json b/package.json index 1167b30..b7b4391 100644 --- a/package.json +++ b/package.json @@ -5,19 +5,15 @@ "license": "MIT", "scripts": { "env": "vercel env pull .env", - "polling": "npm run env && npm run start", - "webhook": "npm run tunnel & vercel dev --listen 3000", - "tunnel": "cloudflared tunnel --url http://localhost:3000", - "start": "node --watch -r dotenv/config scripts/start.mjs" + "build": "node scripts/build.mjs", + "start:dev": "vercel dev --listen 3000", + "start:local": "node --watch --env-file=.env scripts/start.mjs", + "start:tunnel": "npx -y cloudflared tunnel --url http://localhost:3000", + "start:webhook": "npm run start:tunnel & npm run start:dev", + "start:polling": "npm run env && npm run start:local" }, "dependencies": { - "grammy": "^1.19.1", - "vercel-grammy": "^0.2.1" - }, - "devDependencies": { - "dotenv": "^16.3.1" - }, - "engines": { - "node": "^18.0.0" + "grammy": "^1.21.1", + "vercel-grammy": "^1.0.0" } } diff --git a/public/index.html b/public/index.html index 05c750d..6d9712d 100644 --- a/public/index.html +++ b/public/index.html @@ -20,7 +20,7 @@ -
+ Press button, if you want set webhook host to: localhost

diff --git a/scripts/build.mjs b/scripts/build.mjs index a7b2754..0196130 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -1,5 +1,5 @@ -import {getURL} from "vercel-grammy"; import {bot, secretToken} from "../src/bot.mjs"; +import {getURL} from "vercel-grammy"; const {VERCEL_ENV} = process.env; @@ -9,6 +9,9 @@ const allowedEnvs = [ // "preview" ]; +// Check bot +await bot.init() + // Exit in case of unsuitable environments if (!allowedEnvs.includes(VERCEL_ENV)) process.exit(); @@ -26,5 +29,6 @@ if (await bot.api.setWebhook(url, options)) { console.info("Webhook set to URL:", url); console.info("Secret token:", secretToken); + console.info("Info:", bot.botInfo); } diff --git a/scripts/start.mjs b/scripts/start.mjs index be49525..ff101c2 100644 --- a/scripts/start.mjs +++ b/scripts/start.mjs @@ -1,4 +1,7 @@ import {bot} from "../src/bot.mjs"; +// Prevent error throw +bot.catch(console.error); + // Starts bot in long-polling mode await bot.start();