139 lines
3.3 KiB
Perl
139 lines
3.3 KiB
Perl
|
package TgMagicPdf::Telegram;
|
||
|
|
||
|
use v5.38.2;
|
||
|
use strict;
|
||
|
use warnings;
|
||
|
|
||
|
use feature 'signatures';
|
||
|
|
||
|
use Moo;
|
||
|
use Mojo::UserAgent;
|
||
|
use Mojo::IOLoop;
|
||
|
|
||
|
use TgMagicPdf::PdfBuilder;
|
||
|
|
||
|
has _token => ( is => 'lazy', );
|
||
|
|
||
|
has _tg_root => ( is => 'lazy', );
|
||
|
|
||
|
has _ua => ( is => 'lazy', );
|
||
|
|
||
|
has _last_offset_update => ( is => 'rw', );
|
||
|
|
||
|
sub _build__token($self) {
|
||
|
require TgMagicPdf;
|
||
|
return TgMagicPdf->new->config->{telegram}{token};
|
||
|
}
|
||
|
|
||
|
sub run($self) {
|
||
|
$self->_dispatch_updates;
|
||
|
Mojo::IOLoop->recurring(
|
||
|
5 => sub {
|
||
|
$self->_dispatch_updates;
|
||
|
}
|
||
|
);
|
||
|
}
|
||
|
|
||
|
sub _dispatch_updates($self) {
|
||
|
my $updates_p = $self->_get_updates;
|
||
|
$updates_p->then(
|
||
|
sub ($res) {
|
||
|
my $updates = $res->result->json->{result};
|
||
|
for my $update (@$updates) {
|
||
|
$self->_dispatch_update($update);
|
||
|
}
|
||
|
}
|
||
|
)->catch(
|
||
|
sub ($err) {
|
||
|
say $err;
|
||
|
}
|
||
|
);
|
||
|
}
|
||
|
|
||
|
sub _dispatch_update( $self, $update ) {
|
||
|
if ( !defined $self->_last_offset_update
|
||
|
|| $self->_last_offset_update < $update->{update_id} )
|
||
|
{
|
||
|
$self->_last_offset_update( $update->{update_id} );
|
||
|
}
|
||
|
if ( defined $update->{message} ) {
|
||
|
$self->_handle_message( $update->{message} );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
sub _handle_message( $self, $message ) {
|
||
|
my $chat_type = $message->{chat}{type};
|
||
|
if ( $chat_type ne 'private' ) {
|
||
|
return;
|
||
|
}
|
||
|
my $chat_id = $message->{chat}{id};
|
||
|
my $text = $message->{text};
|
||
|
if (!defined $text) {
|
||
|
return;
|
||
|
}
|
||
|
my $pdf_builder = TgMagicPdf::PdfBuilder->new;
|
||
|
my $pdf_p;
|
||
|
eval {
|
||
|
$pdf_p = $pdf_builder->from_text($text);
|
||
|
$pdf_p->then(sub ($pdf) {
|
||
|
$self->sendDocument($chat_id, 'mtgprint.pdf', $pdf);
|
||
|
})->catch(sub ($err) {
|
||
|
if ($err eq $TgMagicPdf::PdfBuilder::ERR_INVALID_CARD) {
|
||
|
warn $pdf_builder->last_invalid_card;
|
||
|
}
|
||
|
if ($err eq $TgMagicPdf::PdfBuilder::ERR_UNABLE_TO_FIND_IMAGE) {
|
||
|
warn $pdf_builder->last_invalid_card;
|
||
|
}
|
||
|
warn $err
|
||
|
});
|
||
|
};
|
||
|
if ($@) {
|
||
|
if ($@ eq $TgMagicPdf::PdfBuilder::ERR_INVALID_CARD) {
|
||
|
warn $pdf_builder->last_invalid_card;
|
||
|
}
|
||
|
if ($@ eq $TgMagicPdf::PdfBuilder::ERR_UNABLE_TO_FIND_IMAGE) {
|
||
|
warn $pdf_builder->last_invalid_card;
|
||
|
}
|
||
|
warn $@;
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
sub sendDocument($self, $chat_id, $filename, $file_contents) {
|
||
|
my $url = "@{[$self->_tg_root]}/sendDocument";
|
||
|
my $ua = $self->_ua;
|
||
|
my $res = $ua->post($url, form => {
|
||
|
chat_id => $chat_id,
|
||
|
document => {
|
||
|
content => $file_contents,
|
||
|
'Content-Type' => 'application/pdf',
|
||
|
filename => $filename,
|
||
|
},
|
||
|
});
|
||
|
}
|
||
|
|
||
|
sub _pdf_builder {
|
||
|
return TgMagicPdf::PdfBuilder->new;
|
||
|
}
|
||
|
|
||
|
sub _build__ua($self) {
|
||
|
my $ua = Mojo::UserAgent->new;
|
||
|
$ua->inactivity_timeout(30);
|
||
|
return $ua;
|
||
|
}
|
||
|
|
||
|
sub _get_updates($self) {
|
||
|
my $ua = $self->_ua;
|
||
|
my $url = "@{[$self->_tg_root]}/getUpdates";
|
||
|
my %params;
|
||
|
if ( defined $self->_last_offset_update ) {
|
||
|
$params{offset} = $self->_last_offset_update + 1;
|
||
|
}
|
||
|
return $ua->post_p( $url, json => {%params} );
|
||
|
}
|
||
|
|
||
|
sub _build__tg_root($self) {
|
||
|
return "https://api.telegram.org/bot@{[$self->_token]}";
|
||
|
}
|
||
|
1;
|