Created
October 16, 2020 04:27
-
-
Save suadanwar/098955686c719ab3c45902840a0c6cf0 to your computer and use it in GitHub Desktop.
This sample code is for Send video to telegram bot using Raspberry Pi tutorial.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import telepot | |
from picamera import PiCamera | |
import RPi.GPIO as GPIO | |
import time | |
from time import sleep | |
import datetime | |
from telepot.loop import MessageLoop | |
from subprocess import call | |
PIR = 4 | |
camera = PiCamera() | |
camera.resolution = (640, 480) | |
camera.framerate = 25 | |
GPIO.setwarnings(False) | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(PIR, GPIO.IN) | |
motion = 0 | |
motionNew = 0 | |
def handle(msg): | |
global telegramText | |
global chat_id | |
chat_id = msg['chat']['id'] | |
telegramText = msg['text'] | |
print('Message received from ' + str(chat_id)) | |
if telegramText == '/start': | |
bot.sendMessage(chat_id, 'Security camera is activated.')#Put your welcome note here | |
while True: | |
main() | |
bot = telepot.Bot('1225028462:AAE5Kvalk2pRVWkF8nGNvO8B9cq5E_j7ip8') | |
bot.message_loop(handle) | |
def main(): | |
global chat_id | |
global motion | |
global motionNew | |
if GPIO.input(PIR) == 1: | |
print("Motion detected") | |
motion = 1 | |
if motionNew != motion: | |
motionNew = motion | |
sendNotification(motion) | |
elif GPIO.input(PIR) == 0: | |
print("No motion detected") | |
motion = 0 | |
if motionNew != motion: | |
motionNew = motion | |
def sendNotification(motion): | |
global chat_id | |
if motion == 1: | |
filename = "./video_" + (time.strftime("%y%b%d_%H%M%S")) | |
camera.start_recording(filename + ".h264") | |
sleep(5) | |
camera.stop_recording() | |
command = "MP4Box -add " + filename + '.h264' + " " + filename + '.mp4' | |
print(command) | |
call([command], shell=True) | |
bot.sendVideo(chat_id, video = open(filename + '.mp4', 'rb')) | |
bot.sendMessage(chat_id, 'The motion sensor is triggered!') | |
while 1: | |
time.sleep(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code ok but if we want to edit and put end / process to end then where we have to edit.