From c5191f10359f32a3af44f4087322348602734a3c Mon Sep 17 00:00:00 2001 From: Guillermo Ramos Date: Sun, 26 Apr 2020 11:49:06 +0200 Subject: Support photos in tgserver --- lib/TgLib/Api.pm | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'lib/TgLib/Api.pm') 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 -- cgit v1.2.3