diff --git a/bin/doctor_karma.pl b/bin/doctor_karma.pl index c5ba9d4..a9008b7 100755 --- a/bin/doctor_karma.pl +++ b/bin/doctor_karma.pl @@ -24,6 +24,22 @@ my $logger = DoctorKarma::Logger->new; my $telegram = DoctorKarma::Telegram->new( telegram_token => $config->telegram_token ); my $db = DoctorKarma::DB->dbh; +$telegram->set_my_commands( + commands => [ + { + command => 'karma', + description => 'Shows your karma or the one of the user you are replying to', + }, + { + command => 'doctortop', + description => 'Shows the most popular users.', + }, + { + command => 'doctorhate', + description => 'Shows the most hated users.', + } + ] +); while (1) { my $updates = $telegram->get_updates; @@ -70,15 +86,15 @@ sub process_message_with_text { substract_karma_to_replied_message_user($message); return; } - if ( $text eq '/karma' ) { + if ( $text =~ m{^/karma(?:@\w+)?$} ) { show_karma($message); return; } - if ( $text eq '/doctortop' ) { + if ( $text =~ m{^/doctortop(?:@\w+)?$} ) { show_top_ten($message); return; } - if ( $text eq '/doctorhate' ) { + if ( $text =~ m{^/doctorhate(?:@\w+)?$} ) { show_top_ten_hate($message); return; } diff --git a/lib/DoctorKarma/Telegram.pm b/lib/DoctorKarma/Telegram.pm index d83f2ad..257fa8c 100644 --- a/lib/DoctorKarma/Telegram.pm +++ b/lib/DoctorKarma/Telegram.pm @@ -5,7 +5,7 @@ use v5.30.0; use strict; use warnings; -use Types::Standard qw/Str Int Ref/; +use Types::Standard qw/Str Int Ref ArrayRef/; use Params::ValidationCompiler qw(validation_for); use Mojo::UserAgent; @@ -72,6 +72,28 @@ sub _user_agent { } } +{ + my $validator = validation_for( + params => { + commands => { type => ArrayRef } + } + ); + + sub set_my_commands { + my $self = shift; + my %params = $validator->(@_); + my $commands = $params{commands}; + + my $response = $self->_request( + method => q/setMyCommands/, + body => { + commands => $commands, + } + ); + return $response; + } +} + { my $validator = validation_for( params => {