From 719980465513f2fe23282997ce1de04915394a3c Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Sun, 6 Feb 2022 00:46:00 +0100 Subject: [PATCH] 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. --- bin/doctor_agustin.pl | 20 ++++++- lib/DoctorAgustin/Logger.pm | 11 +++- lib/DoctorAgustin/Telegram.pm | 100 +++++++++++++++++++++++++++++++++- 3 files changed, 127 insertions(+), 4 deletions(-) diff --git a/bin/doctor_agustin.pl b/bin/doctor_agustin.pl index 4fd141c..76d2f80 100755 --- a/bin/doctor_agustin.pl +++ b/bin/doctor_agustin.pl @@ -5,9 +5,25 @@ use v5.30.0; use strict; use warnings; +use Data::Dumper; use JSON; -use DoctorAgustin::Config; +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"); + } + } +} diff --git a/lib/DoctorAgustin/Logger.pm b/lib/DoctorAgustin/Logger.pm index 30f4c78..03be91a 100644 --- a/lib/DoctorAgustin/Logger.pm +++ b/lib/DoctorAgustin/Logger.pm @@ -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); diff --git a/lib/DoctorAgustin/Telegram.pm b/lib/DoctorAgustin/Telegram.pm index 769fafe..521f8c2 100644 --- a/lib/DoctorAgustin/Telegram.pm +++ b/lib/DoctorAgustin/Telegram.pm @@ -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};