Skip to content

Instantly share code, notes, and snippets.

@idriszmy
Created June 15, 2023 14:20
Show Gist options
  • Save idriszmy/afca41a8870294269070623fef0b61a7 to your computer and use it in GitHub Desktop.
Save idriszmy/afca41a8870294269070623fef0b61a7 to your computer and use it in GitHub Desktop.
Send photo through Telegram using Raspberry Pi Zero W and Pi Camera Module 3
'''
Send '/picam3' to your Telegram bot to request photo
Devices:
- Raspberry Pi Zero W
https://my.cytron.io/p-raspberry-pi-zero-w-and-bundles
- Raspberry Pi Camera Module 3
https://my.cytron.io/p-raspberry-pi-camera-module-3-12mp-with-auto-focus-lens
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.start()
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")
else:
receiveTelegramMessage = True
def capture():
print("Capturing photo...")
picam2.autofocus_cycle()
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')
bot.message_loop(handle)
receiveTelegramMessage = False
sendTelegramMessage = False
cameraEnable = True
sendPhoto = False
print("Telegram bot is ready")
try:
while True:
if receiveTelegramMessage == True:
receiveTelegramMessage = False
statusText = ""
if telegramText == "/picam3":
sendPhoto = True
statusText = "Capturing photo..."
else:
statusText = "Command is not valid"
sendTelegramMessage = True
if sendTelegramMessage == True:
sendTelegramMessage = False
bot.sendMessage(chat_id, statusText)
if sendPhoto == True:
sendPhoto = False
capture()
except KeyboardInterrupt:
picam2.close()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment