Adding methods to retrieve updates.

Currently the bot is a logger of every text sent in
a room, this is going to change in the future.
This commit is contained in:
sergiotarxz 2022-02-06 00:46:00 +01:00
parent 825cd884fc
commit 7199804655
3 changed files with 127 additions and 4 deletions

View File

@ -5,9 +5,25 @@ use v5.30.0;
use strict;
use warnings;
use Data::Dumper;
use JSON;
use DoctorAgustin::Config;
use DoctorAgustin::Telegram;
use DoctorAgustin::Logger;
my $config = DoctorAgustin::Config->new;
say $config->telegram_token;
my $logger = DoctorAgustin::Logger->new;
my $telegram =
DoctorAgustin::Telegram->new( telegram_token => $config->telegram_token );
while (1) {
my $updates = $telegram->get_updates;
for my $update ($updates->@*) {
if (exists $update->{message}{text}) {
my $message = $update->{message}{text};
my $user_id = $update->{message}{from}{id};
my $username = $update->{message}{from}{username};
$logger->log_info("'$message' received from $username:$user_id");
}
}
}

View File

@ -1,5 +1,6 @@
package DoctorAgustin::Logger;
use Carp;
use Sys::Syslog;
sub new {
@ -12,8 +13,9 @@ sub _log {
my $self = shift;
my $level = shift;
my $message = shift;
my $critical = shift;
openlog ('Doctor Agustín: ', 'perror', 'user');
openlog ('Doctor Agustín: ', $critical ? '': 'perror', 'user');
syslog ($level, $message);
closelog();
}
@ -23,6 +25,13 @@ sub log_error {
$self->_log (LOG_ERR, shift);
}
sub log_critical {
my $self = shift;
my $error = shift;
$self->_log (LOG_ERR, $error, 1);
confess $error;
}
sub log_info {
my $self = shift;
$self->_log (LOG_INFO, shift);

View File

@ -5,9 +5,14 @@ use v5.30.0;
use strict;
use warnings;
use Types::Standard qw/Str/;
use Types::Standard qw/Str Int Ref/;
use Params::ValidationCompiler qw(validation_for);
use Mojo::UserAgent;
use JSON;
use DoctorAgustin::Logger;
{
my $validator = validation_for(
params => {
@ -25,6 +30,99 @@ use Params::ValidationCompiler qw(validation_for);
}
}
sub _logger {
my $self = shift;
if (!defined $self->{logger}) {
$self->{logger} = DoctorAgustin::Logger->new;
}
return $self->{logger};
}
sub _user_agent {
my $self = shift;
if ( !defined $self->{user_agent} ) {
$self->{user_agent} = Mojo::UserAgent->new;
}
return $self->{user_agent};
}
{
my $validator = validation_for(
params => {
method => { type => Str },
body => { type => Ref },
}
);
sub _request {
my $self = shift;
my %params = $validator->(@_);
my $method = $params{method};
my $body = $params{body};
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});
}
return $response;
}
}
sub get_updates {
my $self = shift;
my $last_update = $self->_last_update;
if ( !defined $last_update ) {
$last_update = 0;
}
my $response = $self->_request(
method => q/getUpdates/,
body => { offset => $last_update + 1 }
);
if ( scalar $response->{result}->@* ) {
$last_update = $response->{result}[-1]{update_id};
$self->_set_last_update($last_update);
}
return $response->{result};
}
{
my $validator = validation_for(
params => {
method => { type => Str },
}
);
sub _generate_url {
my $self = shift;
my %params = $validator->(@_);
my $method = $params{method};
my $telegram_token = $self->_telegram_token;
my $url = qq(https://api.telegram.org/bot$telegram_token/$method);
return $url;
}
}
{
my $validator = validation_for( params => [ { type => Int } ] );
sub _set_last_update {
my $self = shift;
my ($last_update) = $validator->(@_);
$self->{last_update} = $last_update;
}
}
sub _last_update {
my $self = shift;
if ( !exists $self->{last_update} ) {
return undef;
}
return $self->{last_update};
}
sub _telegram_token {
my $self = shift;
return $self->{telegram_token};