1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
#!/usr/bin/env perl
#
# Author: Guillermo Ramos <gramos@gramos.me> (2019)
#
# Run `tgrecv -h` for quick help, or `tgrecv -h -v` for full manual.
#
# Dependencies: HTTP-Message, JSON
################################################################################
use strict;
use warnings;
$main::VERSION = "0.1.1";
use Getopt::Long qw(:config auto_version);
use Pod::Usage qw<pod2usage>;
use Data::Dumper;
use JSON qw<encode_json>;
# Local modules
use FindBin;
use lib "$FindBin::Bin/lib";
use TgLib qw<fetch_token>;
require TgLib::Api;
require TgLib::Cache;
require TgLib::Logger;
my $TOKEN;
my $OUTPUT;
my $OFFSET;
my $AUTO_OFFSET;
my $TIMEOUT = 60;
my $VERBOSE = 0;
my $HELP;
GetOptions("token=s" => \$TOKEN,
"offset=i" => \$OFFSET,
"auto-offset" => \$AUTO_OFFSET,
"timeout=i" => \$TIMEOUT,
"output=s" => \$OUTPUT,
"verbose+" => \$VERBOSE,
"help" => \$HELP);
pod2usage(-verbose => $VERBOSE+1) if $HELP;
my $logger = TgLib::Logger->new($VERBOSE);
# Fetch token: CLI || env || file
$TOKEN ||= fetch_token() or
pod2usage(-message => "ERROR: Unable to get bot token ($!).\n",
-verbose => 99, -sections => "AUTHENTICATION");
my $cache = TgLib::Cache->new($logger);
# Get offset from cache if --auto-offset is enabled (and no --offset provided)
if ($AUTO_OFFSET && ! $OFFSET) {
$OFFSET |= $cache->offset;
}
my $api = TgLib::Api->new($TOKEN, $logger);
my $updates = $api->get_updates($TIMEOUT, $OFFSET);
my $out = STDOUT;
if ($OUTPUT) {
open($out, ">", $OUTPUT) or die "Cannot open $OUTPUT for writing: $!";
}
# Store new offset in cache (last update id +1)
$cache->offset($updates->[-1]{'update_id'}+1) if ($AUTO_OFFSET && @$updates);
print $out encode_json($updates);
__END__
=head1 NAME
tgrecv - Receive updates from Telegram, output them as JSON
=head1 SYNOPSIS
B<tgrecv> [B<-h> | B<--help>] [B<-v>]
B<tgrecv> [I<options>]
=head1 OPTIONS
=over
=item B<--offset>
Offset of the first message to receive - previous ones are discarded
=item B<--auto-offset>
Use offset cache to automatically discard previous updates (if combined with
B<--offset>, cache the last update but still use the provided offset for the
current request)
=item B<--timeout=>I<seconds>
Timeout for long polling (default: 60 seconds)
=item B<--output=>I<file>
Write the output to I<file> instead of stdout
=item B<--token>=I<token>, B<-t> I<token>
Bot token (see B<AUTHENTICATION>)
=item B<--version>
Show version
=item B<--verbose>, B<-v>
Show more information (combine with B<-h> to see full manual)
=item B<--help>, B<-h>
Show this message
=back
=head1 DESCRIPTION
This program receives a single update batch from the Telegram bot identified by
I<token>, and outputs it as a JSON array. The array can contain multiple
updates. The connection is blocking (long polling), so it waits until an update
is available before exiting.
=head1 EXAMPLE
tgrecv --auto-offset --token 123456789:abcdefghijklmnopqrstuvwxyzABCDEFGHI \
| jq .[0].message
=head1 AUTHENTICATION
To get the bot token, this program will check (in order):
=over 2
=item -
The B<--token> CLI argument
=item -
The B<TGUTILS_TOKEN> environment variable
=item -
The contents of I<$XDG_CONFIG_HOME>B</tgutils_token> (usually B<~/.config/tgutils_token>)
=back
=cut
|