Adding support for reporting the current karma.
This commit is contained in:
parent
0132d56c96
commit
567fbd5375
@ -8,6 +8,8 @@ use warnings;
|
||||
use Data::Dumper;
|
||||
use JSON;
|
||||
|
||||
use Mojo::URL;
|
||||
|
||||
use DoctorKarma::Config;
|
||||
use DoctorKarma::Telegram;
|
||||
use DoctorKarma::Logger;
|
||||
@ -36,7 +38,7 @@ sub proccess_new_message {
|
||||
my $message = shift;
|
||||
my $reply_to_message = $message->{reply_to_message};
|
||||
|
||||
update_user ($message);
|
||||
update_user($message);
|
||||
if ( defined $message->{text} ) {
|
||||
process_message_with_text($message);
|
||||
}
|
||||
@ -45,22 +47,23 @@ sub proccess_new_message {
|
||||
sub process_message_with_text {
|
||||
my $message = shift;
|
||||
|
||||
my $text = $message->{text};
|
||||
my $username = $message->{from}{username};
|
||||
my $user_id = $message->{from}{id};
|
||||
my $first_name = $message->{from}{first_name};
|
||||
my $text = $message->{text};
|
||||
my $username = $message->{from}{username};
|
||||
my $user_id = $message->{from}{id};
|
||||
my $first_name = $message->{from}{first_name};
|
||||
my $reply_to_message = $message->{reply_to_message};
|
||||
|
||||
my $log_username = defined $username ? "\@$username" : '';
|
||||
$logger->log_info(
|
||||
"'$message->{text}' received from $first_name:$log_username:$user_id");
|
||||
if ( $text eq '+1' && defined $reply_to_message ) {
|
||||
add_karma_to_replied_message_user ($message);
|
||||
add_karma_to_replied_message_user($message);
|
||||
}
|
||||
}
|
||||
|
||||
sub add_karma_to_replied_message_user {
|
||||
my $message = shift;
|
||||
my $chat_id = $message->{chat}{id};
|
||||
|
||||
my $reply_to_message = $message->{reply_to_message};
|
||||
my $user_giving_id = $message->{from}{id};
|
||||
@ -73,17 +76,29 @@ sub add_karma_to_replied_message_user {
|
||||
$logger->log_info(<<"EOF");
|
||||
User @{[$sending_karma_user->first_name]}:@{[$sending_karma_user->username]}:@{[$sending_karma_user->id_user]} tried to give karma to itself, refusing.
|
||||
EOF
|
||||
$telegram->send_message(
|
||||
chat_id => $chat_id,
|
||||
text => 'You cannot give karma to yourself'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
my $receiving_karma_user =
|
||||
$user_dao->recover_id( id => $user_receiving_id );
|
||||
if (!defined $receiving_karma_user) {
|
||||
update_user ($reply_to_message);
|
||||
$receiving_karma_user = $user_dao->recover_id (id => $user_receiving_id);
|
||||
if ( !defined $receiving_karma_user ) {
|
||||
update_user($reply_to_message);
|
||||
$receiving_karma_user =
|
||||
$user_dao->recover_id( id => $user_receiving_id );
|
||||
}
|
||||
$user_dao->add_1_karma(user => $receiving_karma_user);
|
||||
|
||||
$user_dao->add_1_karma( user => $receiving_karma_user );
|
||||
my $id_receptor = $receiving_karma_user->id_user;
|
||||
my $receptor_telegram_url = Mojo::URL->new('tg://user');
|
||||
$receptor_telegram_url->query( id => $id_receptor );
|
||||
$telegram->send_message(
|
||||
chat_id => $chat_id,
|
||||
text =>
|
||||
"[@{[$receiving_karma_user->first_name]}]($receptor_telegram_url) has now @{[$receiving_karma_user->karma]} of karma\\."
|
||||
);
|
||||
}
|
||||
|
||||
sub update_user {
|
||||
@ -97,13 +112,21 @@ sub update_user {
|
||||
my $user = $user_dao->recover_id( id => $user_id );
|
||||
if ( !defined $user ) {
|
||||
$user = DoctorKarma::Model::User->new(
|
||||
id_user => $user_id,
|
||||
(defined $username) ? (username => $username) : (),
|
||||
karma => 0
|
||||
id_user => $user_id,
|
||||
( defined $username ) ? ( username => $username ) : (),
|
||||
karma => 0
|
||||
);
|
||||
$user_dao->store(user => $user);
|
||||
$user_dao->store( user => $user );
|
||||
}
|
||||
if ( ( $user->username // '' ) ne ( $username // '' ) ) {
|
||||
if ( defined $username ) {
|
||||
my $user_already_with_username =
|
||||
$user_dao->recover_username( username => $username );
|
||||
$user_dao->update_username(
|
||||
user => $user_already_with_username,
|
||||
username => undef,
|
||||
) if defined $user_already_with_username;
|
||||
}
|
||||
$user_dao->update_username(
|
||||
user => $user,
|
||||
username => $username
|
||||
|
@ -71,7 +71,7 @@ sub last_karma_given_date {
|
||||
my $self = shift;
|
||||
my ($new_karma) = $validator->(@_);
|
||||
if ( defined $new_karma ) {
|
||||
$self->{new_karma} = $new_karma;
|
||||
$self->{karma} = $new_karma;
|
||||
}
|
||||
return $self->{karma};
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ use DoctorKarma::Logger;
|
||||
|
||||
sub _logger {
|
||||
my $self = shift;
|
||||
if (!defined $self->{logger}) {
|
||||
if ( !defined $self->{logger} ) {
|
||||
$self->{logger} = DoctorKarma::Logger->new;
|
||||
}
|
||||
return $self->{logger};
|
||||
@ -62,15 +62,42 @@ sub _user_agent {
|
||||
my $ua = $self->_user_agent;
|
||||
my $logger = $self->_logger;
|
||||
my $url = $self->_generate_url( method => $method );
|
||||
my $response = decode_json(
|
||||
$ua->post( $url => {} => json => $body )->result->body );
|
||||
unless ($response->{ok}) {
|
||||
$logger->log_critical($response->{description});
|
||||
my $response =
|
||||
decode_json( $ua->post( $url => {} => json => $body )->result->body );
|
||||
|
||||
unless ( $response->{ok} ) {
|
||||
$logger->log_critical( $response->{description} );
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
my $validator = validation_for(
|
||||
params => {
|
||||
chat_id => { type => Int },
|
||||
text => { type => Str },
|
||||
}
|
||||
);
|
||||
|
||||
sub send_message {
|
||||
my $self = shift;
|
||||
my %params = $validator->(@_);
|
||||
my $chat_id = $params{chat_id};
|
||||
my $text = $params{text};
|
||||
|
||||
my $response = $self->_request(
|
||||
method => q/sendMessage/,
|
||||
body => {
|
||||
chat_id => $chat_id,
|
||||
text => $text,
|
||||
parse_mode => 'MarkdownV2',
|
||||
}
|
||||
);
|
||||
return $response->{result};
|
||||
}
|
||||
}
|
||||
|
||||
sub get_updates {
|
||||
my $self = shift;
|
||||
my $last_update = $self->_last_update;
|
||||
|
Loading…
Reference in New Issue
Block a user