Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
feat(telegram): add help for telegram bot
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Apr 8, 2019
1 parent 0a5cb56 commit 983f9c2
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 9 deletions.
8 changes: 4 additions & 4 deletions back/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export class AppModule implements NestModule {
public configure(consumer: MiddlewareConsumer) {
this.telegramBot.init(this.moduleRef)

// if (this.config.isDev()) {
// // in dev use long poll
// this.telegramBot.startPolling()
// }
if (this.config.isDev()) {
// in dev use long poll
this.telegramBot.startPolling()
}
}
}
5 changes: 4 additions & 1 deletion back/src/telegram/telegram.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'
import { TelegramModule as OriginalTelegramModule } from 'nest-telegram'

import { ConfigModule } from '@back/config/config.module'
import { UtilsModule } from '@back/utils/utils.module'

import { TelegramOptionsFactory } from './TelegramOptionsFactory'
import { HelpActions } from './telegram/actions/HelpActions'

@Module({
imports: [
OriginalTelegramModule.fromFactory({
imports: [ConfigModule],
useClass: TelegramOptionsFactory,
}),
UtilsModule,
],
providers: [TelegramOptionsFactory],
providers: [TelegramOptionsFactory, HelpActions],
exports: [OriginalTelegramModule],
})
export class TelegramModule implements NestModule {
Expand Down
16 changes: 16 additions & 0 deletions back/src/telegram/telegram/actions/HelpActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Injectable } from '@nestjs/common'
import { Context, TelegramActionHandler } from 'nest-telegram'

import { Templating } from '@back/utils/infrastructure/Templating/Templating'

@Injectable()
export class HelpActions {
public constructor(private readonly templating: Templating) {}

@TelegramActionHandler({ command: '/help' })
public async income(ctx: Context) {
const responseText = await this.templating.render('telegram/help', {})

await ctx.reply(responseText)
}
}
10 changes: 6 additions & 4 deletions back/src/user/presentation/telegram/actions/AuthActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TelegramActionHandler, Context, PipeContext } from 'nest-telegram'

import { Authenticator } from '@back/user/application/Authenticator'
import { Registrator } from '@back/user/application/Registrator'
import { Templating } from '@back/utils/infrastructure/Templating/Templating'

import { IsKnownUser } from '../transformer/IsKnownUser'

Expand All @@ -11,18 +12,19 @@ export class AuthActions {
constructor(
private readonly authenticator: Authenticator,
private readonly registrator: Registrator,
private readonly templating: Templating,
) {}

@TelegramActionHandler({ onStart: true })
public async hello(
ctx: Context,
@PipeContext(IsKnownUser) loggedIn: boolean,
) {
await ctx.reply('Hello!')
const responseText = await this.templating.render('telegram/start', {
loggedIn,
})

await ctx.reply(
loggedIn ? 'Welcome back!' : 'To auth send me "/auth login password"',
)
await ctx.reply(responseText)
}

@TelegramActionHandler({ command: 'auth' })
Expand Down
2 changes: 2 additions & 0 deletions back/src/user/user.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TypeOrmModule } from '@nestjs/typeorm'

import { ConfigModule } from '@back/config/config.module'
import { DbModule } from '@back/db/db.module'
import { UtilsModule } from '@back/utils/utils.module'

import { AuthController } from './presentation/http/controller/AuthController'
import { ProfileController } from './presentation/http/controller/ProfileController'
Expand All @@ -29,6 +30,7 @@ import { PasswordEncoder } from './infrastructure/PasswordEncoder/PasswordEncode
@Module({
imports: [
DbModule,
UtilsModule,
TypeOrmModule.forFeature([User]),
JwtModule.registerAsync({
imports: [ConfigModule],
Expand Down
14 changes: 14 additions & 0 deletions back/templates/telegram/help.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
I am Checkmoney Robot

My site — https://checkmoney.space
Mirror — https://checkmoney.ml

Available commands:

To create outcome transaction:
/outcome 100 usd Lunch

To create income transaction:
/income 10000 eur ESA

Of course, you can use any amounts, currencies and comments.
5 changes: 5 additions & 0 deletions back/templates/telegram/start.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Hello!

{{#if loggedIn}}Welcome back{{else}}To auth send me "/auth login password"{{/if}}

If you need help, just send me /help

0 comments on commit 983f9c2

Please sign in to comment.