Zoneminder&Telegram (event instant messaging to your phone!)

Forum for questions and support relating to the 1.27.x releases only.
PacoLM
Posts: 971
Joined: Wed Dec 02, 2009 9:55 pm
Location: Spain

Zoneminder&Telegram (event instant messaging to your phone!)

Post by PacoLM »

Hi,

I was thinking in using my ZM server to be able to send instant messages to my phone with the pictures or videos. I found some information about how to send instant messaging with WhatsApp, but the company has changed the auth process, so I moved to Telegram (http://www.telegram.org).
You need to install expect (apt-get install expect).

Right now, I have Telegram CLI working in my ZM server (easy, just followed some instructions detailed here https://github.com/vysheng/tg), and wrote a simple bash script that allows me to send a message and a picture of the event.

This is the script, adapted from many different sources on the web:

Code: Select all

#!/usr/bin/expect
set user [lindex $argv 0]
set text [lindex $argv 1]
set image [lindex $argv 2]
set video [lindex $argv 3]
# this is the path where telegram CLI is installed and the public key for the server
spawn /usr/src/telegram/tg-master/telegram -k /usr/src/telegram/tg-master/tg.pub
expect "User"
send "msg $user $text\r"
send "send_photo $user $image\r"
send "send_video $user $video\r"
expect "Sent:"
send "safe_quit\r"
expect eof
It seems reasonable to add a new option in the filter settings, to allow users to send also a instant message instead of sending an email, ftp to a server, etc. If I have time, I will try to add the option in the filter window and also add the code in the zmfilter.pl. I have experience programming in C#, Tcl and VB, but I'm not 'fluent' in Python, Perl or PHP, so, if anyone wants to take the credits and add this feature...just go ahead!

Regards,

PacoLM

After more than 15 years, no longer using ZM as surveillance system.
Now in the dark side, using a commercial system...
PacoLM
Posts: 971
Joined: Wed Dec 02, 2009 9:55 pm
Location: Spain

Re: Zoneminder&Telegram (event instant messaging to your pho

Post by PacoLM »

Hi,

Some advances, using python:

Code: Select all

#!usr/bin/python
import pexpect
import time
import argparse

parser = argparse.ArgumentParser(description='Script to send images to Telegram')
parser.add_argument('-u','--user',help='User name', required=True)
parser.add_argument('-m','--message',help='Message', required=True)
parser.add_argument('-i','--imagefile',help='Image filename', required=True)
args = parser.parse_args()
# use your telegram location here
telegram = pexpect.spawn('/etc/telegram/telegram -k /etc/telegram/tg-server.pub') 
telegram.expect('0m')
telegram.sendline('msg '+args.user+' '+args.message)
telegram.sendline('send_photo '+args.user+' '+args.imagefile)
telegram.expect('100',timeout = 1200)
telegram.expect('photo')
telegram.expect('0m')
telegram.sendline('safe_quit')
The python script allows to send a message to the user with the parameters: User, Message & Image.

Regards,

PacoLM

After more than 15 years, no longer using ZM as surveillance system.
Now in the dark side, using a commercial system...
PacoLM
Posts: 971
Joined: Wed Dec 02, 2009 9:55 pm
Location: Spain

Re: Zoneminder&Telegram (event instant messaging to your pho

Post by PacoLM »

No one interested?

After more than 15 years, no longer using ZM as surveillance system.
Now in the dark side, using a commercial system...
6i6i
Posts: 7
Joined: Mon Jan 11, 2016 8:55 am

Re: Zoneminder&Telegram (event instant messaging to your phone!)

Post by 6i6i »

Hi!

I am very interested in doing the same thing! Have you made any progress on this?

BR
Martin
PacoLM
Posts: 971
Joined: Wed Dec 02, 2009 9:55 pm
Location: Spain

Re: Zoneminder&Telegram (event instant messaging to your phone!)

Post by PacoLM »

6i6i wrote:Hi!

I am very interested in doing the same thing! Have you made any progress on this?

BR
Martin
Hi,

Yes, I have setup telegram-cli in the same server running as a daemon. It's executing a script and responding to a set of user defined commands, like '1', that sends a capture of the camera number 1, '1x' makes a small video of 10 captures.

Regards,

PacoLM

After more than 15 years, no longer using ZM as surveillance system.
Now in the dark side, using a commercial system...
6i6i
Posts: 7
Joined: Mon Jan 11, 2016 8:55 am

Re: Zoneminder&Telegram (event instant messaging to your phone!)

Post by 6i6i »

Sounds cool. Could you share the script?

BR
Martin
User avatar
asker
Posts: 1553
Joined: Sun Mar 01, 2015 12:12 pm

Re: Zoneminder&Telegram (event instant messaging to your phone!)

Post by asker »

Does it make sense to contribute that feature to https://github.com/pliablepixels/zmeventserver ?
I no longer work on zmNinja, zmeventnotification, pyzm or mlapi. I may respond on occasion based on my available time/interest.

Please read before posting:
How to set up logging properly
How to troubleshoot and report - ES
How to troubleshoot and report - zmNinja
ES docs
zmNinja docs
6i6i
Posts: 7
Joined: Mon Jan 11, 2016 8:55 am

Re: Zoneminder&Telegram (event instant messaging to your phone!)

Post by 6i6i »

That would be very nice.
PacoLM
Posts: 971
Joined: Wed Dec 02, 2009 9:55 pm
Location: Spain

Re: Zoneminder&Telegram (event instant messaging to your phone!)

Post by PacoLM »

Hi,

First of all, I recommend you to follow the instructions explained here (https://github.com/vysheng/tg) in order to have the telegram client installed in your server. Once you have done this, you need to run the telegram client as a daemon and run a script when a custom command is sent. I have 5 cameras, so sending the camera number from my telegram ID to myself, request and send a picture of the camera.
This could be improved, being executed only when motion is detected, so a video could be created and be sent. It is in my huge lists of ToDo things. I will post detailed instructions later, but is not difficult after reading some how-to guides in the internet.

Regards,

PacoLM

After more than 15 years, no longer using ZM as surveillance system.
Now in the dark side, using a commercial system...
6i6i
Posts: 7
Joined: Mon Jan 11, 2016 8:55 am

Re: Zoneminder&Telegram (event instant messaging to your phone!)

Post by 6i6i »

I already have the telgram client installed and running (but not as an daemon).
I think you mean I should configure a zoneminder filter which launches a custom script if the filter is being executed (I think I am able to do this with "Execute command on all matches" line)?
But how do I get an actual picture of the camera for example if I send 1 do my telegram daemon?

BR
StNick
Posts: 6
Joined: Tue Feb 16, 2016 5:17 pm

Re: Zoneminder&Telegram (event instant messaging to your phone!)

Post by StNick »

HI PacoLM.

I came here because I was writing a Python script myself to do the very same thing. I have it executing my script just fine, but it seems that Zoneminder only passes a single argument to the command specified in "Execute command on all events". That argument is the path to the event itself. How are you getting it to pass the path to the image?
olgs
Posts: 3
Joined: Wed Feb 24, 2016 8:52 am

Re: Zoneminder&Telegram (event instant messaging to your phone!)

Post by olgs »

You get an image by:

- shelling out to bash in the script and doing a wget or curl directly at the camera, e.g. wget http://ip.of.the.camera/snapshot.cgi ; or
- use the argument directly and write a script to create a video using ffmpeg, send it using telegram then delete the video, see below.

Note that the code below send the video twice--this is to get around the bug/feature of newer Telegram clients that change all small MP4 to inline looping video and 3GP as videos you can actually download.

Code: Select all


#!/bin/bash
/usr/bin/ffmpeg -hide_banner -nostats -loglevel 0 -y -r 1 -i $2/%05d-capture.jpg -an -r 5 -filter:v "setpts=0.2*PTS" -vcodec libx264 -crf 40 -f mp4 /usr/share/zoneminder/www/video.mp4
/usr/local/bin/telegram-cli -D -k /etc/telegram-cli/server.pub -c /etc/telegram-cli/config -W -e "send_video $1 /usr/share/zoneminder/www/video.mp4" </dev/null >/dev/null 2>&1
mv /usr/share/zoneminder/www/video.mp4 /usr/share/zoneminder/www/video.3gp
/usr/local/bin/telegram-cli -D -k /etc/telegram-cli/server.pub -c /etc/telegram-cli/config -W -e "send_document $1 /usr/share/zoneminder/www/video.3gp" </dev/null >/dev/null 2>&1
rm -f /usr/share/zoneminder/www/sendtg.3gp


kl
woody4165
Posts: 5
Joined: Fri Feb 03, 2017 3:02 pm

Re: Zoneminder&Telegram (event instant messaging to your phone!)

Post by woody4165 »

PacoLM wrote:Hi,

First of all, I recommend you to follow the instructions explained here (https://github.com/vysheng/tg) in order to have the telegram client installed in your server. Once you have done this, you need to run the telegram client as a daemon and run a script when a custom command is sent. I have 5 cameras, so sending the camera number from my telegram ID to myself, request and send a picture of the camera.
This could be improved, being executed only when motion is detected, so a video could be created and be sent. It is in my huge lists of ToDo things. I will post detailed instructions later, but is not difficult after reading some how-to guides in the internet.

Regards,

PacoLM
Hi PacoLM

I am trying to send notification via Telegram, already installed in my RPi, but with no success.

I still have to understand more, especially about Filters.
What I was trying to do, looking at the example in the Options/Email tab, was to integrate with Telegram, sending a notification and an image to it.

So I wrote a small script like this:

Code: Select all

#!/bin/sh
message="Test $1"
curl --data chat_id=xxxxxx --data-urlencode "text=$message"  "https://api.telegram.org/botxxxxxxxxxxx/sendMessage"
curl -s -X POST "https://api.telegram.org/botxxxxxxxx/sendPhoto" -F chat_id
=xxxxxxx -F photo="@$2"
exit 0

and in the field "Execute command on all matches" I wrote "/pathto/example.sh %ED% %EIM%"
but what I get is a message "Test %ED%"

How can I give the script the right informations?

Thanks
PacoLM
Posts: 971
Joined: Wed Dec 02, 2009 9:55 pm
Location: Spain

Re: Zoneminder&Telegram (event instant messaging to your phone!)

Post by PacoLM »

Hi,

What I did is to write a LUA script that runs the Telegram daemon, I send a command in the Telegram app that generates a capture of the selected camera(s). Is just the other way around, Telegram is used to get the information, is not ZM sending a message on demand.

Regards,
PacoLM

After more than 15 years, no longer using ZM as surveillance system.
Now in the dark side, using a commercial system...
woody4165
Posts: 5
Joined: Fri Feb 03, 2017 3:02 pm

Re: Zoneminder&Telegram (event instant messaging to your phone!)

Post by woody4165 »

Ok. I got ir. But how do you activate it from Zm?
Can you share the activation part? In the previous msg, I have not send Lua script, but other language one, is it correct?
Thanks
Locked