Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

NestJS Telegraf module was fine-tuned

Yevhenii Kulisidif12927902024
diff --git a/.env.example b/.env.example
index fb9db0e40..5510c8ab0 100644
--- a/.env.example
+++ b/.env.example
@@ -63,3 +63,11 @@ GOOGLE_OAUTH2_CLIENT_SECRET=
# Github SSO
GITHUB_OAUTH2_CLIENT_ID=
GITHUB_OAUTH2_CLIENT_SECRET=
+
+# Telegram integration
+TELEGRAM_BOT_TOKEN=''
+# Telegram webhook (production only)
+TELEGRAM_WEBHOOK_DOMAIN='' # domain name
+TELEGRAM_WEBHOOK_PATH='/v1/telegram/webhook'
+TELEGRAM_WEBHOOK_IP_ADDRESS='' # server ip address
+TELEGRAM_WEBHOOK_SECRET_TOKEN='' # random string
diff --git a/src/app.module.ts b/src/app.module.ts
index 94f6ab27b..39b2bcccf 100644
--- a/src/app.module.ts
+++ b/src/app.module.ts
@@ -25,7 +25,12 @@ import { isDevelopment, isTgTokenPresent } from './common/constants'
import { IntegrationsModule } from './integrations/integrations.module'
const modules = [
- ConfigModule.forRoot({ envFilePath: '.env', isGlobal: true }),
+ ConfigModule.forRoot({
+ cache: true,
+ envFilePath: '.env',
+ expandVariables: true,
+ isGlobal: true,
+ }),
TypeOrmModule.forRoot({
type: 'mysql',
host: process.env.MYSQL_HOST,
diff --git a/src/integrations/telegram/telegram.module.ts b/src/integrations/telegram/telegram.module.ts
index 25383ff58..fed01bf7d 100644
--- a/src/integrations/telegram/telegram.module.ts
+++ b/src/integrations/telegram/telegram.module.ts
@@ -1,4 +1,31 @@
import { Module } from '@nestjs/common'
+import { ConfigService } from '@nestjs/config'
+import { TelegrafModule } from 'nestjs-telegraf'
-@Module({})
+@Module({
+ imports: [
+ TelegrafModule.forRootAsync({
+ inject: [ConfigService],
+ useFactory: (configService: ConfigService) => ({
+ token: configService.get<string>('TELEGRAM_BOT_TOKEN'),
+ launchOptions: {
+ dropPendingUpdates: false,
+ ...(process.env.NODE_ENV === 'production' && {
+ webhook: {
+ domain: configService.get<string>('TELEGRAM_WEBHOOK_DOMAIN'),
+ hookPath: configService.get<string>('TELEGRAM_WEBHOOK_PATH'),
+ ipAddress: configService.get<string>(
+ 'TELEGRAM_WEBHOOK_IP_ADDRESS',
+ ),
+ maxConnections: 100,
+ secretToken: configService.get<string>(
+ 'TELEGRAM_WEBHOOK_SECRET_TOKEN',
+ ),
+ },
+ }),
+ },
+ }),
+ }),
+ ],
+})
export class TelegramModule {}