/
c3mc-base-sendmesg-feishu-bot
executable file
·87 lines (66 loc) · 2.12 KB
/
c3mc-base-sendmesg-feishu-bot
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
#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/Connector/lib
use strict;
use warnings;
binmode STDIN, ':utf8';
binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';
$|++;
use MYDan::Util::OptConf;
use LWP::UserAgent;
use JSON;
use POSIX;
=head1 SYNOPSIS
$0 user1 user2 ... user3
=cut
my $option = MYDan::Util::OptConf->load();
my %o = $option->get()->dump();
$option->assert() unless @ARGV;
local $/ = undef;
my $mesg = Encode::encode('utf8', <STDIN> );
local $/ = "\n";
my $ua = LWP::UserAgent->new;
$ua->timeout( 15 );
my $name = 'feishu-bot';
my $path = sprintf "/data/open-c3-data/send/%s/$name/", POSIX::strftime( "%F", localtime );
system "mkdir -p '$path'" unless -d $path;
sub call
{
my ( $user, $mesg ) = @_;
my %form = (
msg_type => 'text',
content => +{ text => $mesg },
);
my $url = "https://open.feishu.cn/open-apis/bot/v2/hook/$user";
my $res = $ua->post( $url,
Content => JSON::to_json( \%form ), 'Content-Type' => 'application/json'
);
if( $res->is_success )
{
my $data = eval{JSON::from_json $res->content};
die sprintf( "res from json fail: %s", $res->content ) if $@;
die "resp error: " . Encode::decode('utf8', $res->content ) unless $data && $data->{StatusMessage} && $data->{StatusMessage} eq 'success';
return 1;
}
die sprintf "http error: %s", YAML::XS::Dump $res;
}
sub sendmesg
{
my ( $user, $mesg ) = @_;
my ( $err, $status );
for my $i ( reverse 1 .. 3 )
{
my $stat = eval{ call( $user, $mesg ); };
$err = $@ ? "call $name error: $@" : "";
$status = ( $stat && ! $err ) ? "ok": "fail";
YAML::XS::DumpFile sprintf( "$path/%s.%03d.$user.$status", POSIX::strftime( "%Y%m%d%H%M%S", localtime ), rand 1000 ),
+{ mesg => Encode::decode('utf8', $mesg ), user => $user, err => $err };
last if $status eq 'ok';
if( $i > 1 )
{
warn sprintf( "sendmesg error %s", $err );
sleep 3;
}
}
die sprintf( "%s: status %s", $err, $status ) unless $status eq 'ok';
}
map{ sendmesg( $_, $mesg ) }@ARGV;