Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement scenes support #72

Closed
ghost opened this issue Apr 29, 2020 · 6 comments
Closed

Implement scenes support #72

ghost opened this issue Apr 29, 2020 · 6 comments
Labels
bug Something isn't working good first issue Good for newcomers question Further information is requested

Comments

@ghost
Copy link

ghost commented Apr 29, 2020

Hey

Tried to use scenes, but got some errors.

Scene init:

  @TelegrafUse()
  useScenes(ctx, next) {
    // Greeter scene
    const greeter = new Scene('greeter');
    greeter.enter(ctx => ctx.reply('Hi'));
    greeter.leave(ctx => ctx.reply('Bye'));
    greeter.hears(/hi/gi, leave());
    greeter.on('message', ctx => ctx.reply('Send `hi`'));

    // Create scene manager
    const stage = new Stage();
    stage.command('cancel', leave());

    // Scene registration
    stage.register(greeter);

    stage.middleware(ctx);
    next();
  }

But, when i tried to enter in:

  @TelegrafStart()
  async startHandler(ctx) {
    ctx.scene.enter('greeter');
}

Got this one: Cannot read property 'enter' of undefined.

@bukhalo bukhalo added good first issue Good for newcomers question Further information is requested labels Apr 29, 2020
@bukhalo
Copy link
Owner

bukhalo commented Apr 29, 2020

@VyacheslavSaloidWork I need a little time to answer this question.

@all-contributors please add @VyacheslavSaloidWork for bug

@allcontributors
Copy link
Contributor

@bukhalo

I've put up a pull request to add @VyacheslavSaloidWork! 🎉

@bukhalo
Copy link
Owner

bukhalo commented May 2, 2020

Really, you can’t connect middleware by forwarding the arguments from @TelegrafUse function decorator. Temporary, you can connect middleware through getting TelegrafProvider from app context. This is shit, but it works 😁.

As example:

// somewhere in main.ts
const telegrafProvider = app.get('TelegrafProvider');
telegrafProvider.use(stage.middleware());

I fix that ASAP, thx.

@bukhalo bukhalo added the bug Something isn't working label May 2, 2020
@bukhalo
Copy link
Owner

bukhalo commented May 3, 2020

Now (in v1.2.0) you can connect any middleware using a @InjectBot() decorator.

Try something like that:

import {
  InjectBot,
  Stage,
  Composer,
  WizardScene,
  TelegrafProvider,
  session,
  Markup,
} from 'nestjs-telegraf';

export class AppUpdate {
  constructor(@InjectBot() private readonly bot: TelegrafProvider) {
    this.superWizardScene();
  }

  superWizardScene() {
    const stepHandler = new Composer();
    stepHandler.action('next', ctx => {
      ctx.reply('Step 2. Via inline button');
      return ctx.wizard.next();
    });
    stepHandler.command('next', ctx => {
      ctx.reply('Step 2. Via command');
      return ctx.wizard.next();
    });
    stepHandler.use(ctx =>
      ctx.replyWithMarkdown('Press `Next` button or type /next'),
    );

    const superWizard = new WizardScene(
      'super-wizard',
      ctx => {
        ctx.reply(
          'Step 1',
          Markup.inlineKeyboard([
            Markup.urlButton('❤️', 'http://telegraf.js.org'),
            Markup.callbackButton('➡️ Next', 'next'),
          ]).extra(),
        );
        return ctx.wizard.next();
      },
      stepHandler,
      ctx => {
        ctx.reply('Step 3');
        return ctx.wizard.next();
      },
      ctx => {
        ctx.reply('Step 4');
        return ctx.wizard.next();
      },
      ctx => {
        ctx.reply('Done');
        return ctx.scene.leave();
      },
    );

    const stage = new Stage([superWizard], { default: 'super-wizard' });
    this.bot.use(session());
    this.bot.use(stage.middleware());
  }
}

@bukhalo bukhalo closed this as completed May 3, 2020
@ghost
Copy link
Author

ghost commented May 6, 2020

Thank you, man

It's really so helpful

@meowlanguages
Copy link

Thank you, man

It's really so helpful

Nothing helpful, it didnt work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants