#!/usr/bin/env perl # # Author: Guillermo Ramos (2019) # # Run `tgrecv -h` for quick help, or `tgrecv -h -v` for full manual. # # Dependencies: HTTP-Message, JSON ################################################################################ $main::VERSION = "0.1.1"; use Getopt::Long qw(:config auto_version); use Pod::Usage qw; use Data::Dumper; use JSON qw; # Local modules use FindBin; use lib "$FindBin::Bin/lib"; use TgLib qw; 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 tgrecv [-h | --help] [-v] tgrecv [options] =head1 OPTIONS --offset Offset of the first message to receive - previous ones are discarded --auto-offset Use offset cache to automatically discard previous updates (if combined with --offset, cache the last update but still use the provided offset for the current request) --timeout Timeout for long polling (default: 60 seconds) --output=file Write the output to file instead of stdout --token | -t Bot token (see AUTHENTICATION) --version Show version --verbose | -v Show more information (combine with -h to see full manual) --help | -h Show this message =head1 DESCRIPTION This program receives a single update batch from the Telegram bot identified by B, 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 AUTHENTICATION To get the bot token, this program will check (in order): - The "--token" CLI argument - The "TGUTILS_TOKEN" environment variable - The contents of "$XDG_CONFIG_HOME/tgutils_token" (usually ~/.config/tgutils_token) =cut