Naltox / telegram-node-bot

Node module for creating Telegram bots.
MIT License
720 stars 144 forks source link

redirect to another controller #190

Open developerium opened 6 years ago

developerium commented 6 years ago

Hi after being done with message received, I want to redirect them to another controller of mine.

for example after user sending /start , I want to process what was registered for /start and then redirect them to /hello

is there any way to achieve this functionality?

RobotCharly commented 6 years ago

same question. you solve it?

rotimi-best commented 5 years ago

Hi after being done with message received, I want to redirect them to another controller of mine.

for example after user sending /start , I want to process what was registered for /start and then redirect them to /hello

is there any way to achieve this functionality?

In my opinion I see 2 options,

  1. You can choose to create an inline button for the user to click and then that triggers the other controller.
  2. Or you could make your /start and /hello in one controller so when done with /start you pass the scope as a variable into /hello function and I think it should work Like this:
class IntroController extends TelegramBaseController {
    /**
     * @param {Scope} $
     */
    startHandler($) {
        const scope = $;
        $.sendMessage('Welcome');
        this.helloHandler(scope); //call hello handler here
    }

   /**
     * @param {Scope} $
     */
    helloHandler($) {
       const user = $.message.chat.firstName;
        $.sendMessage(`Hello ${user}`);
    }

    get routes() {
        return {
            'startCommand': 'startHandler',
            'helloCommand': 'helloHandler'
        }
    }
}

tg.router
    .when( new TextCommand('/start', 'startCommand'), new IntroController())
    .when( new TextCommand('/hello', 'helloCommand'), new IntroController())

It might not be the best solution please let me know what you think about this approach

rotimi-best commented 5 years ago

Another simpler would be to call your hello controller in the start. Like this

class StartController extends TelegramBaseController {

    startHandler($) {
        const scope = $;
        $.sendMessage('Welcome');
        const helloController = new HelloController(); //call 2nd controller
        helloController.helloHandler(scope); //Pass the scope as a variable
    }

    get routes() {
        return {
            'startCommand': 'startHandler',
        }
    }
}
class HelloController extends TelegramBaseController {

   /**
     * @param {Scope} $
     */
    helloHandler($) {
       const user = $.message.chat.firstName;
        $.sendMessage(`Hello ${user}`);
    }

    get routes() {
        return {
            'helloCommand': 'helloHandler'
        }
    }
}

bot.router
    .when( new TextCommand('/start', 'startCommand'), new StartController())
    .when( new TextCommand('/hello', 'helloCommand'), new HelloController())