This repository was archived by the owner on Nov 24, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslack.pl
More file actions
executable file
·122 lines (91 loc) · 2.59 KB
/
slack.pl
File metadata and controls
executable file
·122 lines (91 loc) · 2.59 KB
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
#!/usr/bin/perl
use strict;
use warnings;
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
use JSON;
use Getopt::Long;
use Term::ANSIColor;
#
# You need to setup an incoming webhook in your slack account.
#
# Navigate to this url: https://my.slack.com/services/new/incoming-webhook/
#
# Create a new incoming webhook and set the url below (or use the -w flag).
# The url will look something like 'https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXX'
#
#
my $default_webhook_url = '';
#
# This is set during the init phase of the script.
#
my $webhook_url;
#
# This is the default channel and can be overriden with -c flag.
#
my $default_channel = '#general';
#
# This is the default username which can be overriden with the -u flag.
#
my $default_username = 'WSPerlBot';
my %payload;
&init();
&main();
sub main
{
my $ua = LWP::UserAgent->new;
$ua->timeout(15);
my $json = encode_json \%payload;
my $req = POST("${webhook_url}", ['payload' => $json]);
my $resp = $ua->request($req);
if ($resp->is_success)
{
#print $resp->decoded_content; # or whatever
}
else
{
die $resp->status_line;
}
exit(0);
}
sub usage
{
my ($error) = @_;
print STDERR << "EOF";
This program does...
usage: $0 [ -h ] [ -c channel ] [ -u username ] [ -w webhook ] message
-h : this (help) message
-c channel : the channel to post to
-u username : the username to post as
-u webhook : the webhook url from slack
example: $0 -c '#test' -u 'MyBot' 'This is a test'
EOF
print " ", colored(sprintf("Error: %s", $error), 'white on_bright_red'), "\n\n" if (defined($error));
exit;
}
sub init
{
my $options = {};
my ($channel, $tmp_channel, $username, $tmp_username, $tmp_webhook_url, $message, $help);
Getopt::Long::Configure('bundling');
GetOptions("c=s" => \$tmp_channel,
"channel=s" => \$tmp_channel,
"u=s" => \$tmp_username,
"username=s" => \$tmp_username,
"w=s" => \$tmp_webhook_url,
"webhook=s" => \$tmp_webhook_url,
"h" => \$help,
"help" => \$help);
$message = shift @ARGV;
usage if (defined($help));
usage('Missing message') if (!defined($message));
$channel = $tmp_channel || $default_channel;
$username = $tmp_username || $default_username;
$webhook_url = $tmp_webhook_url || $default_webhook_url;
usage('Missing webhook url') if ($webhook_url eq '');
%payload = (
channel => $channel,
username => $username,
text => $message,
);
}