telegram-node-bot
telegram-node-bot copied to clipboard
redirect to another controller
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?
same question. you solve it?
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/startand then redirect them to/hellois there any way to achieve this functionality?
In my opinion I see 2 options,
- You can choose to create an inline button for the user to click and then that triggers the other controller.
- 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
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())