
Another Telegram bot for video surveillance
In this article we will consider the basic principles of the telegram bot for video surveillance.
After reading an article about the created telegram bot from improvised materials , I wanted to share my solution for video surveillance with the public.
Iron
Unlike the approach of the author of this article, I purposefully purchased the necessary equipment for organizing video surveillance in the apartment for the purpose of additional security. I bought an IP camera that was able to monitor the movement in the frame, put it in a folder on ftp and even send it by email. But all this was, firstly, inconvenient, and secondly, there was an error in the firmware that sending to e-mail did not work properly, and, finally, there were a lot of false positives. Therefore, it was decided to write a telegram bot, as the most convenient way to alert and manage surveillance.
So, from iron, I have an IPEYE camera that can take power over the network (POE), a POE injector, a small server on Ubuntu Server, the Internet with backup via a USB modem, and UPS to support the power of the entire system.
Determine the door opening
I have only one camera aimed at the front door. Ideally, I wanted me to get an image of a person entering this door. First I tried to set up motion detection using the camera itself:

But unfortunately, no matter how I twisted the sliders, the camera in the twilight went crazy and began to constantly alert about the movement in the frame when switching from day to night mode and vice versa. Could, of course, be raised
motion
, he monitors the movement in the frame much more accurately. But I did not want to raise an additional service, so I decided to leave the camera’s standard ability to store images on ftp, and check for false positives on my own.To do this, I wrote a simple demon in php that monitors the folder for new images, opens them and analyzes the difference in the illumination of pre-selected areas, something like this (the areas are specially selected above so that a person does not accidentally close the areas):

If the difference exceeds the experimentally selected threshold, then the door is really open.
We set up a bot
So, our demon determined that there is movement in the frame and the door is open. Next, send this image to the telegram chat. To do this, we need to register a new bot through BotFather and get an API key. To interact with the Telegram API, I used the longman / telegram-bot library . Telegram provides two ways to receive new long-polling messages and a web hook. The second method seemed preferable to me, but it would require a static URL with an SSL certificate to work. To do this, use letsencrypt or a self-signed certificate that must be sent to BotFather. You can read more about registering bots in the telegram documentation.
Analysis of Wi-Fi Clients
When you receive a lot of messages, you just start to ignore them, so some automation was necessary so that notifications were sent only when no one was home. This can be done very simply, just scan the network for the presence of certain wi-fi clients in it. To do this, you need to install arp-scan
( sudo apt-get install arp-scan
), then you can determine that the client with the necessary mac address is connected as follows:
$output = exec("arp-scan -q --retry=1 --timeout=500 --numeric --destaddr={$mac} {$ip} | grep -oP --color=never \"{$mac}\"");
$result = $output === $mac;
Here I specifically indicate a specific ip address so as not to scan the entire network. But for this it is necessary to fix the IP-shnik in DHCP for this MAC address. But, in principle, specifying ip is optional.
Video recording
Photography is, of course, good. But best of all will show the incoming person - video. Since the video is not being written anywhere, you can "get it" only by recording the stream. Fortunately, my camera provides several rtsp streams encoded in h264. To send a video, you need to record a stream, and send it as a file. We’ll use it avconv
(fork ffmpeg
in Ubuntu):
$ffmpeg = "avconv -rtsp_transport tcp -i rtsp://user:password@192.168.1.10/1/h264major -t 10 -an -vcodec copy {$file}";
$mv = "mv {$file} {$out}";
passthru("nohup sh -c '{$ffmpeg} && {$mv}' > /dev/null 2>&1 &");
Here I write a video in the background, after which I move it to a folder for tracking. The daemon will pick up the new file and send it as a video. Since I don’t have sound in the video, Telegram automatically converts it to gif on the desktop client, which is very convenient.
Demon Auto Up
The daemon is written in php, and although it can work for months, no one is safe from errors. Therefore, it would be nice to see that the demon did not complete the process. It could, of course, be set up supervisor
, but I decided to do just auto-cron-raising. If the process is alive, the daemon does not restart:
passthru("ps -p $pid > /dev/null", $result);
return !$result;
Conclusion
Thanks to all the actions done, the bot notifies me of an incoming person with two photos and a five-second video. As a bonus, the bot can also notify about the time of departure and arrival of certain wi-fi clients, and whether they are now at home.
You can view the result on github .