SMS::Send::Adapter::Node::Red - SMS::Send Adapter to Node-RED JSON HTTP request
=head1 SYNOPSIS
CGI Application
use SMS::Send::Adapter::Node::Red;
my $service = SMS::Send::Adapter::Node::Red->new(content => join('', <>));
$service->cgi_response;
PSGI Application
use SMS::Send::Adapter::Node::Red;
SMS::Send::Adapter::Node::Red->psgi_app
PSGI Plack Mount
use SMS::Send::Adapter::Node::Red;
use Plack::Builder qw{builder mount};
builder {
mount '/sms' => SMS::Send::Adapter::Node::Red->psgi_app;
mount '/' => sub {[404=> [], []]};
}
=head1 DESCRIPTION
This Perl package provides an adapter from Node-RED HTTP request object with a JSON payload to the SMS::Send infrastructure using either a PSGI or a CGI script. The architecture works easiest with SMS::Send drivers based on the L<SMS::Send::Driver::WebService> base object since common settings can be stored in the configuration file.
=head1 CONSTRUCTOR
=head2 new
my $object = SMS::Send::Adapter::Node::Red->new(content=>$string_of_json_object);
=head1 PROPERTIES
=head2 content
JSON string payload of the HTTP post request.
Example Payload:
{
"to" : "7035551212",
"text" : "My Text Message",
"driver" : "VoIP::MS",
"options" : {}
}
The Perl logic is based on this one-liner with lots of error trapping
my $sent = SMS::Send->new($driver, %$options)->send_sms(to=>$to, text=>$text);
I use a Node-RED function like this to format the JSON payload.
my_text = msg.payload;
msg.payload = {
"driver" : "VoIP::MS",
"text" : my_text,
"to" : "7035551212",
"options" : {"key" : "value"},
};
return msg;
=cut
subcontent {
my$self= shift;
die("Error: content not defined on construction") unlessdefined$self->{'content'};
HTTP Status Code returned to Node-RED is one of 200, 400, 500 or 502. Typically, a 200 means the SMS message was successfully sent to the provider, a 400 means the input is malformed, a 500 means the server is misconfigured (verify installation), and a 502 means that the remote service has issues or is unreachable.
=cut
substatus {
my$self= shift;
$self->{'status'} = shiftif@_;
die("Error: status not set. sms_send method must be called first") unless$self->{'status'};
return$self->{'status'};
}
=head2 status_string
Format HTTP Status Code as string for web response
=cut
our$STATUS_STRING= {
200=> 'OK',
400=> 'Bad Request',
500=> 'Internal Server Error',
502=> 'Bad Gateway',
};
substatus_string {
my$self= shift;
my$status= $self->status;
my$status_string= $STATUS_STRING->{$status} or die("Error: STATUS_STRING not defined for $status");
return"$status $status_string";
}
=head2 error
Error string passed in the JSON return object.
=cut
suberror {
my$self= shift;
$self->{'error'} = shiftif@_;
return$self->{'error'};
}
=head2 set_status_error
Method to set the HTTP status and error with one function call.
=cut
subset_status_error {
my$self= shift;
my$status= shiftor die;
my$error= shift|| '';
$self->status($status);
$self->error($error);
return$self;
}
=head2 driver
Returns configured SMS driver from input, environment, or SMS-Send.ini.
my$text= qq{Failed to load Perl package SMS::Send with driver "$driver". Ensure SMS::Send::$driver is installed. $error};
$self->set_status_error(500=>$text);
}
} else{
$self->set_status_error(400=>'Error: JSON input "options" not an object.');
}
} else{
$self->set_status_error(400=>'Error: "driver" not defined in JSON payload, environment variable SMS_SEND_ADAPTER_NODE_RED_DRIVER, or in SMS-Send.ini.');
}
}
return$self->{'SMS'};
}
=head1 SEE ALSO
L<SMS::Send>, L<CGI>, L<JSON::XS>
=head1 AUTHOR
Michael R. Davis
=head1 COPYRIGHT AND LICENSE
MIT License
Copyright (c) 2020 Michael R. Davis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
=cut
1;
Keyboard Shortcuts
Global
s
Focus search bar
?
Bring up this help dialog
GitHub
gp
Go to pull requests
gi
go to github issues (only if github is preferred repository)