Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
New template
Browse files Browse the repository at this point in the history
  • Loading branch information
PonomareVlad committed Mar 17, 2024
1 parent 396bd76 commit d28ada1
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 20 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v20
4 changes: 2 additions & 2 deletions api/update.mjs
Original file line number Diff line number Diff line change
@@ -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,
});
8 changes: 4 additions & 4 deletions api/webhook.mjs
Original file line number Diff line number Diff line change
@@ -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,
});
20 changes: 8 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</style>
</head>
<body>
<form action="/api/webhook.mjs" name="webhook">
<form action="/api/webhook.mjs" method="post" name="webhook">
<span>Press button, if you want set webhook host to:</span>
<output id="host">localhost</output>
<br><br>
Expand Down
6 changes: 5 additions & 1 deletion scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();

Expand All @@ -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);

}
3 changes: 3 additions & 0 deletions scripts/start.mjs
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit d28ada1

Please sign in to comment.