Closed zverev closed 8 years ago
Use a controller in otherwise() and write the logic in there. Just remember you need to set privacy mode off to receive non-command messages.
How do I do this? There is nothing about privacy mode in docs.
Why not just let routes to be parsed by multiple controllers? Like this:
tg.router.
when(['/start'], ['StartController', 'LogController']).
when(['/stop'], ['StopController', 'LogController']).
when(['/restart'], ['RestartController', 'LogController']).
otherwise('OtherwiseController')
It seems to me, that otherwise
statement has different meaning: to parse message, that hasn't been caught by when
.
Here is the other solution - to add any
statement (also supporting multiple controllers).
tg.router.
any(['LogController'])
when(['/start'], 'StartController').
when(['/stop'], ['StopController', 'FooBarController']).
when(['/restart'], 'RestartController').
otherwise('OtherwiseController')
Maybe it would be useful to add special statements for different content types (e.g. location, photo).
@zverev, you're right otherwise will be called when there is no controller for that message, i like the any
idea, i will add this in new version of module
@Naltox, what about multiple controllers?
@zverev I dont think it's a good idea, how do you decide witch controller will answer?
@Naltox, every controller will execute. Sequentially or parallel (perhaps, we'll need some kind of flag for that).
Here is a live example: I store users in my db and for some requests I want to get user object or create user if it doesn't exists. So I could create EnsureUser
controller and execute it before each controller, that needs user.
when(['/request'], ['EnsureUser', 'FooBarController'])
Otherwise I have to repeat 'ensure user' code in each controller.
Well, while I wrote this, I realized, that the problem is deeper that it seemed to me. At least we'll need to pass arguments between controllers..
@zverev I will add 'middleware' in new version, i think it will be helpful in this cases. You will be able to add prepare function for all updates, and that function will decide call or not to call your controller, and it will be able to pass some argument. I think it will solve your problem.
Also there will be layer for db in new version.
How do I listen on any message sent by user? For example I want to update some internal data on location sent. Or log any user message.