Is there any way to execute helper outside the route?

73 views
Skip to first unread message

Pavel Serikov

unread,
Jan 28, 2017, 4:32:26 AM1/28/17
to Mojolicious
It's better to explain the problem on example. I'm writing bot for Telegram and want to implement getting updates via WebHook and polling (via Mojo::IOLoop) both.

helper answer => sub {
 
my ($c, $update) = shift;
 
...
 
if ($update->{message}{text} eq "something") {
   $api
->sendMessage ({ chat_id => $chat_id, text => '/shot - Get online camera shot' });
 
}
};

...

post
'/'.$config->{telegram_api_token} => sub {
 
my $c = shift;
 
my $update = $c->req->json;
  $c
->answer($update);
  $c
->render(json => "ok");
};

if ($config->{polling}) {
 $api
->deleteWebhook();

 
Mojo::IOLoop->recurring($config->{polling_timeout} => sub {
   
my $updates = $api->getUpdates;
   
my @updates = @{$updates->{result}};
   
   
for my $u (@updates) {
     app
->controller_class->helpers->answer($u); ##### How to call helper answer here ?
   
}
 
});

}

Is there any way to execute helper 'answer' outside the routes or I need to move project from Mojolicious::Lite to Mojolicious ?

Sebastian Riedel

unread,
Jan 28, 2017, 6:31:56 AM1/28/17
to mojol...@googlegroups.com

Shane Burgess

unread,
Feb 20, 2017, 11:43:10 AM2/20/17
to Mojolicious
You can call helpers right from the app.

#Lite
app->helper_name

#Full
$self->app->helper_name

You can also call them from controllers

#Lite
$c->helper_name

#Full
$self->helper_name

You can call them either way in controllers as well. If you need session data in the helper then it is best to use it from the controller. You will lose session data if you call from app but there are ways around it.
Reply all
Reply to author
Forward
0 new messages