aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/TgLib/Api.pm4
-rwxr-xr-xtgserver26
2 files changed, 16 insertions, 14 deletions
diff --git a/lib/TgLib/Api.pm b/lib/TgLib/Api.pm
index 73aee4b..b0a66bf 100644
--- a/lib/TgLib/Api.pm
+++ b/lib/TgLib/Api.pm
@@ -64,11 +64,11 @@ sub send_message {
}
sub send_document {
- my ($self, $chat_id, $photo) = @_;
+ my ($self, $chat_id, $photo, $caption) = @_;
my $logger = $self->{'logger'};
my $uri = "$self->{'uri'}/sendDocument";
my $content = {'chat_id' => $chat_id,
- 'caption' => 'Tenga, ayúdese',
+ 'caption' => $caption,
'document' => [undef, 'cosa.png', Content => $photo]};
my $req = POST $uri, 'Content-Type' => "multipart/form-data", 'Content' => $content;
diff --git a/tgserver b/tgserver
index 589054b..ac221fc 100755
--- a/tgserver
+++ b/tgserver
@@ -65,18 +65,14 @@ sub handle_photo {
my $photos = $msg->{'photo'};
my $chat_id = $msg->{'chat'}{'id'};
my $photo = (sort { $b->{'width'} <=> $a->{'width'} } @$photos)[0];
- $logger->info(sprintf "Received photo %s (size=%d)\n", $photo->{'file_id'}, $photo->{'file_size'});
+ $logger->info(sprintf "Received photo %s (size=%d) from chat %s\n",
+ $photo->{'file_id'}, $photo->{'file_size'}, $chat_id);
my $file = $api->get_file($photo->{'file_id'});
$ENV{'TGUTILS_TYPE'} = 'IMAGE';
my $response = pipe_send($file, @ARGV);
- my $type = ref $response eq 'HASH' ? $response->{'type'} : undef;
- if ($type eq 'DOCUMENT') {
- $api->send_document($chat_id, decode_base64 $response->{'content'});
- } else {
- $api->send_message($chat_id, $response);
- }
+ reply($chat_id, $response);
}
sub handle_text {
@@ -87,7 +83,17 @@ sub handle_text {
$ENV{'TGUTILS_TYPE'} = 'TEXT';
my $response = pipe_send($text, @ARGV);
- if ($response) {
+ reply($chat_id, $response);
+}
+
+sub reply {
+ my $chat_id = shift;
+ my $response = shift;
+ my $type = ref $response eq 'HASH' ? $response->{'type'} : undef;
+ if ($type eq 'DOCUMENT') {
+ my $caption = $response->{'caption'};
+ $api->send_document($chat_id, decode_base64 $response->{'content'}, $caption);
+ } elsif ($response) {
$api->send_message($chat_id, $response);
} else {
$logger->warn("Empty response, skipping\n");
@@ -137,10 +143,6 @@ B<tgserver> [I<options>] -- I<prog>
=over
-=item B<--on-image>=I<cmd>
-
-Command to launch whenever an image is received
-
=item B<--token>=I<token>, B<-t> I<token>
Bot token (see B<AUTHENTICATION>)