File Coverage

blib/lib/Google/Chat/WebHooks.pm
Criterion Covered Total %
statement 34 53 64.1
branch 4 8 50.0
condition n/a
subroutine 11 14 78.5
pod 1 2 50.0
total 50 77 64.9


line stmt bran cond sub pod time code
1             package Google::Chat::WebHooks;
2 1     1   197253 use strict;
  1         4  
  1         28  
3 1     1   6 use warnings;
  1         2  
  1         25  
4 1     1   679 use LWP::UserAgent;
  1         43384  
  1         39  
5 1     1   558 use subs 'timeout';
  1         28  
  1         5  
6 1     1   558 use Class::Tiny qw(room_webhook_url _ua timeout);
  1         1809  
  1         4  
7 1     1   353 use Carp;
  1         3  
  1         70  
8 1     1   687 use JSON;
  1         10218  
  1         5  
9 1     1   138 use Try::Tiny;
  1         3  
  1         55  
10 1     1   465 use Data::Validate::URI qw(is_uri);
  1         49104  
  1         78  
11              
12             BEGIN {
13 1     1   343 our $VERSION = '0.3';
14             }
15             my $DIAG = 1;
16              
17             sub BUILD
18             {
19 3     3 0 8590 my ($self, $args) = @_;
20            
21 3         21 $self->_ua(LWP::UserAgent->new);
22 3         3538 $self->_ua->timeout(10);
23 3         108 $self->_ua->env_proxy;
24            
25 3 100       4410 croak "parameter 'room_webhook_url' must be supplied to new" unless $self->room_webhook_url;
26 2 100       42 croak "Room URL is malformed" unless is_uri($self->room_webhook_url);
27             }
28              
29             sub timeout
30             {
31 0     0     my $self = shift;
32 0 0         if (@_) {
33 0           $self->_ua->timeout(shift);
34             }
35 0           return $self->_ua->timeout;
36             }
37              
38             sub simple_message($)
39             {
40 0     0 1   my $self = shift;
41 0           my $msg = shift;
42              
43 0           my $msg_json = "{\"text\": \"$msg\"}";
44 0           my $req = HTTP::Request->new('POST', $self->room_webhook_url);
45 0           $req->header('Content-Type' => 'application/json');
46 0           $req->content($msg_json);
47 0           my $response = $self->_ua->request($req);
48 0 0         if($response->is_error)
49             {
50 0           my $content = $response->decoded_content();
51 0           my $json;
52             try
53             {
54 0     0     $json = decode_json($content);
55 0           };
56 0           my $error_message = $response->code." ".$response->message;
57 0           return { result => 0, message => $error_message, detail => $response->decoded_content};
58             }
59 0           return { result => 1, message => "success"};
60             }
61              
62             #################### main pod documentation begin ###################
63              
64             =head1 SYNOPSIS
65              
66             use Google::Chat::WebHooks;
67              
68             my $room = Google::Chat::WebHooks->new(room_webhook_url => 'https://chat.googleapis.com/v1/spaces/someid/messages?key=something&token=something');
69             my $result = $room->simple_text("This is a message");
70             $result = $room->simple_text("Message with some *bold*");
71              
72             =head1 DESCRIPTION
73              
74             Google::Chat::WebHooks - Send notifications to Google Chat Rooms as provided by G-Suite. Does not work with Google Hangouts as used with your free Google account. Cannot receive messages - for that you need a bot. I'm sure I'll write that module some day.
75              
76             =head1 USAGE
77              
78             Just create an object, passing the webhook URL of the room to which you want to send notifications. Then fire away. If you need help setting up a webhook for your room, see L.
79              
80             =over 3
81              
82             =item new(room_webhook_url => value)
83              
84             =item new(room_webhook_url => value, timeout => integer)
85              
86             Create a new instance of this class, passing in the webhook URL to send messages to. This argument is mandatory. Failure to set it upon creation will result in the method croaking.
87              
88             Optionally also set the connect timeout in seconds. Default value is 10.
89              
90             =item simple_message(string)
91              
92             my $result = $room->simple_text("This is a message");
93             $result->{'result'}; # 1 if success, 0 if not
94             $result->{'message'}; # "success" if result was 1, error message if not
95              
96             Send a message to the room. L. Returns a hash ref.
97              
98             =item room_webhook_url(), room_webhook_url(value)
99              
100             Get/set the URL of the room.
101              
102             =back
103              
104             =head1 BUGS & SUPPORT
105              
106             Please log them L.
107              
108             =head1 AUTHOR
109              
110             I Gibbs
111             CPAN ID: IGIBBS
112             igibbs@cpan.org
113             https://github.com/realflash/perl-google-chat-webhoooks
114              
115             =head1 COPYRIGHT
116              
117             This program is free software licensed under the...
118              
119             The General Public License (GPL)
120             Version 3
121              
122             The full text of the license can be found in the
123             LICENSE file included with this module.
124              
125             =cut
126              
127             #################### main pod documentation end ###################
128              
129              
130             1;
131             # The preceding line will help the module return a true value
132