Skip to content

Instantly share code, notes, and snippets.

Created June 15, 2023 14:20
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Send photo through Telegram using Raspberry Pi Zero W and Pi Camera Module 3
Send '/picam3' to your Telegram bot to request photo
- Raspberry Pi Zero W
- Raspberry Pi Camera Module 3
Install Telepot
- sudo pip3 install telepot
import time
import sys
import telepot
from picamera2 import Picamera2
from libcamera import controls
from libcamera import Transform
picam2 = Picamera2()
capture_config = picam2.create_still_configuration(transform = Transform(hflip=True, vflip=True))
picam2.set_controls({"AfMetering": controls.AfMeteringEnum.Auto})
picam2.set_controls({"AfMode": controls.AfModeEnum.Auto})
def handle(msg):
global telegramText
global chat_id
global receiveTelegramMessage
chat_id = msg['chat']['id']
telegramText = msg['text']
print("Message received from " + str(chat_id))
if telegramText == "/start":
bot.sendMessage(chat_id, "Welcome to Idris Bot")
receiveTelegramMessage = True
def capture():
print("Capturing photo...")
image = picam2.switch_mode_and_capture_file(capture_config, "photo.jpg")
print("Sending photo to " + str(chat_id))
bot.sendPhoto(chat_id, photo = open('./photo.jpg', 'rb'))
bot = telepot.Bot('YOUR TELEGRAM BOT TOKEN')
receiveTelegramMessage = False
sendTelegramMessage = False
cameraEnable = True
sendPhoto = False
print("Telegram bot is ready")
while True:
if receiveTelegramMessage == True:
receiveTelegramMessage = False
statusText = ""
if telegramText == "/picam3":
sendPhoto = True
statusText = "Capturing photo..."
statusText = "Command is not valid"
sendTelegramMessage = True
if sendTelegramMessage == True:
sendTelegramMessage = False
bot.sendMessage(chat_id, statusText)
if sendPhoto == True:
sendPhoto = False
except KeyboardInterrupt:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment