Send Video to Telegram Bot Using Raspberry Pi Camera

Want to send a video to Telegram Bot?

Introduction

Do you want to send a video to the telegram bot using a raspberry pi? In this tutorial, we record a video using Raspberry Pi camera and send the video to the Telegram bot. The default video file recorded by Raspberry Pi by default is an H264 file. Telegram cannot play this type of video file. So, we need to convert the video to an mp4 file before we send it to the Telegram bot.

Video

This video shows how to get make a simple security system that can record a video using Raspberry Pi Camera and send it to telegram bot.

Hardware Preparation

This is the list of items used in the video.

Sample Program

This is python3 sample program for sending a video to telegram. You can use it with Thonny Python IDE.

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)

view raw
video_telegram.py
hosted with ❤ by GitHub

Thank You

References:

Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.

Please be reminded, this tutorial is prepared for you to try and learn.
You are encouraged to improve the code for better application.

1 thought on “Send Video to Telegram Bot Using Raspberry Pi Camera”

  1. hei i just try using mq2 sensor module but the result is flip
    when input 1 the value is “no gas leaked”

    and when input is 0 the value is “gas leaked”

    can you help me?

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

micro:bit Quick Start Kit
Design 3D Model Using TinkerCAD and Print
3D Print Lithophane for Hari Raya Aidilfitri
With Raspberry Pi Imager, you can write Raspberry Pi OS and remote SSH easier
3D Printing Pelita Raya
Tutorials of Cytron Technologies Scroll to Top