#!/usr/bin/perl use strict; use warnings; use Plack::Builder; use Plack::Request; use Plack::Middleware::ReverseProxy; use Plack::App::File; use File::Path qw(make_path); use Fcntl qw(:flock); use POSIX qw(strftime); use warnings; use WWW::Telegram::BotAPI; my $bot = WWW::Telegram::BotAPI->new(token => 'bot-token'); sub save_customer { my ($cinput, $cip) = @_; return unless defined $cinput && length $cinput; make_path('customers'); open my $fh, '>>', 'customers/customers.txt' or die $!; flock $fh, LOCK_EX; my $ts = strftime('%Y-%m-%d %H:%M:%S', localtime); print $fh "$cip :: $ts\t$cinput\n\n"; close $fh; $bot->sendMessage({chat_id => 'teleuser', text => "At $ts\n$cip\n$cinput",}); } my $root = './public'; my $static_app = Plack::App::File->new(root => $root)->to_app; my $index_app = Plack::App::File->new(file => "$root/index.html")->to_app; my $thank_you_app = Plack::App::File->new(file => "$root/thank-you.html")->to_app; builder { enable "Plack::Middleware::ReverseProxy"; sub { my $env = shift; my $req = Plack::Request->new($env); # Handle form POST if ($req->method eq 'POST' && $req->path eq '/submit') { my $cinput = $req->parameters->{cinput}; my $cip = $env->{REMOTE_ADDR}; save_customer($cinput, $cip); return [ 303, [ Location => '/thank-you.html' ], [] ]; } my $res = $static_app->($env); return $res if $res->[0] != 404; return $thank_you_app->($env) if $req->path eq '/thank-you.html'; return $index_app->($env); }; };