#!/usr/bin/env perl # # Author: Guillermo Ramos (2019) # # Run `tgserver -h` for quick help, or `tgserver -h -v` for full manual. ################################################################################ $main::VERSION = "0.1.1"; use Getopt::Long qw(:config auto_version); use Pod::Usage qw; use Data::Dumper; use FindBin; use lib "$FindBin::Bin/lib"; use TgLib qw; require TgLib::Api; require TgLib::Cache; require TgLib::Logger; my $TOKEN; my $VERBOSE = 0; my $HELP; GetOptions("token=s" => \$TOKEN, "verbose+" => \$VERBOSE, "help" => \$HELP); pod2usage(-verbose => $VERBOSE+1) if $HELP or not @ARGV; my $logger = TgLib::Logger->new($VERBOSE); # If token was not specified in CLI, get it from 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); my $api = TgLib::Api->new($TOKEN, $logger); while (1) { # FIXME magic timeout foreach my $update (@{$api->get_updates(3600, $cache->offset)}) { # Cache current offset $cache->offset($update->{'update_id'}+1); $logger->debug(sprintf "Update %s", Dumper($update)); my $text = $update->{'message'}{'text'}; my $chat_id = $update->{'message'}{'chat'}{'id'}; $logger->info("Received from chat $chat_id: '$text'\n"); use IPC::Open2 qw; my $pid = open2(my $progr, my $progw, "@ARGV"); print $progw $text; close($progw); my $response = join "", <$progr>; chomp $response; $logger->debug("'$text' -> @ARGV -> '$response'\n"); close($progr); if ($response) { my $api = TgLib::Api->new($TOKEN, $logger); $api->send_message($chat_id, $text, $response); } else { $logger->warn("Empty response, skipping\n"); } } } __END__ =head1 NAME tgserver - Interact with a Telegram Bot =head1 SYNOPSIS tgserver [-h | --help] [-v] tgserver [options] -- B =head1 OPTIONS --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 waits for Telegram updates from the bot specified by the token. For every update it runs B with stdin piped to the update, and sending stdout back as response. =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