summaryrefslogtreecommitdiff
path: root/bin/msg_template_http-demo.pl
diff options
context:
space:
mode:
authorMark Wells <mark@freeside.biz>2015-08-29 13:37:23 -0700
committerMark Wells <mark@freeside.biz>2015-08-29 13:37:23 -0700
commita008b601383d5693a197f4bf57ed5ba7887f3065 (patch)
tree3dede7f6edd9361239091cbf89716c02fe747158 /bin/msg_template_http-demo.pl
parent81e562e6067ccf33c24ab3713163a0eefb1438bd (diff)
#21564, external message services: REST client
Diffstat (limited to 'bin/msg_template_http-demo.pl')
-rwxr-xr-xbin/msg_template_http-demo.pl76
1 files changed, 76 insertions, 0 deletions
diff --git a/bin/msg_template_http-demo.pl b/bin/msg_template_http-demo.pl
new file mode 100755
index 000000000..8d184fc85
--- /dev/null
+++ b/bin/msg_template_http-demo.pl
@@ -0,0 +1,76 @@
+=head1 NAME
+
+FS::msg_template::http example server.
+
+=head1 DESCRIPTION
+
+This is an incredibly crude Mojo web service for demonstrating how to talk
+to the HTTP customer messaging interface in Freeside.
+
+It implements an endpoint for the "password reset" messaging case which
+creates a simple password reset message using some template variables,
+and a "send" endpoint that just delivers the message by sendmail. The
+configuration to use this as your password reset handler would be:
+
+prepare_url = 'http://localhost:3000/prepare/password_reset'
+send_url = 'http://localhost:3000/send'
+No username, no password, no additional content.
+
+=cut
+
+use Mojolicious::Lite;
+use Mojo::JSON qw(decode_json encode_json);
+use Email::Simple;
+use Email::Simple::Creator;
+use Email::Sender::Simple qw(sendmail);
+
+post '/prepare/password_reset' => sub {
+ my $self = shift;
+
+ my $json_data = $self->req->body;
+ #print STDERR $json_data;
+ my $input = decode_json($json_data);
+ if ( $input->{username} ) {
+ my $output = {
+ 'to' => $input->{invoicing_email},
+ 'subject' => "Password reset for $input->{username}",
+ 'body' => "
+To complete your $input->{company_name} password reset, please go to
+$input->{selfservice_server_base_url}/selfservice.cgi?action=process_forgot_password;session_id=$input->{session_id}
+
+This link will expire in 24 hours.",
+ };
+
+ return $self->render( json => $output );
+
+ } else {
+
+ return $self->render( text => 'Username required', status => 500 );
+
+ }
+};
+
+post '/send' => sub {
+ my $self = shift;
+
+ my $json_data = $self->req->body;
+ my $input = decode_json($json_data);
+ my $email = Email::Simple->create(
+ header => [
+ From => $ENV{USER}.'@localhost',
+ To => $input->{to},
+ Subject => $input->{subject},
+ ],
+ body => $input->{body},
+ );
+ local $@;
+ eval { sendmail($email) };
+ if ( $@ ) {
+ return $self->render( text => $@->message, status => 500 );
+ } else {
+ return $self->render( text => '' );
+ }
+};
+
+app->start;
+