Skip to content

Commit

Permalink
feat: seamless scene transition
Browse files Browse the repository at this point in the history
  • Loading branch information
dodyagung committed Apr 4, 2024
1 parent 99b5f94 commit d110e7f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 76 deletions.
33 changes: 33 additions & 0 deletions src/sale/sale.common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Markup } from 'telegraf';
import { InlineKeyboardButton } from 'telegraf/typings/core/types/typegram';
import { ExtraEditMessageText } from 'telegraf/typings/telegram-types';
import { SceneContext } from 'telegraf/scenes';

type Hideable<B> = B & { hide?: boolean };
type HideableIKBtn = Hideable<InlineKeyboardButton>;

const extra = (keyboard: HideableIKBtn[][]): ExtraEditMessageText => {
return {
parse_mode: 'MarkdownV2',
link_preview_options: {
is_disabled: true,
},
reply_markup: Markup.inlineKeyboard(keyboard).reply_markup,
};
};

export const editMessage = async (
ctx: SceneContext,
message: string,
keyboard: HideableIKBtn[][],
): Promise<void> => {
await ctx.editMessageText(message, extra(keyboard));
};

export const sendMessage = async (
ctx: SceneContext,
message: string,
keyboard: HideableIKBtn[][],
): Promise<void> => {
await ctx.reply(message, extra(keyboard));
};
3 changes: 1 addition & 2 deletions src/sale/sale.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import { SaleUpdate } from './sale.update';
import { PrismaModule } from 'src/prisma/prisma.module';
import { ConfigModule } from '@nestjs/config';
import { SaleService } from './sale.service';
import { SaleWizard } from './sale.wizard';
import { SaleScene } from './scene/sale.scene';
import { WelcomeScene } from './scene/welcome.scene';

@Module({
providers: [SaleUpdate, SaleService, SaleWizard, SaleScene, WelcomeScene],
providers: [SaleUpdate, SaleService, SaleScene, WelcomeScene],
imports: [ConfigModule, PrismaModule],
})
export class SaleModule {}
2 changes: 1 addition & 1 deletion src/sale/sale.update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ export class SaleUpdate {

@Hears(/.+/)
async onFallback(): Promise<string> {
return 'Wrong command or input, please restart by clicking /start.';
return 'Wrong input, invalid command or something error.\n\nPlease restart by clicking /start.';
}
}
39 changes: 0 additions & 39 deletions src/sale/sale.wizard.ts

This file was deleted.

24 changes: 6 additions & 18 deletions src/sale/scene/sale.scene.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
import { Scene, SceneEnter, Ctx, Action } from 'nestjs-telegraf';
import { SceneContext } from 'telegraf/scenes';
import { Context, Markup } from 'telegraf';
import { Markup } from 'telegraf';
import { SaleUpdate } from '../sale.update';
import { editMessage } from '../sale.common';

@Scene('SALE_SCENE')
export class SaleScene {
constructor(private saleUpdate: SaleUpdate) {}

@SceneEnter()
async onSceneEnter(@Ctx() ctx: Context): Promise<void> {
const keyboard = Markup.inlineKeyboard([
[
Markup.button.callback('🔙 Back', 'back'),
// Markup.button.callback('👤 My Profile', 'profile'),
],
// [
// Markup.button.callback('❓ Tutorial', 'tutorial'),
// Markup.button.callback('🤖 About', 'about'),
// ],
]);
async onSceneEnter(@Ctx() ctx: SceneContext): Promise<void> {
const keyboard = [[Markup.button.callback('🔙 Back', 'back')]];
const message = ' sale scene';

await ctx.editMessageText('this is sale scene', {
link_preview_options: {
is_disabled: true,
},
reply_markup: keyboard.reply_markup,
});
await editMessage(ctx, message, keyboard);
}

@Action('back')
Expand Down
28 changes: 12 additions & 16 deletions src/sale/scene/welcome.scene.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import { Scene, SceneEnter, Ctx, Action, Sender } from 'nestjs-telegraf';
import { SceneContext } from 'telegraf/scenes';
import { Context, Markup } from 'telegraf';
import { SaleUpdate } from '../sale.update';
import { Markup } from 'telegraf';
import { ConfigService } from '@nestjs/config';
import { RESET_DAY, SALE_DAY, TIMEZONE, TODAY } from '../sale.constant';
import { editMessage, sendMessage } from '../sale.common';

@Scene('WELCOME_SCENE')
export class WelcomeScene {
constructor(
private saleUpdate: SaleUpdate,
private configService: ConfigService,
) {}
constructor(private configService: ConfigService) {}

@SceneEnter()
async onSceneEnter(
@Ctx() ctx: Context,
@Ctx() ctx: SceneContext,
@Sender('first_name') firstName: string,
@Sender('last_name') lastName: string,
): Promise<void> {
const keyboard = Markup.inlineKeyboard([
const keyboard = [
[
Markup.button.callback('💰 My Sale', 'sale'),
Markup.button.callback('👤 My Profile', 'profile'),
Expand All @@ -27,13 +24,13 @@ export class WelcomeScene {
Markup.button.callback('❓ Tutorial', 'tutorial'),
Markup.button.callback('🤖 About', 'about'),
],
]);
];

const user_joined = ['creator', 'administrator', 'member'].includes(
(
await ctx.telegram.getChatMember(
this.configService.get<string>('TELEGRAM_GROUP_ID')!,
ctx.message?.from.id ?? 0,
ctx.from?.id ?? 0,
)
).status,
);
Expand All @@ -55,12 +52,11 @@ export class WelcomeScene {
message += `├ Joined : \`${user_joined ? 'Yes' : 'No'}\`\n`;
message += `└ Link : [Click Here](${this.configService.get<string>('TELEGRAM_GROUP_LINK')})`;

await ctx.replyWithMarkdownV2(message, {
link_preview_options: {
is_disabled: true,
},
reply_markup: keyboard.reply_markup,
});
if (ctx.callbackQuery) {
await editMessage(ctx, message, keyboard);
} else {
await sendMessage(ctx, message, keyboard);
}
}

@Action('sale')
Expand Down

0 comments on commit d110e7f

Please sign in to comment.