Adding /help.

This commit is contained in:
sergiotarxz 2022-02-09 19:09:18 +01:00
parent bca8c0c8d1
commit 9bc1419231
1 changed files with 49 additions and 28 deletions

View File

@ -23,33 +23,36 @@ my $config = DoctorKarma::Config->new;
my $logger = DoctorKarma::Logger->new; my $logger = DoctorKarma::Logger->new;
my $telegram = my $telegram =
DoctorKarma::Telegram->new( telegram_token => $config->telegram_token ); DoctorKarma::Telegram->new( telegram_token => $config->telegram_token );
my $db = DoctorKarma::DB->dbh; my $db = DoctorKarma::DB->dbh;
$telegram->set_my_commands( my @commands = (
commands => [ {
{ command => 'karma',
command => 'karma', description =>
description => 'Shows your karma or the one of the user you are replying to.',
'Shows your karma or the one of the user you are replying to', },
}, {
{ command => 'doctortop',
command => 'doctortop', description => 'Shows the most popular users.',
description => 'Shows the most popular users.', },
}, {
{ command => 'doctorhate',
command => 'doctorhate', description => 'Shows the most hated users.',
description => 'Shows the most hated users.', },
}, {
{ command => 'about',
command => 'about', description =>
description => 'Shows license information, author and where to find the code.',
'Shows license information, author and where to find the code.', },
}, {
{ command => 'donate',
command => 'donate', description => 'Shows how to donate.',
description => 'Shows how to donate', },
} {
] command => 'help',
description => 'Shows this help.',
}
); );
$telegram->set_my_commands( commands => [@commands] );
while (1) { while (1) {
my $updates = $telegram->get_updates; my $updates = $telegram->get_updates;
@ -116,14 +119,32 @@ sub process_message_with_text {
show_donate($message); show_donate($message);
return; return;
} }
if ( $text =~ m{^/help(?:@\w+)?$} ) {
show_help($message);
return;
}
if ( defined $reply_to_message && $text =~ /^(?:-|\+)\d+$/ ) { if ( defined $reply_to_message && $text =~ /^(?:-|\+)\d+$/ ) {
error_too_much_karma($message); error_too_much_karma($message);
return; return;
} }
} }
sub show_help {
my $message = shift;
my $chat_id = $message->{chat}{id};
my $html = "<b><u>List of commands</u></b>\n\n";
for my $command (@commands) {
$html .= "<b>@{[xml_escape('/'.$command->{command})]}</b> @{[xml_escape($command->{description})]}\n";
}
$telegram->send_message(
text => $html,
chat_id => $chat_id,
);
}
sub show_donate { sub show_donate {
my $message =shift; my $message = shift;
my $chat_id = $message->{chat}{id}; my $chat_id = $message->{chat}{id};
my $html = <<'EOF'; my $html = <<'EOF';
@ -152,7 +173,7 @@ If you are interested in donate use the /donate command.
EOF EOF
$telegram->send_message( $telegram->send_message(
chat_id => $chat_id, chat_id => $chat_id,
text => $html, text => $html,
); );
} }