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!

added basic tg integration bot

Blue Mouse3f0c1455f099
diff --git a/.env.example b/.env.example
index 26b3e1100..aab5e7b42 100644
--- a/.env.example
+++ b/.env.example
@@ -36,4 +36,7 @@ PASSWORD=12345678
# Use New Relic monitoring, leave empty if not using it
USE_NEW_RELIC=
-NEW_RELIC_LICENSE_KEY=
\ No newline at end of file
+NEW_RELIC_LICENSE_KEY=
+
+# Telegram Integration Bot
+TG_BOT_TOKEN=
\ No newline at end of file
diff --git a/package.json b/package.json
index f63b48c06..3f2d1bd81 100644
--- a/package.json
+++ b/package.json
@@ -51,6 +51,7 @@
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
"mysql2": "^2.3.3",
+ "nestjs-telegraf": "^2.6.0",
"newrelic": "^9.0.0",
"otplib": "^12.0.1",
"passport": "^0.6.0",
@@ -61,6 +62,7 @@
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.5.6",
+ "telegraf": "^4.8.6",
"typeorm": "^0.2.45",
"ua-parser-js": "^1.0.2",
"uuid": "^8.3.2",
diff --git a/src/app.module.ts b/src/app.module.ts
index e3ee75498..19ddc868c 100644
--- a/src/app.module.ts
+++ b/src/app.module.ts
@@ -2,6 +2,7 @@ import { Module } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'
import { ConfigModule } from '@nestjs/config'
import { ScheduleModule } from '@nestjs/schedule'
+import { TelegrafModule } from 'nestjs-telegraf'
import { AuthModule } from './auth/auth.module'
import { UserModule } from './user/user.module'
@@ -17,32 +18,42 @@ import { ProjectShare } from './project/entity/project-share.entity'
import { TaskManagerModule } from './task-manager/task-manager.module'
import { WebhookModule } from './webhook/webhook.module'
import { PingModule } from './ping/ping.module'
+import { TGModule } from './tg-integration/tg.module'
+
+const modules = [
+ ConfigModule.forRoot({ envFilePath: '.env' }),
+ TypeOrmModule.forRoot({
+ type: 'mysql',
+ host: process.env.MYSQL_HOST,
+ port: 3306,
+ username: process.env.MYSQL_USER,
+ password: process.env.MYSQL_ROOT_PASSWORD,
+ database: process.env.MYSQL_DATABASE,
+ synchronize: process.env.NODE_ENV === 'development',
+ entities: [User, ActionToken, Project, ProjectShare],
+ }),
+ ScheduleModule.forRoot(),
+ TaskManagerModule,
+ AuthModule,
+ UserModule,
+ MailerModule,
+ ActionTokensModule,
+ TwoFactorAuthModule,
+ ProjectModule,
+ AnalyticsModule,
+ WebhookModule,
+ PingModule,
+ TGModule,
+]
+
+if (process.env.TG_BOT_TOKEN) {
+ modules.push(TelegrafModule.forRoot({
+ token: process.env.TG_BOT_TOKEN,
+ }))
+}
@Module({
- imports: [
- ConfigModule.forRoot({ envFilePath: '.env' }),
- TypeOrmModule.forRoot({
- type: 'mysql',
- host: process.env.MYSQL_HOST,
- port: 3306,
- username: process.env.MYSQL_USER,
- password: process.env.MYSQL_ROOT_PASSWORD,
- database: process.env.MYSQL_DATABASE,
- synchronize: process.env.NODE_ENV === 'development',
- entities: [User, ActionToken, Project, ProjectShare],
- }),
- ScheduleModule.forRoot(),
- TaskManagerModule,
- AuthModule,
- UserModule,
- MailerModule,
- ActionTokensModule,
- TwoFactorAuthModule,
- ProjectModule,
- AnalyticsModule,
- WebhookModule,
- PingModule,
- ],
+ imports: modules,
})
export class AppModule {}
diff --git a/src/common/decorators/update-type.decorator.ts b/src/common/decorators/update-type.decorator.ts
new file mode 100644
index 000000000..9b877b861
--- /dev/null
+++ b/src/common/decorators/update-type.decorator.ts
@@ -0,0 +1,7 @@
+import { createParamDecorator, ExecutionContext } from '@nestjs/common'
+import { TelegrafExecutionContext } from 'nestjs-telegraf'
+
+export const UpdateType = createParamDecorator(
+ (_, ctx: ExecutionContext) =>
+ TelegrafExecutionContext.create(ctx).getContext().updateType,
+)
diff --git a/src/common/interfaces/context.interface.ts b/src/common/interfaces/context.interface.ts
new file mode 100644
index 000000000..aaa3405a8
--- /dev/null
+++ b/src/common/interfaces/context.interface.ts
@@ -0,0 +1,4 @@
+import { Scenes } from 'telegraf'
+
+// eslint-disable-next-line @typescript-eslint/no-empty-interface
+export interface Context extends Scenes.SceneContext {}
diff --git a/src/tg-integration/tg.module.ts b/src/tg-integration/tg.module.ts
new file mode 100644
index 000000000..e308395f7
--- /dev/null
+++ b/src/tg-integration/tg.module.ts
@@ -0,0 +1,7 @@
+import { Module } from '@nestjs/common'
+import { SwetrixUpdate } from './tg.update'
+
+@Module({
+ providers: [SwetrixUpdate],
+})
+export class TGModule {}
diff --git a/src/tg-integration/tg.update.ts b/src/tg-integration/tg.update.ts
new file mode 100644
index 000000000..b7ec1fc6d
--- /dev/null
+++ b/src/tg-integration/tg.update.ts
@@ -0,0 +1,27 @@
+import { Command, Ctx, Hears, Start, Update, Sender } from 'nestjs-telegraf';
+import { UpdateType as TelegrafUpdateType } from 'telegraf/typings/telegram-types';
+import { Context } from 'src/common/interfaces/context.interface'
+import { UpdateType } from 'src/common/decorators/update-type.decorator'
+
+const HELLO_SCENE_ID = 'HELLO_SCENE_ID'
+
+@Update()
+export class SwetrixUpdate {
+ @Start()
+ onStart(): string {
+ return 'Say hello to me';
+ }
+
+ @Hears(['hi', 'hello', 'hey', 'qq'])
+ onGreetings(
+ @UpdateType() updateType: TelegrafUpdateType,
+ @Sender('first_name') firstName: string,
+ ): string {
+ return `Hey ${firstName}`;
+ }
+
+ @Command('scene')
+ async onSceneCommand(@Ctx() ctx: Context): Promise<void> {
+ await ctx.scene.enter(HELLO_SCENE_ID);
+ }
+}