| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | # ABSTRACT: Telegram bot that send you a snapshot from IP camera using ffmpeg (don't forget to install it!) | ||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 | |||
| 9 | package Telegram::CamshotBot; | ||
| 10 | $Telegram::CamshotBot::VERSION = '0.01'; | ||
| 11 | use Telegram::CamshotBot::Util qw(first_existing_file random_caption abs_path_of_sample_mojo_conf fev); | ||
| 12 | use Mojolicious::Lite; | ||
| 13 | use Mojolicious::Plugin::JSONConfig; | ||
| 14 | use WWW::Telegram::BotAPI; | ||
| 15 | use Date::Format; | ||
| 16 | use Telegram::Bot::Message; | ||
| 17 | use feature 'say'; | ||
| 18 | use Data::Dumper; | ||
| 19 | use Data::Printer; | ||
| 20 | use Cwd; | ||
| 21 | use Net::Ping; | ||
| 22 | use Regexp::Common qw /net/; | ||
| 23 | |||
| 24 | my $config_file_path = first_existing_file( | ||
| 25 | $ENV{"CAMSHOTBOT_CONFIG"}, | ||
| 26 | $ENV{"HOME"}.'/.camshotbot', | ||
| 27 | getcwd.'/camshotbot.conf.json', | ||
| 28 | abs_path_of_sample_mojo_conf(__PACKAGE__), | ||
| 29 | ); | ||
| 30 | |||
| 31 | print "Using config: ".$config_file_path."\n"; | ||
| 32 | |||
| 33 | my $config_values = plugin 'JSONConfig' => { file => $config_file_path }; | ||
| 34 | |||
| 35 | plugin( 'Webtail', file => $config_values->{log_file} ); # https://metacpan.org/pod/Mojolicious::Plugin::Webtail | ||
| 36 | |||
| 37 | # BEGIN { $ENV{TELEGRAM_BOTAPI_DEBUG}=1 }; | ||
| 38 | |||
| 39 | my $api; | ||
| 40 | my $bot_name = ''; | ||
| 41 | my $telegram_token = $ENV{CAMSHOTBOT_TELEGRAM_API_TOKEN} || $config_values->{telegram_api_token}; | ||
| 42 | my $screenshot_file = $ENV{CAMSHOTBOT_LAST_SHOT_FILENAME} || $config_values->{last_shot_filename} || 'latest.jpg'; # name of last screenshot or env | ||
| 43 | my $stream_url = $ENV{CAMSHOTBOT_STREAM_URL} || $config_values->{stream_url}; | ||
| 44 | my $ffmpeg_cmd = 'ffmpeg -hide_banner -loglevel panic -i '.$stream_url.' -f image2 -vframes 1 '.$screenshot_file if ($stream_url); | ||
| 45 | my ($camera_ip) = ($stream_url =~ /($RE{net}{IPv4})/) if ($stream_url); | ||
| 46 | my $bot_domain = $ENV{VIRTUAL_HOST} || $ENV{LETSENCRYPT_HOST} || $ENV{CAMSHOTBOT_DOMAIN} || $config_values->{bot_domain}; | ||
| 47 | my $polling_flag = $ENV{CAMSHOTBOT_POLLING} || $config_values->{polling}; | ||
| 48 | my $docker_flag = $ENV{CAMSHOTBOT_FFMPEG_DOCKER} || $config_values->{ffmpeg_docker}; | ||
| 49 | |||
| 50 | if ($telegram_token) { # maybe add | ||
| 51 | $api = WWW::Telegram::BotAPI->new ( | ||
| 52 | token => $telegram_token | ||
| 53 | ); | ||
| 54 | $bot_name = $api->getMe->{result}{username}; | ||
| 55 | } else { | ||
| 56 | say "Attention! Telegram API token isn't specified. Please edit ".$config_file_path." or CAMSHOTBOT_TELEGRAM_API_TOKEN"; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | helper answer => sub { | ||
| 61 | my ($c, $update) = @_; | ||
| 62 | |||
| 63 | app->log->info("Processing new update..."); | ||
| 64 | my $mo = Telegram::Bot::Message->create_from_hash($update->{message}); | ||
| 65 | |||
| 66 | my $msg = $mo->text; | ||
| 67 | my $chat_id = $mo->chat->id; | ||
| 68 | my $from_id = $mo->from->id; | ||
| 69 | my $date = $mo->date; | ||
| 70 | my $date_str = time2str("%R %a %o %b %Y" ,$mo->date); # 11:59 Sun 29th Jan 2017 | ||
| 71 | |||
| 72 | |||
| 73 | ###### Loggging | ||
| 74 | if ($config_values->{debug}) { | ||
| 75 | # full log, convenient if you need to restict chat_id's and check what's wrong | ||
| 76 | app->log->info("Update from Telegram API: ".Dumper $update); | ||
| 77 | app->log->info("Update parsed by Telegram::Bot::Message: ".Dumper $mo); | ||
| 78 | } else { | ||
| 79 | my $from_str = ''; | ||
| 80 | my $username = $mo->from->username; | ||
| 81 | if ($username) { | ||
| 82 | $from_str = $username; | ||
| 83 | } else { | ||
| 84 | $from_str = $mo->from->first_name." ".$mo->from->first_name." (id ".$from_id.")"; | ||
| 85 | } | ||
| 86 | app->log->info($msg." from ".$from_str." sent at ".$date_str); | ||
| 87 | }; | ||
| 88 | ###### end loggging | ||
| 89 | |||
| 90 | if ($docker_flag) { | ||
| 91 | app->log->info("Sending a screenshot generated by ffmpeg docker container some time ago"); | ||
| 92 | } else { | ||
| 93 | `rm -f $screenshot_file`; # remove old screenshot | ||
| 94 | my $o = `$ffmpeg_cmd`; | ||
| 95 | app->log->info("Screenshot got with command: ".$ffmpeg_cmd.', result : '.$o); | ||
| 96 | } | ||
| 97 | |||
| 98 | if ( ($msg eq "/shot") || ($msg eq '/shot@'.$bot_name )) { | ||
| 99 | |||
| 100 | $api->sendPhoto ({ | ||
| 101 | chat_id => $chat_id, | ||
| 102 | photo => { | ||
| 103 | file => $screenshot_file | ||
| 104 | }, | ||
| 105 | caption => random_caption(), | ||
| 106 | reply_to_message_id => $mo->message_id | ||
| 107 | }); | ||
| 108 | } | ||
| 109 | |||
| 110 | if ($msg eq "/help") { | ||
| 111 | |||
| 112 | $api->sendMessage ({ | ||
| 113 | chat_id => $chat_id, | ||
| 114 | text => '/shot - Get online camera shot', | ||
| 115 | reply_to_message_id => $mo->message_id | ||
| 116 | }); | ||
| 117 | } | ||
| 118 | |||
| 119 | }; | ||
| 120 | |||
| 121 | # for local testing purposes. also shows how many unprocessed updates in queue on server | ||
| 122 | helper check_for_updates => sub { | ||
| 123 | my $c = shift; | ||
| 124 | my $res = $api->deleteWebhook() ; # disable webhooks | ||
| 125 | # warn Dumper $res; | ||
| 126 | my $updates = $api->getUpdates(); | ||
| 127 | my $h = { | ||
| 128 | updates_in_queue => {} | ||
| 129 | }; | ||
| 130 | $h->{updates_in_queue}{count} = scalar @{$updates->{result}}; | ||
| 131 | $h->{updates_in_queue}{details} = \@{$updates->{result}}; | ||
| 132 | |||
| 133 | my @u_ids; | ||
| 134 | for (@{$updates->{result}}) { | ||
| 135 | push @u_ids, $_->{update_id}; | ||
| 136 | } | ||
| 137 | |||
| 138 | $h->{updates_in_queue}{update_ids} = \@u_ids; | ||
| 139 | |||
| 140 | $c->setWebhook() if !($config_values->{polling}); # set Webhook again if needed | ||
| 141 | |||
| 142 | return $h; | ||
| 143 | }; | ||
| 144 | |||
| 145 | helper setWebhook => sub { | ||
| 146 | my $c = shift; | ||
| 147 | return $api->setWebhook({ url => 'https://'.$bot_domain.'/'.$config_values->{telegram_api_token} }); | ||
| 148 | }; | ||
| 149 | |||
| 150 | |||
| 151 | post '/'.$telegram_token => sub { | ||
| 152 | my $c = shift; | ||
| 153 | my $update = $c->req->json; | ||
| 154 | $c->answer($update); | ||
| 155 | $c->render(json => "ok"); | ||
| 156 | }; | ||
| 157 | |||
| 158 | get '/' => sub { | ||
| 159 | shift->render(text => 'bot is running'); | ||
| 160 | }; | ||
| 161 | |||
| 162 | get '/status' => sub { | ||
| 163 | my $c = shift; | ||
| 164 | my $status = {}; | ||
| 165 | $status->{telegram_api} = eval { $api->getMe } or $status->{telegram_api} = $api->parse_error->{msg}; | ||
| 166 | $status->{stream_url} = $stream_url; | ||
| 167 | $status->{ffmpeg_docker} = $docker_flag; | ||
| 168 | my $p = Net::Ping->new(); | ||
| 169 | $status->{vpn_status} = 'down'; | ||
| 170 | $status->{vpn_status} = 'up' if $p->ping($camera_ip); | ||
| 171 | $status->{WebhookInfo} = $api->getWebhookInfo; | ||
| 172 | $p->close(); | ||
| 173 | $c->render(json => $status); | ||
| 174 | }; | ||
| 175 | |||
| 176 | get '/setwebhook' => sub { | ||
| 177 | my $c = shift; | ||
| 178 | my $res = $c->setWebhook(); | ||
| 179 | $c->render( json => $res ); | ||
| 180 | }; | ||
| 181 | |||
| 182 | # shows info about unprocessed updates on server | ||
| 183 | get '/debug' => sub { | ||
| 184 | my $c = shift; # $c = Mojolicious::Controller object | ||
| 185 | $c->render( json => $c->check_for_updates() ); | ||
| 186 | }; | ||
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | |||
| 191 | if ($telegram_token && $polling_flag) { | ||
| 192 | |||
| 193 | my $res = $api->deleteWebhook(); | ||
| 194 | app->log->info("Webhook was deleted. Starting polling with ".$config_values->{polling_timeout}."secs timeout ...") if $res; | ||
| 195 | |||
| 196 | Mojo::IOLoop->recurring($config_values->{polling_timeout} => sub { | ||
| 197 | |||
| 198 | my @updates = @{$api->getUpdates->{result}}; | ||
| 199 | |||
| 200 | if (@updates) { | ||
| 201 | for my $u (@updates) { | ||
| 202 | #app->build_controller->answer($u); # Mojolicious::Lite -> Mojolicious::Controller -> Mojolicious::Helper | ||
| 203 | app->answer($u); # Mojolicious::Lite -> Mojolicious::Controller -> Mojolicious::Helper | ||
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | }); | ||
| 208 | } | ||
| 209 | |||
| 210 | # my $queue = app->build_controller->check_for_updates()->{updates_in_queue}; | ||
| 211 | # if ne daemon | ||
| 212 | |||
| 213 | if ($telegram_token) { | ||
| 214 | my $queue = app->check_for_updates()->{updates_in_queue}; | ||
| 215 | app->log->info('Starting bot @'.$bot_name."..."); | ||
| 216 | app->log->info("Having ".$queue->{count}." stored Updates at Telegram server"); | ||
| 217 | app->log->info("Unprocessed update ids (for offset debug): ".join(',', @{$queue->{update_ids}}) ); | ||
| 218 | } | ||
| 219 | |||
| 220 | app->start; | ||
| 221 | |||
| 222 | 1; | ||
| 223 | |||
| 224 | __END__ |