diff options
-rw-r--r-- | lib/TgLib/Api.pm | 25 | ||||
-rwxr-xr-x | tgserver | 8 |
2 files changed, 29 insertions, 4 deletions
diff --git a/lib/TgLib/Api.pm b/lib/TgLib/Api.pm index 02e8b83..87f227d 100644 --- a/lib/TgLib/Api.pm +++ b/lib/TgLib/Api.pm @@ -62,12 +62,31 @@ sub send_message { } sub send_document { - my ($self, $chat_id, $photo, $caption) = @_; + my ($self, $chat_id, $file, $filename, $caption) = @_; my $logger = $self->{'logger'}; my $uri = "$self->{'uri'}/sendDocument"; my $content = {'chat_id' => $chat_id, - 'caption' => $caption, - 'document' => [undef, 'cosa.png', Content => $photo]}; + 'caption' => $caption, + 'document' => [undef, $filename, Content => $file]}; + + my $req = POST $uri, 'Content-Type' => "multipart/form-data", 'Content' => $content; + $logger->debug(sprintf "Request:\n%s\n", Dumper($req)); # DEBUG + + my $resp = $self->{'ua'}->request($req); + $logger->debug(sprintf "Response:\n%s\n", Dumper($resp)); + if ($resp->is_error()) { + print decode_json($resp->content)->{'description'}; + die $resp->message; + } +} + +sub send_photo { + my ($self, $chat_id, $photo, $filename, $caption) = @_; + my $logger = $self->{'logger'}; + my $uri = "$self->{'uri'}/sendPhoto"; + my $content = {'chat_id' => $chat_id, + 'caption' => $caption, + 'photo' => [undef, $filename, Content => $photo]}; my $req = POST $uri, 'Content-Type' => "multipart/form-data", 'Content' => $content; $logger->debug(sprintf "Request:\n%s\n", Dumper($req)); # DEBUG @@ -114,7 +114,13 @@ sub reply { if ($type eq 'DOCUMENT') { my $caption = $response->{'caption'}; $logger->info("🤖: [Document '$caption'] -> $chat_id\n"); - $api->send_document($chat_id, decode_base64 $response->{'content'}, $caption); + $api->send_document($chat_id, decode_base64 $response->{'content'}, + $response->{'filename'}, $caption); + } elsif ($type eq 'PHOTO') { + my $caption = $response->{'caption'}; + $logger->info("🤖: [Photo '$caption'] -> $chat_id\n"); + $api->send_photo($chat_id, decode_base64 $response->{'content'}, + $response->{'filename'}, $caption); } elsif ($type eq 'TEXT' and $response) { $logger->info("🤖: '$response' -> $chat_id\n"); $api->send_message($chat_id, $response); |