-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlackWebhookSock.pm
84 lines (63 loc) · 1.48 KB
/
SlackWebhookSock.pm
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
package SlackWebhookSock;
# SlackWebhookSock.pm
# walkure at 3pf.jp
use strict;
use warnings;
use Encode;
use URI;
use JSON qw/encode_json/;
use HTTP::Response;
use base qw/IO::Socket::SSL/;
sub configure
{
my ($self, $args) = @_;
my $host = $args->{PeerHost};
my $name = '';
if(ref $host eq 'HASH'){
$name = (keys %{$args->{PeerHost}})[0];
$host = $args->{PeerHost}{$name};
}
unless(defined $host){
$name = (keys %$args)[0];
$host = $args->{$name};
}
my $path = URI->new($host);
return if $path->scheme() ne 'https';
$args->{PeerHost} = $path->host_port();
*$self->{path_query} = $path->path_query();
*$self->{host} = $path->host();
*$self->{response} = '';
*$self->{name} = $name || $path->host();
#$args->{Blocking} = 0;
$self->SUPER::configure($args);
}
sub parse_body
{
my($self,$body) = @_;
*$self->{response} .= $body;
}
sub send_json
{
my($self,$body) = @_;
my $json = encode_json($body);
my $path_query = *$self->{path_query};
my $host = *$self->{host};
#required by slack server.
my $contentLength = length($json);
# HTTP::Response does not parse chunked transfer.
print $self "POST $path_query HTTP/1.0\r\nConnection:Close\r\nHost: $host\r\n"
."Content-type: application/json; charset=utf-8\r\nContent-Length: $contentLength\r\n\r\n$json";
}
sub name
{
my $self = shift;
my $old = *$self->{name};
*$self->{name} = $_[0] if @_;
$old;
}
sub get_response
{
my ($self) = @_;
HTTP::Response->parse( *$self->{response} );
}
1;