aboutsummaryrefslogtreecommitdiff
path: root/tgsend
blob: 3303da0e2ee7fbeebca1f764f87b7f6feaf5c196 (plain) (blame)
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
#!/usr/bin/env perl
#
# Author: Guillermo Ramos <gramos@gramos.me> (2019)
#
# Run `tgsend -h` for quick help, or `tgsend -h -v` for full manual.
#
# Dependencies: HTTP-Message, JSON
################################################################################

$main::VERSION = "0.1.0";

use Getopt::Long qw(:config auto_version);
use Pod::Usage qw<pod2usage>;
use LWP::UserAgent;
use Data::Dumper;

use HTTP::Request;
use JSON qw<encode_json>;

my $TOKEN;
my $PRETEND;
my $VERBOSE = 0;
my $HELP;

GetOptions("token=s" => \$TOKEN,
           "pretend" => \$PRETEND,
           "verbose+" => \$VERBOSE,
           "help" => \$HELP);
pod2usage(-verbose => $VERBOSE+1) if $HELP or ! @ARGV;

# If token was not specified in CLI, try to get it from ENV
$TOKEN ||= $ENV{'TGUTILS_TOKEN'};

# If still no token, try to get it from ~/.config/tgutils_token
unless ($TOKEN) {
    my $CONFIG_HOME = $ENV{'XDG_CONFIG_HOME'} || $ENV{'HOME'} . "/.config";
    my $CONFIG = "$CONFIG_HOME/tgutils_token";
    open(my $cfg, "<", $CONFIG) or
        pod2usage(-message => "ERROR: Unable to get bot token ($CONFIG: $!).\n",
                  -verbose => 99, -sections => "AUTHENTICATION");
    $TOKEN = <$cfg>;
    chomp $TOKEN;
    close $cfg;
}

# Sanity check
$TOKEN =~ /^[0-9]+:[a-zA-Z0-9]+$/ or die "Invalid bot token ($TOKEN)";

# Read text from stdin
undef $/;
my $TEXT = <STDIN>;

my $ua = LWP::UserAgent->new;
my $URI = "https://api.telegram.org/bot$TOKEN/sendMessage";
sub send_message {
    my $chat_id = shift;
    my $content = encode_json {'chat_id' => $chat_id, 'text' => $TEXT};
    my $req = HTTP::Request->new("POST", $URI,
                               ["Content-Type", "application/json"], $content);
    if ($VERBOSE || $PRETEND) {
        print "Sending to $chat_id:\n====\n$TEXT\n====\n";
        print "Request:\n", Dumper($req) if $VERBOSE > 1;
    }
    unless ($PRETEND) {
        my $resp = $ua->request($req);
        print "Response:\n", Dumper($resp) if $VERBOSE > 1;
        if ($resp->is_error()) {
            die $resp->message;
        }
    }
}

send_message($_) foreach @ARGV;


__END__

=head1 NAME

tgsend - Send message to a Telegram chat using a bot token

=head1 SYNOPSIS

tgsend [-h | --help]

tgsend [options] [chatid1 chatid2 ...]

=head1 OPTIONS

  --token | -t        Bot token (see AUTHENTICATION)
  --pretend | -p      Do not actually do anything
  --version           Show version
  --verbose | -v      Show more information (combine with -h to see full manual)
  --help | -h         Show this message

=head1 DESCRIPTION

This program sends its standard input to the Telegram chats whose IDs are passed
as arguments, using the given bot token.

=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