App-TeleGramma

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN


New plugin App::TeleGramma::Plugin::Core::YearProgress, keep abreast of the
terrifyingly fast progress of the year.

0.11      2017-04-27 22:38:32+09:30 Australia/Adelaide

Clean up command line options, add --help and --version.

0.10      2017-04-27 20:36:19+09:30 Australia/Adelaide

Fix bug where the /timer plugin would always respond with help.

When timer expires, include the @username of the requester.

0.09      2017-04-27 15:35:34+09:30 Australia/Adelaide

New plugin App::TeleGramma::Plugin::Core::Timer to allow users to set timers.

0.08      2017-04-26 16:46:46+09:30 Australia/Adelaide

Use newest Mojolicious and change from deprecated/removed functions.

0.07      2017-03-16 20:15:29+10:30 Australia/Adelaide

Support for commands to be bare regexes.

Add App::TeleGramma::Plugin::Core::Thants plugin to give thanks, the

MANIFEST  view on Meta::CPAN

lib/App/TeleGramma/Plugin/Base.pm
lib/App/TeleGramma/Plugin/Core/Fortune.pm
lib/App/TeleGramma/Plugin/Core/Stenographer.pm
lib/App/TeleGramma/Plugin/Core/Thants.pm
lib/App/TeleGramma/Plugin/Core/Timer.pm
lib/App/TeleGramma/Plugin/Core/YearProgress.pm
lib/App/TeleGramma/PluginManager.pm
lib/App/TeleGramma/Store.pm
t/basic.t
t/plugins/core-thants.t
t/plugins/core-timer.t
t/plugins/prereqs.t
t/store/basic.t

lib/App/TeleGramma/Plugin/Core/Timer.pm  view on Meta::CPAN

package App::TeleGramma::Plugin::Core::Timer;
$App::TeleGramma::Plugin::Core::Timer::VERSION = '0.14';
# ABSTRACT: TeleGramma plugin to set timers

use Mojo::Base 'App::TeleGramma::Plugin::Base';
use App::TeleGramma::BotAction::Listen;
use App::TeleGramma::Constants qw/:const/;
use Time::Duration::Parse qw/parse_duration/;

sub synopsis {
  "Set timers for a future reminder"
}

sub default_config {
  my $self = shift;
  return { };
}

sub register {
  my $self = shift;

  my $timer_help = App::TeleGramma::BotAction::Listen->new(
    command  => qr{/timer}i,
    response => sub { $self->timer_help(@_) }
  );

  my $timer_set_exact = App::TeleGramma::BotAction::Listen->new(
    command  => qr{/timer\s(.*)}i,
    response => sub { $self->timer_set(@_) }
  );

  return ($timer_set_exact, $timer_help);
}

sub timer_help {
  my $self = shift;
  my $msg  = shift;
  $self->reply_to($msg, "examples: /timer remind me to weed and feed in 3 hours");
  return PLUGIN_RESPONDED_LAST;
}

sub timer_set {
  my $self = shift;
  my $msg  = shift;

  my $text = $msg->text;
  my ($request) = ($text =~ m{/timer\s+(.*)}i);

  # remove some common prefixes from the request
  $request =~ s/^remind me to//;
  $request =~ s/^remind me//;
  $request =~ s/^tell me to//;

  # try to parse the thing, starting at the first number
  my ($timer_text, $duration_text) = ($request =~ /^\s*(.+)\s+in\s+(\d.*)/);
  if (! $duration_text) {
    $self->reply_to($msg, "Sorry, I can't work out when you mean from '$text'");
    return PLUGIN_RESPONDED_LAST;
  }

  my $duration = eval { parse_duration($duration_text) };
  if ($@ || ! $duration) {
    $self->reply_to($msg, "Sorry, I can't work out when you mean from '$duration_text'");
    return PLUGIN_RESPONDED_LAST;
  }

  Mojo::IOLoop->timer($duration => sub {
    my $loop = shift;
    my $message = "Hey \@" . $msg->from->username . ", this is your reminder to $timer_text";
    $self->reply_to($msg, $message);
  });

  $self->reply_to($msg, "Will remind you '$timer_text' in $duration_text");
  return PLUGIN_RESPONDED_LAST;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

App::TeleGramma::Plugin::Core::Timer - TeleGramma plugin to set timers

=head1 VERSION

version 0.14

=head1 AUTHOR

Justin Hawkins <justin@hawkins.id.au>

=head1 COPYRIGHT AND LICENSE

t/plugins/core-timer.t  view on Meta::CPAN

ok($t, 'created');
$t->app($app);

my ($a_l, $h_l) = $t->register;
my $msg = Telegram::Bot::Message->new;
my $user = Telegram::Bot::Object::User->new(id => 123, username => 'fred');
$msg->chat($user);
$msg->from($user);

# help
$expected_text = "examples: /timer remind me to weed and feed in 3 hours";
$expected_chat_id = 123;
$msg->text("/timer");
is ($h_l->process_message($msg), PLUGIN_RESPONDED_LAST);
is ($count, 1);

# non matching help
$msg->text("/blah blah");
is($h_l->process_message($msg), PLUGIN_DECLINED);

# non matching timer set
$msg->text("/blah blah");
is($a_l->process_message($msg), PLUGIN_DECLINED);

# bad entry
$expected_text = "Sorry, I can't work out when you mean from '/timer remind me to impeach Trump tomorrow'";
$expected_chat_id = 123;
$msg->text("/timer remind me to impeach Trump tomorrow");
is($a_l->process_message($msg), PLUGIN_RESPONDED_LAST);
is ($count, 2);

# set a timer
$expected_text = "Will remind you 'weed and feed' in 1 second";
$expected_chat_id = 123;
$msg->text("/timer remind me to weed and feed in 1 second");
$a_l->process_message($msg);
is ($count, 3);

# sleep and see that we get the timer message
#sleep 1;
$expected_text = "Hey \@fred, this is your reminder to weed and feed";
$expected_chat_id = 123;
Mojo::IOLoop->one_tick;
is ($count, 4, 'now received 4 replies');

# bad timer
$expected_text = "Sorry, I can't work out when you mean from '1 hogshead'";
$expected_chat_id = 123;
$msg->text("/timer remind me to weed and feed in 1 hogshead");
$a_l->process_message($msg);
is ($count, 5);

# test the timer actually works
my $time = time();
$expected_text = "Will remind you 'weed and feed' in 5 seconds";
$expected_chat_id = 123;
$msg->text("/timer remind me to weed and feed in 5 seconds");
$a_l->process_message($msg);
is ($count, 6);

$expected_text = "Hey \@fred, this is your reminder to weed and feed";
$expected_chat_id = 123;
Mojo::IOLoop->one_tick;
is ($count, 7);
my $diff = time() - $time;
ok ($diff > 4 && $diff < 7, 'correct time has passed');



( run in 0.575 second using v1.01-cache-2.11-cpan-49f99fa48dc )