aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillermo Ramos2020-04-26 11:49:06 +0200
committerGuillermo Ramos2020-04-26 11:49:10 +0200
commitc5191f10359f32a3af44f4087322348602734a3c (patch)
tree2d1de0e76b522157323a23c049689a55c48a18df
parent6f347a1ff480a16a4010ef5b5a044bc55ff2c6f7 (diff)
downloadtgutils-c5191f10359f32a3af44f4087322348602734a3c.tar.gz
Support photos in tgserver
-rw-r--r--lib/TgLib/Api.pm25
-rwxr-xr-xtgserver8
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
diff --git a/tgserver b/tgserver
index 4de8fdb..73c68ac 100755
--- a/tgserver
+++ b/tgserver
@@ -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);