« lib/Monitoring/Spooler/Cmd/Command/rm.pm
↑ Summary
lib/Monitoring/Spooler/Cmd/Command/bootstrap.pm »
| 1 | package Monitoring::Spooler::Transport::Smstrade; | ||
| 2 | $Monitoring::Spooler::Transport::Smstrade::VERSION = '0.05'; | ||
| 3 | BEGIN { | ||
| 4 | $Monitoring::Spooler::Transport::Smstrade::AUTHORITY = 'cpan:TEX'; | ||
| 5 | } | ||
| 6 | # ABSTRACT: a monitoring spooler transport for the smstrade service | ||
| 7 | |||
| 8 | use 5.010_000; | ||
| 9 | use mro 'c3'; | ||
| 10 | use feature ':5.10'; | ||
| 11 | |||
| 12 | use Moose; | ||
| 13 | use namespace::autoclean; | ||
| 14 | |||
| 15 | # use IO::Handle; | ||
| 16 | # use autodie; | ||
| 17 | # use MooseX::Params::Validate; | ||
| 18 | # use Carp; | ||
| 19 | # use English qw( -no_match_vars ); | ||
| 20 | # use Try::Tiny; | ||
| 21 | use LWP::UserAgent; | ||
| 22 | use URI::Escape; | ||
| 23 | |||
| 24 | # extends ... | ||
| 25 | extends 'Monitoring::Spooler::Transport'; | ||
| 26 | # has ... | ||
| 27 | has '_ua' => ( | ||
| 28 | 'is' => 'rw', | ||
| 29 | 'isa' => 'LWP::UserAgent', | ||
| 30 | 'lazy' => 1, | ||
| 31 | 'builder' => '_init_ua', | ||
| 32 | ); | ||
| 33 | |||
| 34 | has 'url' => ( | ||
| 35 | 'is' => 'rw', | ||
| 36 | 'isa' => 'Str', | ||
| 37 | 'default' => 'https://gateway.smstrade.de/', | ||
| 38 | ); | ||
| 39 | |||
| 40 | has 'apikey' => ( | ||
| 41 | 'is' => 'rw', | ||
| 42 | 'isa' => 'Str', | ||
| 43 | 'required' => 1, | ||
| 44 | ); | ||
| 45 | |||
| 46 | has 'route' => ( | ||
| 47 | 'is' => 'rw', | ||
| 48 | 'isa' => 'Str', | ||
| 49 | 'default' => 'basic', | ||
| 50 | ); | ||
| 51 | |||
| 52 | has 'responses' => ( | ||
| 53 | 'is' => 'ro', | ||
| 54 | 'isa' => 'HashRef', | ||
| 55 | 'lazy' => 1, | ||
| 56 | 'builder' => '_init_responses', | ||
| 57 | ); | ||
| 58 | # with ... | ||
| 59 | # initializers ... | ||
| 60 | sub _init_ua { | ||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | my $UA = LWP::UserAgent::->new(); | ||
| 64 | $UA->agent('Monitoring::Spooler/0.11'); | ||
| 65 | if($UA->can('ssl_opts')) { | ||
| 66 | $UA->ssl_opts( verify_hostname => 0, ); | ||
| 67 | } | ||
| 68 | |||
| 69 | return $UA; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub _init_responses { | ||
| 73 | my $self = shift; | ||
| 74 | |||
| 75 | # see http://www.smstrade.de/pdf/SMS-Gateway_HTTP_API_v2_de.pdf, page 5 | ||
| 76 | my $resp_ref = { | ||
| 77 | '10' => 'Destination Number not correct (Parameter: to)', | ||
| 78 | '20' => 'Source Number not correct (Parameter: from)', | ||
| 79 | '30' => 'Message not correct (Parameter: message)', | ||
| 80 | '31' => 'Message type not correct (Parameter: messagetype)', | ||
| 81 | '40' => 'SMS Route not correct (Parameter: route)', | ||
| 82 | '50' => 'Identification failed (Parameter: key)', | ||
| 83 | '60' => 'Insufficient Funds.', | ||
| 84 | '70' => 'Destination Network not covered. Use another route.', | ||
| 85 | '71' => 'Feature not available. Use another route.', | ||
| 86 | '80' => 'Failed to submit to SMS-C. Use another route or contact support.', | ||
| 87 | '100' => 'SMS successfull submitted.', | ||
| 88 | }; | ||
| 89 | |||
| 90 | return $resp_ref; | ||
| 91 | } | ||
| 92 | |||
| 93 | # your code here ... | ||
| 94 | sub provides { | ||
| 95 | my $self = shift; | ||
| 96 | my $type = shift; | ||
| 97 | |||
| 98 | if($type =~ m/^text$/i) { | ||
| 99 | return 1; | ||
| 100 | } | ||
| 101 | |||
| 102 | return; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub run { | ||
| 106 | my $self = shift; | ||
| 107 | my $destination = shift; | ||
| 108 | my $message = shift; | ||
| 109 | |||
| 110 | $destination = $self->_clean_number($destination); | ||
| 111 | $message = substr($message,0,159); | ||
| 112 | |||
| 113 | my %args = ( | ||
| 114 | 'key' => $self->apikey(), | ||
| 115 | 'message' => $message, | ||
| 116 | 'to' => $destination, | ||
| 117 | 'route' => $self->route(), | ||
| 118 | 'from' => 'Zabbix', | ||
| 119 | 'cost' => 1, | ||
| 120 | 'message_id' => 1, | ||
| 121 | 'count' => 1, | ||
| 122 | ); | ||
| 123 | |||
| 124 | my $content = join('&', map { uri_escape($_).'='.uri_escape($args{$_}) } keys %args); | ||
| 125 | |||
| 126 | my $req = HTTP::Request::->new( GET => $self->url().'?'.$content, ); | ||
| 127 | my $res = $self->_ua()->request($req); | ||
| 128 | |||
| 129 | $self->logger()->log( message => 'Requesting URL '.$self->url(), level => 'debug', ); | ||
| 130 | |||
| 131 | if($res->is_success() && $res->content() =~ m/^100\D/) { | ||
| 132 | $self->logger()->log( message => 'Sent '.$message.' to '.$destination, level => 'debug', ); | ||
| 133 | return 1; | ||
| 134 | } else { | ||
| 135 | my $errstr = $res->content(); | ||
| 136 | if($self->responses()->{$errstr}) { | ||
| 137 | $errstr .= ' - '.$self->responses()->{$errstr}; | ||
| 138 | } | ||
| 139 | $self->logger()->log( message => 'Failed to send '.$message.' to '.$destination.'. Error: '.$errstr, level => 'debug', ); | ||
| 140 | return; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | no Moose; | ||
| 145 | __PACKAGE__->meta->make_immutable; | ||
| 146 | |||
| 147 | 1; | ||
| 148 | |||
| 149 | __END__ |