#!/usr/bin/env perl # # Author: Guillermo Ramos (2019) # # Run `tgsend -h` for quick help, or `tgsend -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; # Local modules use FindBin; use lib "$FindBin::Bin/lib"; use TgLib qw; require TgLib::Api; require TgLib::Logger; my $TOKEN; my $PRETEND; my $VERBOSE = 0; my $HELP; # Parse CLI options GetOptions("token=s" => \$TOKEN, "pretend" => \$PRETEND, "verbose+" => \$VERBOSE, "help" => \$HELP); pod2usage(-verbose => $VERBOSE+1) if $HELP or ! @ARGV; 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"); # Read text from stdin undef $/; binmode STDIN, ':encoding(UTF-8)'; my $text = ; $text =~ s/^\s+|\s+$//g; # Trim input # Send message to chats (or pretend to) if ($PRETEND) { $logger->info("(prentend) Sending to $_: '$text'\n") foreach @ARGV; } else { my $api = TgLib::Api->new($TOKEN, $logger); $api->send_message($_, $text) foreach @ARGV; } __END__ =head1 NAME tgsend - Send message to one or more Telegram chats =head1 SYNOPSIS B [B<-h> | B<--help>] [B<-v>] B [I] I... =head1 OPTIONS =over =item B<--pretend>, B<-p> Do not actually do anything =item B<--token>=I, B<-t> I Bot token (see B) =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 will send its standard input to the given Telegram I, from the bot identified by I. =head1 EXAMPLE echo "Hello, word!" | tgsend --token 123456789:abcdefghijklmnopqrstuvwxyzABCDEFGHI 12121212 \ =head1 AUTHENTICATION To get the bot token, this program will check (in order): =over 2 =item - The B<--token> CLI argument =item - The B environment variable =item - The contents of I<$XDG_CONFIG_HOME>B (usually B<~/.config/tgutils_token>) =back =cut