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 user check for the start command

Yevhenii Kulisidi520239900625
diff --git a/src/integrations/telegram/scene/start.scene.ts b/src/integrations/telegram/scene/start.scene.ts
index f001bc09f..0385b85a1 100644
--- a/src/integrations/telegram/scene/start.scene.ts
+++ b/src/integrations/telegram/scene/start.scene.ts
@@ -1,20 +1,20 @@
import { Ctx, Hears, Scene, SceneEnter } from 'nestjs-telegraf'
-import { Markup } from 'telegraf'
import { Context } from '../interface/context.interface'
import { PROJECTS_SCENE_ID } from './projects.scene'
import { SETTINGS_SCENE_ID } from './settings.scene'
+import { TelegramService } from '../telegram.service'
export const START_SCENE_ID = 'start'
@Scene(START_SCENE_ID)
export class StartScene {
+ constructor(private readonly telegramService: TelegramService) {}
+
@SceneEnter()
async onSceneEnter(@Ctx() context: Context) {
- await context.reply(
- '...',
- Markup.keyboard([['πŸ“‚ Projects', 'βš™οΈ Settings']])
- .oneTime()
- .resize(),
+ const { text, extra } = await this.telegramService.getStartMessage(
+ context.from.id,
)
+ await context.replyWithHTML(text, extra)
}
@Hears('πŸ“‚ Projects')
diff --git a/src/integrations/telegram/telegram.module.ts b/src/integrations/telegram/telegram.module.ts
index 94b15d01c..46df5b7c0 100644
--- a/src/integrations/telegram/telegram.module.ts
+++ b/src/integrations/telegram/telegram.module.ts
@@ -2,12 +2,14 @@ import { Module } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { TelegrafModule } from 'nestjs-telegraf'
import { session } from 'telegraf'
+import { UserModule } from 'src/user/user.module'
import { TelegramController } from './telegram.controller'
import { TelegramUpdate } from './telegram.update'
import { StartScene } from './scene/start.scene'
import { ProjectsScene } from './scene/projects.scene'
import { SettingsScene } from './scene/settings.scene'
import { UnlinkAccountScene } from './scene/unlink-account.scene'
+import { TelegramService } from './telegram.service'
@Module({
imports: [
@@ -34,6 +36,7 @@ import { UnlinkAccountScene } from './scene/unlink-account.scene'
middlewares: [session()],
}),
}),
+ UserModule,
],
controllers: [TelegramController],
providers: [
@@ -42,6 +45,7 @@ import { UnlinkAccountScene } from './scene/unlink-account.scene'
ProjectsScene,
SettingsScene,
UnlinkAccountScene,
+ TelegramService,
],
})
export class TelegramModule {}
diff --git a/src/integrations/telegram/telegram.service.ts b/src/integrations/telegram/telegram.service.ts
new file mode 100644
index 000000000..406075e85
--- /dev/null
+++ b/src/integrations/telegram/telegram.service.ts
@@ -0,0 +1,41 @@
+import { Injectable } from '@nestjs/common'
+import { UserService } from 'src/user/user.service'
+import { Markup } from 'telegraf'
+
+@Injectable()
+export class TelegramService {
+ constructor(private readonly userService: UserService) {}
+
+ async getStartMessage(telgramId: number) {
+ const user = await this.userService.getUserByTelegramId(telgramId)
+
+ let text: string
+ let extra: unknown = {}
+
+ if (!user) {
+ text =
+ 'Welcome to the Swetrix Bot!' +
+ '\n\n' +
+ 'Your Telegram ID is: ' +
+ `<code>${telgramId.toString()}</code>` +
+ '\n\n' +
+ 'You can use this ID to link your Telegram account with your Swetrix account.' +
+ '\n\n' +
+ 'To do this, go to your <a href="https://swetrix.com/settings">Swetrix account settings</a> and enter this ID in the Telegram field.' +
+ '\n\n' +
+ 'After that, you can use the bot to manage your projects.'
+ extra = { disable_web_page_preview: true }
+ }
+
+ if (user) {
+ text = 'Welcome to the Swetrix Bot!'
+ extra = {
+ ...Markup.keyboard([['πŸ“‚ Projects', 'βš™οΈ Settings']])
+ .oneTime()
+ .resize(),
+ }
+ }
+
+ return { text, extra }
+ }
+}
diff --git a/src/user/user.service.ts b/src/user/user.service.ts
index d78631ff1..b9ade1602 100644
--- a/src/user/user.service.ts
+++ b/src/user/user.service.ts
@@ -218,4 +218,10 @@ export class UserService {
async getUserByEmail(email: string) {
return this.usersRepository.findOne({ where: { email } })
}
+
+ async getUserByTelegramId(telegramId: number) {
+ return this.usersRepository.findOne({
+ where: { telegramChatId: telegramId },
+ })
+ }
}