-
Notifications
You must be signed in to change notification settings - Fork 32
/
fastify.ts
35 lines (26 loc) · 1.1 KB
/
fastify.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// npm i grammy fastify @ngrok/[email protected]
import { Bot, webhookCallback } from 'grammy';
import fastify from 'fastify';
import ngrok from '@ngrok/ngrok'; // Note: This example is pinned to 0.9.0 (unstable)
// This example to use grammY with Fastify, Ngrok with secret_token
// Fastify server instance
const server = fastify();
const { TOKEN, PORT, SECRET_TOKEN } = process.env;
const bot = new Bot(TOKEN);
bot.command('start', ctx => ctx.reply("Hi, I'm run faster than you :)"));
bot.on('message:text', ctx => ctx.reply(`You said: ${ctx.message.text}`));
// Receive webhook updates on path https://example.com/<YOUR-TOKEN>
server.post(`/${TOKEN}`, webhookCallback(bot, 'fastify', { secretToken: SECRET_TOKEN }));
// Fastify error handler
server.setErrorHandler(async error => {
console.error(error);
});
// Run The Server!
server.listen({ port: +PORT }, async (error) => {
if (error) {
console.error(error);
process.exit(1);
}
const listener = await ngrok.connect(+PORT);
await bot.api.setWebhook(`${listener.url()}/${TOKEN}`, { secret_token: SECRET_TOKEN });
});